首页 > 代码库 > JAVA 导出Excel2003格式文件实现代码

JAVA 导出Excel2003格式文件实现代码

 1     public String exportExcel(String name, String name2, String name3, 2             String name4, String name5, HttpServletRequest request, 3             HttpServletResponse response){ 4          5         String path = request.getRealPath("/"); 6         List list = this.findVsendList(name,name2,name3,name4,name5); 7         WritableWorkbook book = null; 8         int pagernum,mus=65535,count,num; 9         WritableSheet sheet = null;10     11         try {12 13             // 在位置0的前方插入表格页14             OutputStream os = response.getOutputStream();// 取得输出流15             response.reset();// 清空输出流16             response.setHeader("Content-disposition", "attachment; filename=" + new String("文件名".getBytes("GB2312"), "8859_1") + ".xls");// 设定输出文件头17             response.setContentType("application/msexcel");// 定义输出类型18 19             book = Workbook.createWorkbook(os);20             pagernum=list.size()/mus;21             for(int i=0;i<pagernum+1;i++){22                 sheet = book.createSheet("sheet"+(i+1), i);23 24                 // 删除页25                 sheet.addCell(new Label(0, 0, "列一")); // 位置从0开始26 27                 sheet.addCell(new Label(1, 0, "列二"));28                 29                 sheet.addCell(new Label(2, 0, "列三"));30                 31                 sheet.addCell(new Label(3, 0, "列四"));32                 33                 sheet.addCell(new Label(4, 0, "列五"));34                 35                 sheet.addCell(new Label(5, 0, "列六"));36                 37                 sheet.addCell(new Label(6, 0, "列七"));38                 39                 sheet.addCell(new Label(7, 0, "列八"));40                 41                 if(i==pagernum){42                     count=list.size()%mus;43                 }else{44                     count=mus;45                 }46                 num=i*mus;47                 48                 for (int j = num; j < num+count; j++) {49                     VSendList sendlist = (VSendList) list.get(j);                50                     if (sendlist != null) {51                         String Type = "";52                         if (1 == sendlist.getXx()) {53                             Type = "XX";54                         } else if (2 == sendlist.getXx()) {55                             Type = "XX";                        56                         } else if (3 == sendlist.getXx()){57                             Type = "XX";58                         } else {59                             Type = "XX";60                         }61                         sheet.addCell(new Label(0, j + 1 - num, sendlist.getXxxx())); // 位置从0开始62                         sheet.addCell(new Label(1, j + 1 - num, sendlist.getXx()));63                         sheet.addCell(new Label(2, j + 1 - num, sendlist.getXxxx()));64                         sheet.addCell(new Label(3, j + 1 - num, Type));65                         sheet.addCell(new Label(4, j + 1 - num, sendlist.getXxxxx().substring(0, 19)));66                         sheet.addCell(new Label(5, j + 1 - num, sendlist.getXxxxxx()));67                         sheet.addCell(new Label(6, j + 1 - num, sendlist.getXxxxxx().toString()));68                         sheet.addCell(new Label(7, j + 1 - num, sendlist.getXxxxx()));69                     }70                 }71             }72             book.write();73         } catch (Exception e) {74             e.printStackTrace();75         } finally {76             if (book != null) {77                 try {78                     book.close();79                 } catch (Exception e) {80                     e.printStackTrace();81                 }82             }83         }84         return null;85     }

 

JAVA 导出Excel2003格式文件实现代码