Home » excel » c# – How to pin the header row in Excel in .NET?

c# – How to pin the header row in Excel in .NET?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am creating an Excel spreadsheet in C#. I like to have the header (first) row pinned in place when the user scrolls the rows. How can I do this in C# (or VB.NET)?

How to&Answers:

I know that this not a fully detailed answer, but it should help you in the right direction. When I previously did a lot of Perl and later Ruby automation of Excel and wanted to know how to achieve this and that I usually recorded a macro and inspected its code to see how VBA interacted with the objects. I also did so for your task and this is what I got:

Sub Makro1()
'
' Makro1 Makro
'

'
    ActiveWindow.SplitRow = 1.1
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
End Sub

I will leave it to somebody else to translate into C#, but it should be a walk in the park.

Answer:

This should do it in C#.

Private freezePain ()
{
    ...initialize objects

    ExcelObject.ActiveWindow.FreezePanes = false;
    WorksheetObject.get_Range(yourRange).Select();
    ExcelObject.ActiveWindow.FreezePanes = true;
}