Java原样导出excel模板

前言:此处采用了restful的接口形式,提供导出的api,前端采用react调用。

@PostMapping(value = "exportBlankTemplate")
    public void exportBlankTemplate(HttpServletResponse response) {
        APIResult<String> result = new APIResult<>();
        org.springframework.core.io.Resource resource = new ClassPathResource("template/" + EXCEL_BLANK_TEMPLATE);
        try {
            InputStream in = resource.getInputStream();
            OutputStream out = response.getOutputStream();
            response.reset();
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + EXCEL_BLANK_TEMPLATE);
            // 循环取出流中的数据
            byte[] b = new byte[200];
            int len;
            while ((len = in.read(b)) > 0) {
                out.write(b, 0, len);
            }
            out.flush();
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
private static final String EXCEL_BLANK_TEMPLATE = "KBJBTZS.xlsx";

excel的文件路径为:

备注:需要注意此方法的返回值,需要给void或者String 之类的,不然会报一个错误:Could not find acceptable representation

 

此问题解决的参考博客:https://blog.csdn.net/abelethan/article/details/100559862

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值