Commit 0d6e0518 by zhouhaibao

refactor(StoreExcelImportService): 优化Excel导入服务中的行删除逻辑

- 简化了清除旧数据行的代码实现
- 移除了冗余的日志输出和复杂的样式清除操作
- 保留了核心的行删除功能,提高代码可读性
- 修复了可能存在的索引错位问题
1 parent 24440167
...@@ -1559,7 +1559,7 @@ public class StoreExcelImportService { ...@@ -1559,7 +1559,7 @@ public class StoreExcelImportService {
log.info("[步骤3/5] 参考行(第2行)行高:{} 点, 样式对象: {}", referenceRowHeight, referenceCellStyle); log.info("[步骤3/5] 参考行(第2行)行高:{} 点, 样式对象: {}", referenceRowHeight, referenceCellStyle);
} }
// 如果没有找到参考样式,创建一个默认的带边框样式作为保底 // 如果没有找到参考样式,创建一个默认的带边框样式作为保底
if (referenceCellStyle == null) { if (referenceCellStyle == null) {
referenceCellStyle = workbook.createCellStyle(); referenceCellStyle = workbook.createCellStyle();
// 设置默认边框为细实线(根据你的实际需求调整) // 设置默认边框为细实线(根据你的实际需求调整)
...@@ -1579,83 +1579,12 @@ public class StoreExcelImportService { ...@@ -1579,83 +1579,12 @@ public class StoreExcelImportService {
log.info("[步骤3/5] 清除旧数据行:从第{}行到第{}行", dataStartRow, lastRowNumBefore); log.info("[步骤3/5] 清除旧数据行:从第{}行到第{}行", dataStartRow, lastRowNumBefore);
for (int i = lastRowNumBefore; i >= dataStartRow; i--) { for (int i = lastRowNumBefore; i >= dataStartRow; i--) {
Row row = sheet.getRow(i); Row row = sheet.getRow(i);
log.info("[步骤3/5] 清除旧数据行:第{}行", i); if (row != null) {
if(i==lastRowNumBefore){
log.info("[步骤3/5] 清除旧数据行样式:第{}行", i);
// 1. 清除该行所有单元格的样式
// 注意:getLastCellNum() 返回的是列数索引+1,所以循环条件要注意
// 1. 清除该行涉及的所有合并单元格记录
// 倒序遍历合并区域列表,安全删除
for (int m = sheet.getNumMergedRegions() - 1; m >= 0; m--) {
CellRangeAddress region = sheet.getMergedRegion(m);
// 只要合并区域的“起始行”是当前行,就移除该合并记录
if (region.getFirstRow() == i) {
sheet.removeMergedRegion(m);
}
// 注意:如果合并区域跨越了当前行(例如从上一行合并下来),
// 通常 removeRow 后 Excel 会自动处理,或者你可以选择保留/调整。
// 这里主要解决“起始于本行”导致的报错或残留。
}
// 2. 强制清除该行所有单元格的样式(解决“读到13行”的核心)
int lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
cell.setCellStyle(null); // 移除样式引用
/* cell.setBlank(); // 将内容设为空值(比 null 更安全)*/
}
}
sheet.removeRow(row); sheet.removeRow(row);
}else {
if (row != null) {
sheet.removeRow(row);
}
} }
} }
log.info("[步骤3/5] 开始清除旧数据:从第{}行到第{}行", dataStartRow, lastRowNumBefore); log.info("[步骤3/5] 开始清除旧数据:从第{}行到第{}行", dataStartRow, lastRowNumBefore);
// 必须倒序遍历,防止删除行后索引错位
for (int i = lastRowNumBefore; i >= dataStartRow; i--) {
Row row = sheet.getRow(i);
// 如果该行不存在(可能是空行),跳过或继续(视情况而定,通常removeRow前需判空)
if (row == null) continue;
// --- 【关键处理】如果是尾行 (i == lastRowNumBefore) ---
if (i == lastRowNumBefore) {
log.info("检测到动态尾行:第{}行,正在清理合并区域与样式...", i + 1);
// 1. 清除该行涉及的所有合并单元格记录
// 倒序遍历合并区域列表,安全删除
for (int m = sheet.getNumMergedRegions() - 1; m >= 0; m--) {
CellRangeAddress region = sheet.getMergedRegion(m);
// 只要合并区域的“起始行”是当前行,就移除该合并记录
if (region.getFirstRow() == i) {
sheet.removeMergedRegion(m);
}
// 注意:如果合并区域跨越了当前行(例如从上一行合并下来),
// 通常 removeRow 后 Excel 会自动处理,或者你可以选择保留/调整。
// 这里主要解决“起始于本行”导致的报错或残留。
}
// 2. 强制清除该行所有单元格的样式(解决“读到13行”的核心)
int lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
cell.setCellStyle(null); // 移除样式引用
/* cell.setBlank(); // 将内容设为空值(比 null 更安全)*/
}
}
}
// 3. 物理删除该行
sheet.removeRow(row);
}
// 填充数据行 // 填充数据行
log.info("[步骤3/5] 开始填充数据行..."); log.info("[步骤3/5] 开始填充数据行...");
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!