diff --git a/src/MiniPdf/ExcelReader.cs b/src/MiniPdf/ExcelReader.cs index 5d8c7ff3..d51da833 100644 --- a/src/MiniPdf/ExcelReader.cs +++ b/src/MiniPdf/ExcelReader.cs @@ -733,7 +733,11 @@ 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, 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); @@ -747,9 +751,10 @@ 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); + 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); var relativeCol = c - startCol; if (relativeCol >= 0 && relativeCol < columnDataDxfs.Count && @@ -758,17 +763,12 @@ private static void ApplyTableStyleFormatting(ZipArchive archive, string workshe cell = ApplyDxfStyle(cell, columnDataStyle); } - rows[r][c] = cell; - } - } + 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; } } } @@ -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 + { + Assert.Equal(wholeTableFill, cell.FillColor); + Assert.True(cell.Italic); + }); + Assert.Null(sheet.Rows[1][2].FillColor); + 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); + } + + [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); + } + + [Fact] + public void EmptyDataDxfFill_PreservesRowStripe() + { + using var stream = CreateWorkbookWithStyledTable(stripesAndFirstColumn: true, emptyDataPatternFill: 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[1][1].FillColor); + } + + [Fact] + public void ExplicitWhiteCellFill_StaysOnTopOfTableStyle() + { + using var stream = CreateWorkbookWithStyledTable(); + + var sheet = Assert.Single(ExcelReader.ReadSheets(stream)); + + Assert.Equal(PdfColor.FromRgb(0xFF, 0xFF, 0xFF), sheet.Rows[4][0].FillColor); + Assert.Equal(PdfColor.FromRgb(0xDD, 0xF4, 0xF9), sheet.Rows[4][1].FillColor); + } + + private static MemoryStream CreateWorkbookWithStyledTable(bool stripesAndFirstColumn = false, bool emptyDataPatternFill = false) + { + var flag = stripesAndFirstColumn ? "1" : "0"; + var dataDxfFill = emptyDataPatternFill ? "" : string.Empty; + 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 + 315 + 310 + + + + """); + + AddEntry(archive, "xl/worksheets/_rels/sheet1.xml.rels", + """ + + + + + """); + + AddEntry(archive, "xl/tables/table1.xml", + $""" + + + + + + + +
+ """); + + AddEntry(archive, "xl/styles.xml", + $""" + + + + + + + + + + + + + + + + + + {dataDxfFill} + + + + + + + + + + + + + """); + + 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];