Commit 0223c57a by zhangxingmin

oss-v1版本

parent e5a8e18a
...@@ -37,30 +37,45 @@ public class ExcelImportServiceImpl implements ExcelImportService { ...@@ -37,30 +37,45 @@ public class ExcelImportServiceImpl implements ExcelImportService {
ImportResult result = new ImportResult(); ImportResult result = new ImportResult();
log.info("开始导入Excel,文件名:{},headerRow:{},dataStartRow:{}",
file.getOriginalFilename(), headerRow, dataStartRow);
try { try {
// 参数默认值处理 // 参数默认值处理
int headerRowNum = headerRow != null ? headerRow : 0; int headerRowNum = headerRow != null ? headerRow : 0;
int dataStartRowNum = dataStartRow != null ? dataStartRow : 1; int dataStartRowNum = dataStartRow != null ? dataStartRow : 1;
log.info("实际参数:headerRowNum={}, dataStartRowNum={}", headerRowNum, dataStartRowNum);
// 1. 获取表头信息 // 1. 获取表头信息
List<String> headers = getExcelHeaders(file, headerRowNum); List<String> headers = getExcelHeaders(file, headerRowNum);
log.info("获取到表头:{}", headers);
result.setHeaders(headers); result.setHeaders(headers);
// 2. 导入Excel数据 // 2. 导入Excel数据
ExcelImportResult<Map<String, Object>> importResult = ExcelImportResult<Map<String, Object>> importResult =
importExcel(file, headerRowNum, dataStartRowNum); importExcel(file, headerRowNum, dataStartRowNum);
System.out.println(JSON.toJSONString(importResult.getList())); log.info("EasyPOI导入结果:总行数={}, 成功行数={}, 失败行数={}",
importResult.getList() != null ? importResult.getList().size() : 0,
importResult.getList() != null ? "N/A" : "N/A",
importResult.isVerifyFail());
if (importResult.getList() != null) {
System.out.println("原始数据:" + JSON.toJSONString(importResult.getList()));
}
// 3. 处理导入结果 // 3. 处理导入结果
List<Map<String, Object>> data = List<Map<String, Object>> data =
processImportResult(importResult, headers); processImportResult(importResult, headers);
log.info("处理后数据行数:{}", data.size());
result.setData(data); result.setData(data);
result.setTotalCount(data.size()); result.setTotalCount(data.size());
// 4. 数据验证 // 4. 数据验证
if (requiredFields != null && !requiredFields.isEmpty()) { if (requiredFields != null && !requiredFields.isEmpty()) {
ValidationResult validationResult = ValidationResult validationResult =
validateData(data, requiredFields); validateData(data, requiredFields, dataStartRowNum); // 传入dataStartRowNum
result.setValid(validationResult.isValid()); result.setValid(validationResult.isValid());
result.setErrorMessages(validationResult.getErrors()); result.setErrorMessages(validationResult.getErrors());
} else { } else {
...@@ -71,8 +86,13 @@ public class ExcelImportServiceImpl implements ExcelImportService { ...@@ -71,8 +86,13 @@ public class ExcelImportServiceImpl implements ExcelImportService {
result.setMessage("导入成功,共导入" + data.size() + "条数据"); result.setMessage("导入成功,共导入" + data.size() + "条数据");
} catch (Exception e) { } catch (Exception e) {
log.error("Excel导入失败", e);
result.setSuccess(false); result.setSuccess(false);
result.setMessage("导入失败:" + e.getMessage()); String errorMsg = e.getMessage();
if (errorMsg == null || errorMsg.isEmpty()) {
errorMsg = e.getClass().getSimpleName();
}
result.setMessage("导入失败:" + errorMsg);
} }
return result; return result;
...@@ -100,28 +120,37 @@ public class ExcelImportServiceImpl implements ExcelImportService { ...@@ -100,28 +120,37 @@ public class ExcelImportServiceImpl implements ExcelImportService {
int headerRowNum, int headerRowNum,
int dataStartRowNum) throws Exception { int dataStartRowNum) throws Exception {
ImportParams params = new ImportParams(); // 检查文件是否为空
if (file == null || file.isEmpty()) {
// 关键修改:EasyPOI的startRows是从1开始计数的 throw new IllegalArgumentException("文件不能为空");
// 所以我们需要将0-based索引转换为1-based索引 }
// params.setHeadRows(headerRowNum + 1); // 表头行数(从1开始)
// params.setTitleRows(0); // 不读取标题行
// params.setStartRows(dataStartRowNum); // 数据开始行(从0开始计数?需要测试)
// 或者尝试这种方式: // 检查文件格式
params.setStartRows(0); // 从第0行开始读取 String originalFilename = file.getOriginalFilename();
params.setHeadRows(headerRowNum); // 跳过表头前的行 if (originalFilename == null ||
(!originalFilename.endsWith(".xlsx") && !originalFilename.endsWith(".xls"))) {
throw new IllegalArgumentException("仅支持Excel文件(.xlsx, .xls)");
}
params.setNeedVerify(true); // 关闭验证 ImportParams params = new ImportParams();
params.setHeadRows(headerRowNum + 1); // 表头行数
params.setStartRows(0); // 数据开始行
params.setNeedVerify(true); // 需要校验
// 使用Map接收数据 try {
return ExcelImportUtil.importExcelMore( // 使用Map接收数据
file.getInputStream(), return ExcelImportUtil.importExcelMore(
Map.class, file.getInputStream(),
params Map.class,
); params
);
} catch (Exception e) {
log.error("Excel导入失败,文件:{},headerRow:{},dataStartRow:{}",
originalFilename, headerRowNum, dataStartRowNum, e);
throw new RuntimeException("Excel解析失败:" + e.getMessage(), e);
}
} }
/** /**
* 获取Excel表头信息 * 获取Excel表头信息
* @param file Excel文件 * @param file Excel文件
...@@ -157,7 +186,7 @@ public class ExcelImportServiceImpl implements ExcelImportService { ...@@ -157,7 +186,7 @@ public class ExcelImportServiceImpl implements ExcelImportService {
if (cell == null) { if (cell == null) {
return null; return null;
} }
switch (cell.getCellType()) { switch (cell.getCellType()) {
case STRING: case STRING:
return cell.getStringCellValue(); return cell.getStringCellValue();
...@@ -238,32 +267,31 @@ public class ExcelImportServiceImpl implements ExcelImportService { ...@@ -238,32 +267,31 @@ public class ExcelImportServiceImpl implements ExcelImportService {
return hasOnlySystemFields; return hasOnlySystemFields;
} }
/** /**
* 验证导入数据 * 验证导入数据
* @param data 导入的数据 * @param data 导入的数据
* @param requiredFields 必填字段 * @param requiredFields 必填字段
* @return 验证结果 * @return 验证结果
*/ */
public static ValidationResult validateData(List<Map<String, Object>> data, List<String> requiredFields) { public static ValidationResult validateData(List<Map<String, Object>> data,
List<String> requiredFields,
int dataStartRow) { // 新增参数
ValidationResult result = new ValidationResult(); ValidationResult result = new ValidationResult();
result.setValid(true); result.setValid(true);
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
Map<String, Object> row = data.get(i); Map<String, Object> row = data.get(i);
int rowNum = i + 1; // 实际行号(从1开始) int excelRowNum = dataStartRow + i + 1; // 计算Excel中的实际行号
// 检查必填字段 // 检查必填字段
for (String field : requiredFields) { for (String field : requiredFields) {
Object value = row.get(field); Object value = row.get(field);
if (value == null || value.toString().trim().isEmpty()) { if (value == null || value.toString().trim().isEmpty()) {
result.setValid(false); result.setValid(false);
result.getErrors().add("第" + rowNum + "行,字段[" + field + "]不能为空"); result.getErrors().add("第" + excelRowNum + "行,字段[" + field + "]不能为空");
} }
} }
// 这里可以添加更多的验证规则
// 例如:数据类型验证、格式验证等
} }
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment