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

相关推荐
邹荣乐4 分钟前
微信小程序动态tabBar实现:基于自定义组件,灵活支持不同用户角色与超过5个tab自由组合
前端·微信小程序·uni-app
不吃糖葫芦31 小时前
App使用webview套壳引入h5(三)——解决打包为app后在安卓机可物理返回但是在苹果手机无法测滑返回的问题
uni-app·webview
weixin_472339464 小时前
python批量解析提取word内容到excel
python·word·excel
半兽先生4 小时前
uniapp微信小程序视频实时流+pc端预览方案
微信小程序·uni-app·音视频
KerwinChou_CN5 小时前
自由开发者计划 004:创建一个苹果手机长截屏小程序
图像处理·算法·智能手机·小程序
Uyker5 小时前
空间利用率提升90%!小程序侧边导航设计与高级交互实现
前端·微信小程序·小程序
说私域5 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的生态农庄留存运营策略研究
人工智能·小程序·开源·零售
Nightne5 小时前
小程序引入deepseek
小程序
胡斌附体14 小时前
uniapp路由跳转toolbar页面
小程序·uni-app·switch·路由·type·uview-ui
阿諪諪15 小时前
在VSCode中开发一个uni-app项目
ide·vscode·uni-app