1. 程式人生 > >提高POI 讀寫效率

提高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(); } } } } }