E1ED922C1E9526DD63272D7EC5C6CB77
2020-11-02 b2fdfd80054cadaafd394b624224db3803933180
添加CVS导出
2个文件已修改
2个文件已添加
76 ■■■■■ 已修改文件
.idea/libraries/Maven__org_apache_commons_commons_csv_1_6.xml 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hx-common.iml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/util/CVSUtil.java 56 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.idea/libraries/Maven__org_apache_commons_commons_csv_1_6.xml
New file
@@ -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>
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" />
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>
src/main/java/com/hx/util/CVSUtil.java
New file
@@ -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();
    }
}