原创

Java项目中生成.csv文件

1.真是项目中生成.csv文件需要先用sql查询到想要的数据,然后将这些数据生成.csv模板的文件。
2.查询数据

public void uploadFile() throws  Exception{
        List<List<BoeingTransaction>> list = new ArrayList<List<BoeingTransaction>>();
        List<BoeingTransaction> boeList = new ArrayList<BoeingTransaction>();
        //获取交易记录数据
        BoeingTransaction tran = new BoeingTransaction();
        tran.setIsExport("false");
        List<BoeingTransaction> boeingList = super.findList(tran);
            list.add(boeList);
            //调用文件创建方法
            File csvFile = createCSVFile(list);
    }

3.createCSVFile方法

public File createCSVFile(List<List<BoeingTransaction>> list){

        try {
            List<BoeingTask> listJob = quartzManager.getRunningJob();
            // 设置表格头
            Object[] head = {"Transaction ID", "trans_date", "emp_num","group", "trans_type", "loc", "P/N", "P/N_QTY","Item ID", "qty_moved", "Item_QTY" };
            List<Object> headList = Arrays.asList(head);

            // 导出文件路径
            DateFormat format = new SimpleDateFormat("yyMMdd");
            String formatDate = format.format(new Date());
            String downloadFilePath = filePathOK+"\\" + formatDate;
            // 导出文件名称
            String datetimeStr = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss");
            String fileName = "All Transactions_" + datetimeStr;
            File csvFile = CSVUtils.createCSVFile(headList, list, downloadFilePath, fileName);
            return csvFile;
        }catch (Exception e){
            e.printStackTrace();
            return null;
     }
    }
  1. CSVUtils工具类
    ```
    package com.jeesite.modules.utils;

import com.jeesite.modules.boeing.entity.BoeingTransaction;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**