EasyExcelUti.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.crm.rely.backend.util;
  2. import com.alibaba.excel.ExcelReader;
  3. import com.alibaba.excel.metadata.BaseRowModel;
  4. import com.alibaba.excel.metadata.Sheet;
  5. import com.crm.rely.backend.core.constant.Constants;
  6. import com.crm.rely.backend.core.entity.base.PageEntity;
  7. import com.crm.rely.backend.exception.ServiceException;
  8. import org.springframework.beans.BeanUtils;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import java.io.BufferedInputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.HashMap;
  14. import java.util.LinkedList;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * 导入工具
  19. *
  20. * @Author: houn
  21. * @Date: 2020/12/16 21:36
  22. * @Description:
  23. */
  24. public class EasyExcelUti {
  25. /**
  26. * 读取某个 sheet 的 Excel
  27. *
  28. * @param excel 文件
  29. * @param rowModel 实体类映射,继承 BaseRowModel 类
  30. * @return Excel 数据 list
  31. */
  32. public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel) throws IOException {
  33. return readExcel(excel, rowModel, 1, 1);
  34. }
  35. /**
  36. * 读取某个 sheet 的 Excel
  37. *
  38. * @param excel 文件
  39. * @param rowModel 实体类映射,继承 BaseRowModel 类
  40. * @param sheetNo sheet 的序号 从1开始
  41. * @return Excel 数据 list
  42. */
  43. public static <T extends BaseRowModel> List<T> readExcel(MultipartFile excel, T rowModel, int sheetNo) throws IOException {
  44. ExcelListener excelListener= getExcelListener(excel,rowModel,sheetNo);
  45. return excelListener.getDatas();
  46. }
  47. public static <T> List<T> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo, Class<T> clazz) throws IOException, ServiceException, IllegalAccessException, InstantiationException {
  48. ExcelListener excelListener= getExcelListener(excel,rowModel,sheetNo);
  49. if (!excelListener.getImportHeads().equals(excelListener.getModelHeads())) {
  50. throw ServiceException.exception(Constants.UPLOAD_EXCEL_ERROR);
  51. }
  52. if (excelListener.getDatas() != null && excelListener.getDatas().size() > 0) {
  53. List<T> list = new LinkedList<>();
  54. for (Object o : excelListener.getDatas()) {
  55. BaseRowModel model = (BaseRowModel) o;
  56. T t = clazz.newInstance();
  57. BeanUtils.copyProperties(model, t);
  58. list.add(t);
  59. }
  60. return list;
  61. } else {
  62. throw ServiceException.exception(Constants.UPLOAD_EXCEL_ERROR);
  63. }
  64. }
  65. private static <T extends BaseRowModel>ExcelListener getExcelListener(MultipartFile excel, T rowModel, int sheetNo) throws IOException {
  66. ExcelListener excelListener = new ExcelListener();
  67. ExcelReader reader = getReader(excel, excelListener);
  68. if (reader == null) {
  69. return null;
  70. }
  71. reader.read(new Sheet(sheetNo, 0, rowModel.getClass()));
  72. return excelListener;
  73. }
  74. /**
  75. * 读取某个 sheet 的 Excel
  76. *
  77. * @param excel 文件
  78. * @param rowModel 实体类映射,继承 BaseRowModel 类
  79. * @param sheetNo sheet 的序号 从1开始
  80. * @param headLineNum 表头行数,默认为1
  81. * @return Excel 数据 list
  82. */
  83. public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo, int headLineNum) throws IOException {
  84. ExcelListener excelListener = new ExcelListener();
  85. ExcelReader reader = getReader(excel, excelListener);
  86. if (reader == null) {
  87. return null;
  88. }
  89. reader.read(new Sheet(sheetNo, headLineNum, rowModel.getClass()));
  90. return excelListener.getDatas();
  91. }
  92. /**
  93. * 读取指定sheetName的Excel(多个 sheet)
  94. *
  95. * @param excel 文件
  96. * @param rowModel 实体类映射,继承 BaseRowModel 类
  97. * @return Excel 数据 list
  98. * @throws IOException
  99. */
  100. public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, String sheetName) throws IOException {
  101. ExcelListener excelListener = new ExcelListener();
  102. ExcelReader reader = getReader(excel, excelListener);
  103. if (reader == null) {
  104. return null;
  105. }
  106. for (Sheet sheet : reader.getSheets()) {
  107. if (rowModel != null) {
  108. sheet.setClazz(rowModel.getClass());
  109. }
  110. //读取指定名称的sheet
  111. if (sheet.getSheetName().contains(sheetName)) {
  112. reader.read(sheet);
  113. break;
  114. }
  115. }
  116. return excelListener.getDatas();
  117. }
  118. /**
  119. * 返回 ExcelReader
  120. *
  121. * @param excel 需要解析的 Excel 文件
  122. * @param excelListener new ExcelListener()
  123. * @throws IOException
  124. */
  125. private static ExcelReader getReader(MultipartFile excel, ExcelListener excelListener) throws IOException {
  126. String filename = excel.getOriginalFilename();
  127. if (filename != null && (filename.toLowerCase().endsWith(".xls") || filename.toLowerCase().endsWith(".xlsx"))) {
  128. InputStream is = new BufferedInputStream(excel.getInputStream());
  129. return new ExcelReader(is, null, excelListener, false);
  130. } else {
  131. return null;
  132. }
  133. }
  134. }