修改物资设备清单导入修改

This commit is contained in:
2025-09-23 19:52:09 +08:00
parent a8744cc4cc
commit 278004788b
3 changed files with 48 additions and 16 deletions

View File

@ -1,10 +1,7 @@
package org.dromara.common.excel.coryUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
@ -84,7 +81,7 @@ public class ExcelReader {
SheetData sheetData = new SheetData();
sheetData.setSheetName(sheet.getSheetName());
processSheetData(sheet, sheetData);
processSheetData(workbook,sheet, sheetData);
sheetDataList.add(sheetData);
}
}
@ -114,7 +111,7 @@ public class ExcelReader {
sheetData.setSheetName(sheet.getSheetName());
// 处理单个sheet的数据
processSheetData(sheet, sheetData);
processSheetData(workbook, sheet, sheetData);
sheetDataList.add(sheetData);
}
@ -129,7 +126,7 @@ public class ExcelReader {
/**
* 处理单个工作表的数据
*/
private static void processSheetData(org.apache.poi.ss.usermodel.Sheet sheet, SheetData sheetData) {
private static void processSheetData(Workbook workbook, Sheet sheet, SheetData sheetData) {
boolean foundChineseStart = false;
List<List<String>> data = new ArrayList<>();
boolean isFirstRow = true;
@ -140,12 +137,12 @@ public class ExcelReader {
isFirstRow = false;
continue;
}
if(hasValidData(row)){
if(hasValidData(workbook,row)){
List<String> rowData = new ArrayList<>();
// 读取A到E列索引0到4
for (int cellIndex = 0; cellIndex < 6; cellIndex++) {
Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
rowData.add(getCellValue(cell));
rowData.add(getCellValue(workbook,cell));
}
// 检查是否找到中文数字开头的行
@ -165,11 +162,11 @@ public class ExcelReader {
sheetData.setData(data);
}
private static boolean hasValidData(Row row) {
private static boolean hasValidData(Workbook workbook,Row row) {
// 遍历行中的所有单元格
for (int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) {
Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String cellValue = getCellValue(cell).trim();
String cellValue = getCellValue(workbook, cell).trim();
// 只要有一个单元格有非空值,就认为是有效行
if (!cellValue.isEmpty()) {
@ -278,7 +275,7 @@ public class ExcelReader {
/**
* 获取单元格的值,处理不同数据类型
*/
private static String getCellValue(Cell cell) {
private static String getCellValue(Workbook workbook, Cell cell) {
if (cell == null) {
return "";
}
@ -293,6 +290,21 @@ public class ExcelReader {
return numericValue.substring(0, numericValue.length() - 2);
}
return numericValue;
case FORMULA:
//这样对于字符串cell.getStringCellValue()方法即可取得其值如果公式生成的是数值使用cell.getStringCellValue()方法会抛出IllegalStateException异常在异常处理中使用cell.getNumericCellValue();
// 1. 获取公式文本(如 "A1+B1"
String formula = cell.getCellFormula();
System.out.println("公式文本:" + formula);
// 2. 创建公式计算器(关键步骤:用于计算公式结果)
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// 3. 计算公式返回包含结果类型和值的CellValue对象
CellValue cellValue = evaluator.evaluate(cell);
// 4. 根据结果类型提取值
return getValue( cellValue.getCellType(), cellValue);
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
default:
@ -300,6 +312,19 @@ public class ExcelReader {
}
}
private static String getValue( CellType resultType, CellValue cellValue) {
return switch (resultType) {
case NUMERIC -> String.valueOf(cellValue.getNumberValue());
case STRING -> String.valueOf(cellValue.getStringValue());
case BOOLEAN -> String.valueOf(cellValue.getBooleanValue());
case ERROR -> String.valueOf(cellValue.getErrorValue());
case BLANK -> String.valueOf(CellType.BLANK);
default ->
// 保留原公式(不处理的类型)
cellValue.getStringValue();
};
}
/**
* 根据A列的值确定父节点的键
*/