From bdce05a19748999d2fc94b1d87008aab12877141 Mon Sep 17 00:00:00 2001 From: henry3260 Date: Fri, 20 Nov 2026 17:24:56 +0800 Subject: [PATCH 1/5] fix(xlsx): keep the whole-table style when a table has a data format A table can carry its own data format (dataDxfId) next to its table style. The reader applied that format instead of the wholeTable element of the style, so the fill of the table style was lost on every data row. In "Simple invoice1" the table body was printed white on a tinted page. Apply the wholeTable element first, then row stripes, then the table's own data format, so each layer adds to the one below it. The assertion for "Business expenses budget2" expected no fill on the rows between stripes. The Excel reference shows the whole-table fill there, so the assertion now expects it. --- src/MiniPdf/ExcelReader.cs | 6 +- tests/MiniPdf.Tests/ExcelTableStyleTests.cs | 153 ++++++++++++++++++++ tests/MiniPdf.Tests/XlsxIssueFileTests.cs | 6 +- 3 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 tests/MiniPdf.Tests/ExcelTableStyleTests.cs diff --git a/src/MiniPdf/ExcelReader.cs b/src/MiniPdf/ExcelReader.cs index 5d8c7ff3..7b111ced 100644 --- a/src/MiniPdf/ExcelReader.cs +++ b/src/MiniPdf/ExcelReader.cs @@ -747,9 +747,11 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe for (var c = startCol; c <= endCol && c < rows[r].Count; c++) { var cell = rows[r][c]; - if (directDataStyle != null) cell = ApplyDxfStyle(cell, directDataStyle); - else if (namedWholeStyle != null) cell = ApplyDxfStyle(cell, namedWholeStyle); + // The table style is the base layer; the table's own data + // format adds to it and does not replace it. + if (namedWholeStyle != null) cell = ApplyDxfStyle(cell, namedWholeStyle); if (stripeStyle != null) cell = ApplyDxfStyle(cell, stripeStyle); + if (directDataStyle != null) cell = ApplyDxfStyle(cell, directDataStyle); var relativeCol = c - startCol; if (relativeCol >= 0 && relativeCol < columnDataDxfs.Count && diff --git a/tests/MiniPdf.Tests/ExcelTableStyleTests.cs b/tests/MiniPdf.Tests/ExcelTableStyleTests.cs new file mode 100644 index 00000000..8d919155 --- /dev/null +++ b/tests/MiniPdf.Tests/ExcelTableStyleTests.cs @@ -0,0 +1,153 @@ +using System.IO.Compression; +using System.Text; + +namespace MiniSoftware.Tests; + +public class ExcelTableStyleTests +{ + [Fact] + public void TableDataFormat_LayersOverWholeTableStyle() + { + // The table carries its own data format (dataDxfId, italic only) and a + // table style whose wholeTable element defines a fill. + using var stream = CreateWorkbookWithStyledTable(); + + var sheet = Assert.Single(ExcelReader.ReadSheets(stream)); + + var wholeTableFill = PdfColor.FromRgb(0xDD, 0xF4, 0xF9); + Assert.All(new[] { sheet.Rows[1][0], sheet.Rows[1][1], sheet.Rows[2][0], sheet.Rows[2][1] }, cell => + { + Assert.Equal(wholeTableFill, cell.FillColor); + Assert.True(cell.Italic); + }); + Assert.Null(sheet.Rows[1][2].FillColor); + Assert.False(sheet.Rows[1][2].Italic); + } + + private static MemoryStream CreateWorkbookWithStyledTable() + { + var ms = new MemoryStream(); + using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true)) + { + AddEntry(archive, "[Content_Types].xml", + """ + + + + + + + + + + + """); + + AddEntry(archive, "_rels/.rels", + """ + + + + + """); + + AddEntry(archive, "xl/_rels/workbook.xml.rels", + """ + + + + + + + """); + + AddEntry(archive, "xl/workbook.xml", + """ + + + + + + + """); + + AddEntry(archive, "xl/worksheets/sheet1.xml", + """ + + + + 012 + 3352 + 4252 + + + + """); + + AddEntry(archive, "xl/worksheets/_rels/sheet1.xml.rels", + """ + + + + + """); + + AddEntry(archive, "xl/tables/table1.xml", + """ + + + + + + + +
+ """); + + AddEntry(archive, "xl/styles.xml", + """ + + + + + + + + + + + + + + + + + + + + + """); + + AddEntry(archive, "xl/sharedStrings.xml", + """ + + + ItemQtyOutsidePeoniesTulips + + """); + } + + ms.Position = 0; + return ms; + } + + private static void AddEntry(ZipArchive archive, string path, string content) + { + var entry = archive.CreateEntry(path); + using var writer = new StreamWriter(entry.Open(), new UTF8Encoding(false)); + writer.Write(content); + } +} diff --git a/tests/MiniPdf.Tests/XlsxIssueFileTests.cs b/tests/MiniPdf.Tests/XlsxIssueFileTests.cs index 9e76bcc5..991c269e 100644 --- a/tests/MiniPdf.Tests/XlsxIssueFileTests.cs +++ b/tests/MiniPdf.Tests/XlsxIssueFileTests.cs @@ -183,7 +183,11 @@ public void BusinessExpensesBudget2_GroupedDrawingUsesPrintScale() Assert.InRange(firstStripeFill.G, 0.84f, 0.86f); Assert.InRange(firstStripeFill.B, 0.84f, 0.86f); Assert.Equal("Benefits", sheet.Rows[9][1].Text); - Assert.Null(sheet.Rows[9][1].FillColor); + // Rows between stripes keep the whole-table fill, as in the Excel reference. + var wholeTableFill = Assert.IsType(sheet.Rows[9][1].FillColor); + Assert.InRange(wholeTableFill.R, 0.94f, 0.96f); + Assert.InRange(wholeTableFill.G, 0.94f, 0.96f); + Assert.InRange(wholeTableFill.B, 0.94f, 0.96f); var doc = ExcelToPdfConverter.Convert(issuePath); var page = doc.Pages[0]; From b316db527448fcab69cbdc477a09f61c8bdc07bc Mon Sep 17 00:00:00 2001 From: henry3260 Date: Fri, 20 Nov 2026 17:32:57 +0800 Subject: [PATCH 2/5] docs(xlsx): state the full layer order for table data rows --- src/MiniPdf/ExcelReader.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MiniPdf/ExcelReader.cs b/src/MiniPdf/ExcelReader.cs index 7b111ced..d096478d 100644 --- a/src/MiniPdf/ExcelReader.cs +++ b/src/MiniPdf/ExcelReader.cs @@ -733,7 +733,9 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe } } - // Data rows: apply wholeTable fill and row stripe banding + // Data rows: layer the wholeTable element, row stripes, the table's + // data format, then each column's data format. Each layer adds to + // the ones before it. var dataStart = startRow + headerRowCount; var dataEnd = totalsRowCount > 0 ? endRow - totalsRowCount : endRow; var directDataStyle = GetDxfStyle(dxfInfos, dataDxf); @@ -747,8 +749,6 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe for (var c = startCol; c <= endCol && c < rows[r].Count; c++) { var cell = rows[r][c]; - // The table style is the base layer; the table's own data - // format adds to it and does not replace it. if (namedWholeStyle != null) cell = ApplyDxfStyle(cell, namedWholeStyle); if (stripeStyle != null) cell = ApplyDxfStyle(cell, stripeStyle); if (directDataStyle != null) cell = ApplyDxfStyle(cell, directDataStyle); From ebc0263d40befd8e0663eeea4f6878ca37c8101b Mon Sep 17 00:00:00 2001 From: henry3260 Date: Fri, 20 Nov 2026 21:00:20 +0800 Subject: [PATCH 3/5] fix(xlsx): keep a cell's own fill on top of the table style A fill set on a table body cell is direct formatting. The table style layers replaced it, so an explicitly filled cell took the whole-table or stripe fill. Restore the cell's own fill after the table layers are applied. The firstColumn element moves into the data-row loop so the same rule covers it. Font properties are not covered: the reader cannot yet tell an explicit cell font from the workbook default. --- src/MiniPdf/ExcelReader.cs | 20 ++++++++--------- tests/MiniPdf.Tests/ExcelTableStyleTests.cs | 24 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/MiniPdf/ExcelReader.cs b/src/MiniPdf/ExcelReader.cs index d096478d..25bda668 100644 --- a/src/MiniPdf/ExcelReader.cs +++ b/src/MiniPdf/ExcelReader.cs @@ -735,7 +735,8 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe // Data rows: layer the wholeTable element, row stripes, the table's // data format, then each column's data format. Each layer adds to - // the ones before it. + // the ones before it. A fill set on the cell itself is direct + // formatting and stays on top of every table layer. var dataStart = startRow + headerRowCount; var dataEnd = totalsRowCount > 0 ? endRow - totalsRowCount : endRow; var directDataStyle = GetDxfStyle(dxfInfos, dataDxf); @@ -749,6 +750,7 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe for (var c = startCol; c <= endCol && c < rows[r].Count; c++) { var cell = rows[r][c]; + var ownFill = cell.FillColor; if (namedWholeStyle != null) cell = ApplyDxfStyle(cell, namedWholeStyle); if (stripeStyle != null) cell = ApplyDxfStyle(cell, stripeStyle); if (directDataStyle != null) cell = ApplyDxfStyle(cell, directDataStyle); @@ -760,17 +762,13 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe cell = ApplyDxfStyle(cell, columnDataStyle); } - rows[r][c] = cell; - } - } + // First column: apply firstColumn DXF style (when showFirstColumn) + if (c == startCol && showFirstColumn && namedFirstColStyle != null) + cell = ApplyDxfStyle(cell, namedFirstColStyle); - // First column: apply firstColumn DXF style (when showFirstColumn) - if (showFirstColumn && namedFirstColStyle != null) - { - for (var r = dataStart; r <= dataEnd && r < rows.Count; r++) - { - if (startCol < rows[r].Count) - rows[r][startCol] = ApplyDxfStyle(rows[r][startCol], namedFirstColStyle); + if (ownFill != null) cell = cell with { FillColor = ownFill }; + + rows[r][c] = cell; } } } diff --git a/tests/MiniPdf.Tests/ExcelTableStyleTests.cs b/tests/MiniPdf.Tests/ExcelTableStyleTests.cs index 8d919155..718bfba4 100644 --- a/tests/MiniPdf.Tests/ExcelTableStyleTests.cs +++ b/tests/MiniPdf.Tests/ExcelTableStyleTests.cs @@ -24,6 +24,19 @@ public void TableDataFormat_LayersOverWholeTableStyle() Assert.False(sheet.Rows[1][2].Italic); } + [Fact] + public void CellFill_StaysOnTopOfTableStyle() + { + // A4 has its own red fill. B4 in the same row has none. + using var stream = CreateWorkbookWithStyledTable(); + + var sheet = Assert.Single(ExcelReader.ReadSheets(stream)); + + Assert.Equal(PdfColor.FromRgb(0xFF, 0x00, 0x00), sheet.Rows[3][0].FillColor); + Assert.True(sheet.Rows[3][0].Italic); + Assert.Equal(PdfColor.FromRgb(0xDD, 0xF4, 0xF9), sheet.Rows[3][1].FillColor); + } + private static MemoryStream CreateWorkbookWithStyledTable() { var ms = new MemoryStream(); @@ -81,6 +94,7 @@ private static MemoryStream CreateWorkbookWithStyledTable() 012 3352 4252 + 315 @@ -98,7 +112,7 @@ private static MemoryStream CreateWorkbookWithStyledTable() """ + id="1" name="Items" displayName="Items" ref="A1:B4" dataDxfId="0"> @@ -112,13 +126,17 @@ private static MemoryStream CreateWorkbookWithStyledTable() - + + - + + + + From dedc8203b60cbf67363435c74eb66e541319ecf0 Mon Sep 17 00:00:00 2001 From: henry3260 Date: Fri, 20 Nov 2026 21:09:25 +0800 Subject: [PATCH 4/5] test(xlsx): cover a cell's own fill under row stripes and firstColumn The firstColumn branch that moved into the data-row loop had no test. Add row stripes and a firstColumn element to the fixture behind a flag, and list firstColumn in the layer-order comment. --- src/MiniPdf/ExcelReader.cs | 8 ++--- tests/MiniPdf.Tests/ExcelTableStyleTests.cs | 33 +++++++++++++++++---- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/MiniPdf/ExcelReader.cs b/src/MiniPdf/ExcelReader.cs index 25bda668..1c6a00e4 100644 --- a/src/MiniPdf/ExcelReader.cs +++ b/src/MiniPdf/ExcelReader.cs @@ -734,9 +734,10 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe } // Data rows: layer the wholeTable element, row stripes, the table's - // data format, then each column's data format. Each layer adds to - // the ones before it. A fill set on the cell itself is direct - // formatting and stays on top of every table layer. + // data format, each column's data format, then the firstColumn + // element. Each layer adds to the ones before it. A fill set on the + // cell itself is direct formatting, so it is restored after the + // table layers. var dataStart = startRow + headerRowCount; var dataEnd = totalsRowCount > 0 ? endRow - totalsRowCount : endRow; var directDataStyle = GetDxfStyle(dxfInfos, dataDxf); @@ -762,7 +763,6 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe cell = ApplyDxfStyle(cell, columnDataStyle); } - // First column: apply firstColumn DXF style (when showFirstColumn) if (c == startCol && showFirstColumn && namedFirstColStyle != null) cell = ApplyDxfStyle(cell, namedFirstColStyle); diff --git a/tests/MiniPdf.Tests/ExcelTableStyleTests.cs b/tests/MiniPdf.Tests/ExcelTableStyleTests.cs index 718bfba4..a5556aaa 100644 --- a/tests/MiniPdf.Tests/ExcelTableStyleTests.cs +++ b/tests/MiniPdf.Tests/ExcelTableStyleTests.cs @@ -37,8 +37,27 @@ public void CellFill_StaysOnTopOfTableStyle() Assert.Equal(PdfColor.FromRgb(0xDD, 0xF4, 0xF9), sheet.Rows[3][1].FillColor); } - private static MemoryStream CreateWorkbookWithStyledTable() + [Fact] + public void CellFill_StaysOnTopOfRowStripeAndFirstColumn() { + // Rows 2 and 4 are first-stripe rows. A4 has its own red fill. + using var stream = CreateWorkbookWithStyledTable(stripesAndFirstColumn: true); + + var sheet = Assert.Single(ExcelReader.ReadSheets(stream)); + + var stripeFill = PdfColor.FromRgb(0x00, 0xB0, 0x50); + Assert.Equal(stripeFill, sheet.Rows[1][0].FillColor); + Assert.Equal(stripeFill, sheet.Rows[3][1].FillColor); + Assert.Equal(PdfColor.FromRgb(0xFF, 0x00, 0x00), sheet.Rows[3][0].FillColor); + // The firstColumn element still sets the font of a cell that keeps its own fill. + Assert.True(sheet.Rows[1][0].Bold); + Assert.True(sheet.Rows[3][0].Bold); + Assert.False(sheet.Rows[3][1].Bold); + } + + private static MemoryStream CreateWorkbookWithStyledTable(bool stripesAndFirstColumn = false) + { + var flag = stripesAndFirstColumn ? "1" : "0"; var ms = new MemoryStream(); using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true)) { @@ -109,7 +128,7 @@ private static MemoryStream CreateWorkbookWithStyledTable() """); AddEntry(archive, "xl/tables/table1.xml", - """ + $"""
@@ -117,7 +136,7 @@ private static MemoryStream CreateWorkbookWithStyledTable() - +
"""); @@ -137,13 +156,17 @@ private static MemoryStream CreateWorkbookWithStyledTable() - + + + - + + + From dc4ff3459d79ca2d00a73d5540d59da9c1b2e51e Mon Sep 17 00:00:00 2001 From: Wei Lin Date: Wed, 25 Nov 2026 16:47:36 +0800 Subject: [PATCH 5/5] Preserve empty and white table fills --- src/MiniPdf/ExcelReader.cs | 16 ++++----- tests/MiniPdf.Tests/ExcelTableStyleTests.cs | 39 +++++++++++++++++---- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/MiniPdf/ExcelReader.cs b/src/MiniPdf/ExcelReader.cs index 1c6a00e4..d51da833 100644 --- a/src/MiniPdf/ExcelReader.cs +++ b/src/MiniPdf/ExcelReader.cs @@ -914,12 +914,8 @@ private static (List FontIndices, List FillIndices, List NumFmtId { if (fgPdf != null) { - var c = fgPdf.Value; - // Skip pure white fills as they're invisible - if (c.R < 0.99f || c.G < 0.99f || c.B < 0.99f) - fills.Add(c); - else - fills.Add(null); + // Preserve explicit white fills so they can override a table-style fill. + fills.Add(fgPdf.Value); } else fills.Add(null); @@ -1238,15 +1234,17 @@ private static List ReadDxfStyles(ZipArchive archive, List" : string.Empty; var ms = new MemoryStream(); using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true)) { @@ -114,6 +138,7 @@ private static MemoryStream CreateWorkbookWithStyledTable(bool stripesAndFirstCo 3352 4252 315 + 310 @@ -131,7 +156,7 @@ private static MemoryStream CreateWorkbookWithStyledTable(bool stripesAndFirstCo $""" + id="1" name="Items" displayName="Items" ref="A1:B5" dataDxfId="0"> @@ -141,23 +166,25 @@ private static MemoryStream CreateWorkbookWithStyledTable(bool stripesAndFirstCo """); AddEntry(archive, "xl/styles.xml", - """ + $""" - + + - + + - + {dataDxfFill}