EasyPoi

1 导入依赖
  1. <!-- EasyPoiUtil导入依赖-->
  2. <dependency>
  3. <groupId>cn.afterturn</groupId>
  4. <artifactId>easypoi-base</artifactId>
  5. <version>4.1.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>cn.afterturn</groupId>
  9. <artifactId>easypoi-web</artifactId>
  10. <version>4.1.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>cn.afterturn</groupId>
  14. <artifactId>easypoi-annotation</artifactId>
  15. <version>4.1.0</version>
  16. </dependency>

踩坑警告

​ 如果项目中已经有其他excel工具类的依赖了,则把本依赖放到后面,

​ EasyPoiUtil的版本尽量选择新一些的,避免导入数据问题

2 工具类
  1. import cn.afterturn.easypoi.cache.manager.POICacheManager;
  2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  3. import cn.afterturn.easypoi.excel.ExcelImportUtil;
  4. import cn.afterturn.easypoi.excel.ExcelXorHtmlUtil;
  5. import cn.afterturn.easypoi.excel.entity.ExcelToHtmlParams;
  6. import cn.afterturn.easypoi.excel.entity.ExportParams;
  7. import cn.afterturn.easypoi.excel.entity.ImportParams;
  8. import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
  9. import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
  10. import cn.afterturn.easypoi.word.WordExportUtil;
  11. import cn.afterturn.easypoi.word.parse.ParseWord07;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.apache.poi.ss.usermodel.Workbook;
  14. import org.apache.poi.ss.usermodel.WorkbookFactory;
  15. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.net.URLEncoder;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.NoSuchElementException;
  26. /**
  27. * Excel导入导出工具类
  28. */
  29. public class ExcelUtils2 {
  30. /**
  31. * excel 导出
  32. *
  33. * @param list 数据列表
  34. * @param fileName 导出时的excel名称
  35. * @param response
  36. */
  37. public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
  38. defaultExport(list, fileName, response);
  39. }
  40. /**
  41. * 默认的 excel 导出
  42. *
  43. * @param list 数据列表
  44. * @param fileName 导出时的excel名称
  45. * @param response
  46. */
  47. private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
  48. //把数据添加到excel表格中
  49. Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
  50. downLoadExcel(fileName, response, workbook);
  51. }
  52. /**
  53. * excel 导出
  54. *
  55. * @param list 数据列表
  56. * @param pojoClass pojo类型
  57. * @param fileName 导出时的excel名称
  58. * @param response
  59. * @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型)
  60. */
  61. private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException {
  62. //把数据添加到excel表格中
  63. Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
  64. downLoadExcel(fileName, response, workbook);
  65. }
  66. /**
  67. * excel 导出
  68. *
  69. * @param list 数据列表
  70. * @param pojoClass pojo类型
  71. * @param fileName 导出时的excel名称
  72. * @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型)
  73. * @param response
  74. */
  75. public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) throws IOException {
  76. defaultExport(list, pojoClass, fileName, response, exportParams);
  77. }
  78. /**
  79. * excel 导出
  80. *
  81. * @param list 数据列表
  82. * @param title 表格内数据标题
  83. * @param sheetName sheet名称
  84. * @param pojoClass pojo类型
  85. * @param fileName 导出时的excel名称
  86. * @param response
  87. */
  88. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
  89. defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
  90. }
  91. /**
  92. * excel 导出
  93. *
  94. * @param list 数据列表
  95. * @param title 表格内数据标题
  96. * @param sheetName sheet名称
  97. * @param pojoClass pojo类型
  98. * @param fileName 导出时的excel名称
  99. * @param isCreateHeader 是否创建表头
  100. * @param response
  101. */
  102. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException {
  103. ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
  104. exportParams.setCreateHeadRows(isCreateHeader);
  105. defaultExport(list, pojoClass, fileName, response, exportParams);
  106. }
  107. /**
  108. * excel下载
  109. *
  110. * @param fileName 下载时的文件名称
  111. * @param response
  112. * @param workbook excel数据
  113. */
  114. private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
  115. try {
  116. response.setCharacterEncoding("UTF-8");
  117. response.setHeader("content-Type", "application/vnd.ms-excel");
  118. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
  119. workbook.write(response.getOutputStream());
  120. } catch (Exception e) {
  121. throw new IOException(e.getMessage());
  122. }
  123. }
  124. /**
  125. * excel 导入
  126. *
  127. * @param file excel文件
  128. * @param pojoClass pojo类型
  129. * @param <T>
  130. * @return
  131. */
  132. public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException {
  133. return importExcel(file, 1, 1, pojoClass);
  134. }
  135. /**
  136. * excel 导入
  137. *
  138. * @param filePath excel文件路径
  139. * @param titleRows 表格内数据标题行
  140. * @param headerRows 表头行
  141. * @param pojoClass pojo类型
  142. * @param <T>
  143. * @return
  144. */
  145. public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
  146. if (StringUtils.isBlank(filePath)) {
  147. return null;
  148. }
  149. ImportParams params = new ImportParams();
  150. params.setTitleRows(titleRows);
  151. params.setHeadRows(headerRows);
  152. // params.setNeedSave(true); //不保存
  153. params.setSaveUrl("/home/server/upload/excel/");
  154. try {
  155. return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
  156. } catch (NoSuchElementException e) {
  157. throw new IOException("模板不能为空");
  158. } catch (Exception e) {
  159. throw new IOException(e.getMessage());
  160. }
  161. }
  162. /**
  163. * excel 导入
  164. *
  165. * @param file 上传的文件
  166. * @param titleRows 表格内数据标题行
  167. * @param headerRows 表头行
  168. * @param pojoClass pojo类型
  169. * @param <T>
  170. * @return
  171. */
  172. public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
  173. if (file == null) {
  174. return null;
  175. }
  176. try {
  177. return importExcel(file.getInputStream(), titleRows, headerRows, pojoClass);
  178. } catch (Exception e) {
  179. throw new IOException(e.getMessage());
  180. }
  181. }
  182. /**
  183. * excel 导入
  184. *
  185. * @param inputStream 文件输入流
  186. * @param titleRows 表格内数据标题行
  187. * @param headerRows 表头行
  188. * @param pojoClass pojo类型
  189. * @param <T>
  190. * @return
  191. */
  192. public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
  193. if (inputStream == null) {
  194. return null;
  195. }
  196. ImportParams params = new ImportParams();
  197. params.setTitleRows(titleRows);
  198. params.setHeadRows(headerRows);
  199. params.setSaveUrl("/home/server/upload/excel/");
  200. // params.setNeedSave(true); //不保存
  201. try {
  202. return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
  203. } catch (NoSuchElementException e) {
  204. throw new IOException("excel文件不能为空");
  205. } catch (Exception e) {
  206. throw new IOException(e.getMessage());
  207. }
  208. }
  209. }
