大数据导出POI之SXSSFWorkbook

https://www.jianshu.com/p/df05c7fd6fa8

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();  
    } // 关闭输出流  
}



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页