提高POI 读写效率

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

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;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

public class TestMain {

    @Test
    public void testExcelByteBuffer() {

        Runtime ru = Runtime.getRuntime();
        long totalMemoryStart = ru.totalMemory();
        long freeMemoryStart = ru.freeMemory();
        long consumeMemoryStart = totalMemoryStart - freeMemoryStart;
        long beginTime = System.currentTimeMillis();
        System.err.println(String.format(">>>> begin <<<< total: %s, free: %s, consume: %s", totalMemoryStart, freeMemoryStart, consumeMemoryStart));
        // 创建excle
        XSSFWorkbook work = new XSSFWorkbook();
        // 创建一个sheet表
        XSSFSheet sheet = work.createSheet("test");
        XSSFRow firstRow = sheet.createRow(0);
        for (int n = 0; n < 10; n++) {
            XSSFCell cell = firstRow.createCell(n);
            cell.setCellValue(String.format("第%s列", n));
        }
        // 创建10列,20000 行数据
        for (int i = 1; i <= 20000; i++) {
            XSSFRow row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                XSSFCell cell = row.createCell(j);
                cell.setCellValue(String.format("%s , %s", i + "行", j + "列"));
            }
        }

        long totalMemoryEnd = ru.totalMemory();
        long freeMemoryEnd = ru.freeMemory();
        long consumeMemoryEnd = totalMemoryEnd - freeMemoryEnd;
        long endTime = System.currentTimeMillis();
        long consumeTime = endTime - beginTime;
        System.err.println(String.format(">>>> end <<<< total: %s, free: %s, consume: %s, memory increase: %s, time: %s ms",
                totalMemoryEnd, freeMemoryEnd, consumeMemoryEnd, consumeMemoryEnd - consumeMemoryStart, consumeTime));

        FileOutputStream outStream = null;
        // 写出文件
        try {
            File outFile = new File("E:/logs/test1.xlsx");
            outStream = new FileOutputStream(outFile);
            work.write(outStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != outStream) {
                try {
                    outStream.flush();
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testSXSSFWorkbookByteBuffer() {

        Runtime ru = Runtime.getRuntime();
        long totalMemoryStart = ru.totalMemory();
        long freeMemoryStart = ru.freeMemory();
        long consumeMemoryStart = totalMemoryStart - freeMemoryStart;
        long beginTime = System.currentTimeMillis();
        System.err.println(String.format(">>>> begin <<<< total: %s, free: %s, consume: %s", totalMemoryStart, freeMemoryStart, consumeMemoryStart));
        /** OPCPackage方式只能用于读,大文件读可以减小内存消耗
        OPCPackage opcPackage = OPCPackage.create("E:/logs/test.xlsx");
        **/
        SXSSFWorkbook work;
        try {
            work = new SXSSFWorkbook(100);
        } catch (Exception e) {
            System.err.println(e);
            return;
        }
        // 创建一个sheet表
        Sheet sheet = work.createSheet("test");
        Row firstRow = sheet.createRow(0);
        for (int n = 0; n < 10; n++) {
            Cell cell = firstRow.createCell(n);
            cell.setCellValue(String.format("第%s列", n));
        }
        // 创建10列,20000 行数据
        for (int i = 1; i <= 20000; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(String.format("%s , %s", i + "行", j + "列"));
            }
        }

        long totalMemoryEnd = ru.totalMemory();
        long freeMemoryEnd = ru.freeMemory();
        long consumeMemoryEnd = totalMemoryEnd - freeMemoryEnd;
        long endTime = System.currentTimeMillis();
        long consumeTime = endTime - beginTime;
        System.err.println(String.format(">>>> end <<<< total: %s, free: %s, consume: %s, memory increase: %s, time: %s ms",
                totalMemoryEnd, freeMemoryEnd, consumeMemoryEnd, consumeMemoryEnd - consumeMemoryStart, consumeTime));

        FileOutputStream outStream = null;
        // 写出文件
        try {
            File outFile = new File("E:/logs/test2.xlsx");
            outStream = new FileOutputStream(outFile);
            work.write(outStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != outStream) {
                try {
                    outStream.flush();
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值