POI分多次向生成的EXCEL中寫入資料
阿新 • • 發佈:2018-12-09
一:分多次將資料寫入EXCEL
1:這種方式效率比較低,資料量越大越明顯,4萬條資料要2分鐘左右
package com.test; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class POIController { /** * 這種方式效率比較低,資料量越大越明顯,4萬條資料要2分鐘左右 * @param args * @throws FileNotFoundException * @throws InvalidFormatException */ public static void main(String[] args) throws FileNotFoundException, InvalidFormatException { long startTime = System.currentTimeMillis(); BufferedOutputStream outPutStream = null; XSSFWorkbook workbook = null; FileInputStream inputStream = null; String filePath = "E:\\txt\\111.xlsx"; try { workbook = getWorkBook(filePath); XSSFSheet sheet = workbook.getSheetAt(0); for (int i = 0; i < 40; i++) { for (int z = 0; z < 1000; z++) { XSSFRow row = sheet.createRow(i*1000+z); for (int j = 0; j < 10; j++) { row.createCell(j).setCellValue("你好:"+j); } } //每次要獲取新的檔案流物件,避免將之前寫入的資料覆蓋掉 outPutStream = new BufferedOutputStream(new FileOutputStream(filePath)); workbook.write(outPutStream); } } catch (IOException e) { e.printStackTrace(); }finally { if(outPutStream!=null) { try { outPutStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(inputStream!=null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(workbook!=null) { try { workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } long endTime = System.currentTimeMillis(); System.out.println(endTime-startTime); } /** * 先建立一個XSSFWorkbook物件 * @param filePath * @return */ public static XSSFWorkbook getWorkBook(String filePath) { XSSFWorkbook workbook = null; try { File fileXlsxPath = new File(filePath); BufferedOutputStream outPutStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath)); workbook = new XSSFWorkbook(); workbook.createSheet("測試"); workbook.write(outPutStream); } catch (Exception e) { e.printStackTrace(); } return workbook; } }
2:這種方式效率高一些,屬於一次性寫入,資料量越大越明顯,4萬條資料要17秒左右
package com.test; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class POIController { /** * 這種方式效率高一些,屬於一次性寫入,資料量越大越明顯,4萬條資料要17秒左右 * @param args * @throws FileNotFoundException * @throws InvalidFormatException */ public static void main(String[] args) throws FileNotFoundException, InvalidFormatException { long startTime = System.currentTimeMillis(); BufferedOutputStream outPutStream = null; XSSFWorkbook workbook = null; FileInputStream inputStream = null; String filePath = "E:\\txt\\111.xlsx"; try { workbook = getWorkBook(filePath); XSSFSheet sheet = workbook.getSheetAt(0); for (int i = 0; i < 40; i++) { for (int z = 0; z < 1000; z++) { XSSFRow row = sheet.createRow(i*1000+z); for (int j = 0; j < 10; j++) { row.createCell(j).setCellValue("你好:"+j); } } } //每次要獲取新的檔案流物件,避免將之前寫入的資料覆蓋掉 outPutStream = new BufferedOutputStream(new FileOutputStream(filePath)); workbook.write(outPutStream); } catch (IOException e) { e.printStackTrace(); }finally { if(outPutStream!=null) { try { outPutStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(inputStream!=null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(workbook!=null) { try { workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } long endTime = System.currentTimeMillis(); System.out.println(endTime-startTime); } /** * 先建立一個XSSFWorkbook物件 * @param filePath * @return */ public static XSSFWorkbook getWorkBook(String filePath) { XSSFWorkbook workbook = null; try { File fileXlsxPath = new File(filePath); BufferedOutputStream outPutStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath)); workbook = new XSSFWorkbook(); workbook.createSheet("測試"); workbook.write(outPutStream); } catch (Exception e) { e.printStackTrace(); } return workbook; } }