ExcelUtil.java 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. package com.ruoyi.common.utils.poi;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.lang.Validator;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.ruoyi.common.annotation.Excel;
  6. import com.ruoyi.common.annotation.Excel.ColumnType;
  7. import com.ruoyi.common.annotation.Excel.Type;
  8. import com.ruoyi.common.annotation.Excels;
  9. import com.ruoyi.common.config.RuoYiConfig;
  10. import com.ruoyi.common.core.domain.AjaxResult;
  11. import com.ruoyi.common.exception.CustomException;
  12. import com.ruoyi.common.utils.DateUtils;
  13. import com.ruoyi.common.utils.DictUtils;
  14. import com.ruoyi.common.utils.file.FileTypeUtils;
  15. import com.ruoyi.common.utils.file.ImageUtils;
  16. import com.ruoyi.common.utils.reflect.ReflectUtils;
  17. import org.apache.poi.ss.usermodel.*;
  18. import org.apache.poi.ss.util.CellRangeAddressList;
  19. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  20. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  21. import org.apache.poi.xssf.usermodel.XSSFDataValidation;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import java.io.*;
  25. import java.lang.reflect.Field;
  26. import java.math.BigDecimal;
  27. import java.text.DecimalFormat;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. /**
  31. * Excel相关处理
  32. *
  33. * @author ruoyi
  34. */
  35. public class ExcelUtil<T>
  36. {
  37. private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
  38. /**
  39. * Excel sheet最大行数,默认65536
  40. */
  41. public static final int sheetSize = 65536;
  42. /**
  43. * 工作表名称
  44. */
  45. private String sheetName;
  46. /**
  47. * 导出类型(EXPORT:导出数据;IMPORT:导入模板)
  48. */
  49. private Type type;
  50. /**
  51. * 工作薄对象
  52. */
  53. private Workbook wb;
  54. /**
  55. * 工作表对象
  56. */
  57. private Sheet sheet;
  58. /**
  59. * 样式列表
  60. */
  61. private Map<String, CellStyle> styles;
  62. /**
  63. * 导入导出数据列表
  64. */
  65. private List<T> list;
  66. /**
  67. * 注解列表
  68. */
  69. private List<Object[]> fields;
  70. /**
  71. * 最大高度
  72. */
  73. private short maxHeight;
  74. /**
  75. * 统计列表
  76. */
  77. private Map<Integer, Double> statistics = new HashMap<Integer, Double>();
  78. /**
  79. * 数字格式
  80. */
  81. private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
  82. /**
  83. * 实体对象
  84. */
  85. public Class<T> clazz;
  86. public ExcelUtil(Class<T> clazz)
  87. {
  88. this.clazz = clazz;
  89. }
  90. public void init(List<T> list, String sheetName, Type type)
  91. {
  92. if (list == null)
  93. {
  94. list = new ArrayList<T>();
  95. }
  96. this.list = list;
  97. this.sheetName = sheetName;
  98. this.type = type;
  99. createExcelField();
  100. createWorkbook();
  101. }
  102. /**
  103. * 对excel表单默认第一个索引名转换成list
  104. *
  105. * @param is 输入流
  106. * @return 转换后集合
  107. */
  108. public List<T> importExcel(InputStream is) throws Exception
  109. {
  110. return importExcel(StrUtil.EMPTY, is);
  111. }
  112. /**
  113. * 对excel表单指定表格索引名转换成list
  114. *
  115. * @param sheetName 表格索引名
  116. * @param is 输入流
  117. * @return 转换后集合
  118. */
  119. public List<T> importExcel(String sheetName, InputStream is) throws Exception
  120. {
  121. this.type = Type.IMPORT;
  122. this.wb = WorkbookFactory.create(is);
  123. List<T> list = new ArrayList<T>();
  124. Sheet sheet = null;
  125. if (Validator.isNotEmpty(sheetName))
  126. {
  127. // 如果指定sheet名,则取指定sheet中的内容.
  128. sheet = wb.getSheet(sheetName);
  129. }
  130. else
  131. {
  132. // 如果传入的sheet名不存在则默认指向第1个sheet.
  133. sheet = wb.getSheetAt(0);
  134. }
  135. if (sheet == null)
  136. {
  137. throw new IOException("文件sheet不存在");
  138. }
  139. int rows = sheet.getPhysicalNumberOfRows();
  140. if (rows > 0)
  141. {
  142. // 定义一个map用于存放excel列的序号和field.
  143. Map<String, Integer> cellMap = new HashMap<String, Integer>();
  144. // 获取表头
  145. Row heard = sheet.getRow(0);
  146. for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++)
  147. {
  148. Cell cell = heard.getCell(i);
  149. if (Validator.isNotNull(cell))
  150. {
  151. String value = this.getCellValue(heard, i).toString();
  152. cellMap.put(value, i);
  153. }
  154. else
  155. {
  156. cellMap.put(null, i);
  157. }
  158. }
  159. // 有数据时才处理 得到类的所有field.
  160. Field[] allFields = clazz.getDeclaredFields();
  161. // 定义一个map用于存放列的序号和field.
  162. Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();
  163. for (int col = 0; col < allFields.length; col++)
  164. {
  165. Field field = allFields[col];
  166. Excel attr = field.getAnnotation(Excel.class);
  167. if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
  168. {
  169. // 设置类的私有字段属性可访问.
  170. field.setAccessible(true);
  171. Integer column = cellMap.get(attr.name());
  172. if (column != null)
  173. {
  174. fieldsMap.put(column, field);
  175. }
  176. }
  177. }
  178. for (int i = 1; i < rows; i++)
  179. {
  180. // 从第2行开始取数据,默认第一行是表头.
  181. Row row = sheet.getRow(i);
  182. if(row == null)
  183. {
  184. continue;
  185. }
  186. T entity = null;
  187. for (Map.Entry<Integer, Field> entry : fieldsMap.entrySet())
  188. {
  189. Object val = this.getCellValue(row, entry.getKey());
  190. // 如果不存在实例则新建.
  191. entity = (entity == null ? clazz.newInstance() : entity);
  192. // 从map中得到对应列的field.
  193. Field field = fieldsMap.get(entry.getKey());
  194. // 取得类型,并根据对象类型设置值.
  195. Class<?> fieldType = field.getType();
  196. if (String.class == fieldType)
  197. {
  198. String s = Convert.toStr(val);
  199. if (StrUtil.endWith(s, ".0"))
  200. {
  201. val = StrUtil.subBefore(s, ".0",false);
  202. }
  203. else
  204. {
  205. String dateFormat = field.getAnnotation(Excel.class).dateFormat();
  206. if (Validator.isNotEmpty(dateFormat))
  207. {
  208. val = DateUtils.parseDateToStr(dateFormat, (Date) val);
  209. }
  210. else
  211. {
  212. val = Convert.toStr(val);
  213. }
  214. }
  215. }
  216. else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && Validator.isNumber(Convert.toStr(val)))
  217. {
  218. val = Convert.toInt(val);
  219. }
  220. else if (Long.TYPE == fieldType || Long.class == fieldType)
  221. {
  222. val = Convert.toLong(val);
  223. }
  224. else if (Double.TYPE == fieldType || Double.class == fieldType)
  225. {
  226. val = Convert.toDouble(val);
  227. }
  228. else if (Float.TYPE == fieldType || Float.class == fieldType)
  229. {
  230. val = Convert.toFloat(val);
  231. }
  232. else if (BigDecimal.class == fieldType)
  233. {
  234. val = Convert.toBigDecimal(val);
  235. }
  236. else if (Date.class == fieldType)
  237. {
  238. if (val instanceof String)
  239. {
  240. val = DateUtils.parseDate(val);
  241. }
  242. else if (val instanceof Double)
  243. {
  244. val = DateUtil.getJavaDate((Double) val);
  245. }
  246. }
  247. else if (Boolean.TYPE == fieldType || Boolean.class == fieldType)
  248. {
  249. val = Convert.toBool(val, false);
  250. }
  251. if (Validator.isNotNull(fieldType))
  252. {
  253. Excel attr = field.getAnnotation(Excel.class);
  254. String propertyName = field.getName();
  255. if (Validator.isNotEmpty(attr.targetAttr()))
  256. {
  257. propertyName = field.getName() + "." + attr.targetAttr();
  258. }
  259. else if (Validator.isNotEmpty(attr.readConverterExp()))
  260. {
  261. val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
  262. }
  263. else if (Validator.isNotEmpty(attr.dictType()))
  264. {
  265. val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
  266. }
  267. ReflectUtils.invokeSetter(entity, propertyName, val);
  268. }
  269. }
  270. list.add(entity);
  271. }
  272. }
  273. return list;
  274. }
  275. /**
  276. * 对list数据源将其里面的数据导入到excel表单
  277. *
  278. * @param list 导出数据集合
  279. * @param sheetName 工作表的名称
  280. * @return 结果
  281. */
  282. public AjaxResult exportExcel(List<T> list, String sheetName)
  283. {
  284. this.init(list, sheetName, Type.EXPORT);
  285. return exportExcel();
  286. }
  287. /**
  288. * 对list数据源将其里面的数据导入到excel表单
  289. *
  290. * @param sheetName 工作表的名称
  291. * @return 结果
  292. */
  293. public AjaxResult importTemplateExcel(String sheetName)
  294. {
  295. this.init(null, sheetName, Type.IMPORT);
  296. return exportExcel();
  297. }
  298. /**
  299. * 对list数据源将其里面的数据导入到excel表单
  300. *
  301. * @return 结果
  302. */
  303. public AjaxResult exportExcel()
  304. {
  305. OutputStream out = null;
  306. try
  307. {
  308. // 取出一共有多少个sheet.
  309. double sheetNo = Math.ceil(list.size() / sheetSize);
  310. for (int index = 0; index <= sheetNo; index++)
  311. {
  312. createSheet(sheetNo, index);
  313. // 产生一行
  314. Row row = sheet.createRow(0);
  315. int column = 0;
  316. // 写入各个字段的列头名称
  317. for (Object[] os : fields)
  318. {
  319. Excel excel = (Excel) os[1];
  320. this.createCell(excel, row, column++);
  321. }
  322. if (Type.EXPORT.equals(type))
  323. {
  324. fillExcelData(index, row);
  325. addStatisticsRow();
  326. }
  327. }
  328. String filename = encodingFilename(sheetName);
  329. out = new FileOutputStream(getAbsoluteFile(filename));
  330. wb.write(out);
  331. return AjaxResult.success(filename);
  332. }
  333. catch (Exception e)
  334. {
  335. log.error("导出Excel异常{}", e.getMessage());
  336. throw new CustomException("导出Excel失败,请联系网站管理员!");
  337. }
  338. finally
  339. {
  340. if (wb != null)
  341. {
  342. try
  343. {
  344. wb.close();
  345. }
  346. catch (IOException e1)
  347. {
  348. e1.printStackTrace();
  349. }
  350. }
  351. if (out != null)
  352. {
  353. try
  354. {
  355. out.close();
  356. }
  357. catch (IOException e1)
  358. {
  359. e1.printStackTrace();
  360. }
  361. }
  362. }
  363. }
  364. /**
  365. * 填充excel数据
  366. *
  367. * @param index 序号
  368. * @param row 单元格行
  369. */
  370. public void fillExcelData(int index, Row row)
  371. {
  372. int startNo = index * sheetSize;
  373. int endNo = Math.min(startNo + sheetSize, list.size());
  374. for (int i = startNo; i < endNo; i++)
  375. {
  376. row = sheet.createRow(i + 1 - startNo);
  377. // 得到导出对象.
  378. T vo = (T) list.get(i);
  379. int column = 0;
  380. for (Object[] os : fields)
  381. {
  382. Field field = (Field) os[0];
  383. Excel excel = (Excel) os[1];
  384. // 设置实体类私有属性可访问
  385. field.setAccessible(true);
  386. this.addCell(excel, row, vo, field, column++);
  387. }
  388. }
  389. }
  390. /**
  391. * 创建表格样式
  392. *
  393. * @param wb 工作薄对象
  394. * @return 样式列表
  395. */
  396. private Map<String, CellStyle> createStyles(Workbook wb)
  397. {
  398. // 写入各条记录,每条记录对应excel表中的一行
  399. Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
  400. CellStyle style = wb.createCellStyle();
  401. style.setAlignment(HorizontalAlignment.CENTER);
  402. style.setVerticalAlignment(VerticalAlignment.CENTER);
  403. style.setBorderRight(BorderStyle.THIN);
  404. style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  405. style.setBorderLeft(BorderStyle.THIN);
  406. style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  407. style.setBorderTop(BorderStyle.THIN);
  408. style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  409. style.setBorderBottom(BorderStyle.THIN);
  410. style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  411. Font dataFont = wb.createFont();
  412. dataFont.setFontName("Arial");
  413. dataFont.setFontHeightInPoints((short) 10);
  414. style.setFont(dataFont);
  415. styles.put("data", style);
  416. style = wb.createCellStyle();
  417. style.cloneStyleFrom(styles.get("data"));
  418. style.setAlignment(HorizontalAlignment.CENTER);
  419. style.setVerticalAlignment(VerticalAlignment.CENTER);
  420. style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
  421. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  422. Font headerFont = wb.createFont();
  423. headerFont.setFontName("Arial");
  424. headerFont.setFontHeightInPoints((short) 10);
  425. headerFont.setBold(true);
  426. headerFont.setColor(IndexedColors.WHITE.getIndex());
  427. style.setFont(headerFont);
  428. styles.put("header", style);
  429. style = wb.createCellStyle();
  430. style.setAlignment(HorizontalAlignment.CENTER);
  431. style.setVerticalAlignment(VerticalAlignment.CENTER);
  432. Font totalFont = wb.createFont();
  433. totalFont.setFontName("Arial");
  434. totalFont.setFontHeightInPoints((short) 10);
  435. style.setFont(totalFont);
  436. styles.put("total", style);
  437. style = wb.createCellStyle();
  438. style.cloneStyleFrom(styles.get("data"));
  439. style.setAlignment(HorizontalAlignment.LEFT);
  440. styles.put("data1", style);
  441. style = wb.createCellStyle();
  442. style.cloneStyleFrom(styles.get("data"));
  443. style.setAlignment(HorizontalAlignment.CENTER);
  444. styles.put("data2", style);
  445. style = wb.createCellStyle();
  446. style.cloneStyleFrom(styles.get("data"));
  447. style.setAlignment(HorizontalAlignment.RIGHT);
  448. styles.put("data3", style);
  449. return styles;
  450. }
  451. /**
  452. * 创建单元格
  453. */
  454. public Cell createCell(Excel attr, Row row, int column)
  455. {
  456. // 创建列
  457. Cell cell = row.createCell(column);
  458. // 写入列信息
  459. cell.setCellValue(attr.name());
  460. setDataValidation(attr, row, column);
  461. cell.setCellStyle(styles.get("header"));
  462. return cell;
  463. }
  464. /**
  465. * 设置单元格信息
  466. *
  467. * @param value 单元格值
  468. * @param attr 注解相关
  469. * @param cell 单元格信息
  470. */
  471. public void setCellVo(Object value, Excel attr, Cell cell)
  472. {
  473. if (ColumnType.STRING == attr.cellType())
  474. {
  475. cell.setCellValue(Validator.isNull(value) ? attr.defaultValue() : value + attr.suffix());
  476. }
  477. else if (ColumnType.NUMERIC == attr.cellType())
  478. {
  479. if (StrUtil.isNotNull(value))
  480. {
  481. cell.setCellValue(StrUtil.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
  482. }
  483. }
  484. else if (ColumnType.IMAGE == attr.cellType())
  485. {
  486. ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
  487. cell.getRow().getRowNum() + 1);
  488. String imagePath = Convert.toStr(value);
  489. if (Validator.isNotEmpty(imagePath))
  490. {
  491. byte[] data = ImageUtils.getImage(imagePath);
  492. getDrawingPatriarch(cell.getSheet()).createPicture(anchor,
  493. cell.getSheet().getWorkbook().addPicture(data, getImageType(data)));
  494. }
  495. }
  496. }
  497. /**
  498. * 获取画布
  499. */
  500. public static Drawing<?> getDrawingPatriarch(Sheet sheet)
  501. {
  502. if (sheet.getDrawingPatriarch() == null)
  503. {
  504. sheet.createDrawingPatriarch();
  505. }
  506. return sheet.getDrawingPatriarch();
  507. }
  508. /**
  509. * 获取图片类型,设置图片插入类型
  510. */
  511. public int getImageType(byte[] value)
  512. {
  513. String type = FileTypeUtils.getFileExtendName(value);
  514. if ("JPG".equalsIgnoreCase(type))
  515. {
  516. return Workbook.PICTURE_TYPE_JPEG;
  517. }
  518. else if ("PNG".equalsIgnoreCase(type))
  519. {
  520. return Workbook.PICTURE_TYPE_PNG;
  521. }
  522. return Workbook.PICTURE_TYPE_JPEG;
  523. }
  524. /**
  525. * 创建表格样式
  526. */
  527. public void setDataValidation(Excel attr, Row row, int column)
  528. {
  529. if (attr.name().indexOf("注:") >= 0)
  530. {
  531. sheet.setColumnWidth(column, 6000);
  532. }
  533. else
  534. {
  535. // 设置列宽
  536. sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
  537. }
  538. // 如果设置了提示信息则鼠标放上去提示.
  539. if (Validator.isNotEmpty(attr.prompt()))
  540. {
  541. // 这里默认设了2-101列提示.
  542. setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
  543. }
  544. // 如果设置了combo属性则本列只能选择不能输入
  545. if (attr.combo().length > 0)
  546. {
  547. // 这里默认设了2-101列只能选择不能输入.
  548. setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
  549. }
  550. }
  551. /**
  552. * 添加单元格
  553. */
  554. public Cell addCell(Excel attr, Row row, T vo, Field field, int column)
  555. {
  556. Cell cell = null;
  557. try
  558. {
  559. // 设置行高
  560. row.setHeight(maxHeight);
  561. // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
  562. if (attr.isExport())
  563. {
  564. // 创建cell
  565. cell = row.createCell(column);
  566. int align = attr.align().value();
  567. cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
  568. // 用于读取对象中的属性
  569. Object value = getTargetValue(vo, field, attr);
  570. String dateFormat = attr.dateFormat();
  571. String readConverterExp = attr.readConverterExp();
  572. String separator = attr.separator();
  573. String dictType = attr.dictType();
  574. if (Validator.isNotEmpty(dateFormat) && Validator.isNotNull(value))
  575. {
  576. cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
  577. }
  578. else if (Validator.isNotEmpty(readConverterExp) && Validator.isNotNull(value))
  579. {
  580. cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
  581. }
  582. else if (Validator.isNotEmpty(dictType) && Validator.isNotNull(value))
  583. {
  584. cell.setCellValue(convertDictByExp(Convert.toStr(value), dictType, separator));
  585. }
  586. else if (value instanceof BigDecimal && -1 != attr.scale())
  587. {
  588. cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());
  589. }
  590. else
  591. {
  592. // 设置列类型
  593. setCellVo(value, attr, cell);
  594. }
  595. addStatisticsData(column, Convert.toStr(value), attr);
  596. }
  597. }
  598. catch (Exception e)
  599. {
  600. log.error("导出Excel失败{}", e);
  601. }
  602. return cell;
  603. }
  604. /**
  605. * 设置 POI XSSFSheet 单元格提示
  606. *
  607. * @param sheet 表单
  608. * @param promptTitle 提示标题
  609. * @param promptContent 提示内容
  610. * @param firstRow 开始行
  611. * @param endRow 结束行
  612. * @param firstCol 开始列
  613. * @param endCol 结束列
  614. */
  615. public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
  616. int firstCol, int endCol)
  617. {
  618. DataValidationHelper helper = sheet.getDataValidationHelper();
  619. DataValidationConstraint constraint = helper.createCustomConstraint("DD1");
  620. CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
  621. DataValidation dataValidation = helper.createValidation(constraint, regions);
  622. dataValidation.createPromptBox(promptTitle, promptContent);
  623. dataValidation.setShowPromptBox(true);
  624. sheet.addValidationData(dataValidation);
  625. }
  626. /**
  627. * 设置某些列的值只能输入预制的数据,显示下拉框.
  628. *
  629. * @param sheet 要设置的sheet.
  630. * @param textlist 下拉框显示的内容
  631. * @param firstRow 开始行
  632. * @param endRow 结束行
  633. * @param firstCol 开始列
  634. * @param endCol 结束列
  635. * @return 设置好的sheet.
  636. */
  637. public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol)
  638. {
  639. DataValidationHelper helper = sheet.getDataValidationHelper();
  640. // 加载下拉列表内容
  641. DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);
  642. // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
  643. CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
  644. // 数据有效性对象
  645. DataValidation dataValidation = helper.createValidation(constraint, regions);
  646. // 处理Excel兼容性问题
  647. if (dataValidation instanceof XSSFDataValidation)
  648. {
  649. dataValidation.setSuppressDropDownArrow(true);
  650. dataValidation.setShowErrorBox(true);
  651. }
  652. else
  653. {
  654. dataValidation.setSuppressDropDownArrow(false);
  655. }
  656. sheet.addValidationData(dataValidation);
  657. }
  658. /**
  659. * 解析导出值 0=男,1=女,2=未知
  660. *
  661. * @param propertyValue 参数值
  662. * @param converterExp 翻译注解
  663. * @param separator 分隔符
  664. * @return 解析后值
  665. */
  666. public static String convertByExp(String propertyValue, String converterExp, String separator)
  667. {
  668. StringBuilder propertyString = new StringBuilder();
  669. String[] convertSource = converterExp.split(",");
  670. for (String item : convertSource)
  671. {
  672. String[] itemArray = item.split("=");
  673. if (StrUtil.containsAny(separator, propertyValue))
  674. {
  675. for (String value : propertyValue.split(separator))
  676. {
  677. if (itemArray[0].equals(value))
  678. {
  679. propertyString.append(itemArray[1] + separator);
  680. break;
  681. }
  682. }
  683. }
  684. else
  685. {
  686. if (itemArray[0].equals(propertyValue))
  687. {
  688. return itemArray[1];
  689. }
  690. }
  691. }
  692. return StrUtil.strip(propertyString.toString(), null,separator);
  693. }
  694. /**
  695. * 反向解析值 男=0,女=1,未知=2
  696. *
  697. * @param propertyValue 参数值
  698. * @param converterExp 翻译注解
  699. * @param separator 分隔符
  700. * @return 解析后值
  701. */
  702. public static String reverseByExp(String propertyValue, String converterExp, String separator)
  703. {
  704. StringBuilder propertyString = new StringBuilder();
  705. String[] convertSource = converterExp.split(",");
  706. for (String item : convertSource)
  707. {
  708. String[] itemArray = item.split("=");
  709. if (StrUtil.containsAny(separator, propertyValue))
  710. {
  711. for (String value : propertyValue.split(separator))
  712. {
  713. if (itemArray[1].equals(value))
  714. {
  715. propertyString.append(itemArray[0] + separator);
  716. break;
  717. }
  718. }
  719. }
  720. else
  721. {
  722. if (itemArray[1].equals(propertyValue))
  723. {
  724. return itemArray[0];
  725. }
  726. }
  727. }
  728. return StrUtil.strip(propertyString.toString(), null,separator);
  729. }
  730. /**
  731. * 解析字典值
  732. *
  733. * @param dictValue 字典值
  734. * @param dictType 字典类型
  735. * @param separator 分隔符
  736. * @return 字典标签
  737. */
  738. public static String convertDictByExp(String dictValue, String dictType, String separator)
  739. {
  740. return DictUtils.getDictLabel(dictType, dictValue, separator);
  741. }
  742. /**
  743. * 反向解析值字典值
  744. *
  745. * @param dictLabel 字典标签
  746. * @param dictType 字典类型
  747. * @param separator 分隔符
  748. * @return 字典值
  749. */
  750. public static String reverseDictByExp(String dictLabel, String dictType, String separator)
  751. {
  752. return DictUtils.getDictValue(dictType, dictLabel, separator);
  753. }
  754. /**
  755. * 合计统计信息
  756. */
  757. private void addStatisticsData(Integer index, String text, Excel entity)
  758. {
  759. if (entity != null && entity.isStatistics())
  760. {
  761. Double temp = 0D;
  762. if (!statistics.containsKey(index))
  763. {
  764. statistics.put(index, temp);
  765. }
  766. try
  767. {
  768. temp = Double.valueOf(text);
  769. }
  770. catch (NumberFormatException e)
  771. {
  772. }
  773. statistics.put(index, statistics.get(index) + temp);
  774. }
  775. }
  776. /**
  777. * 创建统计行
  778. */
  779. public void addStatisticsRow()
  780. {
  781. if (statistics.size() > 0)
  782. {
  783. Cell cell = null;
  784. Row row = sheet.createRow(sheet.getLastRowNum() + 1);
  785. Set<Integer> keys = statistics.keySet();
  786. cell = row.createCell(0);
  787. cell.setCellStyle(styles.get("total"));
  788. cell.setCellValue("合计");
  789. for (Integer key : keys)
  790. {
  791. cell = row.createCell(key);
  792. cell.setCellStyle(styles.get("total"));
  793. cell.setCellValue(DOUBLE_FORMAT.format(statistics.get(key)));
  794. }
  795. statistics.clear();
  796. }
  797. }
  798. /**
  799. * 编码文件名
  800. */
  801. public String encodingFilename(String filename)
  802. {
  803. filename = UUID.randomUUID().toString() + "_" + filename + ".xlsx";
  804. return filename;
  805. }
  806. /**
  807. * 获取下载路径
  808. *
  809. * @param filename 文件名称
  810. */
  811. public String getAbsoluteFile(String filename)
  812. {
  813. String downloadPath = RuoYiConfig.getDownloadPath() + filename;
  814. File desc = new File(downloadPath);
  815. if (!desc.getParentFile().exists())
  816. {
  817. desc.getParentFile().mkdirs();
  818. }
  819. return downloadPath;
  820. }
  821. /**
  822. * 获取bean中的属性值
  823. *
  824. * @param vo 实体对象
  825. * @param field 字段
  826. * @param excel 注解
  827. * @return 最终的属性值
  828. * @throws Exception
  829. */
  830. private Object getTargetValue(T vo, Field field, Excel excel) throws Exception
  831. {
  832. Object o = field.get(vo);
  833. if (Validator.isNotEmpty(excel.targetAttr()))
  834. {
  835. String target = excel.targetAttr();
  836. if (target.contains("."))
  837. {
  838. String[] targets = target.split("[.]");
  839. for (String name : targets)
  840. {
  841. o = getValue(o, name);
  842. }
  843. }
  844. else
  845. {
  846. o = getValue(o, target);
  847. }
  848. }
  849. return o;
  850. }
  851. /**
  852. * 以类的属性的get方法方法形式获取值
  853. *
  854. * @param o
  855. * @param name
  856. * @return value
  857. * @throws Exception
  858. */
  859. private Object getValue(Object o, String name) throws Exception
  860. {
  861. if (Validator.isNotNull(o) && Validator.isNotEmpty(name))
  862. {
  863. Class<?> clazz = o.getClass();
  864. Field field = clazz.getDeclaredField(name);
  865. field.setAccessible(true);
  866. o = field.get(o);
  867. }
  868. return o;
  869. }
  870. /**
  871. * 得到所有定义字段
  872. */
  873. private void createExcelField()
  874. {
  875. this.fields = new ArrayList<Object[]>();
  876. List<Field> tempFields = new ArrayList<>();
  877. tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
  878. tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
  879. for (Field field : tempFields)
  880. {
  881. // 单注解
  882. if (field.isAnnotationPresent(Excel.class))
  883. {
  884. putToField(field, field.getAnnotation(Excel.class));
  885. }
  886. // 多注解
  887. if (field.isAnnotationPresent(Excels.class))
  888. {
  889. Excels attrs = field.getAnnotation(Excels.class);
  890. Excel[] excels = attrs.value();
  891. for (Excel excel : excels)
  892. {
  893. putToField(field, excel);
  894. }
  895. }
  896. }
  897. this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
  898. this.maxHeight = getRowHeight();
  899. }
  900. /**
  901. * 根据注解获取最大行高
  902. */
  903. public short getRowHeight()
  904. {
  905. double maxHeight = 0;
  906. for (Object[] os : this.fields)
  907. {
  908. Excel excel = (Excel) os[1];
  909. maxHeight = maxHeight > excel.height() ? maxHeight : excel.height();
  910. }
  911. return (short) (maxHeight * 20);
  912. }
  913. /**
  914. * 放到字段集合中
  915. */
  916. private void putToField(Field field, Excel attr)
  917. {
  918. if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
  919. {
  920. this.fields.add(new Object[] { field, attr });
  921. }
  922. }
  923. /**
  924. * 创建一个工作簿
  925. */
  926. public void createWorkbook()
  927. {
  928. this.wb = new SXSSFWorkbook(500);
  929. }
  930. /**
  931. * 创建工作表
  932. *
  933. * @param sheetNo sheet数量
  934. * @param index 序号
  935. */
  936. public void createSheet(double sheetNo, int index)
  937. {
  938. this.sheet = wb.createSheet();
  939. this.styles = createStyles(wb);
  940. // 设置工作表的名称.
  941. if (sheetNo == 0)
  942. {
  943. wb.setSheetName(index, sheetName);
  944. }
  945. else
  946. {
  947. wb.setSheetName(index, sheetName + index);
  948. }
  949. }
  950. /**
  951. * 获取单元格值
  952. *
  953. * @param row 获取的行
  954. * @param column 获取单元格列号
  955. * @return 单元格值
  956. */
  957. public Object getCellValue(Row row, int column)
  958. {
  959. if (row == null)
  960. {
  961. return row;
  962. }
  963. Object val = "";
  964. try
  965. {
  966. Cell cell = row.getCell(column);
  967. if (Validator.isNotNull(cell))
  968. {
  969. if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA)
  970. {
  971. val = cell.getNumericCellValue();
  972. if (DateUtil.isCellDateFormatted(cell))
  973. {
  974. val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式转换
  975. }
  976. else
  977. {
  978. if ((Double) val % 1 != 0)
  979. {
  980. val = new BigDecimal(val.toString());
  981. }
  982. else
  983. {
  984. val = new DecimalFormat("0").format(val);
  985. }
  986. }
  987. }
  988. else if (cell.getCellType() == CellType.STRING)
  989. {
  990. val = cell.getStringCellValue();
  991. }
  992. else if (cell.getCellType() == CellType.BOOLEAN)
  993. {
  994. val = cell.getBooleanCellValue();
  995. }
  996. else if (cell.getCellType() == CellType.ERROR)
  997. {
  998. val = cell.getErrorCellValue();
  999. }
  1000. }
  1001. }
  1002. catch (Exception e)
  1003. {
  1004. return val;
  1005. }
  1006. return val;
  1007. }
  1008. }