Easyexcel(4-模板文件)

相关文章链接

  1. Easyexcel(1-注解使用)
  2. Easyexcel(2-文件读取)
  3. Easyexcel(3-文件导出)
  4. Easyexcel(4-模板文件)

文件导出

获取 resources 目录下的文件,使用 withTemplate 获取文件流导出文件模板

@GetMapping("/download1")
public void download1(HttpServletResponse response) {
   
    try (InputStream in = new ClassPathResource("测试.xls").getInputStream()) {
   
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");

        EasyExcel.write(response.getOutputStream())
                .withTemplate(in)
                .sheet("sheet1")
                .doWrite(Collections.emptyList());
    } catch (Exception e) {
   
        e.printStackTrace();
    }
}

注意:获取 resources 目录下的文件需要在 maven 中添加以下配置,过滤对应的文件,防止编译生成后的 class 文件找不到对应的文件信息

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
            <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

对象填充导出

模板文件信息

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
   

    @ExcelProperty(value = "用户Id")
    private Integer userId;

    @ExcelProperty(value = "姓名")
    private String name;

    @ExcelProperty(value = "手机")
    private String phone;

    @ExcelProperty(value = "邮箱")
    private String email;

    @ExcelProperty(value = "创建时间")
    private Date createTime;
}
@GetMapping("/download5")
public void download5(HttpServletResponse response) {
   
    try (InputStream in = new ClassPathResource("测试3.xls").getInputStream()) {
   
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试3", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");

        User user = new User(1, "张三", "12345678901", "zhangsan@qq.com", new Date());
        EasyExcel.write(response.getOutputStream(), User.class)
                .withTemplate(in)
                .sheet("模板")
                .doFill(user);
    } catch (Exception e) {
   
        e.printStackTrace();
    }
}

注意:填充模板跟写文件使用的方法不一致,模板填充使用的方法是 doFill,而不是 doWrite

导出文件内容

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

List 填充导出

对象导出

模板文件信息

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值