1. get形式传参数

  仅限于get方式,注意请求头参数...,需要后台放开

window.location = '/dms-underlying-asset/download?assetType=' + localStorage.getItem('assetType')

2. post形式传参数

  前端项目中使用最多的

  注意:请求类型:responseType: "blob"

  (1)定义接口(post请求,responseType:数据返回类型为blob)

1 export function download(data) {
2 return request({
3 url: `api/exportExcel`,
4 method: 'post',
5 responseType: 'blob',
6 data
7 })
8 }

  (2)调用接口

 1 const res = await download(this.form)
2 // 等同于
3 // axios.post('/api/exportExcal', this.form, {responseType: //"blob"}).then(res => {})
4
5 console.log(res, '这里会看到返回的blob文件流') // 类似这种
 6
7 if (!res) return //
8 const excelTitle = "嘤嘤嘤.xlsx"
9
10 this.readFileDownload(res, excelTitle) // 封装读取文件并下载  

  (3)封装读取文件并下载

    这里涉及到几个问题:

      1. FileReader 读取bolb流文件,参考:https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader

        FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据

      2. content-type(res.type === 'application/octet-stream')我们在解析res.type的时候,参考:https://blog.csdn.net/u012894692/article/details/88846401

export function readFileDownload(data, msg) {
var res = data
if (res.type === 'application/json' || res.type === 'application/json;charset=UTF-8') { // 失败的时候,注意ie兼容问题

let fileReader = new FileReader()
fileReader.onload = function(event) {
let jsonData = JSON.parse(this.result) // this.result是根据event,读取文件后打印的 event.target.reslut
console.log(jsonData, '...............')
if (jsonData.data === null && jsonData.code === 1) {
Message({
message: jsonData.msg || 'Error',
type: 'error',
duration: 5 * 1000
})
}
}
fileReader.readAsText(res)

}
if (res.type === 'application/octet-stream' || res.type === 'application/vnd.ms-excel' || res.type === 'application/vnd.ms-excel;charset=UTF-8') {
console.log('success...') // 成功,注意ie兼容问题 const blob = new Blob([res], { type: 'application/vnd.ms-excel;charset=utf-8' }) if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, msg)
} else {
console.log(blob)
const url = window.URL.createObjectURL(blob)
console.log(url)
const aLink = document.createElement('a')
aLink.style.display = 'none'
aLink.href = url
aLink.setAttribute('download', msg)
document.body.appendChild(aLink)
aLink.click()
document.body.removeChild(aLink)
window.URL.revokeObjectURL(url)
}
}
}

3,将表格的下载成excel表格(table表格绑定id)

  这种方式怎么说呢,感觉不实用,仅限于前端页面的table表格,在没有后台接口的情况下,(有分页的话还比较难搞),不实用

// 导出表格
var wb = XLSX.utils.table_to_book(document.querySelector('#mytable')) //mytable为表格的id名
var wbout = XLSX.write(wb, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
})
try {
FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '统计表.xlsx')
} catch (e) {
if (typeof console !== 'undefined') console.log(e, wbout)
}
return wbout

vue下载文件模板(excel) 和 导出excel表格的更多相关文章

  1. C#变成数据导入Excel和导出Excel

    excel 基础 •整个excel 表格叫工作表:workbook:工作表包含的叫页:sheet:行:row:单元格:cell. •excel 中的电话号码问题,看起来像数字的字符串以半角单引号开头就 ...

  2. [转]Excel.dll 导出Excel控制

    Excel.dll 导出Excel控制 2010-06-12 11:26 2932人阅读 评论(2) 收藏 举报 excelmicrosoftstring产品服务器google 最近做了个导出Exce ...

  3. 纳税服务系统【用户模块之使用POI导入excel、导出excel】

    前言 再次回到我们的用户模块上,我们发现还有两个功能没有完成: 对于将网页中的数据导入或导出到excel文件中,我们是完全没有学习过的.但是呢,在Java中操作excel是相对常用的,因此也有组件供我 ...

  4. [poi使用]使用excel模版导出excel

    ​ Apache POI是基于Office Open XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)处理各种文件格式的开源项目.简而言之,您可以使用Java读写MS ...

  5. 前端vue下载文件时blob返回流中怎么获取文件名

    我很久之前写了一篇前端vue利用blob对象下载文件,有些人私信我,如果后端返回流失败,给出的json对象该怎么获得?前端获取的流怎么能获取原文件名?其实在那篇文章之后,我就已经针对这两个问题进行了优 ...

  6. POI操作EXCEL之导出Excel(设置有效性,下拉列表引用)

    本人使用的是poi-bin-3.10-FINAL-20140208.zip 版本的poi以下是程序关键代码: //需要引用的类 import java.io.File; import java.io. ...

  7. C#导出EXCEL(DataTable导出EXCEL)

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.I ...

  8. javaweb下载文件模板

    import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax ...

  9. vue下载文件

    import fileDownload from 'js-file-download' let params = { ", ", "filename":&quo ...

  10. 【Java excel】导出excel文件

    TestExprot package excel; import java.io.File; import java.io.IOException; import java.text.DateForm ...

随机推荐

  1. 磊磊零基础打卡算法:day16 c++ Trie树

    5.19 Trie树: 用处:快速的查找和高效存储字符串集合的数据结构. 类似如此的查找,存储 其简单的两个操作:插入和删除 插入: void insert(char str[]) { int p; ...

  2. java相关部分配置

    一.mybatis逆向工程 ① generator.properties jdbc.driverLocation=D:/testDir/Maven/repository_g/mysql/mysql-c ...

  3. Sql Server代理作业、定时任务

    需求: 本次需求为每15分钟获取一次思路为,创建结果表,代理作业定时更新数据并存入结果表,后端只需要调用结果表数据数据,如果超期不同的天数则给出不同的提示信息,因为没有触发点,所以用到了本文内容. 右 ...

  4. 编写简单的button配合input实现上传文件操作

    <template> <button> 导入文件 <input type="file" @change="fileChange" ...

  5. [复现]DASCTF Sept X 浙江工业大学秋季挑战赛-PWN

    hehepwn 一开始泄露stack地址,然后写入shellcode返回到shellcode执行 from pwn import * context.os = 'linux' context.log_ ...

  6. js延迟加载、js异步加载

    1.js延迟加载 (1)js延迟加载是js性能优化的一种方式 (2)作用:为了提高网页的加载速度 (3)原理:等网页加载完成之后再加载js文件 ··需要优化的原因:HTML元素是按照其在页面中出现的次 ...

  7. Constant width to height ratio

    <div class="constant-width-to-height-ratio"></div> .constant-width-to-height-r ...

  8. 【转载】Python:logging详细版

    转载自:https://www.cnblogs.com/Nicholas0707/p/9021672.html 一.logging模块 (一).日志相关概念 日志是一种可以追踪某些软件运行时所发生事件 ...

  9. Android笔记--内容提供者+Server端+Client端

    什么是内容提供者ContentProvider 为App存取内部数据提供的统一的外部接口,让不同的应用之间得以实现数据共享 Client App端 用户输入数据的一端,或者说是用户读取到存储的数据的一 ...

  10. 关于wx.panel中添加wx.button按钮无显示问题记录

    本次出现按钮不显示的原因为pos坐标理解出错: 1.按钮之所没有出现,是因为将全局坐标作为按钮pos的定位,导致在有限的panel布局内无法显示出按钮: 2.经过调试发现当pos=(-1,-1)时,按 ...