【springmvc+mybatis项目实战】杰信商贸-27.POI由HSSF升级为XSSF

我们之前使用了POI技术将回货表构造成excel文件输出并打印,但是我们依然遗留了许多的问题,这次我们将使用的POI的HSSF,升级为XSSF。

首先什么是HSSF、XSSF呢?其实它是根据不同的规格提供不同的功能。
结构:
HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HWPF - 提供读写Microsoft Word格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读写Microsoft Visio格式档案的功能。

我们之前提到过,早期微软Office系列,当时使用OLE2文档结构(纯2进制的文件,文件头都有规范的)。
微软在开发office2007版本时,做了一个重大的改革,重写了office。使用OOXML文档结构。现在excel文件实际上是一个XML格式文件。

POI支持OLE2格式文件,还支持OOXML,而且在OOXML格式文件时做了很大的优化。JXL只支持OLE2格式文件。

所以,我们使用XSSF会更大程度上的优化我们打印出来的excel文件,还有一个极其重要的一点,就是XSSF一次能够打印百万数据(具体1048576),而HSSF一次只能打印万行(65536)。
还有一些具体的优化比如:
1)升级对象
2)升级模板
3)升级写出的xlsx

我们接下来把原来Controller中的print方法修改一下,让它由HSSF改为XSSF:
[java]  view plain copy
  1. //打印-XSSF  
  2.     @RequestMapping("/cargo/outproduct/print.action")  
  3.     public void print2(String inputDate,HttpServletResponse response) throws IOException{  //inputDate格式:yyyy-MM  
  4.           
  5.         List<OutProductVO> dataList=outProductService.find(inputDate);  
  6.         /*System.out.println(dataList.size()); 
  7.         System.out.println(inputDate);*/  
  8.       
  9.         Workbook wb=new XSSFWorkbook();//打开一个模板文件,2007Excel以上版本  
  10.         Sheet sheet=wb.createSheet();  
  11.         Row nRow=null;  
  12.         Cell nCell=null;  
  13.           
  14.         int rowNo=0;  //行号  
  15.         int cellNo=1;//列号  
  16.           
  17.         //声明样式对象和字体对象  
  18.         CellStyle nStyle=wb.createCellStyle();  
  19.         Font nFont=wb.createFont();  
  20.           
  21.         //设置表格宽度  
  22.         sheet.setColumnWidth(0,1*300);//列宽  
  23.         sheet.setColumnWidth(1,26*300);//列宽  
  24.         sheet.setColumnWidth(2,12*300);//列宽  
  25.         sheet.setColumnWidth(3,29*300);//列宽  
  26.         sheet.setColumnWidth(4,10*300);//列宽  
  27.         sheet.setColumnWidth(5,12*300);//列宽  
  28.         sheet.setColumnWidth(6,8*300);//列宽  
  29.         sheet.setColumnWidth(7,10*300);//列宽  
  30.         sheet.setColumnWidth(8,10*300);//列宽  
  31.         sheet.setColumnWidth(9,8*300);//列宽  
  32.           
  33.         //大标题,合并单元格  
  34.         sheet.addMergedRegion(new CellRangeAddress(0,0,1,9));//开会行,结束行,开始列,结束列  
  35.         //合并单元格的内容是写在合并前第一个单元格中  
  36.         nRow=sheet.createRow(rowNo++);  
  37.         nRow.setHeightInPoints(36);//行高  
  38.           
  39.         nCell=nRow.createCell(1);  
  40.         nCell.setCellValue(inputDate.replaceFirst("-0","-").replaceFirst("-","年")+"月份出货表");//yyyy-MM  
  41.         nCell.setCellStyle(this.bigTitle(wb, nStyle, nFont));  
  42.           
  43.         //配置标题行  
  44.         String [] title=new String[]{"客户","订单号","货号","数量","工厂","附件","工厂交期","船期","贸易条款"};  
  45.           
  46.         nRow=sheet.createRow(rowNo++);  
  47.         nRow.setHeightInPoints(26.25f);//标题的高  
  48.         nStyle=wb.createCellStyle();//重新初始化样式  
  49.         nFont=wb.createFont();//重新初始化字体  
  50.         for (int i = 0; i < title.length; i++) {  
  51.             nCell=nRow.createCell(i+1);  
  52.             nCell.setCellValue(title[i]);  
  53.             nCell.setCellStyle(this.Title(wb, nStyle, nFont));  
  54.         }  
  55.           
  56.         nStyle=wb.createCellStyle();//重新初始化样式  
  57.         nFont=wb.createFont();//重新初始化字体  
  58.           
  59.         //处理数据  
  60.         for (int i = 0; i < dataList.size(); i++) {  
  61.             OutProductVO op=dataList.get(i);  
  62.   
  63.   
  64.             nRow=sheet.createRow(rowNo++);  
  65.             nRow.setHeightInPoints(24);//数据框的高  
  66.             cellNo=1;//列号初始化  
  67.               
  68.             nCell=nRow.createCell(cellNo++);  
  69.             nCell.setCellValue(op.getCustomName());  
  70.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  71.               
  72.             nCell=nRow.createCell(cellNo++);  
  73.             nCell.setCellValue(op.getcontractNo());  
  74.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  75.               
  76.             nCell=nRow.createCell(cellNo++);  
  77.             nCell.setCellValue(op.getProductNo());  
  78.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  79.               
  80.             nCell=nRow.createCell(cellNo++);  
  81.             nCell.setCellValue(op.getCnumber());  
  82.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  83.               
  84.             nCell=nRow.createCell(cellNo++);  
  85.             nCell.setCellValue(op.getFactoryName());  
  86.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  87.               
  88.             nCell=nRow.createCell(cellNo++);  
  89.             nCell.setCellValue(op.getExts());  
  90.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  91.               
  92.             nCell=nRow.createCell(cellNo++);  
  93.             nCell.setCellValue(op.getDelivery_preriod());  
  94.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  95.               
  96.             nCell=nRow.createCell(cellNo++);  
  97.             nCell.setCellValue(op.getShip_time());  
  98.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  99.               
  100.             nCell=nRow.createCell(cellNo++);  
  101.             nCell.setCellValue(op.getTradeTerms());  
  102.             nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
  103.               
  104.         }  
  105.           
  106.         /*OutputStream os=new FileOutputStream(new File("F:\\outproduct.xls")); 
  107.          
  108.         wb.write(os); 
  109.         os.flush(); 
  110.         os.close();*/  
  111.           
  112.         //下载(使用了我们的工具类)  
  113.         DownloadUtil du=new DownloadUtil();  
  114.         ByteArrayOutputStream bs=new ByteArrayOutputStream();  
  115.         wb.write(bs);  
  116.         du.download(bs, response, "出货表.xlsx");  
  117.           
  118.     }  



仔细观察,我们只改了两个地方:
Workbook wb=new XSSFWorkbook();//打开一个模板文件,2007Excel以上版本

du.download(bs, response, "出货表.xlsx");


我们测试一下:

打开下载的文档


升级成功!很简单吧?

只不过XSSF比HSSF稍微慢了一点,因为要为百万级数据生成做准备,所以,一般我们选择POI的文件创建规则遵循以下原则:
实际工作中
HSSF 比较多,兼顾客户的环境
XSSF 应用比较少,当数据量比较大时,才采用
SXSSF 只用在海量数据的导出
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值