How to Freeze Top Row and Apply Filter in Excel Automation with C#

前端 未结 5 1322
轮回少年
轮回少年 2021-02-02 06:24

I have automation to create an Excel document from C#. I am trying to freeze the top row of my worksheet and apply filter. This is the same as in Excel 2010 if you select View >

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 06:27

    I figured it out!

    @Jaime's solution to freezing the top row worked perfectly. And the following is my solution to applying the filter:

    Thanks, KBP

    // Fix first row
    workSheet.Activate();
    workSheet.Application.ActiveWindow.SplitRow = 1;
    workSheet.Application.ActiveWindow.FreezePanes = true;
    // Now apply autofilter
    Excel.Range firstRow = (Excel.Range)workSheet.Rows[1];
    firstRow.AutoFilter(1, 
                        Type.Missing, 
                        Excel.XlAutoFilterOperator.xlAnd, 
                        Type.Missing, 
                        true);
    

提交回复
热议问题