POI简述

JAVA语言操作excel的api主流有两种方式:
1、POI Apache它是用来操作Office所有的软件excel/word/ppt/..。而且支持所有版本。
2、JXL 它是用来操作excel 2003以下版本,2007以上版本不支持。
早期微软Office系列,当时使用OLE2文档结构;微软在开发Office2207版本时,做了一个重大的改革。重写了Office,使用OOXML文档结构。现在excel文件实际上是一个xml格式文件。
POI支持OLE2格式文件,还支持OOXML,而且在OOXML格式文件时做了很大的优化。
JXL只支持OLE2格式文件。
POI低版本有个致命弱点,当数据量超大(海量数据),性能极具降低。JXL它解决数量大时性能的问题。POI在高版本时迎头追上,加了一个补丁。OOXML支持,解决大数量时的性能问题。
http://poi.apache.org/index.html POI 全方位的操作
http://www.andykhan.com/jexcelapi/index.html JXL 导入导出
java.lang.IllegalArgumentException: Invalid column index(256).  Allowable column range for BIFF8 is(0..255) or (‘A’..’IV’)
java.lang.IllegalArgumentException: Invalid row number (65536)outside allowable range (0..65535)
HSSF对象支持excel 2003,excel 2003它支持行65536,支持列256
XSSF对象支持excel2007版本以上,行1048576,支持列65536。Sheet
SXSSF对象,支持excel 2007版本以上,构造函数。不支持模板方式。只能作为数据导出。
SXSSF如何实现
HSSF/XSSF它们在构建workbook中所有对象,全部放在内存中,数据全放在内存中,wb.write时才从内存向磁盘文件中写。写完才释放内存。数据量大,堆溢出。
SXSSF构造方法Workbook wb = new SXSSFWorkbook(100);
每处理完100个对象,将它写临时文件,这部分数据所占的内存,就释放。

上面代码存在的问题:
1) POI创建的这些对象统统在内存中
2) 行对象,列对象,样式对象,字体对象重复创建
15. 常规poi打印存在问题:
1) 列的宽度不精确,272非常相近
2) 设置纸张方向
3) 设置页眉页脚
4) 标题栏
5) 内容样式稍有不同,就需要创建不同的样式,写很多样式应用的语句
6) 表格头,写死表头,静态的文字方便维护
16. POI操作EXCEL,复杂 表终极解决方案~模板开发
1) 代码量急剧降低
2) POI日常API,将利用模板直接来设置,可视化修改。
3) 业务功能非常方便维护。


  1. //模板打印
  2. @RequestMapping("/cargo/outproduct/printTemplate.action")
  3. public void printTemplate(String inputDate, HttpServletRequestrequest, HttpServletResponse response) throws IOException{
  4. List dataList = outProductService.find(inputDate);
  5. //打开模板文件
  6. String path =request.getSession().getServletContext().getRealPath("/"); //jdk1.8bug 在linux下不带后面你写的路径
  7. String tempFile = path +"/make/xlsprint/tOUTPRODUCT.xls";
  8. Workbook wb = new HSSFWorkbook(new FileInputStream(newFile(tempFile)));
  9. //写入业务内容
  10. Sheet sheet = wb.getSheetAt(0); //获得工作表sheet
  11. Row nRow = null;
  12. Cell nCell = null;
  13. int rowNo = 2;
  14. int colNo = 1;
  15. //获取样式
  16. nRow = sheet.getRow(2); //获得行对象
  17. nCell = nRow.getCell(1); //获取单元格对象
  18. CellStyle customNameStyle = nCell.getCellStyle();//获取到样式
  19. nCell = nRow.getCell(2);
  20. CellStyle contractNoStyle = nCell.getCellStyle();
  21. nCell = nRow.getCell(3);
  22. CellStyle productNoStyle = nCell.getCellStyle();
  23. nCell = nRow.getCell(4);
  24. CellStyle cnumberStyle = nCell.getCellStyle();
  25. nCell = nRow.getCell(5);
  26. CellStyle factoryStyle = nCell.getCellStyle();
  27. nCell = nRow.getCell(6);
  28. CellStyle extStyle = nCell.getCellStyle();
  29. nCell = nRow.getCell(7);
  30. CellStyle dateStyle = nCell.getCellStyle();
  31. nCell = nRow.getCell(9);
  32. CellStyle tradeStyle = nCell.getCellStyle();
  33. //大标题
  34. nRow = sheet.getRow(0);
  35. nCell = nRow.getCell(1);
  36. nCell.setCellValue(inputDate.replaceFirst("-0","-").replaceFirst("-", "年") + "月份出货表"); //yyyy-MM
  37. //处理数据
  38. for(int j=0;j
  39. colNo = 1; //列 初始化
  40. OutProductVO op = dataList.get(j); //获取出货表对象
  41. nRow = sheet.createRow(rowNo++);
  42. nRow.setHeightInPoints(24);
  43. nCell = nRow.createCell(colNo++);
  44. nCell.setCellValue(op.getCustomName());
  45. nCell.setCellStyle(customNameStyle); //设置样式
  46. nCell = nRow.createCell(colNo++);
  47. nCell.setCellValue(op.getContractNo());
  48. nCell.setCellStyle(contractNoStyle);
  49. nCell = nRow.createCell(colNo++);
  50. nCell.setCellValue(op.getProductNo());
  51. nCell.setCellStyle(productNoStyle);
  52. nCell = nRow.createCell(colNo++);
  53. nCell.setCellValue(op.getCnumber());
  54. nCell.setCellStyle(cnumberStyle);
  55. nCell = nRow.createCell(colNo++);
  56. nCell.setCellValue(op.getFactoryName());
  57. nCell.setCellStyle(factoryStyle);
  58. nCell = nRow.createCell(colNo++);
  59. nCell.setCellValue(op.getExts());
  60. nCell.setCellStyle(extStyle);
  61. nCell = nRow.createCell(colNo++);
  62. nCell.setCellValue(op.getDeliveryPeriod());
  63. nCell.setCellStyle(dateStyle);
  64. nCell = nRow.createCell(colNo++);
  65. nCell.setCellValue(op.getShipTime());
  66. nCell.setCellStyle(dateStyle);
  67. nCell = nRow.createCell(colNo++);
  68. nCell.setCellValue(op.getTradeTerms());
  69. nCell.setCellStyle(tradeStyle);
  70. }
  71. //下载
  72. DownloadUtil du = new DownloadUtil();
  73. ByteArrayOutputStream os = new ByteArrayOutputStream();
  74. wb.write(os);
  75. du.download(os, response, "出货表.xls");
  76. }
  77. public class DownloadUtil {
  78. protected void download(String filePath,StringreturnName,HttpServletResponse response,boolean delFlag){
  79. this.prototypeDownload(new File(filePath), returnName,response, delFlag);
  80. }

    声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2017年1月3日
下一篇 2017年1月3日

相关推荐