Articles in this section
Category / Section

How to set freeze panes in Excel document using C#, VB.NET?

4 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can set freeze panes in Excel document.

How to set freeze panes in Excel?

Rows and columns in an Excel document can be frozen by using the FreezePanes() method of IRange interface. To freeze the rows or columns you need to call FreezePanes() for the range next to the row or column.

For example, if you wish to freeze the first row in Excel sheet, you need to select the range A2. If you select the range B2, then the first row and first column will be frozen in the worksheet. So, you need to select the range next to the range that needs to be frozen.


Steps to set freeze panes in Excel, programmatically:

Step 1: Create a new C# console application project.

Create a new C# console application project

Create a new C# console application project

Step 2:

 Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install NuGet package to the project

Install NuGet package to the project

Step 3: Include the following namespaces in Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

Step 4: Include the following code snippet in main method of Program.cs file to set freeze panes.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the excel application object
    IApplication application = excelEngine.Excel;
 
    //Create a new Excel workbook
    IWorkbook workbook = application.Workbooks.Create(1);
                
    //Get the first worksheet of workbook into IWorksheet
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //Add data in the worksheet
    worksheet["A1"].Text = "Id";
    worksheet["B1"].Text = "Name";
    worksheet["C1"].Text = "Age";
 
    worksheet["A2"].Number = 1;
    worksheet["B2"].Text = "Andy Bernard";
    worksheet["C2"].Number = 25;
 
    worksheet["A3"].Number = 2;
    worksheet["B3"].Text = "Jim Halpert";
    worksheet["C3"].Number = 23;
 
    worksheet["A4"].Number = 3;
    worksheet["B4"].Text = "Stanley Hudson";
    worksheet["C4"].Number = 22;
 
    worksheet.Range["A5"].Number = 4;
    worksheet.Range["B5"].Text = "Karen Fillippelli";
    worksheet.Range["C5"].Number = 24;
 
    worksheet.Range["A6"].Number = 5;
    worksheet.Range["B6"].Text = "Phyllis Lapin";
    worksheet.Range["C6"].Number = 27;
 
    //Autofit the coulmns in used range
    worksheet.UsedRange.AutofitColumns();
 
    //Apply freeze panes
    worksheet.Range["A2"].FreezePanes();
 
    //Save the Excel document
    workbook.SaveAs("Output.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the excel application object
    Dim application As IApplication = excelEngine.Excel
 
    'Create a new Excel workbook
    Dim workbook As IWorkbook = application.Workbooks.Create(1)
 
    'Get the first worksheet of workbook into IWorksheet
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Add data in the worksheet
    worksheet("A1").Text = "Id"
    worksheet("B1").Text = "Name"
    worksheet("C1").Text = "Age"
 
    worksheet("A2").Number = 1
    worksheet("B2").Text = "Andy Bernard"
    worksheet("C2").Number = 25
 
    worksheet("A3").Number = 2
    worksheet("B3").Text = "Jim Halpert"
    worksheet("C3").Number = 23
 
    worksheet("A4").Number = 3
    worksheet("B4").Text = "Stanley Hudson"
    worksheet("C4").Number = 22
 
    worksheet.Range("A5").Number = 4
    worksheet.Range("B5").Text = "Karen Fillippelli"
    worksheet.Range("C5").Number = 24
 
    worksheet.Range("A6").Number = 5
    worksheet.Range("B6").Text = "Phyllis Lapin"
    worksheet.Range("C6").Number = 27
 
    'Autofit the coulmns in used range
    worksheet.UsedRange.AutofitColumns()
 
    'Apply freeze panes
    worksheet.Range("A2").FreezePanes()
 
    'Save the Excel document
    workbook.SaveAs("Output.xlsx")
End Using

 

A complete working sample of how to set freeze panes in Excel document can be downloaded from Freeze-Panes.zip.

By executing the program, you will get the output Excel file as follows.


Set freeze panes in Excel document

Output Excel document

Take a moment to peruse the documentation, where you will find other options like move or copy a worksheet, show or hide worksheet and worksheet tabspage setup settings and more with code examples.


See Also:

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.


Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


Conclusion

I hope you enjoyed learning about how to set freeze panes in Excel document using .NET Excel Library.

You can refer to our .NET Excel library page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET Excel library example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or  feedbackportal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied