ExcelUtil.java 27 KB

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