实现将xlxs转化成csv,其中xlsx中时间格式为2012/03/04 06:04:03 PM ,转化后csv中时间为时间戳
实现将xlxs转化成csv,其中xlsx中时间格式为2012/03/04 06:04:03 PM ,转化后csv中时间为时间戳
serivce
import java.util.Date;
import java.util.Map;
public interface ExcelReaderService {
void excelReader(String basePath, String fileName,String exportDir);
void transformCsv(String basePath,String exportDir);
}
serviceImpl
@Service
@Slf4j
public class ExcelReaderServceImpl implements ExcelReaderService {
@Override
public void excelReader(String basePath, String fileName, String exportDir) {
String dir = basePath + "/" + fileName;
log.info("访问文件路径-{}", dir);
Map<String, String> exceptionReasons = new HashMap<>();
//TODO 需要提取文件夹内所有文件
//String path = "E:\\xxx.xlsx";
//List<Object> listMap = EasyExcel.read(path).sheet().doReadSync();
List<ExcelData> resultMap = new ArrayList<>();
try {
log.info("开始读取文件-{}",dir);
resultMap = EasyExcel.read(dir, ExcelData.class, new ExcelListen()).sheet().doReadSync();
} catch (Exception e) {
e.printStackTrace();
exceptionReasons.put(dir, e.getMessage());
}
//TODO 需要提取文件名字
List<List<Object>> datalist = new ArrayList<List<Object>>();
Date prevDate = null;
String prevValue = null;
for (ExcelData result : resultMap) {
if (result == null || result.getTime() == null) {
continue;
}
// 需要根据时间过滤数据
long time = result.getTime().getTime();
String timeStr = ConvertTimeStampToDate.stampToStringTime(time);
if (!StringUtils.hasText(timeStr)) {
continue;
}
//2022-02-24 11:04:04
//2022-02-24 11:04:04
//String timeStr = "2022-02-24 11:04:04";
Integer currMin = Integer.valueOf(timeStr.substring(timeStr.indexOf(":") + 1, timeStr.lastIndexOf(":")));
//分钟需要是偶数
if (currMin % 2 != 0) {
continue;
}
// 当前 时间 和值
//Map<Date, String> currTtimeAndValueMap = filterExcel(prevDate, prevValue, result.getTime(), result.getValue());
//
//prevDate = result.getTime();
//prevValue =result.getValue();
//System.out.println(substring);
//System.out.println();
String value = result.getValue();
List<Object> data = new ArrayList<Object>();
data.add(time);
data.add(value);
datalist.add(data);
}
final String databaseDir = "root.sh.hsh.chlh.";
String dataPointCode = fileName.substring(0, fileName.lastIndexOf("."));
// 实现excel读操作
List<Object> exportData = new ArrayList<>();
exportData.add("Time");
exportData.add(databaseDir + dataPointCode+"(DOUBLE)");
//TODO 路径暂且这样
//String path2 = "d:/exportCsv/";
String path2 = exportDir;
File file = CsvUtils.createCSVFile(exportData, datalist, path2, dataPointCode);
String fileName2 = file.getName();
System.out.println("文件名称:" + fileName2);
log.info("导入时异常信息-{}", exceptionReasons.toString());
}
@Override
public void transformCsv(String basePath, String exportDir) {
File dir = new File(basePath);
List<File> allFileList = new ArrayList<>();
// 判断文件夹是否存在
if (!dir.exists()) {
System.out.println("目录不存在");
return;
}
getAllFile(dir, allFileList);
//导出文件夹目录
File export = new File(exportDir);
List<File> allExprotFileList = new ArrayList<>();
// 判断文件夹是否存在
if (!export.exists()) {
System.out.println("目录不存在");
return;
}
getAllFile(export, allExprotFileList);
/**
* 提取源文件夹和导出文件夹名称对比,已存在的跳过
*/
List<String> allExprotFileListNames = new ArrayList<>();
if (!CollectionUtils.isEmpty(allExprotFileList)) {
for (File file : allExprotFileList) {
String name = file.getName().substring(0, file.getName().lastIndexOf("."));
allExprotFileListNames.add(name);
}
}
for (File file : allFileList) {
String name = null;
if (file == null) {
log.info("当前文件为空-{}", file);
continue;
} else {
name = file.getName().substring(0, file.getName().lastIndexOf("."));
}
if (!CollectionUtils.isEmpty(allExprotFileListNames) && allExprotFileListNames.contains(name)) {
log.info("当前文件在导出文件夹中已经存在,跳过,-{}-{}-{}", file.getName(), exportDir, file.getName());
continue;
}
log.info("当前访问的文件-{}", file.getName());
excelReader(basePath, file.getName(), exportDir);
}
}
public static void getAllFile(File fileInput, List<File> allFileList) {
// 获取文件列表
File[] fileList = fileInput.listFiles();
assert fileList != null;
for (File file : fileList) {
if (file.isDirectory()) {
// 递归处理文件夹
// 如果不想统计子文件夹则可以将下一行注释掉
getAllFile(file, allFileList);
} else {
// 如果是文件则将其加入到文件数组中
allFileList.add(file);
}
}
}
}
excel对应的model
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.util.Date;
@Data
public class ExcelData {
// 设置excel表头名称
//@ExcelProperty(value = "",index = 0)
//private String a;
//
//@ExcelProperty(value = "",index = 1)
//private String b;
//
//@ExcelProperty(value = "",index = 2)
//private String c;
//
//@ExcelProperty(value = "到期日", index = 3)
//private Date time;
//
//
//@ExcelProperty(value = "",index = 4)
//private String value;
@ExcelProperty(value = "到期日", index = 0)
private Date time;
@ExcelProperty(value = "",index = 1)
private String value;
//@ExcelProperty(value = "",index = 5)
//private String f;
}
单元测试用例
@Test
void testExeclReader() {
String basePath = "D:/temp";
String fileName = "xxx.xlsx";
String exportDir = "D:/temp/";
excelReaderService.excelReader(basePath, fileName, exportDir);
}
依赖
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.3</version>
</dependency>
不恋尘世浮华,不写红尘纷扰
标签:
工作中遇到的问题与解决方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)