Description
So I noticed that while some softwares could open generated XLSX files using exceljs
(like Numbers on MacOS and Google Sheets), Microsoft Excel (or Excel Online) was unable to open the files, giving me an error indicating that the file is corrupted. After simplifying the code and isolating the issue, I realized that for some reason MS Excel assumes a maximum length of 31 characters for Worksheet titles and as a result was deeming the generated file corrupt.
So basically, this snippet works fine:
const excel = require('exceljs');
let book = new excel.Workbook();
let sheet = book.addWorksheet('random sheet name that is valid');
sheet.columns = [
{header: 'First Name', key: 'firstName'},
{header: 'Last Name', key: 'lastName'}
];
sheet.addRow({ firstName: 'John', lastName: 'Wick' });
sheet.addRow({ firstName: 'Elliot', lastName: 'Alderson'});
book.xlsx.writeFile('test.xlsx');
While this one doesn't:
const excel = require('exceljs');
let book = new excel.Workbook();
let sheet = book.addWorksheet('random sheet name that is not valid');
sheet.columns = [
{header: 'First Name', key: 'firstName'},
{header: 'Last Name', key: 'lastName'}
];
sheet.addRow({ firstName: 'John', lastName: 'Wick' });
sheet.addRow({ firstName: 'Elliot', lastName: 'Alderson'});
book.xlsx.writeFile('test.xlsx');
Since MS Excel is what most end-users will use to open automatically generated files by a web-service, I would highly recommend adding an error/warning message for when this case occurs, a toggle-able validation that is on by default, or at least a hint to the documentation.