Skip to content

Commit 7555c61

Browse files
committed
优化代码结构
1 parent 283c8b5 commit 7555c61

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

tang-commons/src/main/java/com/tang/commons/utils/poi/ExcelUtils.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ public static <T> List<T> importExcel(Class<T> clazz, MultipartFile file) {
8383
var cellNumIndex = new AtomicInteger();
8484
fields.forEach((field, excel) -> {
8585
var cell = row.getCell(cellNumIndex.getAndIncrement());
86-
if (excel.type() != Type.EXPORT) {
87-
try {
88-
ReflectionUtils.makeAccessible(field);
89-
var setMethod = clazz.getMethod("set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, field.getName()), field.getType());
90-
ReflectionUtils.invokeMethod(setMethod, obj, getCellValue(field, cell, excel));
91-
} catch (ReflectiveOperationException e) {
92-
LOGGER.error("设置值异常", e);
93-
}
86+
if (excel.type() == Type.EXPORT) {
87+
return;
88+
}
89+
90+
try {
91+
ReflectionUtils.makeAccessible(field);
92+
var setMethod = clazz.getMethod("set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, field.getName()), field.getType());
93+
ReflectionUtils.invokeMethod(setMethod, obj, getCellValue(field, cell, excel));
94+
} catch (ReflectiveOperationException e) {
95+
LOGGER.error("设置值异常", e);
9496
}
9597
});
9698
list.add(obj);
@@ -210,25 +212,32 @@ private static <T> void setCellValue(XSSFCell cell, T clazz, Field field, Excel
210212
switch (excel.cellType()) {
211213
case STRING -> {
212214
var dictType = excel.dictType();
213-
if (StringUtils.isNotBlank(dictType)) {
214-
var dictDataList = DictUtils.getDictDataList(dictType);
215-
var validationHelper = new XSSFDataValidationHelper(cell.getSheet());
216-
var validationConstraint = validationHelper.createExplicitListConstraint(dictDataList.stream().map(SysDictDataModel::getDataLabel).toList().toArray(new String[0]));
217-
var cellRangeAddressList = new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getColumnIndex());
218-
var validation = validationHelper.createValidation(validationConstraint, cellRangeAddressList);
219-
cell.setCellValue(DictUtils.getDictLabel(dictType, stringValue));
220-
cell.getSheet().addValidationData(validation);
221-
} else {
215+
if (StringUtils.isBlank(dictType)) {
222216
cell.setCellValue(stringValue);
217+
return;
218+
}
219+
220+
var dictDataList = DictUtils.getDictDataList(dictType);
221+
var validationHelper = new XSSFDataValidationHelper(cell.getSheet());
222+
var validationConstraint = validationHelper.createExplicitListConstraint(dictDataList.stream().map(SysDictDataModel::getDataLabel).toList().toArray(new String[0]));
223+
var cellRangeAddressList = new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getColumnIndex());
224+
var validation = validationHelper.createValidation(validationConstraint, cellRangeAddressList);
225+
cell.setCellValue(DictUtils.getDictLabel(dictType, stringValue));
226+
cell.getSheet().addValidationData(validation);
227+
}
228+
case NUMBER -> {
229+
if (StringUtils.isBlank(stringValue)) {
230+
return;
223231
}
232+
cell.setCellValue(Double.parseDouble(stringValue));
224233
}
225-
case NUMBER -> cell.setCellValue(Double.parseDouble(stringValue));
226234
case DATE -> {
227-
if (StringUtils.isNotBlank(stringValue)) {
228-
var formatter = DateTimeFormatter.ofPattern(excel.dateFormat());
229-
var formattedDate = formatter.format(LocalDateTime.parse(stringValue));
230-
cell.setCellValue(formattedDate);
235+
if (StringUtils.isBlank(stringValue)) {
236+
return;
231237
}
238+
var formatter = DateTimeFormatter.ofPattern(excel.dateFormat());
239+
var formattedDate = formatter.format(LocalDateTime.parse(stringValue));
240+
cell.setCellValue(formattedDate);
232241
}
233242
default -> throw new IllegalArgumentException("不支持的单元格类型: " + excel.cellType());
234243
}

0 commit comments

Comments
 (0)