大数据导出POI之SXSSFWorkbook

poi3.7可以使用HSSFWorkbook导出xls格式,经查数据量只能在65536;

poi3.7可以使用XSSFWorkbook导出xlsx格式,数据量可达到100w,经过跟踪代码发现构建数据起始特别快但当数据量到达5w左右时突然变的很慢,最后报出OutOfMemoryError:java heap space jvm内存溢出错误,调整jvm内存参数无效;

网上搜索得知poi3.8使用SXSSFWorkbook大大优化了大数量的导出,经更换jar包后成功导出!

POI中SXSSFWorkbook专门处理大数据方法:

            OutputStream os = null;
            try {
                HttpServletResponse response = super.getResponse();
                response.setContentType("application/force-download"); // 设置下载类型
                String filename ="risk_event.xlsx";
                response.setHeader("Content-Disposition","attachment;filename=" + filename); // 设置文件的名称
                os = response.getOutputStream(); // 输出流
                SXSSFWorkbook wb = new SXSSFWorkbook(1000);//内存中保留 1000 条数据,以免内存溢出,其余写入 硬盘
                //获得该工作区的第一个sheet   
                Sheet sheet1 = wb.createSheet("sheet1"); 
                int excelRow = 0;
                //标题行
                Row titleRow = (Row) sheet1.createRow(excelRow++);
                for (int i = 0; i < columnList.size(); i++) {
                    Cell cell = titleRow.createCell(i);  
                    cell.setCellValue(columnList.get(i));
                }
                
                for (int m = 0; m < cycleCount; m++) {
                    List<List<String>> eventStrList = this.convertPageModelStrList();
                    if (eventStrList!= null && eventStrList.size() > 0) {
                        for (int i = 0; i < eventStrList.size(); i++) {
                            //明细行
                            Row contentRow = (Row) sheet1.createRow(excelRow++);
                            List<String> reParam = (List<String>) eventStrList.get(i);
                            for (int j = 0; j < reParam.size(); j++) {
                                Cell cell = contentRow.createCell(j);  
                                cell.setCellValue(reParam.get(j));
                            }
                        }
                    }
                }
                wb.write(os);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } // 关闭输出流
            }
下面是我在网上查询的关于SXSSFWorkbook构造方法api说明

SXSSFWorkbook构造方法API说明

public SXSSFWorkbook(int rowAccessWindowSize)
     * Construct an empty workbook and specify the window for row access.
       构造一个空的工作簿并指定对行访问的窗口
     * <p>
     * When a new node is created via createRow() and the total number
       当一个新的节点通过createRow()方法生成,并且未被清除的总的记录数目
     * of unflushed records would exceed the specified value, then the
       将会超过给定的值时,那么
     * row with the lowest index value is flushed and cannot be accessed
       索引值最小的那一行将被清除并且再也不能通过getRow()方法访问到了
     * via getRow() anymore.
     * </p>
     * <p>
     * A value of -1 indicates unlimited access. In this case all
       -1值表示不限制的访问. 在这种情形下,
     * records that have not been flushed by a call to flush() are available
       所有的未被清除的记录都可以通过调用flush方法来随机访问
     * for random access.
     * <p>
     * <p></p>
     * A value of 0 is not allowed because it would flush any newly created row
       0值是不允许的,因为它会在还没有指定单元格的情况下清除任何新建的行
     * without having a chance to specify any cells.
     * </p>
 

猜你喜欢

转载自blog.csdn.net/qq_38819293/article/details/85004982