openfeign调用excel导出接口

1.常见的excel导出核心方式

excel导出在企业应用中比较常见的.
我们一般都是把接口的 header“ContentType” 置为 “application/octet-stream”
“Content-Disposition” 置为 "attachment;filename=${fileName}"
之后我们将excel的流写入 responseoutputstream 中,代码逻辑大体如下

    //这是被调用方(服务提供方)的excel导出接口。(这里excel导出被高度封装,请自行编写excel导出逻辑)
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response) throws IOException {
        sw.buildQuery()
                .exclude("staffFlag")
                .fileName("controller.xlsx")
                .doExport(response, Staff.class);
    }
    
    //其中 doExport函数
    public MpaasQuery doExport(HttpServletResponse response, Class pojo) {
        response.setContentType("application/octet-stream");

        try {
            String f = new String(this.excelFileName.getBytes("UTF-8"), "ISO8859_1");
            response.setHeader("Content-Disposition", "attachment;filename=" + f);
            this.doExport((OutputStream)response.getOutputStream(), pojo);
            return this;
        } catch (Exception var4) {
            throw new MpaasRuntimeException(var4);
        }
    }
2.openfeign如何调用excel导出的接口

而在 openfeign 中,调用excel导出接口不能像普通接口一样输入输出参数保持相同,我们需要得到 excel 的字节流,所以调用方的代码如下所示

//使用feign来调用已经写好的excel导出接口
@FeignClient(name = "staff",url = "http://localhost:8080/controller")
public interface TestFeign {

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public byte[] export();
}
3.最终暴露出来的接口

最后,我们在调用这个feign层的函数时候需要重新设置response的 “ContentType”"Content-Disposition"

    //最终在我们系统中的接口
    @RequestMapping("/feignexport")
    public void health(HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=controller.xlsx");
        response.getOutputStream().write(testFeign.export());
    }
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值