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

前端 未结 5 1317
轮回少年
轮回少年 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);
    
    0 讨论(0)
  • 2021-02-02 06:31

    Try this...

    workSheet.Activate();
    workSheet.Application.ActiveWindow.SplitRow = 1;
    workSheet.Application.ActiveWindow.FreezePanes = true;
    
    0 讨论(0)
  • 2021-02-02 06:39

    //path were excel file is kept string ResultsFilePath = @"C:\Users\krakhil\Desktop\FolderName\FileNameWithoutExtension";

            Excel.Application ExcelApp = new Excel.Application();
            Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(ResultsFilePath);
            ExcelApp.Visible = true;
    
            //Looping through all available sheets
            foreach (Excel.Worksheet ExcelWorksheet in ExcelWorkbook.Sheets)
            {                
                //Selecting the worksheet where we want to perform action
                ExcelWorksheet.Select(Type.Missing);
    
                //Making sure first row is selected - else split and freeze will happen
                //On the visible part and not from the top
                Excel.Range activeCell = ExcelWorksheet.Cells[1, 1];
                activeCell.Select();
    
                //Applying auto filter to Row 10
                activeCell = (Excel.Range)ExcelWorksheet.Rows[10];
                activeCell.AutoFilter(1,
                    Type.Missing,
                    Excel.XlAutoFilterOperator.xlAnd,
                    Type.Missing,
                    true);
    
                //Split the pane and freeze it
                ExcelWorksheet.Application.ActiveWindow.SplitRow = 10;
                ExcelWorksheet.Application.ActiveWindow.FreezePanes = true;
    
                //Auto fit all columns
                ExcelWorksheet.Columns.AutoFit();
    
                //Releasing range object
                Marshal.FinalReleaseComObject(activeCell);
            }
    
            //saving excel file using Interop
            ExcelWorkbook.Save();
    
            //closing file and releasing resources
            ExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
            Marshal.FinalReleaseComObject(ExcelWorkbook);
            ExcelApp.Quit();
            Marshal.FinalReleaseComObject(ExcelApp);
    
    0 讨论(0)
  • 2021-02-02 06:41
    workSheet.EnableAutoFilter = true; 
    workSheet.Cells.AutoFilter(1); 
    
    //Set the header-row bold
    workSheet.Range["A1", "A1"].EntireRow.Font.Bold = true;  
    
    //Adjust all columns
    workSheet.Columns.AutoFit(); 
    

    There could be some System.Reflection.Missing.Value that need to be passed with the arguments, but this was VB.Net code I've converted out of my mind.

    0 讨论(0)
  • 2021-02-02 06:48

    The below solutions are working fine, but it is freezing the first row of the current visible snapshot of the sheet. For ex: If your current sheet visible snapshot is from row 43 to some number say 90.. then freeze row is getting applied to 43.

    If you want only the very first row of sheet (heading row) to be frozen, no matter the excel scroll position, then the below solution worked for me. This code scrolls up the excel sheet to row 1. You have to store the position if you want to go back to the previous position before freeze.

    worksheet.Application.ActiveWindow.ScrollRow = 1;
    worksheet.Application.ActiveWindow.SplitRow = 1;
    worksheet.Application.ActiveWindow.FreezePanes = true; 
    
    0 讨论(0)
提交回复
热议问题