SmartEasyPoiExcelUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package net.lab1024.smartadmin.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 org.apache.commons.lang3.StringUtils;
  8. import org.apache.poi.ss.usermodel.Workbook;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.net.URLEncoder;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.NoSuchElementException;
  17. /**
  18. * @author: zhuoda
  19. * @create: 2020-03-30 14:27 PM from win10
  20. */
  21. public class SmartEasyPoiExcelUtil {
  22. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,
  23. String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException {
  24. ExportParams exportParams = new ExportParams(title, sheetName);
  25. exportParams.setCreateHeadRows(isCreateHeader);
  26. defaultExport(list, pojoClass, fileName, response, exportParams);
  27. }
  28. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
  29. HttpServletResponse response) throws IOException {
  30. defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
  31. }
  32. public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
  33. defaultExport(list, fileName, response);
  34. }
  35. private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName,
  36. HttpServletResponse response, ExportParams exportParams) throws IOException {
  37. Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
  38. if (workbook != null) ;
  39. downLoadExcel(fileName, response, workbook);
  40. }
  41. private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
  42. response.setHeader("content-Type", "application/vnd.ms-excel");
  43. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  44. response.setCharacterEncoding("UTF-8");
  45. workbook.write(response.getOutputStream());
  46. }
  47. private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
  48. Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
  49. if (workbook != null) ;
  50. downLoadExcel(fileName, response, workbook);
  51. }
  52. public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
  53. if (StringUtils.isBlank(filePath)) {
  54. return null;
  55. }
  56. ImportParams params = new ImportParams();
  57. params.setTitleRows(titleRows);
  58. params.setHeadRows(headerRows);
  59. List<T> list = null;
  60. try {
  61. list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
  62. } catch (NoSuchElementException e) {
  63. //throw new NormalException("模板不能为空");
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. //throw new NormalException(e.getMessage());
  67. }
  68. return list;
  69. }
  70. public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
  71. if (file == null) {
  72. return null;
  73. }
  74. ImportParams params = new ImportParams();
  75. params.setTitleRows(titleRows);
  76. params.setHeadRows(headerRows);
  77. List<T> list = null;
  78. try {
  79. list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
  80. } catch (NoSuchElementException e) {
  81. // throw new NormalException("excel文件不能为空");
  82. } catch (Exception e) {
  83. //throw new NormalException(e.getMessage());
  84. System.out.println(e.getMessage());
  85. }
  86. return list;
  87. }
  88. }