How to Add Freeze Panes to Spreadsheets in Excel

In this tutorial, we explore the process of adding freeze panes to Excel spreadsheets using C# and the IronXL library. The tutorial begins with installing IronXL via the NuGet package manager. Once set up, we delve into a pre-written code in the program.cs file. The code starts by importing the IronXL namespace and loading an existing Excel workbook into a workbook object. From this workbook, the first worksheet is extracted and stored in a worksheet object. To create a freeze pane, the 'create freeze pane' method is employed, which requires specifying the number of columns and rows to freeze. After modifying the worksheet, the workbook is saved with the 'save as' function. Additionally, a commented line of code is highlighted for removing existing freeze or split panes if necessary. Upon execution, the project successfully applies freeze panes to columns A and B and rows 1-3, ensuring that these sections remain visible when scrolling. This feature enhances data readability and accessibility. The tutorial concludes with a suggestion to explore a trial subscription to experience the full capabilities of the software.

Here is the corrected and commented C# code snippet:

// Import the necessary namespaces
using IronXL;

class Program
{
    static void Main()
    {
        // Load an existing Excel workbook
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Select the first worksheet from the workbook
        WorkSheet worksheet = workbook.WorkSheets.First();

        // Create a freeze pane at column 2 and row 4 (indices are zero-based)
        // This means columns A and B and rows 1, 2, and 3 will be frozen
        worksheet.CreateFreezePane(2, 4);

        // Uncomment the following line if you want to remove any existing freeze or split panes
        // worksheet.ClearPanes();

        // Save the workbook as a new file
        workbook.SaveAs("example_with_freeze_pane.xlsx");
    }
}  
// Import the necessary namespaces
using IronXL;

class Program
{
    static void Main()
    {
        // Load an existing Excel workbook
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Select the first worksheet from the workbook
        WorkSheet worksheet = workbook.WorkSheets.First();

        // Create a freeze pane at column 2 and row 4 (indices are zero-based)
        // This means columns A and B and rows 1, 2, and 3 will be frozen
        worksheet.CreateFreezePane(2, 4);

        // Uncomment the following line if you want to remove any existing freeze or split panes
        // worksheet.ClearPanes();

        // Save the workbook as a new file
        workbook.SaveAs("example_with_freeze_pane.xlsx");
    }
}  
' Import the necessary namespaces
Imports IronXL

Friend Class Program
	Shared Sub Main()
		' Load an existing Excel workbook
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")

		' Select the first worksheet from the workbook
		Dim worksheet As WorkSheet = workbook.WorkSheets.First()

		' Create a freeze pane at column 2 and row 4 (indices are zero-based)
		' This means columns A and B and rows 1, 2, and 3 will be frozen
		worksheet.CreateFreezePane(2, 4)

		' Uncomment the following line if you want to remove any existing freeze or split panes
		' worksheet.ClearPanes();

		' Save the workbook as a new file
		workbook.SaveAs("example_with_freeze_pane.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Key Points Explained:

  • WorkBook.Load Method: This is used to open the existing Excel file into the application.
  • WorkSheets.First(): Retrieves the first worksheet in the workbook.
  • CreateFreezePane Method: It takes two parameters specifying the number of columns (1-based) and rows (1-based) to freeze.
  • ClearPanes Method (commented): This can be used to remove any existing freeze or split panes from a worksheet.
  • SaveAs Method: Saves the modified workbook to a new file, preserving the original.

Further Reading: How to Add Freeze Pane

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Add Rows and Columns in Excel as a C# Developer
NEXT >
How to Add, Extract, and Remove Images from Worksheets