JAVA读取csv文件,写入excel
前言:
从数据库查到数据,导出来为csv文件,写入指定格式的excel文件,按照某字段分组,将每组数据分别写入对应的sheet中,sheet名称为某字段名称
实现逻辑:
- 定义目录路径变量 directoryPath,表示CSV文件所在的目录路径。
- 创建一个 File 对象 directory,表示目录路径对应的文件对象。
- 通过 File.listFiles() 方法获取目录下所有以 ".csv" 结尾的文件对象数组 csvFiles。
- 创建一个 Csv 对象的列表 totalList,用于存储所有 CSV 文件读取后的数据。
- 如果 csvFiles 不为空,则循环遍历 csvFiles,将每个 CSV 文件读取后的 Csv 对象列表加入到 totalList 中。
- 创建一个 CsvExcel 对象的列表 excelList,用于存储所有 Csv 对象转换为 CsvExcel 对象后的数据。
- 循环遍历 totalList,将每个 Csv 对象转换为 CsvExcel 对象,并加入到 excelList 中。
- 使用 Java 8 的流式操作,将 excelList 按照场地名称分组,生成一个 Map
> groupMap。 - 创建一个 ExcelWriter 对象 writer,并指定输出文件路径。
- 循环遍历 groupMap,将每个分组的 CsvExcel 对象列表写入到对应的 sheet 中,并指定 sheet 名称为当前分组的场地名称。
- 完成 Excel 文件的写入操作,关闭 ExcelWriter 对象。
相关代码:
引入easyexcel:
com.alibaba easyexcel 3.0.5
读取csv文件实体类:
package com.application.test;import lombok.Data;/*** @program: lomir-path-tracing* @description:* @author: ext.manhengwei1* @create: 2023-02-27 14:40**/
@Data
public class Csv {//通道国标编码private String channelCode;//通道列表private String channelList;//区域private String region;//场地private String field;}
导出excel实体类 :
package com.application.test;import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;/*** @program: lomir-path-tracing* @description:* @author: ext.manhengwei1* @create: 2023-02-27 14:58**/
@Data
public class CsvExcel {//序号@ExcelProperty(index = 0,value = "序号")@ColumnWidth(10)private String id;//区域@ColumnWidth(10)@ExcelProperty(index = 1,value = "区域")private String region;//场地@ColumnWidth(20)@ExcelProperty(index = 2,value = "场地")private String field;//通道国标编码@ColumnWidth(20)@ExcelProperty(index = 3,value = "通道国标编码")private String channelCode;//通道列表@ColumnWidth(20)@ExcelProperty(index = 4,value = "通道列表")private String channelList;}
main方法:
public static void main(String[] args) {// 读取CSV文件夹String directoryPath = "C:\\Users\\ext.manhengwei1\\Desktop\\CSV文件夹";File directory = new File(directoryPath);File[] csvFiles = directory.listFiles((dir, name) -> name.endsWith(".csv"));List totalList = new ArrayList<>();if (csvFiles != null) {for (File file : csvFiles) {List csvList = ReadCsvUtils.readCsv(Csv.class, file.getAbsolutePath(), ",");totalList.addAll(csvList);}}List excelList = new ArrayList<>();for (Csv csv : totalList) {CsvExcel excel = new CsvExcel();excel.setRegion(csv.getRegion());excel.setField(csv.getField());excel.setChannelCode(csv.getChannelCode());excel.setChannelList(csv.getChannelList());excelList.add(excel);}// 按照场地名称分组Map> groupMap = excelList.stream().collect(Collectors.groupingBy(CsvExcel::getField));ExcelWriter writer = null;try {// 创建ExcelWriter对象writer = EasyExcel.write("C:\\Users\\ext.manhengwei1\\Desktop\\分拣暴力操作场地及摄像头清单总计Test.xlsx").build();// 遍历每个分组for (Map.Entry> entry : groupMap.entrySet()) {// 场地名称String field = entry.getKey();List groupList = entry.getValue();for (int i = 0; i < groupList.size(); i++) {groupList.get(i).setId(String.valueOf(i + 1));}// 写入当前分组的数据,并指定 sheet 的名称为当前分组的名称writer.write(groupList, EasyExcel.writerSheet(field).head(CsvExcel.class).build());}} catch (Exception e) {e.printStackTrace();} finally {if (writer != null) {// 完成Excel文件的写入writer.finish();}}}
ReadCsv工具类链接:
JAVA实现CSV文件转List<Object>_csv转对象 java_ᅟᅟᅟᅟᅟ的博客-CSDN博客
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
