ExportBeanExcelUtil.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.diagbot.util;
  2. import com.diagbot.dto.RespDTO;
  3. import org.apache.poi.hssf.usermodel.HSSFCell;
  4. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  5. import org.apache.poi.hssf.usermodel.HSSFRow;
  6. import org.apache.poi.hssf.usermodel.HSSFSheet;
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.lang.reflect.Field;
  13. import java.lang.reflect.InvocationTargetException;
  14. import java.lang.reflect.Method;
  15. import java.util.Collection;
  16. import java.util.HashMap;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Map;
  20. @SuppressWarnings({ "deprecation" })
  21. public class ExportBeanExcelUtil<T> {
  22. private final String MSG_SUCCESS = "操作成功!";
  23. private final String MSG_ERROR = "操作失败!";
  24. /**
  25. * 这是一个通用的方法,利用了JAVA的反射机制,可以将放置在JAVA集合中并且符号一定条件的数据以EXCEL 的形式输出
  26. * <p>
  27. * title 表格标题名
  28. * headersName 表格属性列名数组
  29. * headersId 表格属性列名对应的字段---你需要导出的字段名(为了更灵活控制你想要导出的字段)
  30. * dtoList 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象
  31. * out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中
  32. */
  33. public RespDTO exportExcel(String title, List<String> headersName, List<String> headersId,
  34. List<T> dtoList, String lujing, String fileName) {
  35. /*(一)表头--标题栏*/
  36. Map<Integer, String> headersNameMap = new HashMap<>();
  37. int key = 0;
  38. for (int i = 0; i < headersName.size(); i++) {
  39. if (!headersName.get(i).equals(null)) {
  40. headersNameMap.put(key, headersName.get(i));
  41. key++;
  42. }
  43. }
  44. /*(二)字段*/
  45. Map<Integer, String> titleFieldMap = new HashMap<>();
  46. int value = 0;
  47. for (int i = 0; i < headersId.size(); i++) {
  48. if (!headersId.get(i).equals(null)) {
  49. titleFieldMap.put(value, headersId.get(i));
  50. value++;
  51. }
  52. }
  53. /* (三)声明一个工作薄:包括构建工作簿、表格、样式*/
  54. HSSFWorkbook wb = new HSSFWorkbook();
  55. HSSFSheet sheet = wb.createSheet(title);
  56. sheet.setDefaultColumnWidth((short) 15);
  57. // 生成一个样式
  58. HSSFCellStyle style = wb.createCellStyle();
  59. HSSFRow row = sheet.createRow(0);
  60. style.setAlignment(HorizontalAlignment.CENTER);
  61. HSSFCell cell;
  62. Collection c = headersNameMap.values();//拿到表格所有标题的value的集合
  63. Iterator<String> it = c.iterator();//表格标题的迭代器
  64. /*(四)导出数据:包括导出标题栏以及内容栏*/
  65. //根据选择的字段生成表头
  66. short size = 0;
  67. while (it.hasNext()) {
  68. cell = row.createCell(size);
  69. cell.setCellValue(it.next().toString());
  70. cell.setCellStyle(style);
  71. size++;
  72. }
  73. //表格标题一行的字段的集合
  74. Collection zdC = titleFieldMap.values();
  75. Iterator<T> labIt = dtoList.iterator();//总记录的迭代器
  76. int zdRow = 0;//列序号
  77. while (labIt.hasNext()) {//记录的迭代器,遍历总记录
  78. int zdCell = 0;
  79. zdRow++;
  80. row = sheet.createRow(zdRow);
  81. T l = (T) labIt.next();
  82. // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
  83. Field[] fields = l.getClass().getDeclaredFields();//获得JavaBean全部属性
  84. for (short i = 0; i < fields.length; i++) {//遍历属性,比对
  85. Field field = fields[i];
  86. String fieldName = field.getName();//属性名
  87. Iterator<String> zdIt = zdC.iterator();//一条字段的集合的迭代器
  88. while (zdIt.hasNext()) {//遍历要导出的字段集合
  89. if (zdIt.next().equals(fieldName)) {//比对JavaBean的属性名,一致就写入,不一致就丢弃
  90. String getMethodName = "get"
  91. + fieldName.substring(0, 1).toUpperCase()
  92. + fieldName.substring(1);//拿到属性的get方法
  93. Class tCls = l.getClass();//拿到JavaBean对象
  94. try {
  95. Method getMethod = tCls.getMethod(getMethodName,
  96. new Class[] {});//通过JavaBean对象拿到该属性的get方法,从而进行操控
  97. Object val = getMethod.invoke(l, new Object[] {});//操控该对象属性的get方法,从而拿到属性值
  98. String textVal = null;
  99. if (val != null) {
  100. textVal = String.valueOf(val);//转化成String
  101. } else {
  102. textVal = null;
  103. }
  104. row.createCell((short) zdCell).setCellValue(textVal);//写进excel对象
  105. zdCell++;
  106. } catch (SecurityException e) {
  107. e.printStackTrace();
  108. } catch (IllegalArgumentException e) {
  109. e.printStackTrace();
  110. } catch (NoSuchMethodException e) {
  111. e.printStackTrace();
  112. } catch (IllegalAccessException e) {
  113. e.printStackTrace();
  114. } catch (InvocationTargetException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. }
  119. }
  120. }
  121. RespDTO resultMode = new RespDTO();
  122. try {
  123. FileOutputStream exportXls = new FileOutputStream(lujing + fileName + ".xls");
  124. wb.write(exportXls);
  125. exportXls.close();
  126. /* resultMode.setResult(true);
  127. resultMode.setMessage(MSG_SUCCESS);*/
  128. return resultMode.onSuc(MSG_SUCCESS);
  129. } catch (FileNotFoundException e) {
  130. /* System.out.println("导出失败!");
  131. resultMode.setResult(false);
  132. resultMode.setMessage(MSG_ERROR);*/
  133. e.printStackTrace();
  134. return resultMode.onSuc(MSG_ERROR);
  135. } catch (IOException e) {
  136. e.printStackTrace();
  137. return resultMode.onSuc(MSG_ERROR);
  138. }
  139. // return resultMode;
  140. }
  141. }