ExcelUtils.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.diagbot.util;
  2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  3. import cn.afterturn.easypoi.excel.ExcelImportUtil;
  4. import cn.afterturn.easypoi.excel.entity.ExportParams;
  5. import cn.afterturn.easypoi.excel.entity.ImportParams;
  6. import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
  7. import com.diagbot.exception.CommonErrorCode;
  8. import com.diagbot.exception.CommonException;
  9. import com.diagbot.service.impl.ExcelExportStylerUserImpl;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.ss.usermodel.Row;
  14. import org.apache.poi.ss.usermodel.Sheet;
  15. import org.apache.poi.ss.usermodel.Workbook;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.net.URLEncoder;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.NoSuchElementException;
  24. /**
  25. * @Description: excel 导入导出工具类
  26. * @author: gaodm
  27. * @time: 2020/6/2 19:18
  28. */
  29. public class ExcelUtils {
  30. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
  31. boolean isCreateHeader, HttpServletResponse response) {
  32. ExportParams exportParams = new ExportParams(title, sheetName);
  33. exportParams.setCreateHeadRows(isCreateHeader);
  34. defaultExport(list, pojoClass, fileName, response, exportParams);
  35. }
  36. public static void exportExcelUser(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
  37. HttpServletResponse response) {
  38. ExportParams exportParams = new ExportParams(title, sheetName);
  39. exportParams.setStyle(ExcelExportStylerUserImpl.class);
  40. userExport(list, pojoClass, fileName, response, exportParams);
  41. }
  42. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
  43. HttpServletResponse response) {
  44. defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
  45. }
  46. public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
  47. defaultExport(list, fileName, response);
  48. }
  49. private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
  50. ExportParams exportParams) {
  51. Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
  52. if (workbook != null) {
  53. ;
  54. }
  55. downLoadExcel(fileName, response, workbook);
  56. }
  57. private static void userExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
  58. ExportParams exportParams) {
  59. Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
  60. if (workbook != null) {
  61. Sheet sheet = workbook.getSheetAt(0);
  62. //列宽设置
  63. sheet.setColumnWidth(8, 256*20);
  64. sheet.setColumnWidth(9, 256*50);
  65. int rowNum = sheet.getLastRowNum();
  66. Row row = sheet.getRow(0);
  67. for (int i = 1; i <= rowNum; i++) {
  68. row = sheet.getRow(i);
  69. row.setHeightInPoints(12.8f);
  70. }
  71. }
  72. downLoadExcel(fileName, response, workbook);
  73. }
  74. private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
  75. try {
  76. response.setCharacterEncoding("UTF-8");
  77. response.setHeader("content-Type", "application/vnd.ms-excel");
  78. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  79. workbook.write(response.getOutputStream());
  80. } catch (IOException e) {
  81. // throw new NormalException(e.getMessage());
  82. throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "导出Excel异常");
  83. }
  84. }
  85. private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
  86. Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
  87. if (workbook != null) {
  88. ;
  89. }
  90. downLoadExcel(fileName, response, workbook);
  91. }
  92. public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
  93. if (StringUtils.isBlank(filePath)) {
  94. return null;
  95. }
  96. ImportParams params = new ImportParams();
  97. params.setTitleRows(titleRows);
  98. params.setHeadRows(headerRows);
  99. List<T> list = null;
  100. try {
  101. list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
  102. } catch (NoSuchElementException e) {
  103. // throw new NormalException("模板不能为空");
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. // throw new NormalException(e.getMessage());
  107. }
  108. return list;
  109. }
  110. public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
  111. Class<T> pojoClass) {
  112. if (file == null) {
  113. return null;
  114. }
  115. ImportParams params = new ImportParams();
  116. params.setTitleRows(titleRows);
  117. params.setHeadRows(headerRows);
  118. List<T> list = null;
  119. try {
  120. list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
  121. } catch (NoSuchElementException e) {
  122. // throw new NormalException("excel文件不能为空");
  123. } catch (Exception e) {
  124. // throw new NormalException(e.getMessage());
  125. System.out.println(e.getMessage());
  126. }
  127. return list;
  128. }
  129. }