poi导出百万级数据 https://www.cnblogs.com/zou90512/p/3989450.html

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
package com.company.empms.test;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

public class POITest {
    /**
     * 
     * @Title: WriteExcel
     * @Description: 执行导出Excel操作
     * @param
     * @return boolean
     * @throws
     */
    public boolean WriteExcel(boolean isClose) {
 
        String excelFile = "F:/POITEST/bigData.xlsx";
        // 内存中只创建100个对象,写临时文件,当超过100条,就将内存中不用的对象释放。
        SXSSFWorkbook wb = new SXSSFWorkbook(100);
        Sheet sheet = null; // 工作表对象
        Row nRow = null; // 行对象
        Cell nCell = null; // 列对象
 
        try {
            
            long startTime = System.currentTimeMillis();
            System.out.println("开始执行时间 : " + startTime / 1000 + "m");
            int rowNo = 0; // 总行号
            int pageRowNo = 0; // 页行号
            System.out.println("当前sheet页为:" + rowNo / 300000 );
            sheet = wb.createSheet("我的第" + (rowNo / 300000 + 1) + "个工作簿");// 建立新的sheet对象
            sheet = wb.getSheetAt(rowNo / 300000); // 动态指定当前的工作表
            pageRowNo = 1; // 每当新建了工作表就将当前工作表的行号重置为1
            
            //定义表头
            nRow = sheet.createRow(0);
            Cell cel0 = nRow.createCell(0);  
            cel0.setCellValue("第一行");
            Cell cel2 = nRow.createCell(1);  
            cel2.setCellValue("第二行");  
            for(int ii =1;ii<=150000;ii++) {
                // 打印300000条后切换到下个工作表,可根据需要自行拓展,2百万,3百万...数据一样操作,只要不超过1048576就可以
                rowNo++;
                nRow = sheet.createRow(pageRowNo++); // 新建行对象
 
                // 打印每行,每行有6列数据 rsmd.getColumnCount()==6 --- 列属性的个数
                for (int i = 0; i < 2; i++) {
                    nCell = nRow.createCell(i);
                    nCell.setCellValue(ii);
                }
 
                if (rowNo % 10000 == 0) {
                    System.out.println("row no: " + rowNo);
                }
            }
 
            long finishedTime = System.currentTimeMillis(); // 处理完成时间
            System.out.println("数据读取完成耗时 : " + (finishedTime - startTime) / 1000 + "m");
            
            FileOutputStream fOut = new FileOutputStream(excelFile);//将数据写入Excel
            wb.write(fOut);
            fOut.flush(); // 刷新缓冲区
            fOut.close();
 
            long stopTime = System.currentTimeMillis(); // 写文件时间
            System.out.println("数据写入Excel表格中耗时 : " + (stopTime - startTime) / 1000 + "m");
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    //测试方法
    public static void main(String[] args) {
        POITest bdeo = new POITest();
        bdeo.WriteExcel(true);
    }
}

猜你喜欢

转载自www.cnblogs.com/lljboke/p/10785703.html