POI初体验及如何导出百万数据

来源:互联网 发布:锐思数据库怎么用 编辑:程序博客网 时间:2024/06/06 03:12

HSSF 比较多,兼顾客户的环境

XSSF 应用比较少,当数据量比较大时,才采用

SXSSF 只用在海量数据的导出

POI百万数据导出

      在POI高版本中提供了一个特殊的构造方法SXSSF对象,利用这个对象,在构造时可以设定一个参数,产生对象的数量,根据这个数量,在大数据量导出时,当创建这么多对象后,直接POI写到xml临时文件中。当真正写文件时,再从临时文件中获取信息,输出内容。这样做解决了早期版本中,直接在内存中创建大量对象,导致非常容易内存堆溢出。它不支持模板文件操作。

    用户要求实现一个数据备份,要将合同信息备份。这时历史合同超过10万数据。在POI直接使用HSSF对象时,excel 2003只能允许65536条数据。Excel容纳不了,而且在测试时,计算机性能稍低的机器上,很容易导致堆溢出。我查阅资料把它解决。我升级到XSSF对象时,它直接支持2007以上版本,因为它采用ooxml格式。这时excel可以支持1048576。单个sheet就支持近104万数据。这时我导出10万数据时就比较轻松,但偶尔还是会发生堆溢出。在升级到SXSSF对象,这个对象采用新的方式,采用当数据加工时不是类似前面版本的对象,之间将这些对象放在内存中,而是在创建对象到达一定数量后,将其写临时文件。这样导致打印时,占用CPU,占用内存很少。打印时输出非常平稳。很快顺利完成数据的备份。

 

 

