JAVA导出报表 ExportExcel

本文介绍了一种利用模板配置来导出Excel报表的方法。该方法适用于小数据量的报表导出,支持通过Java反射机制获取对象属性并填充到Excel单元格中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  这是一个通过配置模板导出报表的JAR包,只适用于小数据量的报表导出,如果数据量超过Excel最大行数(65536)需要自行添加文件打包后下载的功能。 

    

  依赖 POI-3.0.jar

 

Exportexcel.java代码  收藏代码
  1. package com.goma.exportexcel;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.InputStream;  
  8. import java.util.List;  
  9.   
  10. import org.apache.poi.hssf.usermodel.HSSFCell;  
  11. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  12. import org.apache.poi.hssf.usermodel.HSSFRow;  
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  15. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  16.   
  17. /**  
  18.  * DESC: 读取模板导出Excel报表<BR>  
  19.  * AUTHOR: GuoMM ( Goma ) oma1989@yeah.net<BR>     
  20.  * VERSION: 1.0<BR>  
  21.  * DATE:2011-11-7 14:23:51<BR>  
  22.  */  
  23. public class ExportExcel {  
  24.     private String fileUrl;  
  25.     /**  
  26.      *   
  27.      * @param fileUrl 模板路径<br>  
  28.      * DESC:创建实例,并初始化模板<br>  
  29.      */  
  30.     public ExportExcel(String fileUrl){  
  31.         this.fileUrl = fileUrl;  
  32.     }  
  33.       
  34.     /**  
  35.      *   
  36.      * @param rows 模板内配置所占行数<br>  
  37.      * @param cols 模板内配置所占列数<br>  
  38.      * @param result 要导出报表的数据集<br>  
  39.      * @return excel报表<br>  
  40.      *   
  41.      */  
  42.     public InputStream export(int rows,int cols,List<?> result){  
  43.         try{  
  44.             File file = new File(fileUrl);     
  45.             FileInputStream fint = new FileInputStream(file);     
  46.             POIFSFileSystem poiFileSystem = new POIFSFileSystem(fint);     
  47.             HSSFWorkbook wb = new HSSFWorkbook(poiFileSystem);   
  48.             HSSFSheet sheet = wb.getSheetAt(0);  
  49.             GetProperty gp = new GetProperty();  
  50.             HSSFRow namesRow = sheet.getRow(rows-2);  
  51.             HSSFRow typeRow = sheet.getRow(rows-1);  
  52.               
  53.             for(int i=0;i<result.size();i++){  
  54.                 HSSFRow row = sheet.getRow(rows+i);  
  55.                 if(row==null){  
  56.                     row = sheet.createRow(rows+i);  
  57.                 }  
  58.                 for(int j=0;j<cols;j++){  
  59.                     HSSFCell cell = row.getCell((short)j);  
  60.                     if (cell == null){  
  61.                         cell = row.createCell((short)j);  
  62.                     }  
  63.                     //获得方法名  
  64.                     HSSFCell nameCell = namesRow.getCell((short)j);  
  65.                     HSSFCell typeCell = typeRow.getCell((short)j);  
  66.                     String name = nameCell.getRichStringCellValue().getString();  
  67.                     String type = typeCell.getRichStringCellValue().getString();  
  68.                     //java反射机制获得方法值  
  69.                     Object obj = gp.getProperty(name, result.get(i).getClass(), result.get(i));  
  70.                     //写入Excel中  
  71.                     if("Number".equals(type)){  
  72.                         cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);  
  73.                         if(obj!=null&&!"".equals(obj)){  
  74.                             cell.setCellValue(Double.valueOf(String.valueOf(obj)));  
  75.                         }else{  
  76.                             cell.setCellValue(0);  
  77.                         }  
  78.                     }else{  
  79.                         cell.setCellType(HSSFCell.CELL_TYPE_STRING);  
  80.                         if(obj==null){  
  81.                             cell.setCellValue(new HSSFRichTextString(""));  
  82.                         }else{  
  83.                             cell.setCellValue(new HSSFRichTextString(String.valueOf(obj)));  
  84.                         }  
  85.                     }  
  86.                 }  
  87.             }  
  88.               
  89.             sheet.shiftRows(rows, result.size()+rows, -2);  
  90.             System.out.println("WRITE EXCEL REPORT IS OK..");  
  91.             ByteArrayOutputStream os = new ByteArrayOutputStream();  
  92.             wb.write(os);  
  93.             byte[] content = os.toByteArray();  
  94.             InputStream is = new ByteArrayInputStream(content);  
  95.             return is;  
  96.               
  97.         }catch (Exception e) {  
  98.             // TODO: handle exception  
  99.             e.printStackTrace();  
  100.             return null;  
  101.         }  
  102.     }  
  103. }  

 

 

Getproperty.java代码  收藏代码
  1. package com.goma.exportexcel;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. /**  
  6.  * DESC:通过java反射机制执行类方法<BR>  
  7.  * author:GuoMM(Goma) oma1989@yeah.net<BR>  
  8.  * version:1.0<BR>  
  9.  * Date:2011-11-7 14:22:21<BR>   
  10.  */  
  11. public class GetProperty {  
  12.   
  13.     /**  
  14.      *   
  15.      * @param propertyName 方法名<br>  
  16.      * @param cls 类Class<br>  
  17.      * @param obj 对象<br>  
  18.      * @return 返回对象obj.propertyName的执行结果<br>  
  19.      */  
  20.     public Object getProperty(String propertyName,Class<?> cls,Object obj){  
  21.         try{  
  22.             if(propertyName!=null &&!"".equals(propertyName)){  
  23.                 Method method = cls.getMethod(propertyName);  
  24.                 return method.invoke(obj);  
  25.             }  
  26.             return null;  
  27.         }catch (Exception e) {  
  28.             // TODO: handle exception  
  29.             e.printStackTrace();  
  30.             return null;  
  31.         }  
  32.     }  
  33.       
  34. }  

 

 

 

Action中使用代码  收藏代码
  1. List<TeamModelVO> ls = dao.getData(sql.toString(), TeamModelVO.class);  
  2. System.out.println(ls.size()+"::::Export:::::");  
  3. String fileUrl = request.getSession().getServletContext().getRealPath(  
  4.         "/console/Resources/platformat/型号盈利能力分析.xls");  
  5. ExportExcel ee = new ExportExcel(fileUrl);  
  6. InputStream is = ee.export(653, ls);  
  7. response.addHeader("Content-Disposition","attachment;filename=Report.xls");  
  8. ServletOutputStream out = response.getOutputStream();  
  9. byte[] buffer=new byte[1024];  
  10. int count;  
  11. while((count=is.read(buffer))!=-1){  
  12.     out.write(buffer, 0, count);  
  13. }  
  14. return null;  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值