uniapp小程序下载并导出excel

javascript 复制代码
<button @click="confirmExport">导出excel</button>
javascript 复制代码
    confirmExport() {
      let header =  {
          "X-Access-Token": uni.getStorageSync(ACCESS_TOKEN), //自定义请求头信息
        } 
      let url = "http"/......"; // 后端API地址
      uni.request({
          url: url,
          method: "POST",
          // method:"GET",
          header: header,
          responseType: "arraybuffer",
        })
        .then((res) => {
          const now = new Date();
          const fileName = now.toISOString() + ".xlsx";
          const arrayBuffer = res[1].data; // utf-8编码的文件数据
          const base64String = uni.arrayBufferToBase64(arrayBuffer);
          const buffer = uni.base64ToArrayBuffer(base64String);
          let fs = uni.getFileSystemManager();
          const filePath = wx.env.USER_DATA_PATH + "/" + fileName;
          fs.writeFile({
            filePath: filePath,
            data: buffer,
            encoding: "binary",
            success: (res) => {
              console.log("文件保存成功");
              uni.openDocument({
                filePath: filePath,
                fileType: 'xlsx',
                showMenu: true,
                success: (res) => {
                  console.log("文件预览成功");
                  // 构建分享内容
                },
                fail: (error) => {
                  console.error("文件保存失败", error);
                },
              });
            },
          });
        });
    },

缺陷:iOS 报错 officeImportErrorDomain错误912

解决方案:

1.点击右上角的三个点,选择"用其他应用打开",可选择WPS打开,即可浏览

原因:

iOS 不能打开 `.doc` `.xls` `.ppt` 类型的文档

微信小程序的 接口wx.openDocument 支持打开office相关文件, 但是 IOS端不支持旧版 97-2003版的格式,需要打开office文件的统一上传新版pptx, docx, xlsx格式的文件, 不要用ppt, doc, 和xls

相关推荐
CHB14 小时前
uni-ai:让你的App快速接入AI
uni-app·deepseek
葡萄城技术团队2 天前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?
excel
小徐_23332 天前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
iOS阿玮3 天前
永远不要站在用户的对立面,挑战大众的公知。
uni-app·app·apple
xw53 天前
uni-app中v-if使用”异常”
前端·uni-app
!win !3 天前
uni-app中v-if使用”异常”
前端·uni-app
2501_915918413 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张3 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
QQ3596773453 天前
ArcGIS Pro实现基于 Excel 表格批量创建标准地理数据库(GDB)——高效数据库建库解决方案
数据库·arcgis·excel
破无差3 天前
《赛事报名系统小程序》
小程序·html·uniapp