@Testpublic void testHSSF_base() throws IOException{/* * 开发步骤: * 1、创建一个工作簿 * 2、创建一个工作表 * 3、创建一个行对象 * 4、创建一个单元格对象,指定它的列 * 5、给单元格设置内容 * 6、样式进行修饰(跳过) * 7、保存,写文件 * 8、关闭对象 */Workbook wb = new HSSFWorkbook();        HSSFCellStyle style = wb .createCellStyle();         ...Sheet sheet = wb.createSheet();Row nRow = sheet.createRow(7);//第八行Cell nCell = nRow.createCell(4);//第五列   nCell.setCellValue("传智播客万年长!");   nCell.setCellStyle(cellStyle)OutputStream os = new FileOutputStream("c:\\testpoi.xls");//excel 2003wb.write(os);os.flush();os.close();}


创建一个简单的HSSF 

//模板开发@RequestMapping("/cargo/outproduct/printHSSF.action")public void printHSSF(String inputDate, HttpServletRequest request, HttpServletResponse response) throws IOException{//linux下jdk1.8 方法获取时,不会拼接自己写的目录 String path = request.getSession().getServletContext().getRealPath("/") + "/make/xlsprint/";InputStream is = new FileInputStream(new File(path + "tOUTPRODUCT.xls"));Workbook wb = new HSSFWorkbook(is);//打开一个模板文件,工作簿Sheet sheet = wb.getSheetAt(0);//获取到第一个工作表Row nRow = null;Cell nCell = null;int rowNo = 0;//行号int colNo = 1;//列号//获取模板上的单元格样式nRow = sheet.getRow(2);//客户的样式nCell = nRow.getCell(1);CellStyle customStyle = nCell.getCellStyle();//订单号的样式nCell = nRow.getCell(2);CellStyle contractNoStyle = nCell.getCellStyle();//货号的样式nCell = nRow.getCell(3);CellStyle productNoStyle = nCell.getCellStyle();//数量的样式nCell = nRow.getCell(4);CellStyle numStyle = nCell.getCellStyle();//生产厂家的样式nCell = nRow.getCell(5);CellStyle factoryStyle = nCell.getCellStyle();//日期的样式nCell = nRow.getCell(6);CellStyle dateStyle = nCell.getCellStyle();//贸易条款的样式nCell = nRow.getCell(8);CellStyle tradeStyle = nCell.getCellStyle();//处理大标题nRow = sheet.getRow(rowNo++);//获取一个行对象nCell = nRow.getCell(colNo);//获取一个单元格对象nCell.setCellValue(inputDate.replaceFirst("-0", "-").replaceFirst("-", "年") + "月份出货表");//yyyy-MMrowNo++;//跳过静态表格头//处理内容List<OutProductVO> dataList = outProductService.find(inputDate);for(int j=0;j<dataList.size();j++){colNo = 1;//初始化OutProductVO op = dataList.get(j);nRow = sheet.createRow(rowNo++);nRow.setHeightInPoints(24);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getCustomName());nCell.setCellStyle(customStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getContractNo());nCell.setCellStyle(contractNoStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getProductNo());nCell.setCellStyle(productNoStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getCnumber());nCell.setCellStyle(numStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getFactoryName());nCell.setCellStyle(factoryStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getDeliveryPeriod());nCell.setCellStyle(dateStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getShipTime());nCell.setCellStyle(dateStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getTradeTerms());nCell.setCellStyle(tradeStyle);}//OutputStream os = new FileOutputStream("c:\\outproduct.xls");//wb.write(os);////os.flush();//os.close();ByteArrayOutputStream os = new ByteArrayOutputStream();wb.write(os);DownloadUtil downloadUtil = new DownloadUtil();//直接弹出下载框,用户可以打开,可以保存downloadUtil.download(os, response, "出货表.xls");os.flush();os.close();}

创建一个简单的XSSF 

//模板开发XSSF@RequestMapping("/cargo/outproduct/print.action")public void print(String inputDate, HttpServletRequest request, HttpServletResponse response) throws IOException{//linux下jdk1.8 方法获取时,不会拼接自己写的目录 String path = request.getSession().getServletContext().getRealPath("/") + "/make/xlsprint/";InputStream is = new FileInputStream(new File(path + "tOUTPRODUCT.xlsx"));Workbook wb = new XSSFWorkbook(is);//打开一个模板文件,工作簿 2007以上版本Sheet sheet = wb.getSheetAt(0);//获取到第一个工作表Row nRow = null;Cell nCell = null;int rowNo = 0;//行号int colNo = 1;//列号//获取模板上的单元格样式nRow = sheet.getRow(2);//客户的样式nCell = nRow.getCell(1);CellStyle customStyle = nCell.getCellStyle();//订单号的样式nCell = nRow.getCell(2);CellStyle contractNoStyle = nCell.getCellStyle();//货号的样式nCell = nRow.getCell(3);CellStyle productNoStyle = nCell.getCellStyle();//数量的样式nCell = nRow.getCell(4);CellStyle numStyle = nCell.getCellStyle();//生产厂家的样式nCell = nRow.getCell(5);CellStyle factoryStyle = nCell.getCellStyle();//日期的样式nCell = nRow.getCell(6);CellStyle dateStyle = nCell.getCellStyle();//贸易条款的样式nCell = nRow.getCell(8);CellStyle tradeStyle = nCell.getCellStyle();//处理大标题nRow = sheet.getRow(rowNo++);//获取一个行对象nCell = nRow.getCell(colNo);//获取一个单元格对象nCell.setCellValue(inputDate.replaceFirst("-0", "-").replaceFirst("-", "年") + "月份出货表");//yyyy-MMrowNo++;//跳过静态表格头//处理内容List<OutProductVO> dataList = outProductService.find(inputDate);for(int j=0;j<dataList.size();j++){colNo = 1;//初始化OutProductVO op = dataList.get(j);nRow = sheet.createRow(rowNo++);nRow.setHeightInPoints(24);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getCustomName());nCell.setCellStyle(customStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getContractNo());nCell.setCellStyle(contractNoStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getProductNo());nCell.setCellStyle(productNoStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getCnumber());nCell.setCellStyle(numStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getFactoryName());nCell.setCellStyle(factoryStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getDeliveryPeriod());nCell.setCellStyle(dateStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getShipTime());nCell.setCellStyle(dateStyle);nCell = nRow.createCell(colNo++);nCell.setCellValue(op.getTradeTerms());nCell.setCellStyle(tradeStyle);}//OutputStream os = new FileOutputStream("c:\\outproduct.xls");//wb.write(os);////os.flush();//os.close();ByteArrayOutputStream os = new ByteArrayOutputStream();wb.write(os);DownloadUtil downloadUtil = new DownloadUtil();//直接弹出下载框,用户可以打开,可以保存downloadUtil.download(os, response, "出货表.xlsx");os.flush();os.close();}


摘抄来源:传智播客 杰新项目笔记

1 0