小程序数据导出文件

小程序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: '文档已打开,请先关闭',
          })
        }

      }
    })
  },`
相关推荐
焦糖玛奇朵婷5 分钟前
扭蛋机小程序:线上扭蛋机模式发展新形势[特殊字符]
大数据·数据库·程序人生·小程序·软件需求
云云只是个程序马喽1 小时前
海外短剧系统开发核心功能设计及上线材料准备
小程序·php
2501_916007471 小时前
iOS与Android符号还原服务统一重构实践总结
android·ios·小程序·重构·uni-app·iphone·webview
全栈软件开发2 小时前
最新壁纸头像小程序系统源码 带流量主
小程序
00后程序员张4 小时前
fastlane 结合 appuploader 命令行实现跨平台上传发布 iOS App
android·ios·小程序·https·uni-app·iphone·webview
2501_915106324 小时前
iOS 性能优化这件事,结合多工具分析运行期性能问题
android·ios·性能优化·小程序·uni-app·cocoa·iphone
鲁Q同志4 小时前
微信小程序调用上一页的方法(主包,分包)
微信小程序·小程序
风月歌4 小时前
小程序项目之基于微信小程序的高校课堂教学管理系统源代码(源码+文档)
java·微信小程序·小程序·毕业设计·源码
嘿siri4 小时前
自定义app端、小程序端和H5等多端自定义键盘输入框,跟随系统键盘弹出和隐藏
javascript·小程序·uni-app·uniapp
小小王app小程序开发4 小时前
2025 陪诊小程序趋势:健康管理延伸 + B 端生态共建
小程序