ImportExcelUtil.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. package com.xjrsoft.module.veb.util;
  2. import com.alibaba.excel.annotation.ExcelIgnore;
  3. import com.alibaba.excel.annotation.ExcelProperty;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  6. import com.xjrsoft.common.annotation.Required;
  7. import com.xjrsoft.common.enums.DeleteMark;
  8. import com.xjrsoft.common.enums.YesOrNoEnum;
  9. import com.xjrsoft.common.utils.VoToColumnUtil;
  10. import com.xjrsoft.module.generator.entity.ImportConfig;
  11. import com.xjrsoft.module.system.entity.DictionaryDetail;
  12. import com.xjrsoft.module.system.entity.DictionaryItem;
  13. import com.xjrsoft.module.system.mapper.DictionarydetailMapper;
  14. import com.xjrsoft.module.system.mapper.DictionaryitemMapper;
  15. import org.apache.commons.lang3.ObjectUtils;
  16. import org.apache.poi.ss.usermodel.*;
  17. import org.apache.poi.ss.util.CellRangeAddress;
  18. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.lang.reflect.Field;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.function.Consumer;
  27. import java.util.function.Function;
  28. import java.util.function.Supplier;
  29. /**
  30. * @author phoenix
  31. * @Description 导入工具类
  32. * 2023/12/5
  33. */
  34. public class ImportExcelUtil {
  35. /**
  36. * 下载模板写入
  37. *
  38. * @param obj
  39. * @return
  40. * @throws IOException
  41. */
  42. public static ByteArrayOutputStream writeTemplateSheet(Object obj) throws IOException {
  43. // 开始写入
  44. Workbook workbook = new XSSFWorkbook();
  45. // 创建一个工作表(sheet)
  46. String sheetName = "sheet1";
  47. Sheet sheet = workbook.createSheet(sheetName);
  48. List<ImportConfig> importConfigs = allFields(obj);
  49. // 表头
  50. createHead(workbook, sheet, importConfigs, IndexedColors.YELLOW.getIndex(), IndexedColors.RED.getIndex(), 0);
  51. // 提示必填
  52. String content = "红色背景为必填项,导入时请删除本行。";
  53. createCautionHead(workbook, sheet, 1, content, importConfigs.size() - 1, 12, IndexedColors.RED.getIndex());
  54. //写入文件
  55. ByteArrayOutputStream bot = new ByteArrayOutputStream();
  56. workbook.write(bot);
  57. return bot;
  58. }
  59. /**
  60. * 获取写入对象的所有字段
  61. *
  62. * @param obj
  63. * @return
  64. */
  65. public static List<ImportConfig> allFields(Object obj) {
  66. List<ImportConfig> importConfigs = new ArrayList<>();
  67. if (obj == null) {
  68. return importConfigs;
  69. }
  70. Class<?> clazz = obj.getClass();
  71. Field[] fields = clazz.getDeclaredFields();
  72. int index = 0;
  73. for (int i = 0; i < fields.length; i++) {
  74. Field field = fields[i];
  75. ImportConfig importConfig = new ImportConfig();
  76. field.setAccessible(true); // 访问私有字段
  77. if (field.isAnnotationPresent(Required.class)) {
  78. Required required = field.getAnnotation(Required.class);
  79. Boolean value = required.value();
  80. importConfig.setRequired(value);
  81. }
  82. if (field.isAnnotationPresent(ExcelProperty.class)) {
  83. ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
  84. String[] annotationValues = excelProperty.value();
  85. importConfig.setLabel(annotationValues.length > 0 ? annotationValues[0] : "");
  86. }
  87. if (field.isAnnotationPresent(ExcelIgnore.class)) {
  88. continue;
  89. }
  90. importConfig.setFieldName(field.getName());
  91. importConfig.setSortCode(index++);
  92. importConfig.setWidth(0);
  93. importConfigs.add(importConfig);
  94. }
  95. return importConfigs;
  96. }
  97. /**
  98. * 写大标题行
  99. *
  100. * @param workbook
  101. * @param sheet
  102. * @param bigHead
  103. * @param rowNumber
  104. * @param lastCol
  105. */
  106. public static void createBigHead(Workbook workbook, Sheet sheet, String bigHead, int rowNumber, int lastCol) {
  107. Font font = workbook.createFont();
  108. font.setFontName("宋体");
  109. font.setFontHeightInPoints((short) 18);
  110. // 正常样式
  111. CellStyle normalCellStyle = workbook.createCellStyle();
  112. normalCellStyle.setFont(font); // 将字体应用到样式
  113. normalCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  114. normalCellStyle.setAlignment(HorizontalAlignment.CENTER);
  115. // 设置边框样式为细线
  116. normalCellStyle.setBorderTop(BorderStyle.THIN);
  117. normalCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 设置顶部边框颜色
  118. normalCellStyle.setBorderBottom(BorderStyle.THIN);
  119. normalCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 设置底部边框颜色
  120. normalCellStyle.setBorderLeft(BorderStyle.THIN);
  121. normalCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 设置左边框颜色
  122. normalCellStyle.setBorderRight(BorderStyle.THIN);
  123. normalCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 设置右边框颜色
  124. // 所在行
  125. Row row = sheet.createRow(rowNumber);
  126. Cell cell = row.createCell(0);
  127. cell.setCellValue(bigHead);
  128. cell.setCellStyle(normalCellStyle);
  129. sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, 0, lastCol));
  130. }
  131. /**
  132. * 写标题行
  133. *
  134. * @param workbook
  135. * @param sheet
  136. * @param importConfigs
  137. * @param rowNumber
  138. */
  139. public static void createHead(Workbook workbook, Sheet sheet, List<ImportConfig> importConfigs, short requiredFieldForegroundColor, short requiredFieldFontColor, int rowNumber) {
  140. Font normalFont = workbook.createFont();
  141. normalFont.setFontName("宋体");
  142. normalFont.setFontHeightInPoints((short) 12);
  143. // 正常样式
  144. CellStyle normalCellStyle = workbook.createCellStyle();
  145. normalCellStyle.setFont(normalFont); // 将字体应用到样式
  146. normalCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  147. normalCellStyle.setAlignment(HorizontalAlignment.CENTER);
  148. // 设置边框样式为细线
  149. normalCellStyle.setBorderTop(BorderStyle.THIN);
  150. normalCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 设置顶部边框颜色
  151. normalCellStyle.setBorderBottom(BorderStyle.THIN);
  152. normalCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 设置底部边框颜色
  153. normalCellStyle.setBorderLeft(BorderStyle.THIN);
  154. normalCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 设置左边框颜色
  155. normalCellStyle.setBorderRight(BorderStyle.THIN);
  156. normalCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 设置右边框颜色
  157. // 必填样式
  158. Font requiredFont = workbook.createFont();
  159. requiredFont.setFontName("宋体");
  160. requiredFont.setFontHeightInPoints((short) 12);
  161. CellStyle requiredCellStyle = workbook.createCellStyle();
  162. requiredCellStyle.setFont(requiredFont); // 将字体应用到样式
  163. requiredCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  164. requiredCellStyle.setAlignment(HorizontalAlignment.CENTER);
  165. // requiredCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());//设置背景颜色
  166. requiredCellStyle.setFillForegroundColor(requiredFieldForegroundColor);//设置背景颜色s
  167. requiredCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置填充模式
  168. // 设置边框样式为细线
  169. requiredCellStyle.setBorderTop(BorderStyle.THIN);
  170. requiredCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 设置顶部边框颜色
  171. requiredCellStyle.setBorderBottom(BorderStyle.THIN);
  172. requiredCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 设置底部边框颜色
  173. requiredCellStyle.setBorderLeft(BorderStyle.THIN);
  174. requiredCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 设置左边框颜色
  175. requiredCellStyle.setBorderRight(BorderStyle.THIN);
  176. requiredCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 设置右边框颜色
  177. // 所在行
  178. Row row = sheet.createRow(rowNumber);
  179. // 每一列
  180. for (ImportConfig importConfig : importConfigs) {
  181. Cell cell = row.createCell(importConfig.getSortCode());
  182. String content = importConfig.getLabel();
  183. cell.setCellValue(content);
  184. if (ObjectUtils.isNotEmpty(importConfig.getRequired()) && importConfig.getRequired()) {
  185. cell.setCellStyle(requiredCellStyle);
  186. }
  187. if (ObjectUtils.isEmpty(importConfig.getRequired()) || !importConfig.getRequired()) {
  188. cell.setCellStyle(normalCellStyle);
  189. }
  190. }
  191. }
  192. /**
  193. * 写入副标题,字号14,合并为一个单元格
  194. * @param workbook
  195. * @param sheet
  196. * @param rowNumber
  197. * @param lastCol
  198. */
  199. public static void createSubtitle(Workbook workbook, Sheet sheet, String bigHead, int rowNumber, int lastCol) {
  200. Font font = workbook.createFont();
  201. font.setFontName("宋体");
  202. font.setFontHeightInPoints((short) 14);
  203. // 正常样式
  204. CellStyle normalCellStyle = workbook.createCellStyle();
  205. normalCellStyle.setFont(font); // 将字体应用到样式
  206. normalCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  207. normalCellStyle.setAlignment(HorizontalAlignment.CENTER);
  208. // 设置边框样式为细线
  209. normalCellStyle.setBorderTop(BorderStyle.THIN);
  210. normalCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 设置顶部边框颜色
  211. normalCellStyle.setBorderBottom(BorderStyle.THIN);
  212. normalCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 设置底部边框颜色
  213. normalCellStyle.setBorderLeft(BorderStyle.THIN);
  214. normalCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 设置左边框颜色
  215. normalCellStyle.setBorderRight(BorderStyle.THIN);
  216. normalCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 设置右边框颜色
  217. // 所在行
  218. Row row = sheet.createRow(rowNumber);
  219. Cell cell = row.createCell(0);
  220. cell.setCellValue(bigHead);
  221. cell.setCellStyle(normalCellStyle);
  222. sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, 0, lastCol));
  223. }
  224. /**
  225. * 写单格
  226. *
  227. * @param workbook
  228. * @param sheet
  229. * @param content
  230. * @param rowNumber
  231. * @param starRow
  232. * @param endRow
  233. * @param startcol
  234. * @param lastCol
  235. * @param fontSize
  236. * @param indexedColor
  237. */
  238. public static void createOneCell(Workbook workbook, Sheet sheet, String content, int rowNumber, int starRow, int endRow, int startcol, int lastCol, short fontSize, short indexedColor, HorizontalAlignment horizontalAlignment) {
  239. Font font = workbook.createFont();
  240. font.setFontName("宋体");
  241. font.setFontHeightInPoints(fontSize);
  242. font.setColor(indexedColor);
  243. // 正常样式
  244. CellStyle normalCellStyle = workbook.createCellStyle();
  245. normalCellStyle.setFont(font); // 将字体应用到样式
  246. normalCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  247. normalCellStyle.setAlignment(horizontalAlignment);
  248. // 设置边框样式为细线
  249. normalCellStyle.setBorderTop(BorderStyle.THIN);
  250. normalCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 设置顶部边框颜色
  251. normalCellStyle.setBorderBottom(BorderStyle.THIN);
  252. normalCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 设置底部边框颜色
  253. normalCellStyle.setBorderLeft(BorderStyle.THIN);
  254. normalCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 设置左边框颜色
  255. normalCellStyle.setBorderRight(BorderStyle.THIN);
  256. normalCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 设置右边框颜色
  257. // 所在行
  258. Row row = sheet.createRow(rowNumber);
  259. Cell cell = row.createCell(0);
  260. cell.setCellValue(content);
  261. cell.setCellStyle(normalCellStyle);
  262. sheet.addMergedRegion(new CellRangeAddress(starRow, endRow, startcol, lastCol));
  263. }
  264. /**
  265. * 写提示行
  266. *
  267. * @param workbook
  268. * @param sheet
  269. * @param rowNumber
  270. * @param content
  271. * @param lastCol
  272. * @param fontSize
  273. */
  274. public static void createCautionHead(Workbook workbook, Sheet sheet, int rowNumber, String content, int lastCol, int fontSize, short indexedColor) {
  275. Font font = workbook.createFont();
  276. font.setFontName("宋体");
  277. font.setFontHeightInPoints((short) fontSize);
  278. font.setColor(indexedColor);
  279. // font.setColor(IndexedColors.RED.getIndex());
  280. CellStyle cellStyle = workbook.createCellStyle();
  281. cellStyle.setFont(font); // 将字体应用到样式
  282. cellStyle.setBorderTop(BorderStyle.THIN);
  283. cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 设置顶部边框颜色
  284. cellStyle.setBorderBottom(BorderStyle.THIN);
  285. cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 设置底部边框颜色
  286. cellStyle.setBorderLeft(BorderStyle.THIN);
  287. cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 设置左边框颜色
  288. cellStyle.setBorderRight(BorderStyle.THIN);
  289. cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 设置右边框颜色
  290. Row row = sheet.createRow(rowNumber);
  291. Cell cell = row.createCell(0);
  292. cell.setCellValue(content);
  293. cell.setCellStyle(cellStyle);
  294. sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, 0, lastCol));
  295. }
  296. /**
  297. * 验证子表字段值的合理性并转换为对应的主键
  298. *
  299. * @param getter
  300. * @param fieldName
  301. * @param string2Long
  302. * @param setter
  303. * @param sb
  304. * @param i
  305. * @return
  306. */
  307. public static boolean validateAndSetString2LongField(Supplier<String> getter,
  308. String fieldName,
  309. Map<String, Long> string2Long,
  310. Consumer<Long> setter,
  311. StringBuilder sb, int i) {
  312. String value = getter.get();
  313. if (value != null && !value.trim().isEmpty()) {
  314. Long sublistValue = string2Long.get(value);
  315. if (sublistValue != null) {
  316. setter.accept(sublistValue);
  317. return false;
  318. } else {
  319. sb.append("第");
  320. sb.append(i);
  321. sb.append("行的");
  322. sb.append(fieldName);
  323. sb.append("列的值不存在于系统对应基础数据中,请到基础数据维护");
  324. return true;
  325. }
  326. }
  327. return false; // 字段为空,不进行验证
  328. }
  329. /**
  330. * 验证boolean值的合理性并转换为数值
  331. *
  332. * @param getter
  333. * @param fieldName
  334. * @param setter
  335. * @param sb
  336. * @param i
  337. * @return
  338. */
  339. public static boolean validateAndSetBooleanField(Supplier<String> getter,
  340. String fieldName,
  341. Consumer<Integer> setter,
  342. StringBuilder sb, int i) {
  343. String value = getter.get();
  344. if (value != null && !value.trim().isEmpty()) {
  345. Integer booleanValue = YesOrNoEnum.getCode(value);
  346. if (booleanValue != null) {
  347. setter.accept(booleanValue);
  348. return false;
  349. } else {
  350. sb.append("第");
  351. sb.append(i);
  352. sb.append("行的");
  353. sb.append(fieldName);
  354. sb.append("列的值不符合,该列的值只能为是或否");
  355. return true;
  356. }
  357. }
  358. return false; // 字段为空,不进行验证
  359. }
  360. /**
  361. * 验证字典值的合理性并转换为字典的code
  362. *
  363. * @param getter
  364. * @param prefix
  365. * @param fieldName
  366. * @param dictionary
  367. * @param setter
  368. * @param sb
  369. * @param i
  370. * @return
  371. */
  372. public static boolean validateAndSetDictionaryField(Supplier<String> getter,
  373. String prefix,
  374. String fieldName,
  375. Map<String, String> dictionary,
  376. Consumer<String> setter,
  377. StringBuilder sb, int i) {
  378. String value = getter.get();
  379. if (value != null && !value.trim().isEmpty()) {
  380. String dictValue = dictionary.get(prefix + value.trim());
  381. if (dictValue != null && !dictValue.trim().isEmpty()) {
  382. setter.accept(dictValue.trim());
  383. return false;
  384. } else {
  385. sb.append("第");
  386. sb.append(i);
  387. sb.append("行的");
  388. sb.append(fieldName);
  389. sb.append("列的值不存在于字典中,请到字典中维护");
  390. return true;
  391. }
  392. }
  393. return false; // 字段为空,不进行验证
  394. }
  395. /**
  396. * 验证枚举值的合理性并转换为枚举的code
  397. *
  398. * @param getter
  399. * @param fieldName
  400. * @param setter
  401. * @param sb
  402. * @param i
  403. * @return
  404. */
  405. public static <E extends Enum<E>> boolean validateAndSetEnumField(Supplier<String> getter,
  406. String fieldName,
  407. Map<String, Integer> enumMap,
  408. Consumer<Integer> setter,
  409. StringBuilder sb, int i) {
  410. String value = getter.get();
  411. if (value != null && !value.trim().isEmpty()) {
  412. Integer enumValue = enumMap.get(value);
  413. if (enumValue != null) {
  414. setter.accept(enumValue);
  415. return false;
  416. } else {
  417. sb.append("第");
  418. sb.append(i);
  419. sb.append("行的");
  420. sb.append(fieldName);
  421. sb.append("列的值不存在于枚举值中,请到枚举值中维护");
  422. return true;
  423. }
  424. }
  425. return false; // 字段为空,不进行验证
  426. }
  427. /**
  428. * 获取所有的字典值映射
  429. *
  430. * @param codeList
  431. * @return
  432. */
  433. public static Map<String, String> initDictionary(List<String> codeList, DictionaryitemMapper dictionaryitemMapper, DictionarydetailMapper dictionarydetailMapper) {
  434. List<DictionaryDetail> detailList = dictionarydetailMapper.selectJoinList(DictionaryDetail.class,
  435. new MPJLambdaWrapper<DictionaryDetail>()
  436. .select(DictionaryDetail::getId)
  437. .select(DictionaryDetail.class, x -> VoToColumnUtil.fieldsToColumns(DictionaryDetail.class).contains(x.getProperty()))
  438. .leftJoin(DictionaryItem.class, DictionaryItem::getId, DictionaryDetail::getItemId)
  439. .in(DictionaryItem::getCode, codeList)
  440. );
  441. List<DictionaryItem> dictionaryItemList = dictionaryitemMapper.selectList(
  442. new QueryWrapper<DictionaryItem>().lambda()
  443. .eq(DictionaryItem::getDeleteMark, DeleteMark.NODELETE.getCode())
  444. );
  445. Map<Long, String> itemMap = new HashMap<>();
  446. for (DictionaryItem dictionaryItem : dictionaryItemList) {
  447. itemMap.put(dictionaryItem.getId(), dictionaryItem.getCode());
  448. }
  449. Map<String, String> resultMap = new HashMap<>();
  450. for (DictionaryDetail dictionaryDetail : detailList) {
  451. resultMap.put(itemMap.get(dictionaryDetail.getItemId()) + dictionaryDetail.getName(), dictionaryDetail.getCode());
  452. }
  453. return resultMap;
  454. }
  455. /**
  456. * 判断属性是否为必填属性
  457. *
  458. * @param instance
  459. * @param sb
  460. * @param i
  461. * @return
  462. * @throws IllegalAccessException
  463. */
  464. public static boolean isRequiredFieldsFilled(Object instance,
  465. StringBuilder sb,
  466. int i)
  467. throws IllegalAccessException {
  468. if (instance == null) {
  469. return true; // 如果对象本身为 null,则认为没有通过验证
  470. }
  471. for (Field field : instance.getClass().getDeclaredFields()) {
  472. Required required = field.getAnnotation(Required.class);
  473. if (ObjectUtils.isNotEmpty(required) && required.value()) { // 如果字段被 @Required 标记
  474. field.setAccessible(true); // 允许访问私有字段
  475. Object value = field.get(instance); // 获取字段的值
  476. if (value == null || (value instanceof String && ((String) value).trim().isEmpty())) {
  477. sb.append("第");
  478. sb.append(i);
  479. sb.append("行的");
  480. if (field.isAnnotationPresent(ExcelProperty.class)) {
  481. ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
  482. String[] annotationValues = excelProperty.value();
  483. sb.append(annotationValues.length > 0 ? annotationValues[0] : "");
  484. }
  485. sb.append("列的值为必填");
  486. return true; // 如果任何必需字段为空,则返回 true
  487. }
  488. }
  489. }
  490. return false; // 所有必需字段都不为空
  491. }
  492. /**
  493. * 将对象列表转换为二维字符串列表
  494. *
  495. * @param dataList 数据对象列表
  496. * @param mappers 字段提取函数数组
  497. * @param <T> 数据对象类型
  498. * @return 二维字符串列表
  499. */
  500. @SafeVarargs
  501. public static <T> List<List<String>> convertToDataList(List<T> dataList, Function<T, String>... mappers) {
  502. List<List<String>> result = new ArrayList<>();
  503. for (T data : dataList) {
  504. List<String> row = new ArrayList<>();
  505. for (Function<T, String> mapper : mappers) {
  506. row.add(mapper.apply(data));
  507. }
  508. result.add(row);
  509. }
  510. return result;
  511. }
  512. public static List<ImportConfig> getAllFieldCN(Class<?> clazz) {
  513. List<ImportConfig> importConfigs = new ArrayList<>();
  514. Field[] fields = clazz.getDeclaredFields();
  515. int index = 0;
  516. for (int i = 0; i < fields.length; i++) {
  517. Field field = fields[i];
  518. ImportConfig importConfig = new ImportConfig();
  519. field.setAccessible(true); // 访问私有字段
  520. if (field.isAnnotationPresent(Required.class)) {
  521. Required required = field.getAnnotation(Required.class);
  522. boolean value = required.value();
  523. importConfig.setRequired(value);
  524. }
  525. if (field.isAnnotationPresent(ExcelProperty.class)) {
  526. ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
  527. String[] annotationValues = excelProperty.value();
  528. importConfig.setLabel(annotationValues.length > 0 ? annotationValues[0] : "");
  529. }
  530. if (field.isAnnotationPresent(ExcelIgnore.class)) {
  531. continue;
  532. }
  533. importConfig.setFieldName(field.getName());
  534. importConfig.setSortCode(index++);
  535. importConfig.setWidth(0);
  536. importConfigs.add(importConfig);
  537. }
  538. return importConfigs;
  539. }
  540. }