Skip to content

Adopt PR #2803: Fix corrupted file with conditional formatting and hyperlinks #25

@protobi-pieter

Description

@protobi-pieter

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:

"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:

const ExcelJS = require('exceljs');
const fs = require('fs');

const fileStream = fs.createWriteStream('output.xlsx');
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
  stream: fileStream,
  useSharedStrings: false,
  useStyles: true,
});

const worksheet = workbook.addWorksheet('Sheet1');

// Add rows with hyperlinks
for (let index = 0; index < 4; index++) {
  worksheet.addRow([
    'test',
    {
      text: 'Link',
      hyperlink: 'https://google.com',
      tooltip: 'https://google.com',
    },
  ]);
}

// Add conditional formatting
worksheet.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 240
this._writeConditionalFormatting();  // Line 241

Correct order:

this._writeConditionalFormatting();  // First
this._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
  • ✅ Matches Office Open XML specification ordering
  • ✅ Critical for streaming writer use cases

Attribution

Original PR author: Andrey Kiselev (@TheAsda)

Related

  • Affects streaming writer specifically
  • Required for template-based conditional formatting workflows

Plan

  1. ✅ Create this tracking issue
  2. Apply the 2-line fix
  3. Create test case demonstrating the fix
  4. Comment on upstream PR Fix corrupted file with conditional formatting and hyperlinks exceljs/exceljs#2803

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions