VUE 文件流下载.sheet、.xls、.xlsx

本文介绍了两种在Vue.js前端应用中实现Excel文件流下载的方法,包括.sheet、.xls、.xlsx格式,帮助开发者处理数据导出需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方式

npm install --save js-file-download
 <el-button size="mini" @click="exportExcel">下载</el-button>
import fileDownload from 'js-file-download';

exportExcel() {
            var urls = `download`;//下载接口
            this.$confirm('确定导出?', '操作提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                axios({
                    url: urls,
                    method: "post",
                    responseType: "blob",
                    data: {
                         "Id": this.id,
                         "startTime": this.startTime,
                         "endTime": this.endTime
                        
                    },
                }).then((res) => {
                    console.log(res.data)
                    fileDownload("blob字节流", 'fileName')
                    fileDownload(res.data, "文件名称.xlsx");
                    this.$message({message: '下载成功!', type: 'success'});
                })
            }).catch(err => {
                this.$message({type: 'info', message: '已取消'});
            })
        },

第二种方式

import axios from "axios";

axios({
     url: `/docDownload`,
      method: 'post',
      responseType: 'blob',
      dataType: 'json',
      headers: { 'Content-type': 'application/json;' },
      data: params
    }).then((res) => {
      const xlsx = 'application/vnd.ms-excel'
      const blob = new Blob([res.data], { type: xlsx }) // 转换数据类型
      const a = document.createElement('a') // 转换完成,创建一个a标签用于下载
      a.download = '周围文档列表' + new Date().getTime() + '.xlsx'
      a.href = window.URL.createObjectURL(blob)
      a.click()
      a.remove()
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值