Commit 0223c57a by zhangxingmin

oss-v1版本

parent e5a8e18a
......@@ -37,30 +37,45 @@ public class ExcelImportServiceImpl implements ExcelImportService {
ImportResult result = new ImportResult();
log.info("开始导入Excel,文件名:{},headerRow:{},dataStartRow:{}",
file.getOriginalFilename(), headerRow, dataStartRow);
try {
// 参数默认值处理
int headerRowNum = headerRow != null ? headerRow : 0;
int dataStartRowNum = dataStartRow != null ? dataStartRow : 1;
log.info("实际参数:headerRowNum={}, dataStartRowNum={}", headerRowNum, dataStartRowNum);
// 1. 获取表头信息
List<String> headers = getExcelHeaders(file, headerRowNum);
log.info("获取到表头:{}", headers);
result.setHeaders(headers);
// 2. 导入Excel数据
ExcelImportResult<Map<String, Object>> importResult =
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. 处理导入结果
List<Map<String, Object>> data =
processImportResult(importResult, headers);
log.info("处理后数据行数:{}", data.size());
result.setData(data);
result.setTotalCount(data.size());
// 4. 数据验证
if (requiredFields != null && !requiredFields.isEmpty()) {
ValidationResult validationResult =
validateData(data, requiredFields);
validateData(data, requiredFields, dataStartRowNum); // 传入dataStartRowNum
result.setValid(validationResult.isValid());
result.setErrorMessages(validationResult.getErrors());
} else {
......@@ -71,8 +86,13 @@ public class ExcelImportServiceImpl implements ExcelImportService {
result.setMessage("导入成功,共导入" + data.size() + "条数据");
} catch (Exception e) {
log.error("Excel导入失败", e);
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;
......@@ -100,26 +120,35 @@ public class ExcelImportServiceImpl implements ExcelImportService {
int headerRowNum,
int dataStartRowNum) throws Exception {
ImportParams params = new ImportParams();
// 关键修改:EasyPOI的startRows是从1开始计数的
// 所以我们需要将0-based索引转换为1-based索引
// params.setHeadRows(headerRowNum + 1); // 表头行数(从1开始)
// params.setTitleRows(0); // 不读取标题行
// params.setStartRows(dataStartRowNum); // 数据开始行(从0开始计数?需要测试)
// 检查文件是否为空
if (file == null || file.isEmpty()) {
throw new IllegalArgumentException("文件不能为空");
}
// 或者尝试这种方式:
params.setStartRows(0); // 从第0行开始读取
params.setHeadRows(headerRowNum); // 跳过表头前的行
// 检查文件格式
String originalFilename = file.getOriginalFilename();
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); // 需要校验
try {
// 使用Map接收数据
return ExcelImportUtil.importExcelMore(
file.getInputStream(),
Map.class,
params
);
} catch (Exception e) {
log.error("Excel导入失败,文件:{},headerRow:{},dataStartRow:{}",
originalFilename, headerRowNum, dataStartRowNum, e);
throw new RuntimeException("Excel解析失败:" + e.getMessage(), e);
}
}
/**
......@@ -245,25 +274,24 @@ public class ExcelImportServiceImpl implements ExcelImportService {
* @param requiredFields 必填字段
* @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();
result.setValid(true);
for (int i = 0; i < data.size(); i++) {
Map<String, Object> row = data.get(i);
int rowNum = i + 1; // 实际行号(从1开始)
int excelRowNum = dataStartRow + i + 1; // 计算Excel中的实际行号
// 检查必填字段
for (String field : requiredFields) {
Object value = row.get(field);
if (value == null || value.toString().trim().isEmpty()) {
result.setValid(false);
result.getErrors().add("第" + rowNum + "行,字段[" + field + "]不能为空");
result.getErrors().add("第" + excelRowNum + "行,字段[" + field + "]不能为空");
}
}
// 这里可以添加更多的验证规则
// 例如:数据类型验证、格式验证等
}
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