EXCEL导出POI实现

一、Apache POI
  结构:
   HSSF - 提供读写Microsoft Excel格式档案的功能。
   XSSF - 提供读写Microsoft格式档案的功能。Excel OOXML
   HWPF - 提供读写Microsoft Word格式档案的功能。
   HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
        HDGF - 提供读写 Microsoft Visio格式档案的功能。

 

二、 HSSF导出EXCEL
    我们要用的就是HSSF的写功能,POI的EXCEL导出功能原理其实是一个一个地定义单元格,填充样式和内容。其中的合并单元格功能只要指定起始行列及结束行列,但需要在填充内容之前先定义好。
 
我们先来看下导出的效果:

 
下面我们看下HSSF怎么生成EXCEL:
  
  
  1. //创建EXCEL文档类型  
  2.         HSSFWorkbook wb = new HSSFWorkbook();      
  3.         //创建该EXCEL的第一页名为“订单报表”  
  4.         HSSFSheet sheet = wb.createSheet("订单报表");  
  5. //行宽6000    
  6.         sheet.setColumnWidth(26000);  
  7.         sheet.setColumnWidth(36000);  
  8.         sheet.setColumnWidth(46000);  
  9.         sheet.setColumnWidth(56000);  
  10.         sheet.setColumnWidth(66000);  
  11.           
  12.         //创建单元格样式  
  13.         HSSFCellStyle style = wb.createCellStyle();  
  14.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中  
  15.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中  
  16.           
  17. //        HSSFCellStyle style_bg = wb.createCellStyle(); // 样式对象      
  18. //        style_bg.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直      
  19. //        style_bg.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平      
  20. //        style_bg.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  21. //        style_bg.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);//设置单元格前景色  
  22.           
  23.         HSSFCellStyle style_right = wb.createCellStyle();  
  24.         style_right.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中  
  25.         style_right.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//水平靠右  
  26.           
  27.         HSSFCellStyle style_left = wb.createCellStyle();  
  28.         style_left.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  29.         style_left.setAlignment(HSSFCellStyle.ALIGN_LEFT);  
  30.           
  31. //创建第一页的第一行  
  32.         HSSFRow row = sheet.createRow((short0);   
  33.         //设置行高500    
  34.         row.setHeight((short)500);  
  35.         //合并单元格,参数依次为起始行,结束行,起始列,结束列  
  36.         sheet.addMergedRegion(new CellRangeAddress(0, (short00, (short6));  
  37.         //创建第一行的第一个单元格  
  38.         HSSFCell ce = row.createCell(0);     
  39.         ce.setCellValue("一、订单基本信息"); // 表格的第一行第一列显示的数据      
  40.         ce.setCellStyle(style); // 样式,居中      
  41.           
  42.         HSSFRow row2 = sheet.createRow((short1);   
  43.         sheet.addMergedRegion(new CellRangeAddress(1, (short10, (short1));  
  44.         HSSFCell ce21 = row2.createCell(0);     
  45.         ce21.setCellValue("订单号"); // 表格的第二行第一列显示的数据      
  46.         ce21.setCellStyle(style); // 样式,居中  
  47.         HSSFCell ce22 = row2.createCell(2);     
  48.         if(order.getOrderid() != null){  
  49.             ce22.setCellValue(order.getOrderid()); // 表格的第二行第二列显示的数据    
  50.         }   
  51.         ce22.setCellStyle(style); // 样式,居中  
  52.         HSSFCell ce23 = row2.createCell(3);     
  53.         ce23.setCellValue("生产日期"); // 表格的第二行第三列显示的数据      
  54.         ce23.setCellStyle(style); // 样式,居中  
  55.         HSSFCell ce24 = row2.createCell(4);     
  56.         if(order.getEnddate() != null){  
  57.             ce24.setCellValue(order.getEnddate()); // 表格的第二行第四列显示的数据      
  58.         }  
  59.         ce24.setCellStyle(style); // 样式,居中  
  60.         HSSFCell ce25 = row2.createCell(5);     
  61.         ce25.setCellValue("订单数量"); // 表格的第二行第五列显示的数据      
  62.         ce25.setCellStyle(style); // 样式,居中  
  63.         HSSFCell ce26 = row2.createCell(6);     
  64.         if(order.getOrdernum() != null){  
  65.             ce26.setCellValue(order.getOrdernum()); // 表格的第二行第六列显示的数据    
  66.         }    
  67.         ce26.setCellStyle(style); // 样式,居中  
  68.           
  69.         HSSFRow row3 = sheet.createRow((short2);   
  70.         sheet.addMergedRegion(new CellRangeAddress(2, (short20, (short1));  
  71.         sheet.addMergedRegion(new CellRangeAddress(2, (short24, (short6));  
  72.         HSSFCell ce31 = row3.createCell(0);     
  73.         ce31.setCellValue("生产机种"); // 表格的第二行第一列显示的数据      
  74.         ce31.setCellStyle(style); // 样式,居中  
  75.         HSSFCell ce32 = row3.createCell(2);     
  76.         if(statementForShow.getMachinename() != null){  
  77.             ce32.setCellValue(statementForShow.getMachinename()); // 表格的第二行第二列显示的数据      
  78.         }  
  79.         ce32.setCellStyle(style); // 样式,居中  
  80.         HSSFCell ce33 = row3.createCell(3);     
  81.         ce33.setCellValue("软件信息"); // 表格的第三行第三列显示的数据      
  82.         ce33.setCellStyle(style); // 样式,居中  
  83.         HSSFCell ce34 = row3.createCell(4);     
  84.         if(order.getOs_name() != null){  
  85.             ce34.setCellValue(order.getOs_name()); // 表格的第三行第四列显示的数据     
  86.         }   
  87.         ce34.setCellStyle(style); // 样式,居中  
java文件生成下载:
  
  
  1. HttpServletResponse response = ServletActionContext.getResponse();  
  2.        response.reset();     
  3.        response.setContentType("application/x-msdownload"); 
  4. //EXCEL名字为“工单报表”    
  5.        String pName="工单报表";  
  6.        response.setHeader("Content-Disposition","attachment; filename="+new String(pName.getBytes("gb2312"),"ISO-8859-1")+".xls");     
  7.        ServletOutputStream outStream=null;     
  8.        
  9.        try{     
  10.            outStream = response.getOutputStream();     
  11.            wb.write(outStream);     
  12.        }catch(Exception e)     
  13.        {     
  14.         e.printStackTrace();     
  15.        }finally{     
  16.            outStream.close();     
  17.        }     


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值