實現將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>