The WeChat applet previews the file stream returned by the interface, exports it to an Excel file and forwards it

Just replace the interface url and you can use it

exportExcel () {
    
    
	wx.request({
    
    
		url: importMyApply, //这个地方是你获取二进制流的接口地址
		method: 'POST',
		responseType: "arraybuffer", //特别注意的是此处是请求文件流必须加上的属性,不然你导出到手机上的时候打不开,即使是打开了也是空白
		success: res => {
    
    
			const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器 
			fs.writeFile({
    
     //写文件
				filePath: wx.env.USER_DATA_PATH + "/统计报表.xlsx", // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
				data: res.data, // res.data就是获取到的二进制文件流
				encoding: "binary", //二进制流文件必须是 binary
				success(e) {
    
    
					wx.openDocument({
    
     // 打开文档
						filePath: wx.env.USER_DATA_PATH + "/统计报表.xlsx", //拿上面存入的文件路径
						showMenu: true, // 显示右上角菜单
						success: function(x) {
    
    
							console.log("successfun", x);
						},
					})
				}
			})
		}
	})
}

Effect
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_46099269/article/details/131917881