Loading

poi导出Excel报表

  在做系统的时候,有可能会用到报表的导出,所以这里介绍一下Apache的开源框架POI导出Excel的方法,我们通常都会先写一个Excel模板文件,这里没表头已经设定好,只需要填入数据即可,而在代码里面设置表头会相对麻烦。

  使用模板文件一定要注意文件的格式,一定是xls类型的文件,不要使用xlsx类型的文件,这样会有冲突,使POI无法解析该模板文件。

  这里依然是用的是Servlet来做导出的例子

 1                 //获取需要导出的信息
 2                 RefundService service = new RefundService() ;
 3                 List<Object[]> list = service.getAll() ;
 4                 //连续两次输出流到页面的时候,第二次的流会包括第一次的流,可以使用reset()方法清除
 5                 response.reset() ;
 6                 //拼接导出的文件名
 7                 String fileName = URLEncoder.encode("供热用户退费情况", "UTF-8")+"["+DateFormat.formatTimestamp2()+"].xls" ;
 8                 //设置相应头信息
 9                 response.setContentType("application/vnd.ms-excel") ;
10                 response.addHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"") ;
11                 //获取相应输出流
12                 OutputStream out = response.getOutputStream() ;
13                 //获取模板文件路径
14                 String modleFilePath = request.getSession().getServletContext().getRealPath("xls") ;
15                 //模板文件的输入流
16                 InputStream in = null ;
17                 try {
18                     in = new BufferedInputStream(new FileInputStream(modleFilePath+File.separator+"HeatRefundInfo.xls")) ;
19                     //Excel文件对象
20                     HSSFWorkbook workbook = new HSSFWorkbook(in) ;
21                     //获取第一个Sheet
22                     HSSFSheet sheet = workbook.getSheetAt(0) ;
23                     //获取第二行,第一行头信息已存在
24                     int rowIndex = 1 ;
25                     for(Object[] heat : list) {
26                         //行对象
27                         HSSFRow row = sheet.createRow(rowIndex) ;
28                         //单元格
29                         for(int cellIndex = 0; cellIndex<heat.length; cellIndex++) {
30                             HSSFCell cell = row.createCell(cellIndex) ;
31                             Object obj = heat[cellIndex] ;
32                             //制定类型转换规则,Excel里面只能存入:boolean、double、String、Date
33                             if(obj != null) {
34                                 if(obj instanceof Integer || obj instanceof Double) {
35                                     cell.setCellValue(new Double(obj.toString())) ;
36                                 }else if(obj instanceof BigDecimal) {
37                                     cell.setCellValue(new BigDecimal(obj.toString()).doubleValue()) ;
38                                 }else if(obj instanceof Date) {
39                                     cell.setCellValue(DateFormat.formatTimestamp((Date)obj)) ;
40                                 }else {
41                                     cell.setCellValue(obj.toString()) ;
42                                 }
43                             }else {
44                                 cell.setCellValue("(NULL)") ;
45                             }
46                         }
47                         rowIndex++ ;
48                     }
49                     workbook.write(out) ;
50                 } catch (Exception e) {
51                     e.printStackTrace() ;
52                 } finally {
53                     //关闭资源
54                     in.close() ;
55                     out.flush() ;
56                     out.close() ;
57                 }
58                 return ;

 

设置单元格样式:http://www.bianchengzhe.com/JavaWeb/neirong/198.html

posted @ 2014-04-09 15:14  孤独的天  阅读(267)  评论(0编辑  收藏  举报