EasyPOI 导出及各种问题总结

导出实体类DTO:

public class DealerNameExportDto implements Serializable {

   @Excel(name = "Dealer List", width = 25)
   private String dealerName;

   public String getDealerName() {
       return dealerName;
  }

   public void setDealerName(String dealerName) {
       this.dealerName = dealerName;
  }

   public DealerNameExportDto(String dealerName) {
       this.dealerName = dealerName;
  }

   public DealerNameExportDto() {
  }
}

contoller 层:调用入口

   @GetMapping("/exportDealerName/{campaignId}")
  @ResponseBody
   public String exportDealerName(HttpServletRequest request, HttpServletResponse response,
                                  @PathVariable Long campaignId){

      return campaignBtlService.exportDealerName(request, response, campaignId);

  }

Service 层:处理数据,映射字段

 // 设置excel的文件名称
       String fileName = enName + "_" + System.currentTimeMillis();
       MyExcelExportUtil.exportExcel('要导出的list数据', pojoClass.class, '文件头', 'sheet名称', response, '文件名');

Utils:

public class MyExcelExportUtil {

   /**
    * Excel文件导出,导出的文件名默认为:headTitle+当前系统时间
    * @param listData 要导出的list数据
    * @param pojoClass 定义excel属性信息
    * @param headTitle Excel文件头信息
    * @param sheetName Excel文件sheet名称
    * @param response
    */
   public static void exportExcel(Collection<?> listData, Class<?> pojoClass, String headTitle, String sheetName, HttpServletResponse response, String fileName) {
       ExportParams params = new ExportParams(headTitle, sheetName);
       params.setHeight((short) 8);
       // 需要则添加样式
       // params.setStyle(ExcelExportMyStylerImpl.class);
       try {
           Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, listData);
//           fileName = URLEncoder.encode(fileName, "UTF8");
           response.setContentType("application/vnd.ms-excel;chartset=utf-8");
           response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls\"");
           ServletOutputStream out=response.getOutputStream();
           workbook.write(out);
           out.flush();
           out.close();
      } catch (Exception e) {
           e.printStackTrace();
      }
  }
}

导出完成:

 

 

遇见的问题:

  1. 页面文件名出现乱码

     fileName = URLEncoder.encode(fileName, "UTF8");
    response.setContentType("application/vnd.ms-excel;chartset=utf-8");
    response.setHeader("Content-Disposition", "attachment; filename= "+ fileName + ".xls");

    修改完成:直接拼接fileName

    response.setContentType("application/vnd.ms-excel;chartset=utf-8");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls\"");
  1. 当文件listData 要导出的list数据和pojoClass要实体类一致

        /**
        * Excel文件导出,导出的文件名默认为:headTitle+当前系统时间
        * @param listData 要导出的list数据
        * @param pojoClass 定义excel属性信息
        * @param headTitle Excel文件头信息
        * @param sheetName Excel文件sheet名称
        * @param response
        */
    ExportParams params = new ExportParams(headTitle, sheetName);
    params.setHeight((short) 8);
    // 需要则添加样式
    // params.setStyle(ExcelExportMyStylerImpl.class);
    Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, listData);
posted @ 2020-12-21 16:14  曹丽是我女朋友。  阅读(3160)  评论(0编辑  收藏  举报