自定义注解

  1. import java.lang.annotation.Documented;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.Target;
  4. import static java.lang.annotation.ElementType.FIELD;
  5. import static java.lang.annotation.RetentionPolicy.RUNTIME;
  6. @Retention(RUNTIME)
  7. @Target(FIELD)
  8. @Documented
  9. public @interface ExcelExportAnnotation {
  10. String title() default "";
  11. int order() default 0;
  12. }
  1. import org.apache.commons.collections.CollectionUtils;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.poi.ss.usermodel.*;
  4. import org.apache.poi.ss.util.CellRangeAddress;
  5. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  6. import com.hikvision.idatafusion.dgdpsiot.common.annotation.ExcelExportAnnotation;
  7. import java.lang.reflect.Field;
  8. import java.util.*;
  9. /**
  10. * @author hanrensong
  11. * @date 2021/4/24
  12. */
  13. public class ExcelUtil {
  14. public static <T> void listToSheet(Workbook workbook, List<? extends Object> list, Class<T> cls, String sheetName) {
  15. if(workbook==null) {
  16. return;
  17. }
  18. Sheet sheet = null;
  19. if(StringUtils.isNotBlank(sheetName)) {
  20. sheet = workbook.createSheet(sheetName);
  21. }else {
  22. sheet = workbook.createSheet();
  23. }
  24. Row header = sheet.createRow(0);
  25. writeHead(header,cls);
  26. if (CollectionUtils.isNotEmpty(list)) {
  27. Map<Integer, Field> fields = new HashMap<>();
  28. List<Integer> fieldOrders = new ArrayList<>();
  29. parseClass(cls,fields,fieldOrders);
  30. for(int i=0;i<list.size();i++) {
  31. Object t = list.get(i);
  32. Row row = sheet.createRow(i+1);
  33. for(int j=0; j<fieldOrders.size();j++) {
  34. Cell cell = row.createCell(j);
  35. Field field = fields.get(fieldOrders.get(j));
  36. try {
  37. field.setAccessible(true);
  38. Object value = field.get(t);
  39. if(value != null) {
  40. cell.setCellValue(String.valueOf(value));
  41. }
  42. } catch (IllegalArgumentException e) {
  43. e.printStackTrace();
  44. } catch (IllegalAccessException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49. }
  50. }
  51. /**
  52. * @param cls
  53. * @param fieldMap
  54. * @param fieldOrders
  55. */
  56. private static <T> void parseClass(Class<T> cls, Map<Integer, Field> fieldMap, List<Integer> fieldOrders) {
  57. Field[] fields = cls.getDeclaredFields();
  58. for(Field field : fields) {
  59. if(field.isAnnotationPresent(ExcelExportAnnotation.class)) {
  60. ExcelExportAnnotation annotation = field.getAnnotation(ExcelExportAnnotation.class);
  61. Integer order = new Integer(annotation.order());
  62. fieldMap.put(order, field);
  63. fieldOrders.add(order);
  64. }
  65. }
  66. Collections.sort(fieldOrders);
  67. }
  68. /**
  69. * @param row
  70. * @param cls
  71. */
  72. private static <T> void writeHead(Row row, Class<T> cls) {
  73. Field[] fields = cls.getDeclaredFields();
  74. Map<Integer,String> titles = new HashMap<>();
  75. List<Integer> orders = new ArrayList<>();
  76. for(Field field : fields) {
  77. if(field.isAnnotationPresent(ExcelExportAnnotation.class)) {
  78. ExcelExportAnnotation annotation = field.getAnnotation(ExcelExportAnnotation.class);
  79. Integer order = new Integer(annotation.order());
  80. titles.put(order, annotation.title());
  81. orders.add(order);
  82. }
  83. }
  84. Collections.sort(orders);
  85. for(Integer i : orders) {
  86. Cell cell = row.createCell(i-1);
  87. cell.setCellValue(titles.get(i));
  88. }
  89. }
  90. public static <T> List<T> sheetToList(Sheet sheet, Class<T> voClass, int headerRowNumber){
  91. if(sheet == null || voClass==null ){
  92. return null;
  93. }
  94. List<T> result = new ArrayList<>();
  95. Iterator<Row> rowIterator = sheet.iterator();
  96. while(rowIterator.hasNext()) {
  97. Row row = rowIterator.next();
  98. if(row.getRowNum()<headerRowNumber) {
  99. continue;
  100. }
  101. parseRow(result,row,voClass);
  102. }
  103. return result;
  104. }
  105. /**
  106. * @param result
  107. * @param row
  108. * @param voClass
  109. */
  110. private static <T> void parseRow(List<T> result, Row row, Class<T> voClass) {
  111. Iterator<Cell> cellIterator = row.cellIterator();
  112. T t = null;
  113. try {
  114. t = voClass.newInstance();
  115. DataFormatter formatter = new DataFormatter();
  116. while(cellIterator.hasNext()) {
  117. Cell cell = cellIterator.next();
  118. Field field = getField(cell.getColumnIndex()+1,voClass);
  119. if(field!=null) {
  120. field.setAccessible(true);
  121. field.set(t, formatter.formatCellValue(cell));
  122. }
  123. }
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. if(t!=null) {
  128. result.add(t);
  129. }
  130. }
  131. /**
  132. * @param columnIndex
  133. * @param voClass
  134. */
  135. private static <T> Field getField(int columnIndex, Class<T> voClass) {
  136. Field[] fields = voClass.getDeclaredFields();
  137. for(Field field : fields) {
  138. if(field.isAnnotationPresent(ExcelExportAnnotation.class)) {
  139. ExcelExportAnnotation annotation = field.getAnnotation(ExcelExportAnnotation.class);
  140. if(annotation.order()==columnIndex) {
  141. return field;
  142. }
  143. }
  144. }
  145. return null;
  146. }
  147. public static void mergeSheet(XSSFWorkbook workbook, int sheetIndex, int cellNum) {
  148. Sheet sheet = workbook.getSheetAt(sheetIndex);
  149. if (sheet.getLastRowNum() > 2) {
  150. Iterator<Row> sheetIterator = sheet.rowIterator();
  151. int rowStart =1;
  152. String modelName = sheet.getRow(1).getCell(cellNum).getStringCellValue();
  153. while(sheetIterator.hasNext()) {
  154. Row row = sheetIterator.next();
  155. if(row.getRowNum()<2) {
  156. continue;
  157. }
  158. String cellValue = row.getCell(cellNum).getStringCellValue();
  159. if(cellValue.equals(modelName)) {
  160. continue;
  161. }else {
  162. if(rowStart!=(row.getRowNum()-1)) {
  163. CellRangeAddress region = new CellRangeAddress(rowStart, row.getRowNum()-1, cellNum, cellNum);
  164. sheet.addMergedRegion(region);
  165. }
  166. rowStart = row.getRowNum();
  167. modelName = cellValue;
  168. }
  169. }
  170. if(rowStart!=sheet.getLastRowNum()) {
  171. CellRangeAddress region = new CellRangeAddress(rowStart, sheet.getLastRowNum(), cellNum, cellNum);
  172. sheet.addMergedRegion(region);
  173. }
  174. }
  175. }
  176. // public static void main(String[] args) throws IOException {
  177. //
  178. //
  179. // XSSFWorkbook workbook = new XSSFWorkbook();
  180. //
  181. // XSSFCellStyle style = workbook.createCellStyle();
  182. // style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  183. // style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  184. // Sheet sheet = workbook.createSheet("sheet");
  185. //
  186. // Row row0 = sheet.createRow(0);
  187. // Cell cell_00 = row0.createCell(0);
  188. // cell_00.setCellStyle(style);
  189. // cell_00.setCellValue("日期");
  190. // Cell cell_01 = row0.createCell(1);
  191. // cell_01.setCellStyle(style);
  192. // cell_01.setCellValue("午别");
  193. // Row row1 = sheet.createRow(1);
  194. // Cell cell_10 = row1.createCell(0);
  195. // cell_10.setCellStyle(style);
  196. // cell_10.setCellValue("20180414");
  197. // Cell cell_11 = row1.createCell(1);
  198. // cell_11.setCellStyle(style);
  199. // cell_11.setCellValue("上午");
  200. //
  201. // Row row2 = sheet.createRow(2);
  202. // Cell cell_20 = row2.createCell(0);
  203. // cell_20.setCellStyle(style);
  204. // cell_20.setCellValue("20180413");
  205. // Cell cell_21 = row2.createCell(1);
  206. // cell_21.setCellStyle(style);
  207. // cell_21.setCellValue("下午");
  208. //
  209. // // 合并日期占两行(4个参数,分别为起始行,结束行,起始列,结束列)
  210. // // 行和列都是从0开始计数,且起始结束都会合并
  211. // // 这里是合并excel中日期的两行为一行
  212. // CellRangeAddress region = new CellRangeAddress(1, 2, 0, 0);
  213. // sheet.addMergedRegion(region);
  214. //
  215. // File file = new File("E:\\demo1.xls");
  216. // FileOutputStream fout = new FileOutputStream(file);
  217. // workbook.write(fout);
  218. // fout.close();
  219. //
  220. // }
  221. }