springBoot + Vue 以流的形式下载excel文件

前端代码:
export default {
    components: {
        dptable
    },
    data() {
        return {
            // 访问数据的地址
            exportOrderTemplateUrl: "/api/distrGoodsManage/exportOrdertemplate",
//以下无关省略.........

//导出excel方法:
 exportExcel() {
            const querystring = require("querystring");
            this.$axios
                .post(this.exportOrderTemplateUrl, querystring.stringify({}), {
                    responseType: "blob"
                })
                .then(res => {
                    const blob = new Blob([res.data], {
                        type: "application/vnd.ms-excel"
                    });
                    let fileName = "批量下单Excel模板.xls";
                    if ("download" in document.createElement("a")) {
                        const elink = document.createElement("a");
                        elink.download = fileName;
                        elink.style.display = "none";
                        elink.href = URL.createObjectURL(blob);
                        document.body.appendChild(elink);
                        elink.click();
                        URL.revokeObjectURL(elink.href); // 释放URL 对象
                        document.body.removeChild(elink);
                    } else {
                        navigator.msSaveBlob(blob, fileName);
                    }
                })
                .catch(error => {
                    alert("数据处理异常,请与管理员联系!");
                });
        },

后端代码:

/**
     * 方法名称:exportsumRefund
     * 功能描述:根据分销商id退货单总额,产生excel文件 group by(day)
     * 作    者:loki
     * 日    期:2019-05-22
     */
@RequestMapping(value = "/exportOrdertemplate", method = {RequestMethod.POST})
    @AuthToken(type = "distr")
    public void exportOrdertemplate(HttpServletResponse response, HttpServletRequest request) throws IOException {
        //获取生产文件信息
        
        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet("Sheet1");
        //产生表格标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("商品编码(必填)");
        row.createCell(1).setCellValue("购买数量(必填)");
        row.createCell(2).setCellValue("姓名(必填)");
        row.createCell(3).setCellValue("电话(必填)");
        row.createCell(4).setCellValue("省(必填)");
        row.createCell(5).setCellValue("市(必填)");
        row.createCell(6).setCellValue("区(必填)");
        row.createCell(7).setCellValue("详情地址(必填)");

        //浏览器下载excel
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=salesRefundList.xls");
        response.flushBuffer();
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        
    }

效果图
springBoot + Vue 以流的形式下载excel文件_第1张图片

你可能感兴趣的:(java,vue)