You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using both conditional formatting AND hyperlinks together in the streaming writer, the generated Excel file is corrupted and won't open in Microsoft Excel.
Error message in Excel:
"We found a problem with some content in [filename]. Do you want us to try to recover as much as we can?"
Root Cause
The XML blocks in lib/stream/xlsx/worksheet-writer.js are written in the wrong order. Excel expects conditional formatting to come before hyperlinks in the worksheet XML structure, but ExcelJS currently writes them in reverse order.
Example that triggers corruption:
constExcelJS=require('exceljs');constfs=require('fs');constfileStream=fs.createWriteStream('output.xlsx');constworkbook=newExcelJS.stream.xlsx.WorkbookWriter({stream: fileStream,useSharedStrings: false,useStyles: true,});constworksheet=workbook.addWorksheet('Sheet1');// Add rows with hyperlinksfor(letindex=0;index<4;index++){worksheet.addRow(['test',{text: 'Link',hyperlink: 'https://google.com',tooltip: 'https://google.com',},]);}// Add conditional formattingworksheet.addConditionalFormatting({ref: 'A1:B4',rules: [{priority: 1,type: 'expression',formulae: ['MOD(ROW(),2)=0'],style: {fill: {type: 'pattern',pattern: 'solid',bgColor: 'FFEDEDED',},},},],});worksheet.commit();workbook.commit();// Result: Corrupted file that won't open in Excel! ❌
Solution
Swap the order of two method calls in lib/stream/xlsx/worksheet-writer.js:
Current (wrong) order:
this._writeHyperlinks();// Line 240this._writeConditionalFormatting();// Line 241
Correct order:
this._writeConditionalFormatting();// Firstthis._writeHyperlinks();// Second
This matches the order that Microsoft Excel uses when creating files.
Benefits
✅ Fixes file corruption when using both features together
✅ Enables proper use of conditional formatting with hyperlinks
Source
Adopt PR exceljs#2803 from exceljs#2803
Problem
When using both conditional formatting AND hyperlinks together in the streaming writer, the generated Excel file is corrupted and won't open in Microsoft Excel.
Error message in Excel:
Root Cause
The XML blocks in
lib/stream/xlsx/worksheet-writer.jsare written in the wrong order. Excel expects conditional formatting to come before hyperlinks in the worksheet XML structure, but ExcelJS currently writes them in reverse order.Example that triggers corruption:
Solution
Swap the order of two method calls in
lib/stream/xlsx/worksheet-writer.js:Current (wrong) order:
Correct order:
This matches the order that Microsoft Excel uses when creating files.
Benefits
Attribution
Original PR author: Andrey Kiselev (@TheAsda)
Related
Plan