POI 3.8以后提供了一个SXSSFWorkbook 类用于需要大量写入数据时使用,读取还是用XSSFWorkbook
1 public static void main(String[] args) { 2 // XSSFWorkbook 普通写入和读取 3 // SXSSFWorkbook 超大量数据写入 4 long time1 = System.currentTimeMillis(); 5 File file = new File("test.xlsx"); 6 if (file.exists()) { 7 file.delete(); 8 } 9 file.createNewFile();// 创建文件 10 XSSFWorkbook wb1 = new XSSFWorkbook(); 11 FileOutputStream os = new FileOutputStream("test.xlsx"); 12 wb1.createSheet("test");// 创建第一张工作簿 13 wb1.write(os); 14 os.close(); 15 wb1.close(); 16 XSSFWorkbook wb2 = new XSSFWorkbook(new FileInputStream(file)); 17 SXSSFWorkbook swb = new SXSSFWorkbook(wb2, 100); 18 SXSSFSheet sh = swb.getSheetAt(0);// 获取第一张工作簿 19 for (int i = 0; i < 100000; i++) { 20 if (i == 0) {// 添加表头 21 Row row = sh.createRow(i); 22 row.createCell(0).setCellValue("1"); 23 row.createCell(1).setCellValue("2"); 24 row.createCell(2).setCellValue("3"); 25 row.createCell(3).setCellValue("4"); 26 row.createCell(4).setCellValue("5"); 27 row.createCell(5).setCellValue("6"); 28 row.createCell(6).setCellValue("7"); 29 row.createCell(7).setCellValue("8"); 30 row.createCell(8).setCellValue("9"); 31 row.createCell(9).setCellValue("10"); 32 } else { 33 Row row = sh.createRow(i); 34 row.createCell(0).setCellValue("测试"); 35 row.createCell(1).setCellValue("测试"); 36 row.createCell(2).setCellValue("测试"); 37 row.createCell(3).setCellValue("测试"); 38 row.createCell(4).setCellValue("测试"); 39 row.createCell(5).setCellValue("测试"); 40 row.createCell(6).setCellValue("测试"); 41 row.createCell(7).setCellValue("测试"); 42 row.createCell(8).setCellValue("测试"); 43 row.createCell(9).setCellValue("测试"); 44 } 45 } 46 FileOutputStream os1 = new FileOutputStream(file); 47 swb.write(os1); 48 os1.close(); 49 swb.close(); 50 long time2 = System.currentTimeMillis(); 51 System.out.println((time2 - time1)); 52 }
原文地址:https://www.cnblogs.com/lingdu9527/p/10997801.html
时间: 2024-10-14 01:11:41