@@ -83,14 +83,16 @@ public static <T> List<T> importExcel(Class<T> clazz, MultipartFile file) {
83
83
var cellNumIndex = new AtomicInteger ();
84
84
fields .forEach ((field , excel ) -> {
85
85
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 );
94
96
}
95
97
});
96
98
list .add (obj );
@@ -210,25 +212,32 @@ private static <T> void setCellValue(XSSFCell cell, T clazz, Field field, Excel
210
212
switch (excel .cellType ()) {
211
213
case STRING -> {
212
214
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 )) {
222
216
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 ;
223
231
}
232
+ cell .setCellValue (Double .parseDouble (stringValue ));
224
233
}
225
- case NUMBER -> cell .setCellValue (Double .parseDouble (stringValue ));
226
234
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 ;
231
237
}
238
+ var formatter = DateTimeFormatter .ofPattern (excel .dateFormat ());
239
+ var formattedDate = formatter .format (LocalDateTime .parse (stringValue ));
240
+ cell .setCellValue (formattedDate );
232
241
}
233
242
default -> throw new IllegalArgumentException ("不支持的单元格类型: " + excel .cellType ());
234
243
}
0 commit comments