Easyexcel(3-文件导出)

相关文章链接

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

响应头设置

通过设置文件导出的响应头,可以自定义文件导出的名字信息等

//编码格式为UTF-8
response.setCharacterEncoding("UTF-8");

//让服务器告诉浏览器它发送的数据属于excel文件类型
response.setContentType("application/vnd.ms-excel;charset=UTF-8");

//描述内容在传输过程中的编码格式,BINARY可能不止包含非ASCII字符,还可能不是一个短行(超过1000字符)。
response.setHeader("Content-Transfer-Encoding", "binary");

//must-revalidate:强制页面不缓存,post-check=0, pre-check=0:0秒后,在显示给用户之前,该对象被选中进行更新过
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");

//表示响应可能是任何缓存的,即使它只是通常是非缓存或可缓存的仅在非共享缓存中
response.setHeader("Pragma", "public");

//告诉浏览器这个文件的名字和类型,attachment:作为附件下载;inline:直接打开
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");

写入单个Sheet

一次性写入数据

指定导出内容所对应的对象信息,通过doWrite写入数据

注意:doWrite方法必须传入的是集合

@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("/download1")
public void download1(HttpServletResponse response) {
   
    try {
   
        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");

        User user = new User();
        user.setUserId(123);
        user.setName("as");
        user.setPhone("15213");
        user.setEmail("5456");
        user.setCreateTime(new Date());
        EasyExcel.write(response.getOutputStream(), User.class)
                .sheet("模板")
                .doWrite(Arrays.asList(user));
    } catch (Exception e) {
   
        e.printStackTrace();
    }
}

分批写入数据

@GetMapping("/download2")
public void download2(HttpServletResponse response) {
   
    ExcelWriter excelWriter = null;
    try {
   
        response.setContentType("application/vnd.ms-excel");
        response.set
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值