EasyExcelUtils.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.cyksj.web.util;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.ExcelWriter;
  4. import com.alibaba.excel.support.ExcelTypeEnum;
  5. import com.alibaba.excel.write.metadata.WriteSheet;
  6. import com.cyksj.common.exception.BusinessRuntimeException;
  7. import com.cyksj.model.excel.ExcelSheetAndData;
  8. import com.cyksj.model.excel.ExportExcelError;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.util.CollectionUtils;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Optional;
  18. /**
  19. * @ClassName EasyExcelUtils
  20. * @author chan
  21. * @Date 2019/11/1
  22. * @Description 封装的EasyExcel导出工具类
  23. */
  24. public class EasyExcelUtils {
  25. private static final Logger LOGGER = LoggerFactory.getLogger(EasyExcelUtils.class);
  26. /**
  27. *
  28. * 写回客户端
  29. *
  30. * @author chan
  31. * @param response
  32. * @param fileName
  33. * @param type
  34. */
  35. @SuppressWarnings("rawtypes")
  36. public static void createExcelStreamMutilByEasyExcel(HttpServletResponse response, Class tClass,
  37. List<?> sheetNameAndDateList, String sheetName, String fileName, ExcelTypeEnum type, String charset) {
  38. final String methodName = "createExcelStreamMutilByEasyExcel";
  39. LOGGER.info("{},excel导出开始[end]",methodName);
  40. if (checkParam(sheetNameAndDateList, type)) {
  41. LOGGER.warn("{},excel导出 数据源为空!",methodName);
  42. throw BusinessRuntimeException.getInstance("暂无数据!");
  43. }
  44. try {
  45. if(StringUtils.isBlank(charset)){
  46. charset = "iso-8859-1";
  47. }
  48. response.setContentType("application/x-download");
  49. response.setContentType("application/vnd.ms-excel");
  50. response.setCharacterEncoding("utf-8");
  51. response.setHeader("Content-disposition",
  52. "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1")+ type.getValue());
  53. EasyExcel.write(response.getOutputStream(), tClass).sheet(sheetName).doWrite(sheetNameAndDateList);
  54. LOGGER.info("{},excel导出结束[end]",methodName);// 方法3 如果写到不同的sheet 不同的对象
  55. } catch (IOException e) {
  56. LOGGER.warn("{},excel导出 异常:{}",methodName,e.getMessage());
  57. }
  58. }
  59. /**
  60. *
  61. * 校验
  62. *
  63. * @author chan
  64. * @param type
  65. * @return
  66. */
  67. private static boolean checkParam(List<?> sheetNameAndDateList, ExcelTypeEnum type) {
  68. final String methodName = "Excel工具类 checkParam方法";
  69. if (CollectionUtils.isEmpty(sheetNameAndDateList)) {
  70. LOGGER.warn(methodName, "SheetNameAndDateList不能为空");
  71. return true;
  72. } else if (type == null) {
  73. LOGGER.warn(methodName, "导出的excel类型不能为空");
  74. return true;
  75. }
  76. return false;
  77. }
  78. /**
  79. *
  80. * 写回客户端
  81. *
  82. * @author chan
  83. * @param response
  84. * @param fileName
  85. * @param type
  86. */
  87. @SuppressWarnings("rawtypes")
  88. public static void createExcelStreamMutilByEasyExcel(HttpServletResponse response,
  89. List<ExcelSheetAndData> sheetNameAndDateList, String fileName, ExcelTypeEnum type, String charset) {
  90. final String methodName = "createExcelStreamMutilByEasyExcel";
  91. LOGGER.info("{},excel导出开始[end]",methodName);
  92. if (checkParam(sheetNameAndDateList, type)) {
  93. LOGGER.warn("{},excel导出 数据源为空!",methodName);
  94. throw BusinessRuntimeException.getInstance("暂无数据!");
  95. }
  96. try {
  97. if(StringUtils.isBlank(charset)){
  98. charset = "iso-8859-1";
  99. }
  100. response.setContentType("application/x-download");
  101. response.setContentType("application/vnd.ms-excel");
  102. response.setCharacterEncoding("utf-8");
  103. response.setHeader("Content-disposition",
  104. "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1")+ type.getValue());
  105. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
  106. int i = 0;
  107. for (ExcelSheetAndData excelSheetAndData : sheetNameAndDateList) {
  108. WriteSheet writeSheet = EasyExcel.writerSheet(i, excelSheetAndData.getSheetName()).head(excelSheetAndData.getClazz()).build();
  109. excelWriter.write(excelSheetAndData.getData(),writeSheet);
  110. i++;
  111. }
  112. excelWriter.finish();
  113. LOGGER.info("{},excel导出结束[end]",methodName);
  114. response.getOutputStream().close();
  115. } catch (IOException e) {
  116. LOGGER.warn("{},excel导出 异常:{}",methodName,e.getMessage());
  117. }
  118. }
  119. /**
  120. * 获取模板 Excel
  121. */
  122. public static void createTemplateExcel(HttpServletResponse response, Class tClass, String sheetName, String fileName, ExcelTypeEnum type, String charset) {
  123. final String methodName = "createTemplateExcel";
  124. LOGGER.info("{},excel导出开始[end]",methodName);
  125. try {
  126. response.setContentType("application/x-download");
  127. response.setContentType("application/vnd.ms-excel");
  128. response.setCharacterEncoding("utf-8");
  129. response.setHeader("Content-disposition",
  130. "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1")+ type.getValue());
  131. EasyExcel.write(response.getOutputStream(), tClass).sheet(sheetName).doWrite(null);
  132. LOGGER.info("{},excel导出结束[end]",methodName);
  133. } catch (IOException e) {
  134. LOGGER.warn("{},excel导出 异常:{}",methodName,e.getMessage());
  135. }
  136. }
  137. /**
  138. *
  139. * 错误信息写回
  140. *
  141. * @author chan
  142. * @param code 错误代码
  143. * @param msg 错误信息
  144. * @param response
  145. */
  146. public static void error(String code , String msg, String remark, HttpServletResponse response){
  147. ExportExcelError exportExcelError = new ExportExcelError();
  148. Optional<String> codeOpt = Optional.ofNullable(code);
  149. Optional<String> msgOpt = Optional.ofNullable(msg);
  150. Optional<String> remarkOpt = Optional.ofNullable(remark);
  151. exportExcelError.setCode(codeOpt.orElse("99999"));
  152. exportExcelError.setMsg(msgOpt.orElse("系统错误"));
  153. exportExcelError.setRemark(remarkOpt.orElse("系统错误"));
  154. List<ExportExcelError> errorList = new ArrayList<>();
  155. errorList.add(exportExcelError);
  156. createExcelStreamMutilByEasyExcel(response,ExportExcelError.class,errorList,"Excel导出异常","Excel导出异常", ExcelTypeEnum.XLSX,null);
  157. }
  158. }