手机版 欢迎访问it开发者社区(www.mfbz.cn)网站

当前位置: > 开发

微信小程序 导出Excel 文件

时间:2021/5/29 15:54:32|来源:|点击: 次
	<view  bindtap="exportData">导出</view>
// 导出
  exportData() {
    const auth = wx.getStorageSync("auth");
    wx.showLoading({
      title: "加载中",
    });
    wx.downloadFile({
      //下载
      url: configAPI.exportPlanList + '?searchParam=' + this.data.inputVal,
      header: {
        "content-type": "application/vnd.ms-excel",
        auth: auth,
      },
      success(res) {
        console.log(res);
        if (res.statusCode === 200) {
          let savePath =
            wx.env.USER_DATA_PATH + "/TESUN_PLAN_" + Date.now() + '.xls';
          wx.getFileSystemManager().saveFile({
            //下载成功后保存到本地
            tempFilePath: res.tempFilePath,
            filePath: savePath,
            success(res) {
              wx.hideLoading();
              console.log(res);
              let savePath = res.savedFilePath;
              wx.showModal({
                title: "下载成功",
                content: "是否打开?",
                confirmColor: "#0bc183",
                confirmText: "打开",
                success(res) {
                  if (res.confirm) {
                    wx.openDocument({
                      //打开
                      filePath: savePath,
                      showMenu: true,
                      success(res) {
                        console.log(res);
                      },
                    });
                  } else if (res.cancel) {}
                },
              });
            },
            fail(err) {
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: "文件下载失败",
                showCancel: false, //是否显示取消按钮
                success: function (result) {}
              })
            },
          });
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: "文件下载失败",
            showCancel: false, //是否显示取消按钮
            success: function (result) {}
          })
        }
      },
      fail(err) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: "文件下载失败",
          showCancel: false, //是否显示取消按钮
          success: function (result) {}
        })
      }
    });
  },

Copyright © 2002-2019 某某自媒体运营 版权所有