uni-app导出excel

2023-03-12  本文已影响0人  幽小鬼

1、遇到问题

众所周知,uniapp除了h5没有dom和blob对象,不能使用h5导出excel方法,需要换个方法,走的是系统IO流

2、代码实现

2.1 数据转成表格模板

    tableToExcel () {
      //要导出的json数据
      const jsonData = [{
        name: '测试数据',
        phone: '123456',
        email: '123@456.com'
      }]
      let worksheet = 'sheet1'
      let str = '<tr><td>姓名</td><td>电话</td><td>邮箱</td></tr>'
      //循环遍历,每行加入tr标签,每个单元格加td标签
      for (let i = 0; i < jsonData.length; i++) {
        str += '<tr>'
        for (let key in jsonData[i]) {
          //增加\t为了不让表格显示科学计数法或者其他格式
          str += `<td>${ jsonData[i][key] + '\t'}</td>`
        }
        str += '</tr>'
      }
      //下载的表格模板数据
      let template = `<html xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:x="urn:schemas-microsoft-com:office:excel"
        xmlns="http://www.w3.org/TR/REC-html40">
        <head><!--[if gte mso 9]><xml encoding="UTF-8"><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
        <x:Name>${worksheet}</x:Name>
        <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
        </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
        </head><body><table>${str}</table></body></html>`
      //下载模板
      exportExcel(template, 'excel')
    }

2.2 导出excel

export function exportExcel (fileData, documentName = 'excel') {
  /*
  PRIVATE_DOC: 应用私有文档目录常量
  PUBLIC_DOCUMENTS: 程序公用文档目录常量
  */
  plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, function(fs) {
    let rootObj = fs.root
    let fullPath = rootObj.fullPath
    console.log("开始导出数据********")
    // 创建文件夹
    rootObj.getDirectory(documentName, {
      create: true
    }, function(dirEntry) {
      // 创建文件,防止重名
      let fileName = new Date().getTime()
      console.log(fileName)
      dirEntry.getFile(`${fileName}.xlsx`, {
        create: true
      }, function(fileEntry) {
        fileEntry.createWriter(function(writer) {
          writer.onwritestart = (e) => {
            showLoading('正在导出')
          }
          writer.onwrite = (e) => {
            // 成功导出数据
            hideLoading()
            setTimeout(() => {
              showToast('导出成功')
              const path = `file://${fullPath}${documentName}/${fileName}.xlsx`
              console.log('文件位置:', path)
              // 打开文件
              uni.openDocument({
                filePath: path,
                success: res => {
                  console.log('打开文档成功', res)
                },
                fail: e => {
                  console.log('打开失败', e)
                }
              })
            },500)
          }
          // 写入内容
          writer.write(fileData)
        }, function(e) {
          console.log(e.message)
        })
      })
    })
  })
}

2.3 uniapp api封装

上一个函数用到的三个uniapp api的简单封装

/**
 * show toast
 * @param title
 */
export function showToast (title = '') {
  uni.hideLoading()
  uni.showToast({
    title,
    icon: 'none',
    duration: 1500
  })
}
/**
 * loading
 * @param title
 */
export function showLoading (title = '加载中') {
  uni.hideLoading()
  uni.showLoading({
    title,
    mask: true
  })
}
/**
 * hideLoading
 */
export function hideLoading () {
  uni.hideLoading()
}

3、参考文献

https://www.bbsmax.com/A/Vx5MD3m9JN/
https://www.html5plus.org/doc/zh_cn/io.html

上一篇 下一篇

猜你喜欢

热点阅读