From b2fdfd80054cadaafd394b624224db3803933180 Mon Sep 17 00:00:00 2001 From: E1ED922C1E9526DD63272D7EC5C6CB77 <E1ED922C1E9526DD63272D7EC5C6CB77@i-search.com.cn> Date: 星期一, 02 十一月 2020 19:46:51 +0800 Subject: [PATCH] 添加CVS导出 --- .idea/libraries/Maven__org_apache_commons_commons_csv_1_6.xml | 13 ++++++ pom.xml | 6 +++ hx-common.iml | 1 src/main/java/com/hx/util/CVSUtil.java | 56 ++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 0 deletions(-) diff --git a/.idea/libraries/Maven__org_apache_commons_commons_csv_1_6.xml b/.idea/libraries/Maven__org_apache_commons_commons_csv_1_6.xml new file mode 100644 index 0000000..55a72ff --- /dev/null +++ b/.idea/libraries/Maven__org_apache_commons_commons_csv_1_6.xml @@ -0,0 +1,13 @@ +<component name="libraryTable"> + <library name="Maven: org.apache.commons:commons-csv:1.6"> + <CLASSES> + <root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-csv/1.6/commons-csv-1.6.jar!/" /> + </CLASSES> + <JAVADOC> + <root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-csv/1.6/commons-csv-1.6-javadoc.jar!/" /> + </JAVADOC> + <SOURCES> + <root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-csv/1.6/commons-csv-1.6-sources.jar!/" /> + </SOURCES> + </library> +</component> \ No newline at end of file diff --git a/hx-common.iml b/hx-common.iml index e2d5751..4afd238 100644 --- a/hx-common.iml +++ b/hx-common.iml @@ -21,6 +21,7 @@ <orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="library" name="Maven: com.sun.mail:javax.mail:1.6.2" level="project" /> <orderEntry type="library" name="Maven: javax.activation:activation:1.1" level="project" /> + <orderEntry type="library" name="Maven: org.apache.commons:commons-csv:1.6" level="project" /> <orderEntry type="library" name="Maven: com.aliyun.oss:aliyun-sdk-oss:2.8.3" level="project" /> <orderEntry type="library" name="Maven: org.bouncycastle:bcprov-jdk15on:1.66" level="project" /> <orderEntry type="library" name="Maven: org.dom4j:dom4j:2.1.3" level="project" /> diff --git a/pom.xml b/pom.xml index 66d0d15..5900039 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,12 @@ </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-csv</artifactId> + <version>1.6</version> + </dependency> + + <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> </dependency> diff --git a/src/main/java/com/hx/util/CVSUtil.java b/src/main/java/com/hx/util/CVSUtil.java new file mode 100644 index 0000000..3a71f6c --- /dev/null +++ b/src/main/java/com/hx/util/CVSUtil.java @@ -0,0 +1,56 @@ +package com.hx.util; + +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.util.List; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; + +/** + * CVS宸ュ叿绫� + * + * @author mgchen + * + */ +public class CVSUtil { + + public void writeToCVS() 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("濮撳悕", "骞撮緞", "瀹朵埂"); + // CSVFormat.DEFAULT.withHeader(header) + 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(); + + } + + public static void writeToCVS(String path, String[] headers, List<List<Object>> data) throws Exception { + FileOutputStream fos = new FileOutputStream(path); + byte[] uft8bom = { (byte) 0xef, (byte) 0xbb, (byte) 0xbf }; + fos.write(uft8bom); + OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8"); + + CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(headers); + CSVPrinter csvPrinter = new CSVPrinter(osw, csvFormat); + + for (int i = 0; i < data.size(); i++) { + csvPrinter.printRecord(data.get(i)); + } + + csvPrinter.flush(); + csvPrinter.close(); + osw.close(); + fos.close(); + + } +} -- Gitblit v1.8.0