Repository navigation
fix(xlsx): keep the whole-table style when a table has a data format #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shps951023
merged 5 commits into
mini-software:main
from
henry3260:fix/dotnet-render-next
Nov 25, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bdce05a
fix(xlsx): keep the whole-table style when a table has a data format
henry3260 b316db5
docs(xlsx): state the full layer order for table data rows
henry3260 ebc0263
fix(xlsx): keep a cell's own fill on top of the table style
henry3260 dedc820
test(xlsx): cover a cell's own fill under row stripes and firstColumn
henry3260 dc4ff34
Preserve empty and white table fills
shps951023 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| 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); | ||
| } | ||
|
|
||
| [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 ? "<fill><patternFill/></fill>" : string.Empty; | ||
| var ms = new MemoryStream(); | ||
| using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true)) | ||
| { | ||
| AddEntry(archive, "[Content_Types].xml", | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> | ||
| <Default Extension="xml" ContentType="application/xml"/> | ||
| <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> | ||
| <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/> | ||
| <Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/> | ||
| <Override PartName="/xl/tables/table1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml"/> | ||
| <Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/> | ||
| <Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/> | ||
| </Types> | ||
| """); | ||
|
|
||
| AddEntry(archive, "_rels/.rels", | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> | ||
| <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/> | ||
| </Relationships> | ||
| """); | ||
|
|
||
| AddEntry(archive, "xl/_rels/workbook.xml.rels", | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> | ||
| <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/> | ||
| <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/> | ||
| <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/> | ||
| </Relationships> | ||
| """); | ||
|
|
||
| AddEntry(archive, "xl/workbook.xml", | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" | ||
| xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> | ||
| <sheets> | ||
| <sheet name="Items" sheetId="1" r:id="rId1"/> | ||
| </sheets> | ||
| </workbook> | ||
| """); | ||
|
|
||
| AddEntry(archive, "xl/worksheets/sheet1.xml", | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" | ||
| xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> | ||
| <sheetData> | ||
| <row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>2</v></c></row> | ||
| <row r="2"><c r="A2" t="s"><v>3</v></c><c r="B2"><v>35</v></c><c r="C2" t="s"><v>2</v></c></row> | ||
| <row r="3"><c r="A3" t="s"><v>4</v></c><c r="B3"><v>25</v></c><c r="C3" t="s"><v>2</v></c></row> | ||
| <row r="4"><c r="A4" s="1" t="s"><v>3</v></c><c r="B4"><v>15</v></c></row> | ||
| <row r="5"><c r="A5" s="2" t="s"><v>3</v></c><c r="B5"><v>10</v></c></row> | ||
| </sheetData> | ||
| <tableParts count="1"><tablePart r:id="rId1"/></tableParts> | ||
| </worksheet> | ||
| """); | ||
|
|
||
| AddEntry(archive, "xl/worksheets/_rels/sheet1.xml.rels", | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> | ||
| <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" Target="../tables/table1.xml"/> | ||
| </Relationships> | ||
| """); | ||
|
|
||
| AddEntry(archive, "xl/tables/table1.xml", | ||
| $""" | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" | ||
| id="1" name="Items" displayName="Items" ref="A1:B5" dataDxfId="0"> | ||
| <tableColumns count="2"> | ||
| <tableColumn id="1" name="Item"/> | ||
| <tableColumn id="2" name="Qty"/> | ||
| </tableColumns> | ||
| <tableStyleInfo name="Custom" showFirstColumn="{flag}" showLastColumn="0" showRowStripes="{flag}" showColumnStripes="0"/> | ||
| </table> | ||
| """); | ||
|
|
||
| AddEntry(archive, "xl/styles.xml", | ||
| $""" | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> | ||
| <fonts count="1"><font><sz val="11"/><name val="Calibri"/></font></fonts> | ||
| <fills count="4"> | ||
| <fill><patternFill patternType="none"/></fill> | ||
| <fill><patternFill patternType="gray125"/></fill> | ||
| <fill><patternFill patternType="solid"><fgColor rgb="FFFF0000"/></patternFill></fill> | ||
| <fill><patternFill patternType="solid"><fgColor rgb="FFFFFFFF"/></patternFill></fill> | ||
| </fills> | ||
| <borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders> | ||
| <cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs> | ||
| <cellXfs count="3"> | ||
| <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/> | ||
| <xf numFmtId="0" fontId="0" fillId="2" borderId="0" xfId="0" applyFill="1"/> | ||
| <xf numFmtId="0" fontId="0" fillId="3" borderId="0" xfId="0" applyFill="1"/> | ||
| </cellXfs> | ||
| <dxfs count="4"> | ||
| <dxf><font><i/></font>{dataDxfFill}</dxf> | ||
| <dxf><fill><patternFill><bgColor rgb="FFDDF4F9"/></patternFill></fill></dxf> | ||
| <dxf><fill><patternFill><bgColor rgb="FF00B050"/></patternFill></fill></dxf> | ||
| <dxf><font><b/></font></dxf> | ||
| </dxfs> | ||
| <tableStyles count="1" defaultTableStyle="Custom"> | ||
| <tableStyle name="Custom" pivot="0" count="3"> | ||
| <tableStyleElement type="wholeTable" dxfId="1"/> | ||
| <tableStyleElement type="firstRowStripe" dxfId="2"/> | ||
| <tableStyleElement type="firstColumn" dxfId="3"/> | ||
| </tableStyle> | ||
| </tableStyles> | ||
| </styleSheet> | ||
| """); | ||
|
|
||
| AddEntry(archive, "xl/sharedStrings.xml", | ||
| """ | ||
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="5" uniqueCount="5"> | ||
| <si><t>Item</t></si><si><t>Qty</t></si><si><t>Outside</t></si><si><t>Peonies</t></si><si><t>Tulips</t></si> | ||
| </sst> | ||
| """); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.