ExcelUtil.java 30 KB

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