Java导出CSV文件
发布时间:2020-12-15 06:53:12 所属栏目:Java 来源:网络整理
导读:? 以前导出总是用POI导出为Excel文件,后来当我了解到CSV以后,我发现速度飞快。 如果导出的数据不要求格式、样式、公式等等,建议最好导成CSV文件,因为真的很快。 虽然我们可以用Java再带的文件相关的类去操作以生成一个CSV文件,但事实上有好多第三方类库
? 以前导出总是用POI导出为Excel文件,后来当我了解到CSV以后,我发现速度飞快。 如果导出的数据不要求格式、样式、公式等等,建议最好导成CSV文件,因为真的很快。 虽然我们可以用Java再带的文件相关的类去操作以生成一个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("姓名","年龄","家乡"); 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 void testRead() IOException { InputStream is = new FileInputStream("E:/cjsworkspace/cjs-excel-demo/target/abc.csv"); InputStreamReader isr = new InputStreamReader(is,1)">); Reader reader = BufferedReader(isr); CSVParser parser = CSVFormat.EXCEL.withHeader("name","age","jia").parse(reader); CSVParser csvParser = CSVParser.parse(reader,CSVFormat.DEFAULT.withHeader("name","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 void testParse() Exception { Reader reader = new FileReader("C:/Users/Administrator/Desktop/abc.csv"); CSVParser parser = CSVFormat.EXCEL.parse(reader); (CSVRecord record : parser.getRecords()) { System.out.println(record); } parser.close(); } * Defining a header manually void testParseWithHeader() ); CSVParser parser = CSVFormat.EXCEL.withHeader("id","name","code").parse(reader); (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 void testParseWithEnum() ); CSVParser parser = CSVFormat.EXCEL.withHeader(MyHeaderEnum.class (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 void init() { int i = 0; i < 5; i++) { Map<String,String> map = new HashMap<>(); map.put("name","zhangsan"); map.put("code","001"); recordList.add(map); } } @Test void writeMuti() 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"); } class exprotThread implements Runnable { private String filename; list; CountDownLatch countDownLatch; public exprotThread(String filename,List<Map<String,1)"> list,CountDownLatch countDownLatch) { this.filename = filename; this.list = list; this.countDownLatch = countDownLatch; } @Override 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万数据需要花费多长时间 void testMillion() 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); 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 = XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); ) { XSSFRow row = sheet.createRow(i); 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</artifactId>commons-csvversion>1.5> >org.apache.poi>poi>3.17>poi-ooxml> >junit>4.12scope>test> > 最后,刚才的例子中只写了3个字段,100万行,生成的CSV文件有十几二十兆,太多的话建议分多个文件打包下周,不然想象一个打开一个几百兆的excel都费劲。 ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |