1、新增货物类型模块

2、新增导入失败,导出excel功能
This commit is contained in:
2026-07-27 11:50:23 +08:00
parent fd98744b46
commit f0b515c2e0
88 changed files with 1791 additions and 215 deletions

View File

@@ -0,0 +1,132 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.common.excel;
import cn.idev.excel.FastExcel;
import cn.idev.excel.annotation.ExcelIgnore;
import cn.idev.excel.annotation.ExcelProperty;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* 导入失败明细 Excel 导出工具类
*
* @author Chill
*/
public class ImportFailureExcelUtil {
private static final String FAILURE_REASON = "导入失败原因";
private static final String ERROR_MESSAGE_FIELD = "errorMessage";
private ImportFailureExcelUtil() {
}
/**
* 导出导入失败明细
*
* @param response 响应
* @param fileName 文件名
* @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) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
String encodeFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
response.setHeader("Content-disposition", "attachment;filename=" + encodeFileName + ".xlsx");
List<Field> excelFields = excelFields(excelClass);
List<List<String>> head = buildHead(excelFields);
List<List<Object>> rows = buildRows(data, excelFields);
try {
FastExcel.write(response.getOutputStream()).head(head).sheet(sheetName).doWrite(rows);
} catch (IOException exception) {
throw new IllegalStateException("导出导入失败明细失败", exception);
}
}
private static List<Field> excelFields(Class<?> excelClass) {
return Arrays.stream(excelClass.getDeclaredFields())
.filter(field -> field.getAnnotation(ExcelProperty.class) != null)
.filter(field -> field.getAnnotation(ExcelIgnore.class) == null)
.filter(field -> !Objects.equals(field.getName(), ERROR_MESSAGE_FIELD))
.toList();
}
private static List<List<String>> buildHead(List<Field> excelFields) {
List<List<String>> head = new ArrayList<>();
for (Field field : excelFields) {
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
String[] value = excelProperty.value();
head.add(List.of(value.length == 0 ? field.getName() : value[0]));
}
head.add(List.of(FAILURE_REASON));
return head;
}
private static <T> List<List<Object>> buildRows(List<T> data, List<Field> excelFields) {
List<List<Object>> rows = new ArrayList<>();
for (T item : data) {
List<Object> row = new ArrayList<>();
for (Field field : excelFields) {
row.add(fieldValue(item, field));
}
row.add(errorMessage(item));
rows.add(row);
}
return rows;
}
private static Object fieldValue(Object item, Field field) {
try {
field.setAccessible(true);
return field.get(item);
} catch (IllegalAccessException exception) {
return null;
}
}
private static Object errorMessage(Object item) {
try {
Field field = item.getClass().getDeclaredField(ERROR_MESSAGE_FIELD);
field.setAccessible(true);
Object value = field.get(item);
return value == null ? "" : String.valueOf(value);
} catch (NoSuchFieldException | IllegalAccessException exception) {
return "";
}
}
}