成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術(shù)文章
文章詳情頁

JAVA導(dǎo)出CSV文件實(shí)例教程

瀏覽:3日期:2022-08-31 17:59:36

JAVA導(dǎo)出CSV文件實(shí)例教程

以前導(dǎo)出總是用POI導(dǎo)出為Excel文件,后來當(dāng)我了解到CSV以后,我發(fā)現(xiàn)速度飛快。

如果導(dǎo)出的數(shù)據(jù)不要求格式、樣式、公式等等,建議最好導(dǎo)成CSV文件,因?yàn)檎娴暮芸臁?/p>

雖然我們可以用Java再帶的文件相關(guān)的類去操作以生成一個(gè)CSV文件,但事實(shí)上有好多第三方類庫也提供了類似的功能。

這里我們使用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')); 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萬數(shù)據(jù)需要花費(fèi)多長時(shí)間 */ @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依賴

<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個(gè)字段,100萬行,生成的CSV文件有十幾二十兆,太多的話建議分多個(gè)文件打包下周,不然想象一個(gè)打開一個(gè)幾百兆的excel都費(fèi)勁。

以上就是JAVA導(dǎo)出CSV文件實(shí)例教程的詳細(xì)內(nèi)容,更多關(guān)于JAVA導(dǎo)出CSV文件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 久久久国产99久久国产久 | 欧美99| 草久在线播放 | 国内成人免费视频 | 久草网视频 | 美女啪啪网站又黄又免费 | 欧美黑寡妇特a级做爰 | 欧美一级高清片欧美国产欧美 | 亚洲精品综合 | 亚洲国产欧美精品一区二区三区 | 美国毛片在线 | 欧美a一 | 性欧美f| 免费观看成为人视频 | 国产精品所毛片视频 | 国产精品视频永久免费播放 | 美女张开腿让男生桶出水 | 日本波多野结衣视频 | 日本一区二区高清不卡 | 国产成人精品福利网站人 | 中文日韩字幕一区在线观看 | 亚洲综合无码一区二区 | 一级做a爰片久久毛片苍井优 | 91国偷自产一区二区三区 | 亚洲欧美日韩综合在线一区二区三区 | 国产精品久久久久无码av | 97在线观看完整免费 | 成人午夜视频在线观看 | 一个人看的www日本视频 | 日韩视频在线观看一区二区 | 亚洲精品区 | 五月久久亚洲七七综合中文网 | 美女和男人免费网站视频 | 台湾三级香港三级在线中文 | 九九九国产 | 日本一区三区二区三区四区 | 欧美人拘一级毛片 | 国产亚洲区 | 91久久香蕉国产线看观看软件 | 永久免费不卡在线观看黄网站 | 日韩毛片基地 |