JAVA CSV export

JAVA CSV file export is relatively simple

It is twice as slow as using sqlLoader to export. Generally speaking, it takes more time and time to assemble the data in the query mechanism,
but there is no need to install and configure a lot of things.
sqlLoad export 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);
    }

 

 

Guess you like

Origin blog.csdn.net/qq_37203082/article/details/110234696