优化商品导入Excel,获取真实的行数,剔除掉空白行

This commit is contained in:
季圣华
2023-02-12 18:14:35 +08:00
parent 71b53d0c90
commit 6999aac1ad
2 changed files with 29 additions and 2 deletions

View File

@@ -471,17 +471,19 @@ public class MaterialService {
} }
Workbook workbook = Workbook.getWorkbook(file.getInputStream()); Workbook workbook = Workbook.getWorkbook(file.getInputStream());
Sheet src = workbook.getSheet(0); Sheet src = workbook.getSheet(0);
//获取真实的行数,剔除掉空白行
int rightRows = ExcelUtils.getRightRows(src);
List<Depot> depotList= depotService.getDepot(); List<Depot> depotList= depotService.getDepot();
int depotCount = depotList.size(); int depotCount = depotList.size();
Map<String, Long> depotMap = parseDepotToMap(depotList); Map<String, Long> depotMap = parseDepotToMap(depotList);
User user = userService.getCurrentUser(); User user = userService.getCurrentUser();
List<MaterialWithInitStock> mList = new ArrayList<>(); List<MaterialWithInitStock> mList = new ArrayList<>();
//单次导入超出1000条 //单次导入超出1000条
if(src.getRows()>1002) { if(rightRows > 1002) {
throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_IMPORT_OVER_LIMIT_CODE, throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_IMPORT_OVER_LIMIT_CODE,
String.format(ExceptionConstants.MATERIAL_IMPORT_OVER_LIMIT_MSG)); String.format(ExceptionConstants.MATERIAL_IMPORT_OVER_LIMIT_MSG));
} }
for (int i = 2; i < src.getRows(); i++) { for (int i = 2; i < rightRows; i++) {
String name = ExcelUtils.getContent(src, i, 0); //名称 String name = ExcelUtils.getContent(src, i, 0); //名称
String standard = ExcelUtils.getContent(src, i, 1); //规格 String standard = ExcelUtils.getContent(src, i, 1); //规格
String model = ExcelUtils.getContent(src, i, 2); //型号 String model = ExcelUtils.getContent(src, i, 2); //型号

View File

@@ -207,6 +207,31 @@ public class ExcelUtils {
} }
} }
/**
* 获取真实的行数,剔除掉空白行
* @param src
* @return
*/
public static int getRightRows(Sheet src) {
int rsRows = src.getRows(); //行数
int rsCols = src.getColumns(); //列数
int nullCellNum;
int rightRows = rsRows;
for (int i = 1; i < rsRows; i++) { //统计行中为空的单元格数
nullCellNum = 0;
for (int j = 0; j < rsCols; j++) {
String val = src.getCell(j, i).getContents().trim();
if (StringUtils.isEmpty(val)) {
nullCellNum++;
}
}
if (nullCellNum >= rsCols) { //如果nullCellNum大于或等于总的列数
rightRows--; //行数减一
}
}
return rightRows;
}
public static String getDateContent(Sheet src, int rowNum, int colNum) { public static String getDateContent(Sheet src, int rowNum, int colNum) {
// 日期 类型的处理 // 日期 类型的处理
Cell c = src.getRow(rowNum)[colNum]; Cell c = src.getRow(rowNum)[colNum];