码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-12-15 17:00:42      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   sp   for   java   

 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格式文件实现代码

标签:style   blog   http   io   color   os   sp   for   java   

原文地址:http://www.cnblogs.com/sky-of-chuanqingchen/p/4164950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!