ExcelUtil.java 33 KB

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