java下载excel模板-SpringBoot版本

excel文件放在Resource下面

    @PostMapping("/downloadAddCardFtl")
    @OperateLog(module = Module.CARD, menu = "移动卡列表", operate = "下载导卡OR分配卡模板")
    @ApiOperation("下载导卡OR分配卡模板")
    @PreAuthorize("authenticated and hasPermission('or','card_add,card_assign')")
    public void downloadFtl(HttpServletResponse response, DownLoadCardFtlType type) {
        try {
            if (null == type) {
                log.error("下载导卡模板失败,type:【{}】", type);
                return;
            }
            //获取要下载的模板名称
            String fileName = null;
            switch (type) {
                case ADD_CARD:
                    fileName = "addCard.xlsx";
                    break;
                case ASSIGN_CARD:
                    fileName = "assignCard.xlsx";
                    break;
                default:
                    break;
            }
            // 设置要下载的文件的名称
            response.setHeader("Content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
            // 通知客服文件的MIME类型
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            // 获取文件的路径
            String path = CardController.class.getClassLoader().getResource(fileName).getPath();
            System.out.println(path);
            FileInputStream input = new FileInputStream(path);
            OutputStream out = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = input.read(b)) != -1) {
                out.write(b, 0, len);
            }
            // 修正 Excel在“xxx.xlsx”中发现不可读取的.内容。是否恢复此工作薄的内容?如果信任此工作簿的来源,请点击"是"
            response.setHeader("Content-Length", String.valueOf(input.getChannel().size()));
            input.close();
        } catch (Exception e) {
            log.error("下载导卡模板失败", e);
        }
    }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术武器库

一句真诚的谢谢,胜过千言万语

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值