How can I convert .xls to .xlsx using a nodejs library?

4k Views Asked by At

I'm having trouble reading a .xls file using the ExcelJS library. No problems reading .xlsx files. I used to use only the xlsx js library and did not have any issues reading .xls files.

I was under the impression ExcelJS included xlsx library so my assumption was that reading .xls would work as expected.

So my question is...can I convert .xls to .xlsx using exceljs (I cannot require a user to convert the file themselves in excel) or can I somehow implement the unzip from xlsx js library and incorporate it in the exceljs source code to make it possible?

I switched to exceljs because it seemed more straightforward when it comes to creating excel files.

Thanks All

1

There are 1 best solutions below

1
On

I found a workaround, you can try converting the xls file to CSV with xlsx library and read CSV with ExcelJS.

It doesn't seem to supports converting from xls to xlsx, but if you don't mind forcing users to upload xls file, save the xlsx file and upload it again then here is my codepen for that

        document.getElementById('my_upload').addEventListener('change', function(e) {
            var file = e.target.files[0];
            var reader = new FileReader();
            reader.onload = function(e) {
                var data = e.target.result;
                var workbook = XLSX.read(data, {
                    type: 'binary'
                });
                XLSX.writeFile(workbook, 'test.xlsx');
            };
            reader.readAsBinaryString(file);
        });