hutool:导出表格(Could not find acceptable representation )

导包:

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.1</version>
</dependency>

代码:

    //导出
    public void exportUser(HttpServletResponse response){
        ServletOutputStream out = null;
        try {
            //1.获取user
            List<User> list = userService.list();
            //2.设置excel
            // 通过工具类创建writer,默认创建xls格式
            ExcelWriter writer = ExcelUtil.getWriter(true);
            //自定义标题别名
            writer.addHeaderAlias("fullName", "FULL");
            writer.addHeaderAlias("username", "UUUUserName");
            //强制性重写localCache--------此配置为true-只使用重写后的别名作为表格标题,不使用bean的字段
//            writer.setOnlyAlias(true);
            // 一次性写出内容,使用默认样式,强制中出标题
            writer.write(list, true);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("user", "UTF-8") + ".xlsx");

            out = response.getOutputStream();   //out为OutputStream,需要写出到的目标流
            writer.flush(out, true);
            //3.关闭writer,释放内存
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(out);
        }
    }

注意:

	//controller
    @GetMapping("start/export")
    public void startExport(HttpServletResponse response){
        userTask.exportUser(response);
    }

一开始给controller加了通用返回值,一直在报Could not find acceptable representation 。

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:111)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值