這篇文章主要用實例解析JAVA如何導(dǎo)出CSV文件,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的尚義網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
以前導(dǎo)出總是用POI導(dǎo)出為Excel文件,后來當(dāng)我了解到CSV以后,我發(fā)現(xiàn)速度飛快。
如果導(dǎo)出的數(shù)據(jù)不要求格式、樣式、公式等等,建議最好導(dǎo)成CSV文件,因為真的很快。
雖然我們可以用Java再帶的文件相關(guān)的類去操作以生成一個CSV文件,但事實上有好多第三方類庫也提供了類似的功能。
這里我們使用apache提供的commons-csv組件
Commons CSV
文檔在這里
http://commons.apache.org/
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("姓名", "年齡", "家鄉(xiāng)"); CSVPrinter csvPrinter = new CSVPrinter(osw, csvFormat);// csvPrinter = CSVFormat.DEFAULT.withHeader("姓名", "年齡", "家鄉(xiāng)").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")); Listlist = 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
CSV與EXCEL
/** * 測試寫100萬數(shù)據(jù)需要花費多長時間 */ @Test public void testMillion() throws Exception { int times = 10000 * 10; Object[] cells = {"滿100減15元", "100011", 15}; // 導(dǎo)出為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)); // 導(dǎo)出為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依賴
org.apache.commons commons-csv 1.5 org.apache.poi poi 3.17 org.apache.poi poi-ooxml 3.17 junit junit 4.12 test
最后,剛才的例子中只寫了3個字段,100萬行,生成的CSV文件有十幾二十兆,太多的話建議分多個文件打包下周,不然想象一個打開一個幾百兆的excel都費勁。
看完上述內(nèi)容,是不是對用實例解析JAVA如何導(dǎo)出CSV文件有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。