POIUtil.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. package com.keao.edu.util.excel;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.lang.reflect.Method;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.Map.Entry;
  16. import org.apache.commons.beanutils.NestedNullException;
  17. import org.apache.commons.beanutils.PropertyUtils;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.apache.poi.hssf.usermodel.HSSFCell;
  20. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  21. import org.apache.poi.hssf.usermodel.HSSFFont;
  22. import org.apache.poi.hssf.usermodel.HSSFRow;
  23. import org.apache.poi.hssf.usermodel.HSSFSheet;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.CellStyle;
  27. import org.apache.poi.ss.usermodel.Font;
  28. import org.apache.poi.ss.usermodel.IndexedColors;
  29. import org.apache.poi.ss.usermodel.Row;
  30. import org.apache.poi.ss.usermodel.Sheet;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  33. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import com.keao.edu.util.exception.UtilException;
  37. public class POIUtil {
  38. private final static Logger LOGGER = LoggerFactory.getLogger(POIUtil.class);
  39. // 能导出的最大数据条数
  40. private final static int MAX_DATA_SIZE = 50000;
  41. private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  42. /**
  43. * 将数据集dataset导出到fileName文件中(只支持.xlsx格式)
  44. * @param headColumns 导出文件的列名
  45. * @param dataset 数据源
  46. * @return
  47. * @throws IOException
  48. */
  49. public static SXSSFWorkbook exportBigExcel(String[] headColumns, List<Map<String, Object>> dataset) throws IOException {
  50. if (headColumns == null) {
  51. throw new UtilException("excel列名不能为空");
  52. }
  53. if (dataset == null) {
  54. throw new UtilException("数据集不能为空");
  55. }
  56. SXSSFWorkbook workbook = new SXSSFWorkbook();
  57. CellStyle style = workbook.createCellStyle(); // 获取单元格样式
  58. /************** 设置单元格样式 *************/
  59. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
  60. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
  61. style.setWrapText(true);
  62. Font font = workbook.createFont();
  63. font.setColor(HSSFFont.COLOR_NORMAL);
  64. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  65. style.setFont(font);
  66. Sheet sheet = workbook.createSheet(); // 创建sheet
  67. // 设置表头
  68. // 创建第一行
  69. Row row1 = sheet.createRow(0);
  70. Cell cell = null;
  71. for (int i = 0; i < headColumns.length; i++) {
  72. // 创建列
  73. cell = row1.createCell(i);
  74. // 定义单元格为字符串类型
  75. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  76. // 设置单元格的样式
  77. cell.setCellStyle(style);
  78. // 设置单元格的值
  79. cell.setCellValue(headColumns[i]);
  80. }
  81. Row row = null;
  82. // 添加数据
  83. Map<String, Object> data = null;
  84. for (int i = 1; i <= dataset.size(); i++) {
  85. data = dataset.get(i - 1);
  86. if (data != null) {
  87. row = sheet.createRow(i);
  88. int index = 0;
  89. for (Entry<String, Object> entry : data.entrySet()) {
  90. row.createCell(index).setCellValue(entry.getValue().toString());
  91. index++;
  92. }
  93. }
  94. }
  95. return workbook;
  96. }
  97. /**
  98. * 将数据集dataset导出到fileName文件中
  99. * @param headColumns 导出文件的列名
  100. * @param dataset 数据源
  101. * @return
  102. * @throws IOException
  103. */
  104. public static HSSFWorkbook exportExcel(String[] headColumns, List<Map<String, Object>> dataset) throws IOException {
  105. if (headColumns == null) {
  106. throw new UtilException("excel列名不能为空");
  107. }
  108. if (dataset == null) {
  109. throw new UtilException("数据集不能为空");
  110. }
  111. if (dataset.size() > MAX_DATA_SIZE) {
  112. throw new UtilException("数据集太大,不能导出.最大数据集不能超过" + MAX_DATA_SIZE);
  113. }
  114. HSSFWorkbook workbook = new HSSFWorkbook();
  115. HSSFCellStyle style = workbook.createCellStyle(); // 获取单元格样式
  116. /************** 设置单元格样式 *************/
  117. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
  118. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
  119. style.setWrapText(true);
  120. HSSFFont font = workbook.createFont();
  121. font.setColor(HSSFFont.COLOR_NORMAL);
  122. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  123. style.setFont(font);
  124. HSSFSheet sheet = workbook.createSheet(); // 创建sheet
  125. // 设置表头
  126. // 创建第一行
  127. HSSFRow row1 = sheet.createRow(0);
  128. HSSFCell cell = null;
  129. for (int i = 0; i < headColumns.length; i++) {
  130. // 创建列
  131. cell = row1.createCell(i);
  132. // 定义单元格为字符串类型
  133. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  134. // 设置单元格的样式
  135. cell.setCellStyle(style);
  136. // 设置单元格的值
  137. cell.setCellValue(headColumns[i]);
  138. }
  139. HSSFRow row = null;
  140. // 添加数据
  141. Map<String, Object> data = null;
  142. for (int i = 1; i <= dataset.size(); i++) {
  143. data = dataset.get(i - 1);
  144. if (data != null) {
  145. row = sheet.createRow(i);
  146. int index = 0;
  147. for (Entry<String, Object> entry : data.entrySet()) {
  148. row.createCell(index).setCellValue(entry.getValue().toString());
  149. index++;
  150. }
  151. }
  152. }
  153. return workbook;
  154. }
  155. /**
  156. * 将数据集dataset导出到fileName文件中
  157. * @param headColumns 导出文件的列名
  158. * @param fieldColumns
  159. * @param dataset 数据源
  160. * @return
  161. * @throws IOException
  162. * @throws NoSuchMethodException
  163. * @throws InvocationTargetException
  164. * @throws IllegalAccessException
  165. */
  166. public static <T> HSSFWorkbook exportExcel(String[] headColumns, String[] fieldColumns, List<T> dataset) throws IOException, IllegalAccessException,
  167. InvocationTargetException, NoSuchMethodException {
  168. if (headColumns == null) {
  169. throw new UtilException("excel列名不能为空");
  170. }
  171. if (dataset == null) {
  172. throw new UtilException("数据集不能为空");
  173. }
  174. /*if (dataset.size() > MAX_DATA_SIZE) {
  175. throw new UtilException("数据集太大,不能导出.最大数据集不能超过" + MAX_DATA_SIZE);
  176. }*/
  177. HSSFWorkbook workbook = new HSSFWorkbook();
  178. HSSFCellStyle style = workbook.createCellStyle(); // 获取单元格样式
  179. /************** 设置单元格样式 *************/
  180. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
  181. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
  182. style.setWrapText(true);
  183. HSSFFont font = workbook.createFont();
  184. font.setColor(HSSFFont.COLOR_NORMAL);
  185. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  186. style.setFont(font);
  187. HSSFSheet sheet = workbook.createSheet(); // 创建sheet
  188. // 设置表头
  189. // 创建第一行
  190. HSSFRow row1 = sheet.createRow(0);
  191. HSSFCell cell = null;
  192. for (int i = 0; i < headColumns.length; i++) {
  193. // 创建列
  194. cell = row1.createCell(i);
  195. // 定义单元格为字符串类型
  196. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  197. // 设置单元格的样式
  198. cell.setCellStyle(style);
  199. // 设置单元格的值
  200. cell.setCellValue(headColumns[i]);
  201. }
  202. HSSFRow row = null;
  203. // 添加数据
  204. T data = null;
  205. Object obj = null;
  206. for (int i = 1; i <= dataset.size(); i++) {
  207. data = dataset.get(i - 1);
  208. if (data != null) {
  209. row = sheet.createRow(i);
  210. for (int j = 0; j < fieldColumns.length; j++) {
  211. cell = row.createCell(j);
  212. try {
  213. obj = PropertyUtils.getNestedProperty(data, fieldColumns[j]);
  214. if (obj instanceof Date) {
  215. obj = sdf.format(obj);
  216. }
  217. } catch (NestedNullException e) {
  218. LOGGER.warn(e.getMessage());
  219. obj = null;
  220. }
  221. if (obj != null) {
  222. cell.setCellValue(obj.toString());
  223. } else {
  224. cell.setCellValue("");
  225. }
  226. }
  227. }
  228. }
  229. return workbook;
  230. }
  231. /**
  232. * 将数据集dataset导出到fileName文件中(只支持.xlsx格式)
  233. * @param headColumns 导出文件的列名
  234. * @param fieldColumns
  235. * @param dataset 数据源
  236. * @return
  237. * @throws IOException
  238. * @throws NoSuchMethodException
  239. * @throws InvocationTargetException
  240. * @throws IllegalAccessException
  241. */
  242. public static <T> SXSSFWorkbook exportBigExcel(String[] headColumns, String[] fieldColumns, List<T> dataset) throws IOException, IllegalAccessException,
  243. InvocationTargetException, NoSuchMethodException {
  244. if (headColumns == null) {
  245. throw new UtilException("excel列名不能为空");
  246. }
  247. if (dataset == null) {
  248. throw new UtilException("数据集不能为空");
  249. }
  250. SXSSFWorkbook workbook = new SXSSFWorkbook();
  251. CellStyle style = workbook.createCellStyle(); // 获取单元格样式
  252. /************** 设置单元格样式 *************/
  253. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
  254. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
  255. style.setWrapText(true);
  256. Font font = workbook.createFont();
  257. font.setColor(HSSFFont.COLOR_NORMAL);
  258. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  259. style.setFont(font);
  260. Sheet sheet = workbook.createSheet(); // 创建sheet
  261. // 设置表头
  262. // 创建第一行
  263. Row row1 = sheet.createRow(0);
  264. Cell cell = null;
  265. for (int i = 0; i < headColumns.length; i++) {
  266. // 创建列
  267. cell = row1.createCell(i);
  268. // 定义单元格为字符串类型
  269. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  270. // 设置单元格的样式
  271. cell.setCellStyle(style);
  272. // 设置单元格的值
  273. cell.setCellValue(headColumns[i]);
  274. }
  275. Row row = null;
  276. // 添加数据
  277. T data = null;
  278. Object obj = null;
  279. for (int i = 1; i <= dataset.size(); i++) {
  280. data = dataset.get(i - 1);
  281. if (data != null) {
  282. row = sheet.createRow(i);
  283. for (int j = 0; j < fieldColumns.length; j++) {
  284. cell = row.createCell(j);
  285. try {
  286. obj = PropertyUtils.getNestedProperty(data, fieldColumns[j]);
  287. if (obj instanceof Date) {
  288. obj = sdf.format(obj);
  289. }
  290. } catch (NestedNullException e) {
  291. LOGGER.warn(e.getMessage());
  292. obj = null;
  293. }
  294. if (obj != null) {
  295. cell.setCellValue(obj.toString());
  296. } else {
  297. cell.setCellValue("");
  298. }
  299. }
  300. }
  301. }
  302. return workbook;
  303. }
  304. /**
  305. * 导入指定的excel文件
  306. * @param excelFile excel文件
  307. * @param startRowNum 从第几行数据开始导入
  308. * @return
  309. * @throws IOException
  310. */
  311. public static Map<String, List<Map<String, Object>>> importExcel(File excelFile, int startRowNum) throws IOException {
  312. Map<String, List<Map<String, Object>>> result = new HashMap<String, List<Map<String, Object>>>();
  313. if (!excelFile.exists()) {
  314. LOGGER.error("文件" + excelFile.getAbsolutePath() + "不存在");
  315. return result;
  316. }
  317. Workbook workbook = null;
  318. FileInputStream fis = new FileInputStream(excelFile);
  319. int index = excelFile.getName().lastIndexOf('.');
  320. if (index <= 0) {
  321. fis.close();
  322. throw new UtilException("excel文件的扩展名是.xls or .xlsx!");
  323. }
  324. String ext = excelFile.getName().substring(index).toLowerCase();
  325. try {
  326. if (".xlsx".equals(ext.toLowerCase())) {
  327. // 支持excel2007 xlsx格式
  328. workbook = new XSSFWorkbook(fis);
  329. } else if (".xls".equals(ext)) {
  330. // 支持excel2003以前 xls格式
  331. workbook = new HSSFWorkbook(fis);
  332. } else {
  333. throw new UtilException("excel文件的扩展名是.xls or .xlsx!");
  334. }
  335. } catch (Exception ex) {
  336. LOGGER.error("excel open error.", ex);
  337. return result;
  338. } finally {
  339. if (fis != null) {
  340. fis.close();
  341. }
  342. }
  343. int sheetCount = workbook.getNumberOfSheets();
  344. Sheet sheet = null;
  345. Row row = null;
  346. Cell cell = null;
  347. int currentRowNum = 0, currentCellNum = 0;
  348. // Object fieldValue = null;
  349. Iterator<Row> rowIter = null;
  350. Iterator<Cell> cellIter = null;
  351. for (int i = 0; i < sheetCount; i++) {
  352. String fieldsName[] = null;
  353. // 行号清零
  354. currentRowNum = 0;
  355. // 顺序取sheet
  356. sheet = workbook.getSheetAt(i);
  357. List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
  358. rowIter = sheet.iterator();
  359. while (rowIter.hasNext()) {
  360. Map<String, Object> obj = null;
  361. // 获取当前行
  362. row = rowIter.next();
  363. if (row != null) {
  364. currentRowNum++;
  365. if (currentRowNum == 1) {// 第一列表示英文名称对应表字段
  366. cellIter = row.iterator();
  367. // 列号清零
  368. currentCellNum = 0;
  369. List<String> names = new ArrayList<String>();
  370. while (cellIter.hasNext()) {
  371. cell = cellIter.next();
  372. names.add(cell.getStringCellValue());
  373. }
  374. fieldsName = names.toArray(new String[names.size()]);
  375. continue;
  376. }
  377. // 跳过指定的行
  378. if (currentRowNum < startRowNum) {
  379. continue;
  380. }
  381. // 实例化对象
  382. obj = new HashMap<String, Object>();// clazz.newInstance();
  383. cellIter = row.iterator();
  384. // 列号清零
  385. currentCellNum = 0;
  386. while (cellIter.hasNext()) {
  387. cell = cellIter.next();
  388. cell.setCellType(Cell.CELL_TYPE_STRING);
  389. String fieldValue = cell.getStringCellValue();
  390. obj.put(fieldsName[currentCellNum], fieldValue);
  391. currentCellNum++;
  392. }
  393. }
  394. if (obj != null)
  395. datas.add(obj);
  396. }
  397. if (!datas.isEmpty())
  398. result.put(sheet.getSheetName(), datas);
  399. }
  400. if (workbook != null) {
  401. workbook.close();
  402. }
  403. return result;
  404. }
  405. /**
  406. * 导入指定的excel文件
  407. * @param inputStream excel文件流
  408. * @param startRowNum 从第几行数据开始导入
  409. * @param extName 文件扩展名,仅支持.xls 或 .xlsx
  410. * @return
  411. * @throws IOException
  412. */
  413. public static Map<String, List<Map<String, Object>>> importExcel(InputStream inputStream, int startRowNum, String extName) throws IOException {
  414. Map<String, List<Map<String, Object>>> result = new HashMap<String, List<Map<String, Object>>>();
  415. Workbook workbook = null;
  416. try {
  417. if (extName.endsWith(".xlsx")) {
  418. // 支持excel2007 xlsx格式
  419. workbook = new XSSFWorkbook(inputStream);
  420. } else if (extName.endsWith(".xls")) {
  421. // 支持excel2003以前 xls格式
  422. workbook = new HSSFWorkbook(inputStream);
  423. } else {
  424. throw new UtilException("excel文件的扩展名是.xls or .xlsx!");
  425. }
  426. } catch (Exception ex) {
  427. LOGGER.error("excel open error.", ex);
  428. return result;
  429. } finally {
  430. if (inputStream != null) {
  431. inputStream.close();
  432. }
  433. }
  434. int sheetCount = workbook.getNumberOfSheets();
  435. Sheet sheet = null;
  436. Row row = null;
  437. Cell cell = null;
  438. int currentRowNum = 0, currentCellNum = 0;
  439. // Object fieldValue = null;
  440. Iterator<Row> rowIter = null;
  441. Iterator<Cell> cellIter = null;
  442. for (int i = 0; i < sheetCount; i++) {
  443. String fieldsName[] = null;
  444. // 行号清零
  445. currentRowNum = 0;
  446. // 顺序取sheet
  447. sheet = workbook.getSheetAt(i);
  448. List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
  449. rowIter = sheet.iterator();
  450. while (rowIter.hasNext()) {
  451. Map<String, Object> obj = null;
  452. boolean hasVal = false;
  453. // 获取当前行
  454. row = rowIter.next();
  455. if (row != null) {
  456. currentRowNum++;
  457. if (currentRowNum == 1) {// 第一列表示英文名称对应表字段
  458. cellIter = row.iterator();
  459. // 列号清零
  460. currentCellNum = 0;
  461. List<String> names = new ArrayList<String>();
  462. while (cellIter.hasNext()) {
  463. cell = cellIter.next();
  464. names.add(cell.getStringCellValue());
  465. }
  466. fieldsName = names.toArray(new String[names.size()]);
  467. continue;
  468. }
  469. // 跳过指定的行
  470. if (currentRowNum < startRowNum) {
  471. continue;
  472. }
  473. // 实例化对象
  474. obj = new HashMap<String, Object>();// clazz.newInstance();
  475. cellIter = row.iterator();
  476. // 列号清零
  477. currentCellNum = 0;
  478. while (cellIter.hasNext()) {
  479. cell = cellIter.next();
  480. cell.setCellType(Cell.CELL_TYPE_STRING);
  481. String fieldValue = cell.getStringCellValue();
  482. if(StringUtils.isNotBlank(fieldValue)){
  483. hasVal = true;
  484. }
  485. obj.put(fieldsName[currentCellNum], fieldValue);
  486. currentCellNum++;
  487. }
  488. }
  489. if (obj != null && hasVal)
  490. datas.add(obj);
  491. }
  492. if (!datas.isEmpty())
  493. result.put(sheet.getSheetName(), datas);
  494. }
  495. if (workbook != null) {
  496. workbook.close();
  497. }
  498. return result;
  499. }
  500. /**
  501. * 创建excel文档
  502. *
  503. * @param getters list中map的key数组集合
  504. * @param headers excel的列名
  505. */
  506. public static Workbook createWorkBook(List list, String[] getters, String[] headers, Class clazz) {
  507. List<Method> methods = getMethodsByStrs(getters, clazz);
  508. // 创建.xlsx工作簿
  509. Workbook wb = new XSSFWorkbook();
  510. // 创建第一个sheet(页),并命名
  511. Sheet sheet = wb.createSheet("sheet1");
  512. // 手动设置列宽.第一个参数表示要为第几列设,第二个参数表示列的宽度,n为列高的像素数.
  513. for (int i = 0; i < getters.length; i++) {
  514. sheet.setColumnWidth((short) i, (short) (35.7 * 200));
  515. }
  516. // 创建第一行
  517. Row header = sheet.createRow(0);
  518. // 创建两种单元格格式
  519. CellStyle cellStyle1 = wb.createCellStyle();
  520. CellStyle cellStyle2 = wb.createCellStyle();
  521. // 创建两种字体
  522. Font font1 = wb.createFont(); // 标题字体
  523. Font font2 = wb.createFont(); // 正文字体
  524. // 标题加粗
  525. font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
  526. // 设置两种单元格的样式
  527. setCellStype(cellStyle1, font1);
  528. setCellStype(cellStyle2, font2);
  529. //设置header
  530. for (int i = 0; i < headers.length; i++) {
  531. Cell cell = header.createCell(i);
  532. cell.setCellValue(headers[i]);
  533. cell.setCellStyle(cellStyle1);
  534. }
  535. //设置data
  536. int headersNum = 1;
  537. for (int i = 0; i < list.size(); i++) {
  538. Row row = sheet.createRow(i + headersNum);
  539. for (int j = 0; j < methods.size(); j++) {
  540. try {
  541. Object invoke = methods.get(j).invoke(list.get(i));
  542. if (invoke != null) {
  543. row.createCell(j).setCellValue(invoke.toString());
  544. }
  545. } catch (Exception e) {
  546. e.printStackTrace();
  547. }
  548. }
  549. }
  550. return wb;
  551. }
  552. private static void setCellStype(CellStyle cellStyle, Font font) {
  553. font.setFontHeightInPoints((short) 10);
  554. font.setColor(IndexedColors.BLACK.getIndex());
  555. cellStyle.setFont(font);
  556. cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
  557. cellStyle.setBorderRight(CellStyle.BORDER_THIN);
  558. cellStyle.setBorderTop(CellStyle.BORDER_THIN);
  559. cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
  560. cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
  561. }
  562. private static List<Method> getMethodsByStrs(String[] getters, Class clazz) {
  563. List<Method> list = new ArrayList<>();
  564. for (String getter : getters) {
  565. try {
  566. list.add(clazz.getDeclaredMethod(getter));
  567. } catch (NoSuchMethodException e) {
  568. e.printStackTrace();
  569. }
  570. }
  571. return list;
  572. }
  573. }