JAVA导出CSV文件实例教程
程序员文章站
2022-05-06 23:06:29
以前导出总是用poi导出为excel文件,后来当我了解到csv以后,我发现速度飞快。如果导出的数据不要求格式、样式、公式等等,建议最好导成csv文件,因为真的很快。虽然我们可以用java再带的文件相关...
以前导出总是用poi导出为excel文件,后来当我了解到csv以后,我发现速度飞快。
如果导出的数据不要求格式、样式、公式等等,建议最好导成csv文件,因为真的很快。
虽然我们可以用java再带的文件相关的类去操作以生成一个csv文件,但事实上有好多第三方类库也提供了类似的功能。
这里我们使用apache提供的commons-csv组件
commons csv
文档在这里
先看一下具体用法
@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都费劲。
以上就是java导出csv文件实例教程的详细内容,更多关于java导出csv文件的资料请关注其它相关文章!