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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM