最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Export to Excel head row format filter - Stack Overflow

programmeradmin0浏览0评论

Tell me how to add the filtering option to the head row as in the screenshot? Using Visual studio, SDK DocumentFormat.OpenXML.

 WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
 var sheetData = new SheetData();
 worksheetPart.Worksheet = new Worksheet(sheetData);
 
 Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), 
 SheetId = sheetId, Name = "Equipment" };
 sheets.AppendChild(sheet);
 
 Row headerRow = new Row();
 
 InsertCell(headerRow, 1, "Column 10", CellValues.String);
 InsertCell(headerRow, 2, "Column 20", CellValues.String);

Tell me how to add the filtering option to the head row as in the screenshot? Using Visual studio, SDK DocumentFormat.OpenXML.

 WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
 var sheetData = new SheetData();
 worksheetPart.Worksheet = new Worksheet(sheetData);
 
 Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), 
 SheetId = sheetId, Name = "Equipment" };
 sheets.AppendChild(sheet);
 
 Row headerRow = new Row();
 
 InsertCell(headerRow, 1, "Column 10", CellValues.String);
 InsertCell(headerRow, 2, "Column 20", CellValues.String);

Share Improve this question edited Mar 18 at 6:08 Jack J Jun 6,0061 gold badge13 silver badges44 bronze badges asked Mar 3 at 15:33 Mikhail ZhuikovMikhail Zhuikov 1,2572 gold badges9 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Just need to create AutoFilter object and append to sheet. You could try following sample:

        string filePath = "FilteredExcel.xlsx";

        using (SpreadsheetDocument document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
        {
            WorkbookPart workbookPart = document.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            SheetData sheetData = new SheetData();

            // Define header row
            sheetData.Append(new Row(
                new Cell() { CellReference = "A1", DataType = CellValues.String, CellValue = new CellValue("Name") },
                new Cell() { CellReference = "B1", DataType = CellValues.String, CellValue = new CellValue("Age") },
                new Cell() { CellReference = "C1", DataType = CellValues.String, CellValue = new CellValue("City") }
            ));

            // Append data rows directly
            sheetData.Append(new Row(
                new Cell() { CellReference = "A2", DataType = CellValues.String, CellValue = new CellValue("Alice") },
                new Cell() { CellReference = "B2", DataType = CellValues.String, CellValue = new CellValue("30") },
                new Cell() { CellReference = "C2", DataType = CellValues.String, CellValue = new CellValue("New York") }
            ));

            sheetData.Append(new Row(
                new Cell() { CellReference = "A3", DataType = CellValues.String, CellValue = new CellValue("Bob") },
                new Cell() { CellReference = "B3", DataType = CellValues.String, CellValue = new CellValue("25") },
                new Cell() { CellReference = "C3", DataType = CellValues.String, CellValue = new CellValue("London") }
            ));


            worksheetPart.Worksheet = new Worksheet(sheetData);

            // Add AutoFilter to the first row (A1:C1)
            AutoFilter autoFilter = new AutoFilter() { Reference = "A1:C1" };
            worksheetPart.Worksheet.Append(autoFilter);
            worksheetPart.Worksheet.Save();

            Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
            Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
            sheets.Append(sheet);

            workbookPart.Workbook.Save();
        }

        Console.WriteLine("Excel file with filtering created successfully!");
发布评论

评论列表(0)

  1. 暂无评论