标签:span param dep csv inter tool import 依赖 except
一、添加依赖到pom.xml
<!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.6</version> </dependency>
二、上代码
package com.yungoal.dbtest.tools; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import org.apache.commons.io.FileUtils; import java.io.FileWriter; import java.io.IOException; import java.util.List; public class CsvOutputTool { /** * 写入csv文件 * @param headers 列头 * @param data 数据内容 * @param filePath 创建的csv文件路径 **/ public static void writeCsv(Object[] headers, List<Object[]> data, String filePath) throws IOException { //初始化csvformat CSVFormat formator = CSVFormat.DEFAULT.withRecordSeparator("\n"); //创建FileWriter对象 FileWriter fileWriter = new FileWriter(filePath, true); //创建CSVPrinter对象 CSVPrinter printer = new CSVPrinter(fileWriter, formator); //写入列头数据 printer.printRecord(headers); if (null != data) { //循环写入数据 for (Object[] lineData : data) { printer.printRecord(lineData); } } fileWriter.flush(); printer.close(true); fileWriter.close(); } }
JAVA在Windows使用apache commons-csv导出CSV解决方案
标签:span param dep csv inter tool import 依赖 except
原文地址:https://www.cnblogs.com/songxingzhu/p/10027991.html