Export Java POI for high-volume data to excel

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/weixin_39207535/article/details/86532151

When JavaPOI with export Excel, Excel version and should take into account the amount of data problems. For different versions of Excel, to use different tools.

  1. HSSFWorkbook: Excel2003 is operating before (including 2003) version of the extension is .xls; a table maximum support 65536 rows by 256 columns, that is to say a sheet page, exporting up to 6w multiple data

  2. XSSFWorkbook: Excel2007 version is operational, the extension is .xlsx; it's a table of maximum support 1048576 row, 16384

  3. SXSSFWorkbook: from POI 3.8 release, provides a basis XSSF of low memory usage of the API, which is an enhanced version of XSSF

Man of few words said on the code (demo)

jar package dependencies, Tell me about yourself Quguan network search

 

 

public class Test1 {
    
    public static List getUser() throws ParseException{
        List<Map<String,Object>> li = new ArrayList<>();
        SimpleDateFormat df =new SimpleDateFormat("yyyy-mm-dd");
        Map<String,Object> map=new HashMap<>();
        map.put("name", “张三”);
        map.put("phone", "15636545236");
        map.put("address", "北京市"));
        Map<String,Object> map1=new HashMap<>();
        map1.put("name", “李四”);
        map1.put("phone", "1366566566556");
        map1.put("address", "重庆市"));
        Map<String,Object> map2=new HashMap<>();
        map.put("name", “王五”);
        map.put("phone", "112365487955");
        map.put("address", "上海市"));
        
        li.add(map);
        li.add(map1);
        li.add(map2);
        return li;
    }
    
 
        //逻辑:1、创建工作簿-->2、创建工作表-->3、创建行-->4、创建表格/cell-->5、写入数据-->6、设置储存路径
    public static void main(String[] args) throws ParseException {
         List<Map<String,Object>>list = Test1.getUser();//获取数据
        //1、创建工作簿
        Workbook wb = new XSSFWorkbook();
            //1.1、设置表格的格式----居中
            CellStyle cs = wb.createCellStyle();
            cs.setAlignment(HorizontalAlignment.CENTER);
        //2.1、创建工作表
        Sheet sheet = wb.createSheet("学生信息表格");
                //2.2、合并单元格的方法
                sheet.addMergedRegion(new CellRangeAddress(4, 8, 5, 9));
        //3.1、创建行----表头行
        Row row = sheet.createRow(0);
        //4、创建格
        Cell cell = row.createCell(0);
                cell.setCellValue("姓名");
                cell.setCellStyle(cs);
            cell = row.createCell(1);
                cell.setCellValue("电话");
                cell.setCellStyle(cs);
            cell = row.createCell(2);
                cell.setCellValue("地址");
                cell.setCellStyle(cs);
        //5、写入实体数据
  
        for (int i = 0; i < list.size(); i++) {
            //3.2、创建行----内容行
            row = sheet.createRow(i+1);
            Map<String,Object> us = list.get(i);
                //第几行第几格  第一行第一格为“code”
            row.createCell(0).setCellValue(us.get("name").toString());
            row.createCell(1).setCellValue(us.get("phone").toString());
            row.createCell(2).setCellValue(us.get("address").toString());
        }
        
        //6、将文件储存到指定位置
        try {
            FileOutputStream fout = new FileOutputStream("g:\\uuu.xlsx");
            System.err.println("成功");//后台打印
            wb.write(fout);
            fout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

When large export data (when I export, probably about 30,000 data), will cause the system error:

HTTP Status 500 - Handler processing failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded

That is out of memory

This time should reflect the importance of the SXSSFWorkbook

At     the time created a workbook, adding judge

Workbook wb =null;

if(list.size()<1000){

    wb= new XSSFWorkbook();
}else{

    wb= new SXSSFWorkbook();

}

From SXSSFWorkbook official website we can know: Implementation of a Stream XSSFWorkbook version "BigGridDemo" strategy. This allows writing very large files without running out of memory because any time is only part of the line can be configured to be stored in memory. You can provide data as a basis for writing the template workbook. For more information, see https://poi.apache.org/spreadsheet/how-to.html#sxssf . Please note, may still consume large amounts of memory, which are based on the features you use, such as the merger area, the comment ...... still only stored in memory, so if widely used, could require large amounts of memory. SXSSFWorkbook default inline string instead of a shared string table. This is very effective, because there is no need to save the contents of the document in memory, but is also known to make certain customers is incompatible with the documents. In the case of a string of sharing enabled, unique string of all documents must be kept in memory. Depending on your document content, which could use more resources than shared string is disabled. Before deciding whether to enable shared string, please check your memory compatibility requirements and budget carefully.

Guess you like

Origin blog.csdn.net/weixin_39207535/article/details/86532151