|
@@ -16,6 +16,7 @@ import cn.hutool.poi.excel.ExcelReader;
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eco.common.core.config.EcoConfig;
|
|
|
+import org.eco.common.core.exception.BusinessException;
|
|
|
import org.eco.common.core.utils.DateUtils;
|
|
|
import org.eco.common.core.utils.StringUtils;
|
|
|
import org.eco.common.core.utils.uuid.Seq;
|
|
@@ -104,7 +105,7 @@ public class CsvUtils {
|
|
|
// 将JSON数组写入CSV文件
|
|
|
return FileUtil.writeLines(jsonArrayToCsv(jsonArray), csvFilePath, CharsetUtil.CHARSET_UTF_8);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("json转csv文件错误:{}", e.getMessage());
|
|
|
+ log.error("jsonToFileCsvByJsonArray: json转csv文件错误:{}", e.getMessage());
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
@@ -183,18 +184,21 @@ public class CsvUtils {
|
|
|
* @return 参数名称列表
|
|
|
*/
|
|
|
public static JSONArray getCsvDataByHeaders(String csvFilePath, List<String> headerNames, Integer step) {
|
|
|
+ log.info("参数列:{}", headerNames);
|
|
|
List<CsvRow> rows = getCsvRowList(EcoConfig.getProfile() + csvFilePath);
|
|
|
// 获取CSV表头,即第一行数据
|
|
|
List<String> headers = rows.get(0).getRawList();
|
|
|
+ log.info("CSV表头:{}", headers.toString());
|
|
|
List<Integer> indexList = new ArrayList<>();
|
|
|
headerNames.forEach(name -> {
|
|
|
int index = headers.indexOf(name);
|
|
|
if (index != -1) {
|
|
|
+ log.info("匹配到的表头:{},索引为:{}", name, index);
|
|
|
indexList.add(index);
|
|
|
}
|
|
|
});
|
|
|
if (CollectionUtil.isEmpty(indexList)) {
|
|
|
- return null;
|
|
|
+ throw new BusinessException("参数列匹配为空!");
|
|
|
}
|
|
|
// 去除第一行,保留后续数据
|
|
|
List<CsvRow> dataLines = rows.subList(1, rows.size());
|
|
@@ -212,17 +216,21 @@ public class CsvUtils {
|
|
|
if (step != null) {
|
|
|
jsonArray = reduceFrameRate(jsonArray, 10, step);
|
|
|
}
|
|
|
+ log.info("处理后数据:{}", jsonArray.size());
|
|
|
return jsonArray;
|
|
|
}
|
|
|
|
|
|
public static JSONObject getPlaybackByHeaders(String csvFilePath, List<String> headerNames, Integer step) {
|
|
|
+ log.info("getPlaybackByHeaders 参数列:{}", headerNames);
|
|
|
List<CsvRow> rows = getCsvRowList(EcoConfig.getProfile() + csvFilePath);
|
|
|
// 获取CSV表头,即第一行数据
|
|
|
List<String> headers = rows.get(0).getRawList();
|
|
|
+ log.info("getPlaybackByHeaders CSV表头:{}", headers.toString());
|
|
|
List<Integer> indexList = new ArrayList<>();
|
|
|
headerNames.forEach(name -> {
|
|
|
int index = headers.indexOf(name);
|
|
|
if (index != -1) {
|
|
|
+ log.info("匹配到的表头:{},索引为:{}", name, index);
|
|
|
indexList.add(index);
|
|
|
}
|
|
|
});
|
|
@@ -295,6 +303,32 @@ public class CsvUtils {
|
|
|
return jsonToFileCsv(json, csvFilePath);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * json转csv文件
|
|
|
+ *
|
|
|
+ * @param filePath path
|
|
|
+ * @param csvFilePath path
|
|
|
+ */
|
|
|
+ public static File txtToFileCsv(String filePath, String csvFilePath) {
|
|
|
+ List<String> lines = FileUtil.readLines(filePath, CharsetUtil.CHARSET_UTF_8);
|
|
|
+// // 读取Excel文件,获取ExcelReader
|
|
|
+// ExcelReader reader = ExcelUtil.getReader(filePath);
|
|
|
+// // 通过ExcelReader将Excel文件读取为List<Map>
|
|
|
+// List<Map<String, Object>> readAll = reader.readAll();
|
|
|
+// String json = JSONUtil.toJsonStr(readAll);
|
|
|
+ return txtToFileCsvByJsonArray(lines, csvFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static File txtToFileCsvByJsonArray(List<String> lines, String csvFilePath) {
|
|
|
+ try {
|
|
|
+ // 将JSON数组写入CSV文件
|
|
|
+ return FileUtil.writeLines(lines, csvFilePath, CharsetUtil.CHARSET_UTF_8);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("txtToFileCsvByJsonArray: txt转csv文件错误:{}", e.getMessage());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* json转csv文件
|
|
|
*
|