zoukankan      html  css  js  c++  java
  • jxl自己写的例子

     1 public String writeExcel(){
     2         Workbook  book = null;
     3         Cell cell =null;
     4         try {
     5             //打开文件
     6             book = Workbook.getWorkbook(file);
     7             //获得第一个工作表对象
     8             Sheet sheet = book.getSheet(0);
     9             int rows = sheet.getRows();
    10             //遍历单元格
    11             for(int i = 1;i<rows;i++){
    12                     districtTest=new DistrictTest();
    13                 
    14                     cell = sheet.getCell(1, i);
    15                     districtTest.setDistrictName(cell.getContents().toString());
    16                     cell = sheet.getCell(2, i);
    17                     districtTest.setCode(cell.getContents().toString());
    18                     
    19                     districtTest.setCreateTime(new Date());
    20                     districtTest.setUpdateTime(new Date());
    21                     districtTest.setIsDele(0);
    22                     this.utilService.save(districtTest);
    23                     
    24             }
    25             flag="1";
    26             return "districtTest_show0";
    27         } catch (Exception e) {
    28             // TODO: handle exception
    29             Loggers.error("----DistrictTestAction的writeExcel方法错误"+e.getMessage());
    30             e.printStackTrace();
    31         }
    32         return ERROR;
    33     }
    导入excel
     1 //导出excel
     2     public String exportExcel(){
     3         WritableWorkbook workbook = null;
     4         HttpServletResponse response =ServletActionContext.getResponse();
     5         response.setHeader("Content-Disposition", "attachment; filename="+
     6         "excel0"+".xls"); 
     7         try {
     8             String hql = "from DistrictTest where isDele=0";
     9             //workbook = Workbook.createWorkbook(new File("E:/yang.xls"));
    10             districtTestList =  (List<DistrictTest>) utilService.getHqlAll(hql);
    11             outputStream = response.getOutputStream();
    12             workbook = Workbook.createWorkbook(outputStream);
    13             WritableSheet sheet = workbook.createSheet("sheet1", 0);
    14             Label label = new Label(0, 0, "序号");
    15             Label label1 = new Label(1, 0, "区县名称");
    16             Label label2 = new Label(2, 0, "编码");
    17             sheet.addCell(label);
    18             sheet.addCell(label1);
    19             sheet.addCell(label2);
    20             for(int i=0;i<districtTestList.size();i++){
    21                 int j = i;
    22                 Label label3 = new Label(0, i+1, " "+(++j));
    23                 sheet.addCell(label3);
    24                 if(districtTestList.get(i).getDistrictName()!=null&&!" ".equals(districtTestList.get(i).getDistrictName())){
    25                 
    26                     Label lable4 = new Label(1, i+1, districtTestList.get(i).getDistrictName().toString());
    27                     sheet.addCell(lable4);
    28                 }else{
    29                     
    30                     Label lable4 = new Label(1, i+1, " ");
    31                     sheet.addCell(lable4);
    32                 }
    33                 if(districtTestList.get(i).getCode()!=null&&!" ".equals(districtTestList.get(i).getCode())){
    34                     Label lable5 = new Label(2, i+1, districtTestList.get(i).getCode().toString());
    35                     sheet.addCell(lable5);
    36                 }else{
    37                     Label lable5 = new Label(2, i+1, " ");
    38                     sheet.addCell(lable5);
    39                 }
    40             }
    41             workbook.write();
    42             outputStream.flush();
    43             
    44             System.out.println("下载excel成功");
    45             flag="1";
    46             return districtList();
    47         } catch (Exception e) {
    48             // TODO: handle exception
    49             Loggers.error("----DistrictTestAction的exportExcel方法错误"+e.getMessage());
    50             e.printStackTrace();
    51         }finally{
    52             if(workbook!=null){
    53                 try {
    54                     workbook.close();
    55                 } catch (Exception e) {
    56                     // TODO Auto-generated catch block
    57                     e.printStackTrace();
    58                 } 
    59             }
    60             if(outputStream!=null){
    61                 try {
    62                     outputStream.close();
    63                 } catch (IOException e) {
    64                     // TODO Auto-generated catch block
    65                     e.printStackTrace();
    66                 }
    67             }
    68         
    69         }
    70         
    71         
    72         
    73         
    74         return ERROR;
    75     }
    导出excel
  • 相关阅读:
    22 块级元素和行内元素
    21 文档流
    20101018T3 付账
    20181018T1 括号
    poj3417暗的连锁
    点的距离(LCA)
    浅谈RMQ实现LCA
    小R的调度
    bzoj1798维护序列
    bzoj3211花神游历各国
  • 原文地址:https://www.cnblogs.com/yang1018/p/7170060.html
Copyright © 2011-2022 走看看