|
@@ -16,14 +16,20 @@ import lombok.AccessLevel;
|
|
|
import lombok.NoArgsConstructor;
|
|
|
import org.dromara.common.core.utils.StringUtils;
|
|
|
import org.dromara.common.core.utils.file.FileUtils;
|
|
|
+import org.dromara.common.core.utils.reflect.ReflectUtils;
|
|
|
+import org.dromara.common.excel.annotation.ExcelNotation;
|
|
|
+import org.dromara.common.excel.annotation.ExcelRequired;
|
|
|
import org.dromara.common.excel.convert.ExcelBigNumberConvert;
|
|
|
import org.dromara.common.excel.core.*;
|
|
|
+import org.dromara.common.excel.handler.DataWriteHandler;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -433,4 +439,91 @@ public class ExcelUtil {
|
|
|
return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx";
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取必填列Map
|
|
|
+ *
|
|
|
+ * @param clazz 类class
|
|
|
+ * @return java.util.Map<java.lang.Integer, java.lang.Short>
|
|
|
+ * @author SunLingDa
|
|
|
+ * @date 2022/11/3 13:23
|
|
|
+ */
|
|
|
+ private static Map<Integer, Short> getRequiredMap(Class<?> clazz) {
|
|
|
+ Map<Integer, Short> requiredMap = new HashMap<>();
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
+ // 检查 fields 数组是否为空
|
|
|
+ if (fields.length == 0) {
|
|
|
+ return requiredMap;
|
|
|
+ }
|
|
|
+ Field[] filteredFields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName()));
|
|
|
+
|
|
|
+ for (int i = 0; i < filteredFields.length; i++) {
|
|
|
+ Field field = filteredFields[i];
|
|
|
+ if (!field.isAnnotationPresent(ExcelRequired.class)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ExcelRequired excelRequired = field.getAnnotation(ExcelRequired.class);
|
|
|
+ int columnIndex = excelRequired.index() == -1 ? i : excelRequired.index();
|
|
|
+ requiredMap.put(columnIndex, excelRequired.fontColor().getIndex());
|
|
|
+ }
|
|
|
+ return requiredMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取批注Map
|
|
|
+ *
|
|
|
+ * @param clazz 类class
|
|
|
+ * @return java.util.Map<java.lang.Integer, java.lang.String>
|
|
|
+ * @author SunLingDa
|
|
|
+ * @date 2022/11/3 13:24
|
|
|
+ */
|
|
|
+ private static Map<Integer, String> getNotationMap(Class<?> clazz) {
|
|
|
+ Map<Integer, String> notationMap = new HashMap<>();
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
+ // 检查 fields 数组是否为空
|
|
|
+ if (fields.length == 0) {
|
|
|
+ return notationMap;
|
|
|
+ }
|
|
|
+ Field[] filteredFields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName()));
|
|
|
+ for (int i = 0; i < filteredFields.length; i++) {
|
|
|
+ Field field = filteredFields[i];
|
|
|
+ if (!field.isAnnotationPresent(ExcelNotation.class)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ExcelNotation excelNotation = field.getAnnotation(ExcelNotation.class);
|
|
|
+ int columnIndex = excelNotation.index() == -1 ? i : excelNotation.index();
|
|
|
+ notationMap.put(columnIndex, excelNotation.value());
|
|
|
+ }
|
|
|
+ return notationMap;
|
|
|
+ }
|
|
|
+ public static <T> void exportExcelRequire(List<T> list, String sheetName, Class<T> clazz,HttpServletResponse response) {
|
|
|
+ exportExcelTemplate(list,sheetName,clazz,response);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 导出excel模板
|
|
|
+ *
|
|
|
+ * @param list 导出数据集合
|
|
|
+ * @param sheetName 工作表的名称
|
|
|
+ * @param clazz 实体类
|
|
|
+ * @param response 响应体
|
|
|
+ */
|
|
|
+ public static <T> void exportExcelTemplate(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) {
|
|
|
+ try {
|
|
|
+ Map<Integer, Short> requiredMap = getRequiredMap(clazz);
|
|
|
+ Map<Integer, String> notationMap = getNotationMap(clazz);
|
|
|
+ resetResponse(sheetName, response);
|
|
|
+ ServletOutputStream os = response.getOutputStream();
|
|
|
+ DataWriteHandler writeHandler = new DataWriteHandler(notationMap, requiredMap);
|
|
|
+ ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz)
|
|
|
+ .autoCloseStream(false)
|
|
|
+ // 自动适配
|
|
|
+ .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
|
+ .registerWriteHandler(writeHandler)
|
|
|
+ // 大数值自动转换 防止失真
|
|
|
+ .registerConverter(new ExcelBigNumberConvert())
|
|
|
+ .sheet(sheetName);
|
|
|
+ builder.doWrite(list);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException("导出Excel异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|