3

I have to convert one of the sheets from an xslx to csv, for that I use the following code:

url = 'routes/file.xlsx';
const workbook = XLSX.readFile(url); 
const csv = XLSX.utils.sheet_to_csv(workbook.Sheets.files);
XLSX.writeFile(csv, 'file.csv');

But when I execute it, I get that error, some idea of ​​what to do.

Thank you

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84

1 Answers1

2

First of all generate your CSV content:

var table = document.getElementById(id);
var wb = XLSX.utils.table_to_book(table, { sheet: "Sheet JS" });
var ws1 = wb.Sheets[wb.SheetNames[0]];
var csv = XLSX.utils.sheet_to_csv(ws1, { strip: true });
download_file(csv, 'my_csv.csv', 'text/csv;encoding:utf-8');

And then the download_file function credit to Javascript: set filename to be downloaded:

 function download_file(content, fileName, mimeType) {
        var a = document.createElement('a');
        mimeType = mimeType || 'application/octet-stream';

        if (navigator.msSaveBlob) { // IE10
            navigator.msSaveBlob(new Blob([content], {
                type: mimeType
            }), fileName);
        } else if (URL && 'download' in a) { //html5 A[download]
            a.href = URL.createObjectURL(new Blob([content], {
                type: mimeType
            }));
            a.setAttribute('download', fileName);
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        } else {
            location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
        }
    }  
YManaf
  • 61
  • 3