ExcelUtil.java 28 KB

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