Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit 837219a

Browse files
authored
1 parent 8982017 commit 837219a

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/Simplexcel.TestApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static void Main()
138138
populatedSheet.Populate(EnumeratePopulateTestData(), cacheTypeColumns: true);
139139
wb.Add(populatedSheet);
140140

141-
var frozenTopRowSheet = new Worksheet("Frozen Top Row");
141+
var frozenTopRowSheet = new Worksheet("Frozen Top Row") { AutoFilter = true };
142142
frozenTopRowSheet.Cells[0, 0] = "Header 1";
143143
frozenTopRowSheet.Cells[0, 1] = "Header 2";
144144
frozenTopRowSheet.Cells[0, 2] = "Header 3";

src/Simplexcel/Worksheet.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public ColumnWidthCollection ColumnWidths
9494
/// </summary>
9595
public LargeNumberHandlingMode LargeNumberHandlingMode { get; set; }
9696

97+
/// <summary>
98+
/// Whether to enable the AutoFilter feature on the first non-empty row.
99+
/// <para/>
100+
/// Defaults to <see langword="false"/>.
101+
/// </summary>
102+
/// <remarks>See <a href="https://support.microsoft.com/en-us/office/use-autofilter-to-filter-your-data-7d87d63e-ebd0-424b-8106-e2ab61133d92">Use AutoFilter to filter your data</a></remarks>
103+
public bool AutoFilter { get; set; }
104+
97105
/// <summary>
98106
/// Get the cell with the given cell reference, e.g. Get the cell "A1". May return NULL.
99107
/// </summary>

src/Simplexcel/XlsxInternal/XlsxWriter.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,10 @@ private static Relationship CreateSheetFile(Worksheet sheet, int sheetIndex, Rel
473473
}
474474

475475
var sheetData = new XElement(Namespaces.workbook + "sheetData");
476+
XlsxRow firstRow = null;
476477
foreach (var row in rows.OrderBy(rk => rk.Key))
477478
{
479+
firstRow ??= row.Value;
478480
var re = new XElement(Namespaces.workbook + "row", new XAttribute("r", row.Value.RowIndex));
479481
foreach (var cell in row.Value.Cells)
480482
{
@@ -498,6 +500,15 @@ private static Relationship CreateSheetFile(Worksheet sheet, int sheetIndex, Rel
498500
}
499501
doc.Root.Add(sheetData);
500502

503+
var firstRowCells = firstRow?.Cells;
504+
if (sheet.AutoFilter && firstRowCells?.Count > 0)
505+
{
506+
var firstRef = firstRowCells.First().Reference;
507+
var lastRef = firstRowCells.Last().Reference;
508+
var autoFilter = new XElement(Namespaces.workbook + "autoFilter", new XAttribute("ref", $"{firstRef}:{lastRef}"));
509+
doc.Root.Add(autoFilter);
510+
}
511+
501512
sheetRels = null;
502513
var hyperlinks = sheet.Cells.Where(c => c.Value != null && !string.IsNullOrEmpty(c.Value.Hyperlink)).ToList();
503514
if (hyperlinks.Count > 0)

0 commit comments

Comments
 (0)