当前位置:   article > 正文

Springboot采用EasyPoi采用前后端分离导出Excel并返回前端Blob_文件导出为前端返回bobl 完整流程

文件导出为前端返回bobl 完整流程

实现思路

  1. 封装excel的工具类,采用EasyPoi,简单,快速;
  2. 编写PO对象,采用@Excel注解增加注释;
  3. 查询数据,并将数据封闭至PO对象中;
  4. 通过工具类将对象通过BLOB格式返回给前端;
  5. 前端通过工具类下载该EXCEL;

引依赖

  1. <dependency>
  2. <groupId>cn.afterturn</groupId>
  3. <artifactId>easypoi-base</artifactId>
  4. <version>4.2.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>cn.afterturn</groupId>
  8. <artifactId>easypoi-web</artifactId>
  9. <version>4.2.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>cn.afterturn</groupId>
  13. <artifactId>easypoi-annotation</artifactId>
  14. <version>4.2.0</version>
  15. </dependency>

写工具类ExcelUtil

  1. package test.common.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. * Excel导入导出工具类
  19. * @author pangu
  20. */
  21. public class ExcelUtil {
  22. /**
  23. * 导出工具类
  24. * @param list
  25. * @param title
  26. * @param sheetName
  27. * @param pojoClass
  28. * @param fileName
  29. * @param isCreateHeader
  30. * @param response
  31. */
  32. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,
  33. String fileName, boolean isCreateHeader, HttpServletResponse response){
  34. ExportParams exportParams = new ExportParams(title, sheetName);
  35. exportParams.setCreateHeadRows(isCreateHeader);
  36. defaultExport(list, pojoClass, fileName, response, exportParams);
  37. }
  38. /**
  39. * 导出工具类
  40. * @param list
  41. * @param title
  42. * @param sheetName
  43. * @param pojoClass
  44. * @param fileName
  45. * @param response
  46. */
  47. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
  48. HttpServletResponse response){
  49. defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
  50. }
  51. public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
  52. defaultExport(list, fileName, response);
  53. }
  54. private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName,
  55. HttpServletResponse response, ExportParams exportParams) {
  56. Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
  57. if (workbook != null); downLoadExcel(fileName, response, workbook);
  58. }
  59. private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
  60. try {
  61. response.setCharacterEncoding("UTF-8");
  62. response.setHeader("content-Type", "application/vnd.ms-excel");
  63. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  64. workbook.write(response.getOutputStream());
  65. } catch (IOException e) {
  66. //throw new NormalException(e.getMessage());
  67. }
  68. }
  69. private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
  70. Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
  71. if (workbook != null);
  72. downLoadExcel(fileName, response, workbook);
  73. }
  74. public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){
  75. if (StringUtils.isBlank(filePath)){
  76. return null;
  77. }
  78. ImportParams params = new ImportParams();
  79. params.setTitleRows(titleRows);
  80. params.setHeadRows(headerRows);
  81. List<T> list = null;
  82. try {
  83. list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
  84. }catch (NoSuchElementException e){
  85. //throw new NormalException("模板不能为空");
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. //throw new NormalException(e.getMessage());
  89. } return list;
  90. }
  91. public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
  92. if (file == null){ return null;
  93. }
  94. ImportParams params = new ImportParams();
  95. params.setTitleRows(titleRows);
  96. params.setHeadRows(headerRows);
  97. List<T> list = null;
  98. try {
  99. list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
  100. }catch (NoSuchElementException e){
  101. // throw new NormalException("excel文件不能为空");
  102. } catch (Exception e) {
  103. //throw new NormalException(e.getMessage());
  104. System.out.println(e.getMessage());
  105. }
  106. return list;
  107. }
  108. }

写PO对象

  1. package com.test.order.api.dto;
  2. import cn.afterturn.easypoi.excel.annotation.Excel;
  3. import io.swagger.annotations.ApiModel;
  4. import lombok.Data;
  5. import java.io.Serializable;
  6. /**
  7. * @Author
  8. * @Date 2021/2/18
  9. * @Version 1.0
  10. */
  11. @ApiModel(value = "OrderPOI", description = "返回给前端的Excel表头")
  12. @Data
  13. public class OrderPOI implements Serializable {
  14. private static final long serialVersionUID = 5758487990585198585L;
  15. @Excel(name = "订单编号", orderNum = "0", width = 30, isImportField = "true_st")
  16. private String orderCode;
  17. @Excel(name = "订单名称", orderNum = "1", width = 30, isImportField = "true_st")
  18. private String remarks;
  19. @Excel(name = "订单金额", orderNum = "2", width = 30, isImportField = "true_st")
  20. private Double totalPrice;
  21. @Excel(name = "用户名", orderNum = "3", width = 30, isImportField = "true_st")
  22. private String userName;
  23. @Excel(name = "手机号", orderNum = "4", width = 30, isImportField = "true_st")
  24. private String phone;
  25. @Excel(name = "支付方式", orderNum = "5", width = 30, isImportField = "true_st")
  26. private String payType;
  27. @Excel(name = "下单时间", orderNum = "6", width = 30, isImportField = "true_st")
  28. private String createTime;
  29. @Excel(name = "订单状态", orderNum = "7", width = 30, isImportField = "true_st")
  30. private String status;
  31. @Excel(name = "交易号", orderNum = "8", width = 30, isImportField = "true_st")
  32. private String code;
  33. @Excel(name = "实收金额", orderNum = "9", width = 30, isImportField = "true_st")
  34. private Double payPrice;
  35. }

通过工具类的方法将数据导出成BLOB格式,样例

  1. @ApiOperation(value = "后台-导出订单列表")
  2. @GetMapping("/export-order")
  3. public void export(@ApiIgnore HttpServletResponse response, String[] orderCodes) {
  4. try {
  5. List<OrderPOI> orderPOIS = orderService.export(orderCodes);
  6. // 使用工具类导出excel
  7. ExcelUtil.exportExcel(orderPOIS, null, "订单", OrderPOI.class, "order", response);
  8. } catch (Exception e) {
  9. System.out.println(e);
  10. LogManager.error(e);
  11. }
  12. }

前端接受数据

  1. handleExport(){
  2. exportRole().then(response => {
  3. downloadFile(response, "order", 'xlsx')
  4. })
  5. }
  1. // 下载文件
  2. export function downloadFile(obj, name, suffix) {
  3. const url = window.URL.createObjectURL(new Blob([obj]))
  4. const link = document.createElement('a')
  5. link.style.display = 'none'
  6. link.href = url
  7. const fileName = parseTime(new Date()) + '-' + name + '.' + suffix
  8. link.setAttribute('download', fileName)
  9. document.body.appendChild(link)
  10. link.click()
  11. document.body.removeChild(link)
  12. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/842484
推荐阅读
相关标签
  

闽ICP备14008679号