当前位置: 首页 > 工具软件 > Easy-Xls > 使用案例 >

easy-poi以前端下载方式导出

阎博易
2023-12-01

easy-poi通过下载方式导出


//放入easyPoi中 导出
ExportParams params = new ExportParams("统计表", "统计表", ExcelType.XSSF);
params.setFreezeCol(2); //这个是列比较多时,固定2列
//ExportEntity 导出的模板,表头标题可通过@Excel(name="名称")设置
//userList  需要导出的数据,需要使用ExportEntity相同的字段
Workbook workbook = ExcelExportUtil.exportExcel(params, ExportEntity.class, userList);
        
String suffix = "";
if (workbook instanceof HSSFWorkbook) {
      suffix = ".xls";
} else {
      suffix = ".xlsx";
}
ServletOutputStream os = null;
try {
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
      String s = LocalDateTime.now().format(formatter);
      response.setContentType("application/force-download");
      response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("统计表-" + s, "UTF-8") + suffix);
      response.addHeader("FileName",URLEncoder.encode("统计表-"+s, "UTF-8")+suffix);
      response.setHeader("Access-Control-Expose-Headers", "FileName");
      os = response.getOutputStream();
      workbook.write(os);
} catch (IOException e) {
      throw new DataConflictException("文件下载失败!");
} finally {
      if (os != null) {
           try {
                os.flush();
           } catch (IOException e) {
                e.printStackTrace();
           }
      }
}

学习注定是一条漫长又艰苦的道路,没有捷径可言,所有人都一样。熬过去,你就赢了!

 类似资料: