小程序数据导出文件

小程序josn数据生成excel文件

先从下载传送门将xlsx.mini.min.js拷贝下来,新建xlsx.js文件放入小程序项目文件夹下。

javascript 复制代码
const XLSX = require('./xlsx')//在需要用的页面中引入
javascript 复制代码
// 定义导出 Excel 报表的方法
  exportData() {
    const that = this
    let newData = [{time:2021,value:111},{time:2022,value:222},{time:2023,value:333}]
    // 构建一个表的数据
    let lock = []
    let title = ['时间', '数值']
    lock.push(title)
    that.data.newData.forEach(item => {
      let rowcontent = []
      rowcontent.push(item.time)
      rowcontent.push(item.value)
      lock.push(rowcontent)
    })

    //自定义列宽
    const colWidth = [{
        wch: 10
      },
      {
        wch: 10
      },
    ]

    // XLSX插件使用
    var ws = XLSX.utils.aoa_to_sheet(lock);
    // ws['!cols'] = colWidth
    var wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, "xxx数据");
    var fileData = XLSX.write(wb, {
      bookType: "xlsx",
      type: 'base64'
    });
 
    let filePath = `${wx.env.USER_DATA_PATH}/xxxx记录.xlsx`

    // 写文件
    const fs = wx.getFileSystemManager()
    fs.writeFile({
      filePath: filePath,
      data: fileData,
      encoding: 'base64',
      success(res) {
        console.log(res)
        const sysInfo = wx.getSystemInfoSync()
        // 导出
        if (sysInfo.platform.toLowerCase().indexOf('windows') >= 0) {
          // 电脑PC端导出
          wx.saveFileToDisk({
            filePath: filePath,
            success(res) {
              console.log(res)
            },
            fail(res) {
              console.error(res)
              util.tips("导出失败")
            }
          })
        } else {
          // 手机端导出
          // 打开文档
          wx.openDocument({
            filePath: filePath,
            //默认为false,true可在右上角进行分享转发
            showMenu: true,
            success: function (res) {
              console.log('打开文档成功')
            },
            fail: console.error
          })
        }
      },
      fail(res) {
        console.error(res)
        if (res.errMsg.indexOf('locked')) {
          wx.showModal({
            title: '提示',
            content: '文档已打开,请先关闭',
          })
        }

      }
    })
  },`
相关推荐
—Qeyser10 小时前
小程序UI(自定义Navbar)
小程序
2501_9159214311 小时前
iOS 应用上架多环境实战,Windows、Linux 与 Mac 的不同路径
android·ios·小程序·https·uni-app·iphone·webview
Goona_12 小时前
PyQt多窗口应用开发:构建完整的可二次开发用户登录注册模板
python·小程序·excel·pyqt
yw00yw15 小时前
小程序插件使用
java·小程序·apache
00后程序员张15 小时前
iOS 应用上架常见问题与解决方案,多工具组合的实战经验
android·ios·小程序·https·uni-app·iphone·webview
weixin_lynhgworld1 天前
从闲置到珍宝:旧物回收小程序系统重塑物品价值
小程序·旧物回收
2501_916007471 天前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
小小怪下士_---_2 天前
uniapp开发微信小程序自定义导航栏
前端·vue.js·微信小程序·小程序·uni-app
fakaifa2 天前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
说私域2 天前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序