How to download a table of the quasar in excel format (xlsx)?

Asked

Viewed 87 times

-2

The quasar only provides examples of how to download a table in CSV format. I wonder if it is possible, using the same logic, to download tables in excel. Code of how I am doing. The file comes to be downloaded but does not open. excel says that the file is either with wrong or corrupted extension.

    function wrapExcelValue (val, formatFn) {
  let formatted = typeof formatFn !== 'undefined'
    ? formatFn(val)
    : val

  formatted = typeof formatted === 'undefined' || formatted === null
    ? ''
    : String(formatted)

  formatted = formatted.split('"').join('""').split('\n').join('\\n').split('\r').join('\\r')
  return `"${formatted}"`
}

exportExcel () {
      // naive encoding to excel format
      console.log('Excel')
      const content = [this.columns.map(col => wrapExcelValue(col.label))].concat(
        this.data.map(row => this.columns.map(col => wrapExcelValue(
          row[col.name],
          col.format
        )).join(','))
      ).join('\r\n')

      const date = new Date()

      const status = exportFile(
        'mensagens_' + this.altDate(date.getTime()) + '.xlsx',
        content,
        'text/xlsx'
      )

      if (status !== true) {
        this.$q.notify({
          message: 'Download rejeitado pelo navegador...',
          color: 'negative',
          icon: 'warning'
        })
      }
    }

1 answer

0

Unfortunately, an XLSX file is not as simple as a CSV, so you will need to help and another lib, such as Sheetjs js-xlsx

Here an example of use.

var ws = XLSX.utils.json_to_sheet([
  { A:"S", B:"h", C:"e", D:"e", E:"t", F:"J", G:"S" },
  { A: 1,  B: 2,  C: 3,  D: 4,  E: 5,  F: 6,  G: 7  },
  { A: 2,  B: 3,  C: 4,  D: 5,  E: 6,  F: 7,  G: 8  }
], {header:["A","B","C","D","E","F","G"], skipHeader:true});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.