Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Key Points Explained:
Further Reading: How to Add Freeze Pane