SpringBoot使用easypoi根据设置模板导出数据

使用这个前需要引入依赖

```java
	
            org.apache.poi
            poi
            3.14
        
        
        
            org.apache.poi
            poi-ooxml
            3.17-beta1
        
public void getUpload(HttpServletResponse response,
                                   @RequestParam(defaultValue = "1") int pageNumber,
                                   @RequestParam(defaultValue = "20") int pageSize,
                                  )throws IOException {
       
        Map param = Maps.newHashMap();
      //获取要导出的数据
        List<User> exports = UserMapper.getUserAll(param);
        Map map = Maps.newHashMap();
        map.put("list",exports);
       //获取导出模板地址
       ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191206163150564.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FpX2hhaWJpbg==,size_16,color_FFFFFF,t_70)
        ClassPathResource classPathResource = new ClassPathResource("static/importTemps/cbsj.xlsx");
        String path = classPathResource.getPath();
        /** 一开始下的这个 获取绝对路径会报错{cannot be resolved to absolute file path because it does not reside in the file system},所以改为上面的即可解决问题
           String path = classPathResource.getFile().getPath();
        **/
        
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
        Workbook sheets = ExcelExportUtil.exportExcel(templateExportParams1,map);

        String fileName = "导出数据.xlsx";
        //取得输出流
        OutputStream out = response.getOutputStream();
        //清空输出流
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        //写入文件
        sheets.write(out);
        sheets.close();
        //关闭流
        out.close();
    }

模板样式为:SpringBoot使用easypoi根据设置模板导出数据_第1张图片

文件在项目中地址为:SpringBoot使用easypoi根据设置模板导出数据_第2张图片

你可能感兴趣的:(SpringBoot使用easypoi根据设置模板导出数据)