POI export large quantities of data in Excel

As a tripartite POI database application operating in a wide range of Excel, this article focuses on export processing large quantities of data in Excel, version 4.1.0:

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>

HSSFWorkbook used for export, the following sample code, the code data derived 50,000 rows 20 columns, where the respective printing time, see:

package com.test.poi;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Demo {

    public static void main(String[] args) throws IOException {
        testHSSFWorkbook();
    }

    static void testHSSFWorkbook() throws IOException {
        long start = System.currentTimeMillis();
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < 20; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue("列" + i);
        }
        System.out.println("准备完表头时间:" + (System.currentTimeMillis() - start));
        for (int i = 0; i < 50000; i++) {
            HSSFRow rowInfo = sheet.createRow(i + 1);
            for (int j = 0; j < 20; j++) {
                HSSFCell cellInfo = rowInfo.createCell(j);
                cellInfo.setCellValue("列内容为" + j);
            }
        }
        System.out.println("准备完表内容时间:" + (System.currentTimeMillis() - start));
        FileOutputStream out = new FileOutputStream("D:\\test.xlsx");
        workbook.write(out);
        out.close();
        System.out.println("Input Completion Time:" + (System.currentTimeMillis () - Start)); 
        System.out.println ( "Finish" ); 
    } 

}

The output is:

After preparing the header time: 220
to prepare a table of contents End Time: 1730
input completion time: 4471
Finish

Guess you like

Origin www.cnblogs.com/silenceshining/p/11681924.html