无法从离子的XLSX生成Excel文件

时间:2018-09-27 08:54:24

标签: ios excel file ionic-framework ionic3

我正在根据用户数据创建excel文件,但不幸的是,它没有生成文件,甚至不知道发生了什么错误,因此我至少可以尝试解决该错误。

特别是仅在 iOS平台中会发生此问题。

请找到以下代码以生成excel文件:

public createXSLX(): Promise<any> {
    return new Promise((resolve) => {
      let sheet = XLSX.utils.json_to_sheet(this.data);
      let wb = {
        SheetNames: ["export"],
        Sheets: {
          "export": sheet
        }
      };

  let wbout = XLSX.write(wb, {
    bookType: 'xlsx',
    bookSST: false,
    type: 'binary'
  });

  function s2ab(s) {
    let buf = new ArrayBuffer(s.length);
    let view = new Uint8Array(buf);
    for (let i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
    return buf;
  }

  let blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' });

  resolve(blob);
});


}

以上功能在android上运行良好,但在iOS中,它不是根据提供的数据生成文件。

我如何调用上述功能代码:

onExportNew = function (toEmail) {
    this.createXSLX().then((xclBlob) => {
      let time = new Date().getTime();
      let fileName: string = "roster_" + time + ".xlsx";
      var fs = ''
      if (this.platform.is('ios')){
        fs = this.file.documentsDirectory;
      }else{
        fs = this.file.externalDataDirectory;
      }

      console.log("File Path:- ",fs)

      this.file.writeFile(fs, fileName, xclBlob, true).then(() => {
        let fp = fs + fileName;

        let email = {
          // to: 'lmahajan@cisco.com',
          // cc: 'erika@mustermann.de',
          // bcc: ['john@doe.com', 'jane@doe.com'],
          to: toEmail,
          attachments: [fp],
          subject: 'Roster Excel File',
          body: '<h1>PFA</h1>',
          isHtml: true
        };

        this.emailComposer.open(email).then(() => {
          this.showDone = true;
        }).catch(() => {
          let toast = this.toastCtrl.create({
            message: 'Could not open email composer',
            duration: 3000
          });
          toast.present();
        });

      }).catch(() => {
        this.displayAlert('Error', 'error creating file at: ' + fs);
      });
    }).catch(() => {
      console.log("Excel file creation error");
    });
  }

如果上面的代码中缺少任何内容,请指导我。

谢谢!

0 个答案:

没有答案