以前匯出總是用POI匯出為Excel檔案,後來當我瞭解到CSV以後,我發現速度飛快。
如果匯出的資料不要求格式、樣式、公式等等,建議最好導成CSV檔案,因為真的很快。
雖然我們可以用Java再帶的檔案相關的類去操作以生成一個CSV檔案,但事實上有好多第三方類庫也提供了類似的功能。
這裡我們使用apache提供的commons-csv元件
Commons CSV
文件在這裡
http://commons.apache.org/proper/commons-csv/
http://commons.apache.org/proper/commons-csv/user-guide.html
先看一下具體用法
@Test public void testWrite() throws Exception { FileOutputStream fos = new FileOutputStream("E:/cjsworkspace/cjs-excel-demo/target/abc.csv"); OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK"); CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("姓名", "年齡", "家鄉"); CSVPrinter csvPrinter = new CSVPrinter(osw, csvFormat); // csvPrinter = CSVFormat.DEFAULT.withHeader("姓名", "年齡", "家鄉").print(osw); for (int i = 0; i < 10; i++) { csvPrinter.printRecord("張三", 20, "湖北"); } csvPrinter.flush(); csvPrinter.close(); } @Test public void testRead() throws IOException { InputStream is = new FileInputStream("E:/cjsworkspace/cjs-excel-demo/target/abc.csv"); InputStreamReader isr = new InputStreamReader(is, "GBK"); Reader reader = new BufferedReader(isr); CSVParser parser = CSVFormat.EXCEL.withHeader("name", "age", "jia").parse(reader); // CSVParser csvParser = CSVParser.parse(reader, CSVFormat.DEFAULT.withHeader("name", "age", "jia")); List<CSVRecord> list = parser.getRecords(); for (CSVRecord record : list) { System.out.println(record.getRecordNumber() + ":" + record.get("name") + ":" + record.get("age") + ":" + record.get("jia")); } parser.close(); } /** * Parsing an Excel CSV File */ @Test public void testParse() throws Exception { Reader reader = new FileReader("C:/Users/Administrator/Desktop/abc.csv"); CSVParser parser = CSVFormat.EXCEL.parse(reader); for (CSVRecord record : parser.getRecords()) { System.out.println(record); } parser.close(); } /** * Defining a header manually */ @Test public void testParseWithHeader() throws Exception { Reader reader = new FileReader("C:/Users/Administrator/Desktop/abc.csv"); CSVParser parser = CSVFormat.EXCEL.withHeader("id", "name", "code").parse(reader); for (CSVRecord record : parser.getRecords()) { System.out.println(record.get("id") + "," + record.get("name") + "," + record.get("code")); } parser.close(); } /** * Using an enum to define a header */ enum MyHeaderEnum { ID, NAME, CODE; } @Test public void testParseWithEnum() throws Exception { Reader reader = new FileReader("C:/Users/Administrator/Desktop/abc.csv"); CSVParser parser = CSVFormat.EXCEL.withHeader(MyHeaderEnum.class).parse(reader); for (CSVRecord record : parser.getRecords()) { System.out.println(record.get(MyHeaderEnum.ID) + "," + record.get(MyHeaderEnum.NAME) + "," + record.get(MyHeaderEnum.CODE)); } parser.close(); } private List<Map<String, String>> recordList = new ArrayList<>(); @Before public void init() { for (int i = 0; i < 5; i++) { Map<String, String> map = new HashMap<>(); map.put("name", "zhangsan"); map.put("code", "001"); recordList.add(map); } } @Test public void writeMuti() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(3); CountDownLatch doneSignal = new CountDownLatch(2); executorService.submit(new exprotThread("E:/0.csv", recordList, doneSignal)); executorService.submit(new exprotThread("E:/1.csv", recordList, doneSignal)); doneSignal.await(); System.out.println("Finish!!!"); } class exprotThread implements Runnable { private String filename; private List<Map<String, String>> list; private CountDownLatch countDownLatch; public exprotThread(String filename, List<Map<String, String>> list, CountDownLatch countDownLatch) { this.filename = filename; this.list = list; this.countDownLatch = countDownLatch; } @Override public void run() { try { CSVPrinter printer = new CSVPrinter(new FileWriter(filename), CSVFormat.EXCEL.withHeader("NAME", "CODE")); for (Map<String, String> map : list) { printer.printRecord(map.values()); } printer.close(); countDownLatch.countDown(); } catch (IOException e) { e.printStackTrace(); } } }
CSV與EXCEL
/** * 測試寫100萬資料需要花費多長時間 */ @Test public void testMillion() throws Exception { int times = 10000 * 10; Object[] cells = {"滿100減15元", "100011", 15}; // 匯出為CSV檔案 long t1 = System.currentTimeMillis(); FileWriter writer = new FileWriter("G:/test1.csv"); CSVPrinter printer = CSVFormat.EXCEL.print(writer); for (int i = 0; i < times; i++) { printer.printRecord(cells); } printer.flush(); printer.close(); long t2 = System.currentTimeMillis(); System.out.println("CSV: " + (t2 - t1)); // 匯出為Excel檔案 long t3 = System.currentTimeMillis(); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); for (int i = 0; i < times; i++) { XSSFRow row = sheet.createRow(i); for (int j = 0; j < cells.length; j++) { XSSFCell cell = row.createCell(j); cell.setCellValue(String.valueOf(cells[j])); } } FileOutputStream fos = new FileOutputStream("G:/test2.xlsx"); workbook.write(fos); fos.flush(); fos.close(); long t4 = System.currentTimeMillis(); System.out.println("Excel: " + (t4 - t3)); }
Maven依賴
<dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
最後,剛才的例子中只寫了3個欄位,100萬行,生成的CSV檔案有十幾二十兆,太多的話建議分多個檔案打包下週,不然想象一個開啟一個幾百兆的excel都費勁。