3 导入导出dto
  1. package com.fongtech.cli.mbg.model.dto;
  2. import cn.afterturn.easypoi.excel.annotation.Excel;
  3. import com.fongtech.cli.framework.model.BaseModel;
  4. import lombok.Data;
  5. import lombok.EqualsAndHashCode;
  6. import lombok.NoArgsConstructor;
  7. import java.math.BigDecimal;
  8. import java.util.Date;
  9. /**
  10. * @Author
  11. * @Time 2020/11/3 0003
  12. */
  13. @Data
  14. @NoArgsConstructor
  15. @EqualsAndHashCode(callSuper = false)
  16. public class CommodityImportDTO extends BaseModel {
  17. /**
  18. * id
  19. */
  20. @Excel(name = "id")
  21. private Integer id;
  22. /**
  23. * image
  24. */
  25. @Excel(name = "image")
  26. private String image;
  27. /**
  28. * title
  29. */
  30. @Excel(name = "title")
  31. private String title;
  32. /**
  33. * asin
  34. */
  35. @Excel(name = "asin")
  36. private String asin;
  37. /**
  38. * storeName
  39. */
  40. @Excel(name = "storeName")
  41. private String storeName;
  42. /**
  43. * price
  44. */
  45. @Excel(name = "price")
  46. private String price;
  47. /**
  48. * currencySymbol
  49. */
  50. @Excel(name = "currencySymbol")
  51. private String currencySymbol;
  52. /**
  53. * country
  54. */
  55. @Excel(name = "country")
  56. private String country;
  57. /**
  58. * supportedReviewType
  59. */
  60. @Excel(name = "supportedReviewType")
  61. private String supportedReviewType;
  62. /**
  63. * startDate
  64. */
  65. @Excel(name = "startDate")
  66. private Date startDate;
  67. /**
  68. * endDate
  69. */
  70. @Excel(name = "endDate")
  71. private Date endDate;
  72. /**
  73. * dailyTotal
  74. */
  75. @Excel(name = "dailyTotal")
  76. private String dailyTotal;
  77. /**
  78. * total
  79. */
  80. @Excel(name = "total")
  81. private String total;
  82. /**
  83. * commission
  84. */
  85. @Excel(name = "commission")
  86. private String commission;
  87. /**
  88. * url
  89. */
  90. @Excel(name = "url")
  91. private String url;
  92. /**
  93. * productSpecification
  94. */
  95. @Excel(name = "productSpecification")
  96. private String productSpecification;
  97. /**
  98. * createdTime
  99. */
  100. @Excel(name = "createTime")
  101. private Date createdTime;
  102. }

版权声明:本文为superlinb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/superlinb/p/14886642.html