zoukankan      html  css  js  c++  java
  • Java导出CSV

    Java导出CSV

    • 使用jar包为csv-jar

    • maven坐标,我用的是1.3,现在已经有1.8了。

      <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-csv</artifactId>
          <version>1.3</version>
      </dependency>
      
      
    • Java代码

    /**
         * 获取系统类型
         * @return
         */
        public static String getOsName() {
            return System.getProperty("os.name");
        }
    
        /**
         * 返回文件名
         * @param excelUnit
         * @param name
         * @return
         */
        public static String exportCsvToFile(String name) {
            String osName = getOsName();
            String pathPrefix = "";
            if (osName.startsWith("Windows")) {
                File file=new File("C:\logFile");
                if(!file.exists()){
                    file.mkdir();
                }
                pathPrefix = "C:\logFile\";
            } else {
                File file=new File("/usr/apache-tomcat-6.0.44/workdata/logFile");
                if(!file.exists()){
                    file.mkdir();
                }
                pathPrefix = "/usr/apache-tomcat-6.0.44/workdata/logFile/";
            }
    
            String fileChName = pathPrefix + name + ".csv";
            BufferedWriter bufferedWriter = null;
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(fileChName);
                // 避免中文乱码
                byte[] bytes = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
                out.write(bytes);
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
                // 设置第一行标题
                String[] titleNames = new String[]{"时间","用户","来源","操作类型", "内容","操作IP"};
                CSVFormat csvFormat = CSVFormat.EXCEL.withHeader(titleNames);
                // 设置内容输出目标
                CSVPrinter csvPrinter = new CSVPrinter(bufferedWriter, csvFormat);
                // 输出数据
                String[] contents = new String[]{"时间xx","用户xx","来源xx","操作类型xx", "内容xx","操作IPxx"};
                List<String[]> cellValueList = new ArrayList<>();
                cellValueList.add(contents);
                for (int i = 0; i < cellValueList.size(); i++) {
                    csvPrinter.printRecord(cellValueList.get(i));
                }
                return fileChName;
    
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                try {
                    bufferedWriter.flush();
                } catch (Exception e){
                    e.printStackTrace();
                }
                try {
                    bufferedWriter.close();
                } catch (Exception e){
                    e.printStackTrace();
                }
                try {
                    out.close();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    

    • 稍微改动以上代码,标题和内容传入这个方法,就可以使用了。
  • 相关阅读:
    完成后台管理系统功能(三)查询商品信息列表
    完成后台管理系统功能(二)有关SSM的整合
    实现后台管理系统功能(一)maven工程的建立
    开始‘京西商城’的电商项目(SSM)
    到此,使用struts2+hibernate实现登陆以及学生列表的增删改查 结束 -------------------------------------------------------
    chrome 中 preview 和 response 数据不一致
    单元测试执行过程中忽略出错的单元测试
    使用Maven 构建时跳过单元测试
    使用 AutoIt3 加密解密数据
    SpringBoot 启动流程
  • 原文地址:https://www.cnblogs.com/darkclouds/p/14031166.html
Copyright © 2011-2022 走看看