JAVA CSV 导出

JAVA  CSV 文件导出比较简单

比使用sqlLoader 导出要慢一倍的,大体是在查询的机制上 耗了更多时间和 对数据进行组装也耗了时间
但是不需要安装和配置一堆东西
sqlLoad导出csv:https://blog.csdn.net/qq_37203082/article/details/110188164

 

  /**
    * Description
    * @param fileNanePath 文件名称路径 D:/test1.csv
    * @param tableHeaderArr 表头数组
    * @param cellList 数据数组 集合
    * @Author junwei
    * @Date 10:58 2020/11/27
    **/
    public static void uploadCsv(String fileNanePath,Object[] tableHeaderArr, List<Object[]> cellList){
        try {
            //  导出为CSV文件
            FileWriter writer = new FileWriter(fileNanePath);
            CSVPrinter printer = CSVFormat.EXCEL.print(writer);
            //录入表头
            if(tableHeaderArr!=null){
                printer.printRecord(tableHeaderArr);
            }
            for(Object [] cells :cellList ){
                printer.printRecord(cells);
            }
            printer.flush();
            printer.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        List<Object[]> cellList=new LinkedList<>();
        
        Object[] headCelss={"头部1","头部2","头部3"};


        Object[] cell1={"a1","a2","a3"};
        Object[] cell2={"b1","b2","b3"};
        Object[] cell3={"c1","c2","c3"};
        cellList.add(cell1);
        cellList.add(cell2);
        cellList.add(cell3);

        String fileName=  "test1.csv";
        String path="D:\\export\\";
        
        //导出CSV文件
        uploadCsv(path+fileName,headCelss,cellList);
    }

猜你喜欢

转载自blog.csdn.net/qq_37203082/article/details/110234696
今日推荐