在uniapp中获取json的数据,将数据导出陈Excel的表格形式

在uniapp中获取json的数据,将数据导出陈Excel的表格形式

whq
whq
2021-12-08 / 0 评论 / 9 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年12月08日,已超过1228天没有更新,若内容或图片失效,请留言反馈。
cc(){
        var that = this
        
       
        // this.removeObjWithArr(this.shuju1,'source_id')
        // var 
        for (var i = 0; i < that.shuju1.length; i++) {
          var newObject = {};
          newObject.company = that.shuju1[i].company;
          newObject.mobile = that.shuju1[i].mobile;
          that.newArray2.push(newObject);
        }
        console.log(this.newArray2)
        // console.log(this.shuju1)
        //要导出的json数据
        // const jsonData = [{
        //   name: '测试数据',
        //   phone: '123456',
        //   email: '123@456.com'
        // }]
        const jsonData = this.newArray2
        //列标题
        let worksheet = "sheet1";
        let str = '<tr><td>公司名称</td><td>电话</td></tr>';
        //循环遍历,每行加入tr标签,每个单元格加td标签
        for (let i = 0; i < jsonData.length; i++) {
          str += '<tr>';
          for (let item in jsonData[i]) {
            //增加\t为了不让表格显示科学计数法或者其他格式
            str += `<td>${ jsonData[i][item] + '\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>`;
        //下载模板
        this.exportFile(template);
      },
      exportFile(fileData, documentName = "项目Excel文件") {
        var that=this
        /*
        PRIVATE_DOC: 应用私有文档目录常量
        PUBLIC_DOCUMENTS: 程序公用文档目录常量
        */
        plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, function(fs) {

          let rootObj = fs.root;
          let fullPath = rootObj.fullPath;
          // let reader = rootObj.createReader();
          // console.log(reader);
          // reader.readEntries((res)=>{
          //     console.log(res); //这里拿到了该目录下所有直接文件和目录
          // },(err)=>{console.log(err);})

          console.log("开始导出数据********");
          // 创建文件夹
          rootObj.getDirectory(documentName, {
            create: true
          }, function(dirEntry) {
            //获取当前的年月继续创建文件夹
            let date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            dirEntry.getDirectory(`${year}年${month}月`, {
              create: true
            }, function(dirEntry2) {
              // 创建文件,防止重名
              let fileName = "excel" + getUnixTime(formatDateThis(new Date()));
              console.log(fileName);
              dirEntry2.getFile(`${fileName}.xlsx`, {
                create: true
              }, function(fileEntry) {
                fileEntry.createWriter(function(writer) {
                  writer.onwritestart = (e) => {
                    uni.showLoading({
                      title: "正在导出",
                      mask: true
                    })
                  }

                  //  /storage/emulated/0指的就是系统路径
                  let pathStr = fullPath.replace("/storage/emulated/0", "");
                  writer.onwrite = (e) => {
                    // 成功导出数据;
                    uni.hideLoading();
                    setTimeout(() => {
                      uni.showToast({
                        title: "成功导出",
                        icon: "success"
                      })
                      that.$refs.popup2.hide()
                      that.duxuan = true
                      that.successTip = `文件位置:${pathStr}/${documentName}/${year}年${month}月`;
                    }, 500)

                  };
                  // 写入内容;
                  writer.write(fileData);

                }, function(e) {
                  console.log(e.message);
                });
              });
            })

          });

        });

      },
3

评论

博主关闭了所有页面的评论