一、Apache POI Apache POI是Apache基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。 软件 结构: HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft格式档案的功能。 Excel OOXML HWPF - 提供读写Microsoft Word格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读写Microsoft Visio格式档案的功能。
二、HSSF导出EXCEL 我们要用的就是HSSF的写功能,POI的EXCEL导出功能原理其实是一个一个地定义单元格,填充样式和内容。其中的合并单元格功能只要指定起始行列及结束行列,但需要在填充内容之前先定义好。 我们先来看下导出的效果: 下面我们看下HSSF怎么生成EXCEL:
-
- HSSFWorkbook wb = new HSSFWorkbook();
-
- HSSFSheet sheet = wb.createSheet("订单报表");
- //行宽6000
- sheet.setColumnWidth(2, 6000);
- sheet.setColumnWidth(3, 6000);
- sheet.setColumnWidth(4, 6000);
- sheet.setColumnWidth(5, 6000);
- sheet.setColumnWidth(6, 6000);
-
-
- HSSFCellStyle style = wb.createCellStyle();
- style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
-
-
-
-
-
-
- HSSFCellStyle style_right = wb.createCellStyle();
- style_right.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- style_right.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
-
- HSSFCellStyle style_left = wb.createCellStyle();
- style_left.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- style_left.setAlignment(HSSFCellStyle.ALIGN_LEFT);
-
-
- HSSFRow row = sheet.createRow((short) 0);
-
- row.setHeight((short)500);
-
- sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) 6));
-
- HSSFCell ce = row.createCell(0);
- ce.setCellValue("一、订单基本信息");
- ce.setCellStyle(style);
-
- HSSFRow row2 = sheet.createRow((short) 1);
- sheet.addMergedRegion(new CellRangeAddress(1, (short) 1, 0, (short) 1));
- HSSFCell ce21 = row2.createCell(0);
- ce21.setCellValue("订单号");
- ce21.setCellStyle(style);
- HSSFCell ce22 = row2.createCell(2);
- if(order.getOrderid() != null){
- ce22.setCellValue(order.getOrderid());
- }
- ce22.setCellStyle(style);
- HSSFCell ce23 = row2.createCell(3);
- ce23.setCellValue("生产日期");
- ce23.setCellStyle(style);
- HSSFCell ce24 = row2.createCell(4);
- if(order.getEnddate() != null){
- ce24.setCellValue(order.getEnddate());
- }
- ce24.setCellStyle(style);
- HSSFCell ce25 = row2.createCell(5);
- ce25.setCellValue("订单数量");
- ce25.setCellStyle(style);
- HSSFCell ce26 = row2.createCell(6);
- if(order.getOrdernum() != null){
- ce26.setCellValue(order.getOrdernum());
- }
- ce26.setCellStyle(style);
-
- HSSFRow row3 = sheet.createRow((short) 2);
- sheet.addMergedRegion(new CellRangeAddress(2, (short) 2, 0, (short) 1));
- sheet.addMergedRegion(new CellRangeAddress(2, (short) 2, 4, (short) 6));
- HSSFCell ce31 = row3.createCell(0);
- ce31.setCellValue("生产机种");
- ce31.setCellStyle(style);
- HSSFCell ce32 = row3.createCell(2);
- if(statementForShow.getMachinename() != null){
- ce32.setCellValue(statementForShow.getMachinename());
- }
- ce32.setCellStyle(style);
- HSSFCell ce33 = row3.createCell(3);
- ce33.setCellValue("软件信息");
- ce33.setCellStyle(style);
- HSSFCell ce34 = row3.createCell(4);
- if(order.getOs_name() != null){
- ce34.setCellValue(order.getOs_name());
- }
- ce34.setCellStyle(style);
java文件生成下载:
- HttpServletResponse response = ServletActionContext.getResponse();
- response.reset();
- response.setContentType("application/x-msdownload");
- //EXCEL名字为“工单报表”
- String pName="工单报表";
- response.setHeader("Content-Disposition","attachment; filename="+new String(pName.getBytes("gb2312"),"ISO-8859-1")+".xls");
- ServletOutputStream outStream=null;
-
- try{
- outStream = response.getOutputStream();
- wb.write(outStream);
- }catch(Exception e)
- {
- e.printStackTrace();
- }finally{
- outStream.close();
- }
参考资料Apache:http://poi.apache.org/spreadsheet/diagram1.html
参考百度百科地址:http://baike.baidu.com/view/517279.htm#sub5442945
本文出自 “雪飘七月” 博客,请务必保留此出处http://xuepiaoqiyue.blog.51cto.com/4391594/977175