java生成CSV文件

时间:2022-08-20 12:02:13

内容简介

本文内容使用java实现数据生成CSV文件,有关CSV文件知识请参考:CSV (逗号分隔值文件格式)

实现代码(仅供参考,请根据实现情况来修改)

java生成CSV文件java生成CSV文件
/**
 * 坐标点参数实体类
 */
public class PointsParamDto {
    /**
     * 坐标id(由1开始,累加1,这样的:1,2,3,4,5...)
     */
    private String pointId;

    /**
     * X 坐标点
     */
    private String x;

    /**
     * X 坐标点
     */
    private String y;

    public PointsParamDto(){}

    public PointsParamDto(String pointId,String x,String y){
        this.pointId = pointId;
        this.x = x;
        this.y = y;
    }

    public String getPointId() {
        return pointId;
    }

    public void setPointId(String pointId) {
        this.pointId = pointId;
    }

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

    public String getY() {
        return y;
    }

    public void setY(String y) {
        this.y = y;
    }

    public String toRow(){
        return String.format("%s,%s,%s",this.pointId,this.x,this.y);
    }
}
PointsParamDto

 

/**
 * 生成csv文件
 * @param pointsList
 * @return
 */
private void PointsToCsvFile(List<PointsParamDto> pointsList){
    if (pointsList!=null && pointsList.size() > 0){
        // 表格头
        String[] headArr = new String[]{"PointId", "X", "Y"};
        //CSV文件路径及名称
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String filePath = "E:\\TestCsvDirectory"; //CSV文件路径
        String fileName = "CSV_"+ df.format(localDateTime) +".csv";//CSV文件名称
        File csvFile = null;
        BufferedWriter csvWriter = null;
        try {
            csvFile = new File(filePath + File.separator + fileName);
            File parent = csvFile.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            csvFile.createNewFile();

            // GB2312使正确读取分隔符","
            csvWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"), 1024);

            //这部分在第一行居中展示文件名称,根据实际情况,可选择取消注释
            /*int num = headArr.length / 2;
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < num; i++) {
                buffer.append(",");
            }
            csvWriter.write(buffer.toString() + fileName + buffer.toString());
            csvWriter.newLine();*/

            // 写入文件头部标题行
            csvWriter.write(String.join(",", headArr));
            csvWriter.newLine();

            // 写入文件内容
            for (PointsParamDto points : pointsList) {
                csvWriter.write(points.toRow());
                csvWriter.newLine();
            }
            csvWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                csvWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

调用

List<PointsParamDto> pointsParamDtos = new ArrayList<>();
pointsParamDtos.add(new PointsParamDto("1","62.05932617","54.60449277"));
pointsParamDtos.add(new PointsParamDto("2","62.42138672","54.06616269"));
pointsParamDtos.add(new PointsParamDto("3","63.22387695","52.02270566"));
pointsParamDtos.add(new PointsParamDto("4","61.32324219","53.60449277"));
pointsParamDtos.add(new PointsParamDto("5","62.68603516","54.03320371"));
PointsToCsvFile(pointsParamDtos);

结果

生成文件如下图:

java生成CSV文件

打开效果如下:

java生成CSV文件