1、调整导入失败,导出excel功能
2、调整规范
This commit is contained in:
@@ -28,7 +28,11 @@ package org.springblade.common.excel;
|
||||
import cn.idev.excel.FastExcel;
|
||||
import cn.idev.excel.annotation.ExcelIgnore;
|
||||
import cn.idev.excel.annotation.ExcelProperty;
|
||||
import cn.idev.excel.write.metadata.style.WriteCellStyle;
|
||||
import cn.idev.excel.write.metadata.style.WriteFont;
|
||||
import cn.idev.excel.write.style.HorizontalCellStyleStrategy;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -48,6 +52,7 @@ public class ImportFailureExcelUtil {
|
||||
|
||||
private static final String FAILURE_REASON = "导入失败原因";
|
||||
private static final String ERROR_MESSAGE_FIELD = "errorMessage";
|
||||
private static final String FAILURE_REASON_FIELD = "failureReason";
|
||||
|
||||
private ImportFailureExcelUtil() {
|
||||
}
|
||||
@@ -60,9 +65,8 @@ public class ImportFailureExcelUtil {
|
||||
* @param sheetName 工作表名
|
||||
* @param data 失败数据
|
||||
* @param excelClass 原导入 Excel 类型
|
||||
* @param <T> 泛型
|
||||
*/
|
||||
public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> data, Class<T> excelClass) {
|
||||
public static void export(HttpServletResponse response, String fileName, String sheetName, List<?> data, Class<?> excelClass) {
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
String encodeFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
|
||||
@@ -71,7 +75,11 @@ public class ImportFailureExcelUtil {
|
||||
List<List<String>> head = buildHead(excelFields);
|
||||
List<List<Object>> rows = buildRows(data, excelFields);
|
||||
try {
|
||||
FastExcel.write(response.getOutputStream()).head(head).sheet(sheetName).doWrite(rows);
|
||||
FastExcel.write(response.getOutputStream())
|
||||
.registerWriteHandler(failureRowStyle())
|
||||
.head(head)
|
||||
.sheet(sheetName)
|
||||
.doWrite(rows);
|
||||
} catch (IOException exception) {
|
||||
throw new IllegalStateException("导出导入失败明细失败", exception);
|
||||
}
|
||||
@@ -82,6 +90,7 @@ public class ImportFailureExcelUtil {
|
||||
.filter(field -> field.getAnnotation(ExcelProperty.class) != null)
|
||||
.filter(field -> field.getAnnotation(ExcelIgnore.class) == null)
|
||||
.filter(field -> !Objects.equals(field.getName(), ERROR_MESSAGE_FIELD))
|
||||
.filter(field -> !Objects.equals(field.getName(), FAILURE_REASON_FIELD))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -96,9 +105,9 @@ public class ImportFailureExcelUtil {
|
||||
return head;
|
||||
}
|
||||
|
||||
private static <T> List<List<Object>> buildRows(List<T> data, List<Field> excelFields) {
|
||||
private static List<List<Object>> buildRows(List<?> data, List<Field> excelFields) {
|
||||
List<List<Object>> rows = new ArrayList<>();
|
||||
for (T item : data) {
|
||||
for (Object item : data) {
|
||||
List<Object> row = new ArrayList<>();
|
||||
for (Field field : excelFields) {
|
||||
row.add(fieldValue(item, field));
|
||||
@@ -111,16 +120,17 @@ public class ImportFailureExcelUtil {
|
||||
|
||||
private static Object fieldValue(Object item, Field field) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
return field.get(item);
|
||||
} catch (IllegalAccessException exception) {
|
||||
Field targetField = targetField(item.getClass(), field.getName());
|
||||
targetField.setAccessible(true);
|
||||
return targetField.get(item);
|
||||
} catch (NoSuchFieldException | IllegalAccessException exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Object errorMessage(Object item) {
|
||||
try {
|
||||
Field field = item.getClass().getDeclaredField(ERROR_MESSAGE_FIELD);
|
||||
Field field = targetField(item.getClass(), ERROR_MESSAGE_FIELD, FAILURE_REASON_FIELD);
|
||||
field.setAccessible(true);
|
||||
Object value = field.get(item);
|
||||
return value == null ? "" : String.valueOf(value);
|
||||
@@ -129,4 +139,28 @@ public class ImportFailureExcelUtil {
|
||||
}
|
||||
}
|
||||
|
||||
private static Field targetField(Class<?> itemClass, String... fieldNames) throws NoSuchFieldException {
|
||||
Class<?> currentClass = itemClass;
|
||||
while (currentClass != null) {
|
||||
for (String fieldName : fieldNames) {
|
||||
try {
|
||||
return currentClass.getDeclaredField(fieldName);
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
// 继续查找其他字段名或父类字段。
|
||||
}
|
||||
}
|
||||
currentClass = currentClass.getSuperclass();
|
||||
}
|
||||
throw new NoSuchFieldException(String.join(",", fieldNames));
|
||||
}
|
||||
|
||||
private static HorizontalCellStyleStrategy failureRowStyle() {
|
||||
WriteFont contentFont = new WriteFont();
|
||||
contentFont.setColor(IndexedColors.RED.getIndex());
|
||||
|
||||
WriteCellStyle contentStyle = new WriteCellStyle();
|
||||
contentStyle.setWriteFont(contentFont);
|
||||
return new HorizontalCellStyleStrategy(null, contentStyle);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user