前端传流文件给接口的方法
https://blog.csdn.net/jianleking/article/details/118442697
json转xlsx文件导出
https://www.jianshu.com/p/7950d40ace40
json转流文件。和 流文件解析
https://www.jianshu.com/p/10bce14f0787
前端调返回流文件的接口下载
https://www.cnblogs.com/kaibindirver/p/15797386.html
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
const data=[{"name":"张三"}]
// 导出xmind
// 转成xlsx源文件
const wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} };
wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(data);//通过json_to_sheet转成单页(Sheet)数据
const wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
// 下载文件到本地
// FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), "xxx"+'.xlsx');
var targe=new Blob([wbout])
// 接口调用
let param = new FormData() // 创建form对象
param.append('files',targe,`${this.$route.query.iterations_name}.xlsx`) #注意第三个参数是文件名要传后端要判断
#这个接口接收file文件 然后返回zip的流
axios.post('/con/xxxxx/download',param,{
"Content-Type":"application/octet-stream",
responseType: 'arraybuffer' #一定要加 不然无法解压出来
}).then(resopnse=>{
#创建虚拟按钮下载
const bl = new Blob([resopnse.data], {type: "application/octet-stream"})
const objectUrl = window.URL.createObjectURL(bl);
const a = document.createElement('a');
a.href = objectUrl;
a.download = `${this.$route.query.iterations_name}.zip`;
a.click();
a.remove();
});
}
xlsx库最近更新了
https://docs.sheetjs.com/docs/getting-started/installation/nodejs
安装
npm rm --save xlsx
npm i --save https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz
引入
import * as XLSX from 'xlsx'