1350ee4bbff7d781987921b55916f969a489eb60.svn-base 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. package cn.com.goldenwater.dcproj.utils.expExcel;
  2. import cn.com.goldenwater.dcproj.annotation.NotExport;
  3. import cn.com.goldenwater.dcproj.model.TacExprRcmm;
  4. import cn.com.goldenwater.dcproj.model.TacWorkerB;
  5. import cn.com.goldenwater.dcproj.utils.impexcel.ExportExcel;
  6. import io.swagger.annotations.ApiModelProperty;
  7. import org.apache.commons.collections.map.HashedMap;
  8. import org.apache.commons.lang3.ArrayUtils;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  11. import org.apache.poi.ss.usermodel.*;
  12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.*;
  16. import java.lang.reflect.Field;
  17. import java.text.ParseException;
  18. import java.text.SimpleDateFormat;
  19. import java.util.*;
  20. import static cn.com.goldenwater.dcproj.utils.BeanUtil.transBean2Map;
  21. /**
  22. * Created by jinshui on 2019/8/26.
  23. */
  24. public class ExportUtil {
  25. private static final String XLS = "xls";
  26. private ExportUtil() {
  27. }
  28. public static final List<String> COMMONLIST_AND_OPEN_ANNOTATION = new ArrayList<String>() {{
  29. add("id");
  30. }};
  31. private static List<String> list = new ArrayList<>();
  32. private static List<String> statList = new ArrayList<>();
  33. static {
  34. list.add("code");
  35. list.add("nodeId");
  36. list.add("ptype");
  37. list.add("lgtd");
  38. list.add("lttd");
  39. list.add("gdX");
  40. list.add("gdY");
  41. list.add("objId");
  42. list.add("id");
  43. list.add("rgstrId");
  44. list.add("wintCode");
  45. list.add("pcX");
  46. list.add("pcY");
  47. list.add("safetyStat");
  48. list.add("measuresStat");
  49. list.add("waterStat");
  50. list.add("intInfoStat");
  51. list.add("apprInfoStat");
  52. }
  53. /**
  54. * 导出所选字段
  55. *
  56. * @param result
  57. * @param response
  58. * @param fileName
  59. * @param list
  60. */
  61. public static void exportExcelColumn(List<?> result, HttpServletResponse response, String fileName,
  62. List<String> list, Class cla) {
  63. if (list == null || list.size() == 0) {
  64. exportExcel(result, response, fileName);
  65. return;
  66. }
  67. List<Map<String, Object>> mapList = new ArrayList<>();
  68. result.forEach(worker -> mapList.add(transObjToMap(worker)));
  69. ExportAbstract export = new ExcelExport();
  70. if (StringUtils.isBlank(fileName)) {
  71. fileName = "结果列表";
  72. }
  73. export.setFileName(fileName);
  74. export.setExport_ps_export(true);
  75. export.setExport_ps_type(ExportAbstract.XLS);
  76. export.setExport_bzip(false);
  77. export.setTitle(fileName);
  78. ArrayList<Object> cols = new ArrayList<Object>();
  79. Field[] fields = cla.getDeclaredFields();
  80. for (int j = 0; j <= fields.length - 1; j++) {
  81. //设置可访问
  82. Field field = fields[j];
  83. if (!list.contains(field.getName())) {
  84. continue;
  85. }
  86. field.setAccessible(true);
  87. if (field.getAnnotation(ApiModelProperty.class) != null) {
  88. cols.add(field.getName() + "[" + fileFormat(field.getAnnotation(ApiModelProperty.class).value()) + "]");
  89. }
  90. }
  91. if (cols.size() < 1) {
  92. cols.add("name[名称]");
  93. cols.add("code[编码]");
  94. }
  95. export.setCols(cols);
  96. export.setGroupable(false);
  97. // 设置视图指标
  98. export.setLevel(1);
  99. export.setLocksize(0);
  100. try {
  101. export.Export(response);
  102. export.ExportHeadForCustom(response);//导出表头
  103. export.ContinueExport(mapList);
  104. export.EndExport();
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. private static Map<String, Object> transObjToMap(Object worker) {
  110. Map<String, Object> map = new HashedMap();
  111. Field[] fields = worker.getClass().getDeclaredFields();
  112. for (int j = 0; j <= fields.length - 1; j++) {
  113. //设置可访问
  114. Field field = fields[j];
  115. field.setAccessible(true);
  116. field.getName();
  117. try {
  118. if (field.getType() == String.class) {
  119. map.put(field.getName(), String.valueOf(field.get(worker)));
  120. } else {
  121. map.put(field.getName(), field.get(worker));
  122. }
  123. } catch (IllegalAccessException e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. return map;
  128. }
  129. /**
  130. * 过滤字段
  131. *
  132. * @param result
  133. * @param response
  134. * @param fileName
  135. * @param list
  136. */
  137. public static void exportExcel(List<?> result, HttpServletResponse response, String fileName, List<String> list, Class cla) {
  138. if (list == null || list.size() == 0) {
  139. exportExcel(result, response, fileName);
  140. return;
  141. }
  142. List<Map<String, Object>> mapList = new ArrayList<>();
  143. result.forEach(worker -> mapList.add(transBean2Map(worker)));
  144. ExportAbstract export = new ExcelExport();
  145. if (StringUtils.isBlank(fileName)) {
  146. fileName = "结果列表";
  147. }
  148. export.setFileName(fileName);
  149. export.setExport_ps_export(true);
  150. export.setExport_ps_type(ExportAbstract.XLS);
  151. export.setExport_bzip(false);
  152. export.setTitle(fileName);
  153. ArrayList<Object> cols = new ArrayList<Object>();
  154. Field[] fields = cla.getDeclaredFields();
  155. Class clazz = cla.getSuperclass();
  156. if (clazz != null && clazz != Objects.class) {
  157. fields = ArrayUtils.addAll(fields, clazz.getDeclaredFields());
  158. }
  159. for (int j = 0; j <= fields.length - 1; j++) {
  160. //设置可访问
  161. Field field = fields[j];
  162. if (list.contains(field.getName())) {
  163. continue;
  164. }
  165. field.setAccessible(true);
  166. if (field.getAnnotation(ApiModelProperty.class) != null && field.getAnnotation(NotExport.class) == null) {
  167. cols.add(field.getName() + "[" + fileFormat(field.getAnnotation(ApiModelProperty.class).value()) + "]");
  168. }
  169. }
  170. if (cols.size() < 1) {
  171. cols.add("name[名称]");
  172. cols.add("code[编码]");
  173. }
  174. export.setCols(cols);
  175. export.setGroupable(false);
  176. // 设置视图指标
  177. export.setLevel(1);
  178. export.setLocksize(0);
  179. try {
  180. export.Export(response);
  181. // 导出表头
  182. export.ExportHeadForCustom(response);
  183. export.ContinueExport(mapList);
  184. export.EndExport();
  185. } catch (Exception e) {
  186. e.printStackTrace();
  187. }
  188. }
  189. /**
  190. * 使用字段
  191. *
  192. * @param result
  193. * @param response
  194. * @param fileName
  195. * @param list
  196. */
  197. public static void exportExcel2(List<?> result, HttpServletResponse response, String fileName, List<String> list, Class cla) {
  198. if (list == null || list.size() == 0) {
  199. exportExcel(result, response, fileName);
  200. return;
  201. }
  202. List<Map<String, Object>> mapList = new ArrayList<>();
  203. result.forEach(worker -> mapList.add(transBean2Map(worker)));
  204. ExportAbstract export = new ExcelExport();
  205. if (StringUtils.isBlank(fileName)) {
  206. fileName = "结果列表";
  207. }
  208. export.setFileName(fileName);
  209. export.setExport_ps_export(true);
  210. export.setExport_ps_type(ExportAbstract.XLS);
  211. export.setExport_bzip(false);
  212. export.setTitle(fileName);
  213. ArrayList<Object> cols = new ArrayList<Object>();
  214. Field[] fields = cla.getDeclaredFields();
  215. Class clazz = cla.getSuperclass();
  216. if (clazz != null && clazz != Objects.class) {
  217. fields = ArrayUtils.addAll(fields, clazz.getDeclaredFields());
  218. }
  219. for (String name : list) {
  220. for (Field field : fields) {
  221. if (name.equals(field.getName()) && field.getAnnotation(ApiModelProperty.class) != null) {
  222. field.setAccessible(true);
  223. cols.add(field.getName() + "[" + fileFormat(field.getAnnotation(ApiModelProperty.class).value()) + "]");
  224. break;
  225. }
  226. }
  227. }
  228. if (cols.size() < 1) {
  229. cols.add("name[名称]");
  230. cols.add("code[编码]");
  231. }
  232. export.setCols(cols);
  233. export.setGroupable(false);
  234. // 设置视图指标
  235. export.setLevel(1);
  236. export.setLocksize(0);
  237. try {
  238. export.Export(response);
  239. // 导出表头
  240. export.ExportHeadForCustom(response);
  241. export.ContinueExport(mapList);
  242. export.EndExport();
  243. } catch (Exception e) {
  244. e.printStackTrace();
  245. }
  246. }
  247. public static String fileFormat(String value) {
  248. if (isContainNum(value)) {
  249. return value;
  250. }
  251. //统一字符处理
  252. value = value.replace("(", "(");
  253. if (value.contains(" ")) {
  254. value = value.substring(0, value.indexOf(" "));
  255. }
  256. if (value.contains("(")) {
  257. value = value.substring(0, value.indexOf("("));
  258. }
  259. if (value.startsWith("*")) {
  260. value = value.substring(1);
  261. }
  262. return value;
  263. }
  264. private static boolean isContainNum(String value) {
  265. if (value.contains("m3")) {
  266. return true;
  267. }
  268. if (value.contains("km2")) {
  269. return true;
  270. }
  271. return false;
  272. }
  273. public static void exportExcel(List<?> result, HttpServletResponse response, String fileName) {
  274. List<Map<String, Object>> mapList = new ArrayList<>();
  275. ExportAbstract export = new ExcelExport();
  276. if (StringUtils.isBlank(fileName)) {
  277. fileName = "结果列表";
  278. }
  279. export.setFileName(fileName);
  280. export.setExport_ps_export(true);
  281. export.setExport_ps_type(ExportAbstract.XLS);
  282. export.setExport_bzip(false);
  283. export.setTitle(fileName);
  284. ArrayList<Object> cols = new ArrayList<Object>();
  285. boolean flag = true;
  286. for (Object o : result) {
  287. mapList.add(transBean2Map(o));
  288. if (flag) {
  289. Field[] fields = o.getClass().getDeclaredFields();
  290. for (int j = 0; j <= fields.length - 1; j++) {
  291. //设置可访问
  292. Field field = fields[j];
  293. if (list.contains(field.getName())) {
  294. continue;
  295. }
  296. field.setAccessible(true);
  297. if (field.getAnnotation(ApiModelProperty.class) != null) {
  298. cols.add(field.getName() + "[" + field.getAnnotation(ApiModelProperty.class).value() + "]");
  299. }
  300. }
  301. flag = false;
  302. }
  303. }
  304. if (cols.size() < 1) {
  305. cols.add("name[名称]");
  306. cols.add("code[编码]");
  307. cols.add("location[地址]");
  308. cols.add("adCode[行政区划编码]");
  309. }
  310. export.setCols(cols);
  311. export.setGroupable(false);
  312. // 设置视图指标
  313. export.setLevel(1);
  314. export.setLocksize(0);
  315. try {
  316. export.Export(response);
  317. //导出表头
  318. export.ExportHeadForCustom(response);
  319. export.ContinueExport(mapList);
  320. export.EndExport();
  321. } catch (Exception e) {
  322. e.printStackTrace();
  323. }
  324. }
  325. public static void exportExcel(String isExport, List<?> result, HttpServletResponse response) {
  326. if ("1".equals(isExport)) {
  327. exportExcel(result, response, "");
  328. }
  329. }
  330. /**
  331. * 返回某个类需要导出的字段列表
  332. *
  333. * @param cla
  334. * @param list
  335. */
  336. public static List<Map<String, Object>> getExoportColumns(Class cla, List<String> list) {
  337. List<Map<String, Object>> cols = new ArrayList<>();
  338. Field[] fields = cla.getDeclaredFields();
  339. for (int j = 0; j <= fields.length - 1; j++) {
  340. //设置可访问
  341. Field field = fields[j];
  342. if (list.contains(field.getName())) {
  343. continue;
  344. }
  345. field.setAccessible(true);
  346. if (field.getAnnotation(ApiModelProperty.class) != null) {
  347. Map<String, Object> map = new HashMap<>();
  348. map.put("code", field.getName());
  349. String value = field.getAnnotation(ApiModelProperty.class).value().trim();
  350. //统一字符处理
  351. value = value.replace("(", "(");
  352. if (value.contains(" ")) {
  353. value = value.substring(0, value.indexOf(" "));
  354. }
  355. if (value.contains("(")) {
  356. value = value.substring(0, value.indexOf("("));
  357. }
  358. if (value.startsWith("*")) {
  359. value = value.substring(1);
  360. map.put("selectFlag", true);
  361. }
  362. map.put("name", value);
  363. cols.add(map);
  364. }
  365. }
  366. return cols;
  367. }
  368. /**
  369. * 根据名称
  370. *
  371. * @param value
  372. * @param name
  373. * @param b
  374. */
  375. public static void setClassFiledValue(Object value, String name, Object b, boolean isFormat) {
  376. if (name == null) {
  377. return;
  378. }
  379. try {
  380. Field[] fields = b.getClass().getDeclaredFields();
  381. for (Field field : fields) {
  382. field.setAccessible(true);
  383. if (field.getAnnotation(ApiModelProperty.class) != null) {
  384. String nameValue = field.getAnnotation(ApiModelProperty.class).value();
  385. if (isFormat) {
  386. nameValue = fileFormat(nameValue);
  387. }
  388. if (name.equals(nameValue) && StringUtils.isNotBlank(String.valueOf(value))) {
  389. field.set(b, value);
  390. }
  391. }
  392. }
  393. } catch (Exception e) {
  394. e.printStackTrace();
  395. }
  396. }
  397. public static File isExist(String originUrl) throws IOException {
  398. // 判断文件是否存在
  399. File file = new File(originUrl);
  400. if (!file.exists()) {
  401. throw new IOException("文件名为" + file.getName() + "Excel文件不存在!");
  402. }
  403. return file;
  404. }
  405. /**
  406. * 根据字段名称下载Excel
  407. *
  408. * @param response
  409. * @param list
  410. * @param filePath
  411. */
  412. public static void download(HttpServletResponse response, List<Map<String, Object>> list, String filePath) throws IOException {
  413. ByteArrayOutputStream os = new ByteArrayOutputStream();
  414. FileInputStream fis = new FileInputStream(isExist(filePath));
  415. String fileExt = filePath.substring(filePath.lastIndexOf(".")).replace(".", "");
  416. Workbook workBook = null;
  417. if (XLS.equals(fileExt)) {
  418. workBook = new HSSFWorkbook(fis);
  419. } else {
  420. workBook = new XSSFWorkbook(fis);
  421. }
  422. Sheet sheet = workBook.getSheetAt(0);
  423. Row keyRow = sheet.getRow(1);
  424. List<String> keys = new ArrayList<>();
  425. for (int r = 0; r < keyRow.getPhysicalNumberOfCells(); r++) {
  426. if (!"".equals(keyRow.getCell(r).toString())) {
  427. keys.add(keyRow.getCell(r).toString());
  428. }
  429. }
  430. Row row = null;
  431. for (int i = 0; i < list.size(); i++) {
  432. row = sheet.createRow(i + 2);
  433. Map<String, Object> map = list.get(i);
  434. for (int j = 0; j < keys.size(); j++) {
  435. Object val = map.get(keys.get(j));
  436. if (val == null) {
  437. continue;
  438. }
  439. if (val.getClass() == Double.class) {
  440. row.createCell(j).setCellValue((double) val);
  441. } else if (val.getClass() == Long.class) {
  442. row.createCell(j).setCellValue((long) val);
  443. } else {
  444. row.createCell(j).setCellValue(val == null ? "" : val.toString());
  445. }
  446. }
  447. }
  448. workBook.write(os);
  449. fis.close();
  450. os.flush();
  451. os.close();
  452. ExportExcel.downloadExcelFile(response, os, "人员列表" + filePath.substring(filePath.lastIndexOf(".")));
  453. if (fis != null) {
  454. fis.close();
  455. }
  456. }
  457. public static Map<String, Object> objToMap(Object obj, boolean isFormat) {
  458. if (obj == null) {
  459. return null;
  460. }
  461. Map<String, Object> map = new HashedMap();
  462. Field[] fields = obj.getClass().getDeclaredFields();
  463. for (int i = 0; i < fields.length; i++) {
  464. Field field = fields[i];
  465. field.setAccessible(true);
  466. if (field.getAnnotation(ApiModelProperty.class) != null) {
  467. String nameValue = field.getAnnotation(ApiModelProperty.class).value();
  468. if (isFormat) {
  469. nameValue = fileFormat(nameValue);
  470. }
  471. try {
  472. map.put(nameValue, field.get(obj));
  473. } catch (IllegalAccessException e) {
  474. e.printStackTrace();
  475. }
  476. }
  477. }
  478. return map;
  479. }
  480. public static List<Row> getRowListByFile(MultipartFile file) {
  481. List<Row> rowList = new ArrayList<>();
  482. Workbook wb = null;
  483. try (InputStream fis = file.getInputStream()) {
  484. if (Objects.requireNonNull(file.getOriginalFilename()).endsWith(XLS)) {
  485. wb = new HSSFWorkbook(fis);
  486. } else {
  487. wb = new XSSFWorkbook(fis);
  488. }
  489. Sheet sheet = wb.getSheetAt(0);
  490. Row row = null;
  491. int lastRowNum = sheet.getPhysicalNumberOfRows();
  492. for (int i = sheet.getFirstRowNum(); i < lastRowNum; i++) {
  493. row = sheet.getRow(i);
  494. if (row != null) {
  495. rowList.add(row);
  496. }
  497. }
  498. } catch (IOException e) {
  499. e.printStackTrace();
  500. } finally {
  501. if (wb != null) {
  502. try {
  503. wb.close();
  504. } catch (IOException e) {
  505. e.printStackTrace();
  506. }
  507. }
  508. }
  509. return rowList;
  510. }
  511. /**
  512. * @param rowList 行集合
  513. * @param i 开始行,一般为名称行数的下表,没有code隐藏行时
  514. * @return
  515. */
  516. public static List<Map<String, Object>> rowToMap(List<Row> rowList, int i) {
  517. if (i == 0) {
  518. i = 1;
  519. }
  520. List<String> keys = new ArrayList<>();
  521. Row keyRow = rowList.get(i);
  522. for (int r = 0; r < keyRow.getPhysicalNumberOfCells(); r++) {
  523. if (!"".equals(keyRow.getCell(r).toString())) {
  524. keys.add(keyRow.getCell(r).toString());
  525. }
  526. }
  527. List<Map<String, Object>> mapList = new ArrayList<>();
  528. Row row = null;
  529. for (int j = i + 1; j < rowList.size(); j++) {
  530. row = rowList.get(j);
  531. Map<String, Object> map = new HashedMap();
  532. for (int k = 0; k < keys.size(); k++) {
  533. Cell cell = row.getCell(k);
  534. if (cell != null) {
  535. if (cell.getCellType() == CellType.NUMERIC) {
  536. map.put(keys.get(k), cell.getNumericCellValue());
  537. } else if (cell.getCellType() == CellType.STRING) {
  538. map.put(keys.get(k), cell.getStringCellValue());
  539. }
  540. }
  541. }
  542. mapList.add(map);
  543. }
  544. return mapList;
  545. }
  546. public static List<TacWorkerB> mapToObj(List<Map<String, Object>> mapList, Class cla, boolean isFormat) {
  547. if (mapList == null || mapList.size() == 0) {
  548. return null;
  549. }
  550. List<TacWorkerB> list = new ArrayList<>();
  551. Field[] fields = cla.getDeclaredFields();
  552. for (Map<String, Object> map : mapList) {
  553. TacWorkerB workerB = new TacWorkerB();
  554. for (Field field : fields) {
  555. field.setAccessible(true);
  556. if (field.getAnnotation(ApiModelProperty.class) != null) {
  557. String nameValue = field.getAnnotation(ApiModelProperty.class).value();
  558. if (isFormat) {
  559. nameValue = fileFormat(nameValue);
  560. }
  561. if (map.containsKey(nameValue)) {
  562. try {
  563. Object object = map.get(nameValue);
  564. if (object == null) {
  565. continue;
  566. }
  567. if (field.getType() == Double.class) {
  568. field.set(workerB, Double.valueOf(String.valueOf(object)));
  569. }
  570. if (field.getType() == String.class) {
  571. if (object.getClass() == Double.class) {
  572. if (80000000.0 - (double) object < 0) {
  573. object = (long) ((double) object);
  574. object = String.valueOf(object);
  575. }
  576. field.set(workerB, object);
  577. } else {
  578. field.set(workerB, String.valueOf(object));
  579. }
  580. }
  581. } catch (IllegalAccessException e) {
  582. e.printStackTrace();
  583. }
  584. }
  585. }
  586. }
  587. list.add(workerB);
  588. }
  589. return list;
  590. }
  591. public static List<Object> mapToObj(List<Map<String, Object>> mapList, Class cla) {
  592. if (mapList == null || mapList.size() == 0) {
  593. return null;
  594. }
  595. List<Object> list = new ArrayList<>();
  596. Field[] fields = cla.getDeclaredFields();
  597. for (Map<String, Object> map : mapList) {
  598. Object obj = null;
  599. try {
  600. obj = cla.newInstance();
  601. for (Field field : fields) {
  602. field.setAccessible(true);
  603. if (field.getAnnotation(ApiModelProperty.class) != null) {
  604. String nameValue = field.getAnnotation(ApiModelProperty.class).value();
  605. if (map.containsKey(nameValue)) {
  606. try {
  607. Object object = map.get(nameValue);
  608. if (object == null) {
  609. continue;
  610. }
  611. if (field.getType() == Double.class) {
  612. field.set(obj, Double.valueOf(String.valueOf(object)));
  613. }
  614. if (field.getType() == String.class) {
  615. field.set(obj, String.valueOf(object));
  616. }
  617. if (field.getType() == Date.class) {
  618. SimpleDateFormat format = new SimpleDateFormat("yyyy/mm/dd");
  619. try {
  620. field.set(obj, format.parse(String.valueOf(object)));
  621. } catch (ParseException e) {
  622. e.printStackTrace();
  623. }
  624. }
  625. } catch (IllegalAccessException e) {
  626. e.printStackTrace();
  627. }
  628. }
  629. }
  630. }
  631. list.add(obj);
  632. } catch (InstantiationException e) {
  633. e.printStackTrace();
  634. } catch (IllegalAccessException e) {
  635. e.printStackTrace();
  636. }
  637. }
  638. return list;
  639. }
  640. public static void exportExcel(Map<String, List<TacExprRcmm>> map, HttpServletResponse response, String fileName, List<String> columns) throws IOException {
  641. // 设置response参数,可以打开下载页面
  642. response.reset();
  643. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  644. response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
  645. Workbook workbook = new XSSFWorkbook();
  646. if (map != null) {
  647. for (String sheetName : map.keySet()) {
  648. Sheet sheet = workbook.createSheet(sheetName);
  649. createExcelContent(sheet, map.get(sheetName), columns);
  650. }
  651. }
  652. workbook.write(response.getOutputStream());
  653. workbook.close();
  654. }
  655. private static void createExcelContent(Sheet sheet, List<?> tacExprRcmms, List<String> columns) {
  656. if (tacExprRcmms == null || tacExprRcmms.size() == 0) {
  657. return;
  658. }
  659. boolean flag = false;
  660. Row titleRow = null;
  661. Map<Integer, String> columnMap = new HashedMap();
  662. for (int i = 0; i < tacExprRcmms.size(); i++) {
  663. Map<String, Object> map = transObjToMap(tacExprRcmms.get(i));
  664. if (!flag) {
  665. titleRow = sheet.createRow(1);
  666. createTitleRow(titleRow, tacExprRcmms.get(i), columnMap, columns);
  667. }
  668. if (titleRow == null) {
  669. continue;
  670. }
  671. Row row = sheet.createRow(i + 2);
  672. row.createCell(0).setCellValue(i + 1);
  673. for (int j = 1; j < titleRow.getPhysicalNumberOfCells(); j++) {
  674. row.createCell(j).setCellValue(map.get(columnMap.get(j)) == null ? "" : String.valueOf(map.get(columnMap.get(j))));
  675. }
  676. }
  677. }
  678. private static void createTitleRow(Row titleRow, Object obj, Map<Integer, String> columnMap, List<String> columns) {
  679. Field[] fields = obj.getClass().getDeclaredFields();
  680. titleRow.createCell(0).setCellValue("序号");
  681. for (int i = 0; i < fields.length; i++) {
  682. Field field = fields[i];
  683. if (columns.contains(field.getName())) {
  684. continue;
  685. }
  686. field.setAccessible(true);
  687. if (field.getAnnotation(ApiModelProperty.class) != null) {
  688. String nameValue = fileFormat(field.getAnnotation(ApiModelProperty.class).value());
  689. titleRow.createCell(i + 1).setCellValue(nameValue);
  690. columnMap.put(i + 1, field.getName());
  691. }
  692. }
  693. }
  694. }