How to Create an Excel File in C#

How to Add Freeze Pane in Excel with C#

In large Excel spreadsheets with 50+ rows or columns beyond 'Z', viewing data while keeping headers visible becomes challenging. The Freeze Pane functionality in C# provides an elegant solution by locking specific rows and columns in place while allowing the rest to scroll freely.

This feature becomes essential when working with financial reports, employee databases, or inventory lists where you need constant visibility of column headers or row identifiers. With IronXL's Excel library, you can programmatically add freeze panes to improve data navigation and user experience in your .NET applications.

Quickstart: Lock Header Rows and Columns in One Line

Use the simple CreateFreezePane(colSplit, rowSplit) method to freeze rows or columns in seconds. No complex setup — just load your sheet, call this method, and your headers stay locked at the top while you scroll.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    workSheet.CreateFreezePane(1, 4);
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


How Do I Add Freeze Pane in Excel?

Freeze panes lock rows and columns in place, allowing them to remain visible while scrolling. This feature keeps header columns or rows in place while quickly comparing information. This functionality is particularly valuable when working with large datasets or when you need to maintain context while navigating through extensive spreadsheets.

The freeze pane feature in IronXL mimics Excel's native functionality, making it intuitive for developers familiar with Excel's interface. Unlike Excel Interop solutions, IronXL provides a more efficient and server-friendly approach to implementing freeze panes programmatically.

How Does CreateFreezePane Work with 2 Parameters?

To add a freeze pane, use the CreateFreezePane method, specifying the column and row from which the freeze pane should start. The specified column and row are not included in the freeze pane. For example, workSheet.CreateFreezePane(1, 4) creates a freeze pane starting from column A and rows 1 to 4.

Understanding zero-based indexing is crucial: column 0 refers to column A, column 1 to B, and so on. Row indexing follows the same pattern. This method is perfect for scenarios where you want to keep headers visible while scrolling through data entries.

The code example below demonstrates how to create a freeze pane starting from column B and row 4:

:path=/static-assets/excel/content-code-examples/how-to/add-freeze-panes-add.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.WorkSheets.First();

// Create freeze pane from column(A-B) and row(1-3)
workSheet.CreateFreezePane(2, 3);

workBook.SaveAs("createFreezePanes.xlsx");
Imports IronXL
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets.First()

' Create freeze pane from column(A-B) and row(1-3)
workSheet.CreateFreezePane(2, 3)

workBook.SaveAs("createFreezePanes.xlsx")
$vbLabelText   $csharpLabel

What Does Freeze Pane Look Like in Action?

Freeze Pane in Action

How Do I Remove Freeze Pane?

Use the RemovePane method to remove all existing freeze panes from your spreadsheet. This is useful when you need to reset the view or apply different freeze settings based on user preferences or data changes.

:path=/static-assets/excel/content-code-examples/how-to/add-freeze-panes-remove.cs
// Remove all existing freeze or split pane
workSheet.RemovePane();
' Remove all existing freeze or split pane
workSheet.RemovePane()
$vbLabelText   $csharpLabel

What Are the Advanced Freeze Pane Options?

The CreateFreezePane method offers an advanced option to create freeze panes with pre-scrolling functionality. This feature is useful when you want to focus attention on a specific area of the spreadsheet while maintaining the freeze pane functionality.

When Should I Use 4 Parameters for Advanced Freeze Panes?

This method allows you to add a freeze pane based on the specified starting column and row. Additionally, it enables you to apply scrolling to the worksheet. This is especially beneficial when working with formatted Excel reports where you need precise control over the initial view.

For instance, using workSheet.CreateFreezePane(5, 2, 6, 7) creates a freeze pane that spans columns A-E and rows 1-2. It includes a 1-column and 5-row scroll. When the worksheet opens, it displays columns A-E, G-... and rows 1-2, 8-....

:path=/static-assets/excel/content-code-examples/how-to/add-freeze-panes-advance.cs
using IronXL;
using System.Linq;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.WorkSheets.First();

// Overwriting freeze or split pane to column(A-E) and row(1-5) as well as applying prescroll
// The column will show E,G,... and the row will show 5,8,...
workSheet.CreateFreezePane(5, 5, 6, 7);

workBook.SaveAs("createFreezePanes.xlsx");
Imports IronXL
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets.First()

' Overwriting freeze or split pane to column(A-E) and row(1-5) as well as applying prescroll
' The column will show E,G,... and the row will show 5,8,...
workSheet.CreateFreezePane(5, 5, 6, 7)

workBook.SaveAs("createFreezePanes.xlsx")
$vbLabelText   $csharpLabel

What Does Advanced Freeze Pane Look Like?

Excel freeze panes demo showing employee data with frozen headers and ID column, blue arrow indicates freeze boundary

Practical Use Cases for Freeze Panes

Freeze panes are invaluable in various business scenarios:

  1. Financial Reports: Keep month/quarter headers visible while scrolling through yearly data
  2. Employee Databases: Lock employee names and IDs while viewing performance metrics
  3. Inventory Management: Fix product codes and names while reviewing stock levels
  4. Sales Dashboards: Maintain visibility of product categories while analyzing regional sales data

When combined with Excel formulas, freeze panes enhance data analysis efficiency significantly.

Complete Example: Building a Data Report with Freeze Panes

Here's a comprehensive example showing how to create a formatted report with freeze panes:

using IronXL;
using IronXL.Styles;

// Create a new workbook
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.CreateWorkSheet("Sales Report");

// Add headers
workSheet["A1"].Value = "Product ID";
workSheet["B1"].Value = "Product Name";
workSheet["C1"].Value = "Q1 Sales";
workSheet["D1"].Value = "Q2 Sales";
workSheet["E1"].Value = "Q3 Sales";
workSheet["F1"].Value = "Q4 Sales";
workSheet["G1"].Value = "Total";

// Style headers
var headerRange = workSheet["A1:G1"];
headerRange.Style.Font.Bold = true;
headerRange.Style.BackgroundColor = "#4472C4";
headerRange.Style.Font.Color = "#FFFFFF";

// Add sample data
for (int i = 2; i <= 50; i++)
{
    workSheet[$"A{i}"].Value = $"P{i-1:D3}";
    workSheet[$"B{i}"].Value = $"Product {i-1}";
    workSheet[$"C{i}"].Value = Random.Shared.Next(1000, 5000);
    workSheet[$"D{i}"].Value = Random.Shared.Next(1000, 5000);
    workSheet[$"E{i}"].Value = Random.Shared.Next(1000, 5000);
    workSheet[$"F{i}"].Value = Random.Shared.Next(1000, 5000);
    workSheet[$"G{i}"].Formula = $"=SUM(C{i}:F{i})";
}

// Apply freeze pane to keep headers visible
workSheet.CreateFreezePane(0, 1);

// Auto-size columns for better visibility
workSheet.AutoSizeColumn(0, 6);

// Save the workbook
workBook.SaveAs("SalesReportWithFreezePanes.xlsx");
using IronXL;
using IronXL.Styles;

// Create a new workbook
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.CreateWorkSheet("Sales Report");

// Add headers
workSheet["A1"].Value = "Product ID";
workSheet["B1"].Value = "Product Name";
workSheet["C1"].Value = "Q1 Sales";
workSheet["D1"].Value = "Q2 Sales";
workSheet["E1"].Value = "Q3 Sales";
workSheet["F1"].Value = "Q4 Sales";
workSheet["G1"].Value = "Total";

// Style headers
var headerRange = workSheet["A1:G1"];
headerRange.Style.Font.Bold = true;
headerRange.Style.BackgroundColor = "#4472C4";
headerRange.Style.Font.Color = "#FFFFFF";

// Add sample data
for (int i = 2; i <= 50; i++)
{
    workSheet[$"A{i}"].Value = $"P{i-1:D3}";
    workSheet[$"B{i}"].Value = $"Product {i-1}";
    workSheet[$"C{i}"].Value = Random.Shared.Next(1000, 5000);
    workSheet[$"D{i}"].Value = Random.Shared.Next(1000, 5000);
    workSheet[$"E{i}"].Value = Random.Shared.Next(1000, 5000);
    workSheet[$"F{i}"].Value = Random.Shared.Next(1000, 5000);
    workSheet[$"G{i}"].Formula = $"=SUM(C{i}:F{i})";
}

// Apply freeze pane to keep headers visible
workSheet.CreateFreezePane(0, 1);

// Auto-size columns for better visibility
workSheet.AutoSizeColumn(0, 6);

// Save the workbook
workBook.SaveAs("SalesReportWithFreezePanes.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This example demonstrates how freeze panes work with cell styling and formulas to create professional reports. The header row remains visible as users scroll through the 50 rows of sales data.

Performance Considerations

When implementing freeze panes in large spreadsheets:

  • Apply freeze panes after populating data for optimal performance
  • Consider using conditional formatting to highlight important data in frozen sections
  • Test with your target data volume to ensure smooth scrolling performance

For applications handling extensive datasets, explore exporting to different formats or implementing pagination strategies alongside freeze panes.

Please noteOnly one freeze pane setting can be applied. Any additional creation of freeze pane will overwrite the previous one. Freeze pane does not work with Microsoft Excel versions 97-2003 (.xls).

Frequently Asked Questions

What is the freeze pane feature and why is it useful in Excel spreadsheets?

The freeze pane feature locks specific rows and columns in place, keeping them visible while scrolling through large spreadsheets. This is particularly useful for maintaining visibility of headers in financial reports, employee databases, or inventory lists. IronXL provides a simple CreateFreezePane method to implement this functionality programmatically in C# applications.

How do I quickly add freeze panes to lock header rows in C#?

With IronXL, you can lock header rows in just one line of code using the CreateFreezePane method. Simply call workSheet.CreateFreezePane(1, 4) to freeze columns and rows. This locks column A and rows 1-4 in place while allowing the rest of the spreadsheet to scroll freely.

What's the difference between using 2 parameters vs 4 parameters with CreateFreezePane?

IronXL's CreateFreezePane method offers two options: using 2 parameters (colSplit, rowSplit) creates a basic freeze pane from the specified position, while using 4 parameters allows you to add freeze panes with pre-scrolled positioning for more advanced control over the viewing area.

How does the zero-based indexing work when setting freeze panes?

In IronXL's CreateFreezePane method, indexing is zero-based. Column 0 refers to column A, column 1 to column B, and so on. Similarly, row indexing starts at 0. For example, CreateFreezePane(1, 4) creates a freeze pane starting from column A and includes rows 1 to 4.

Why should I use this library instead of Excel Interop for freeze panes?

IronXL provides a more efficient and server-friendly approach compared to Excel Interop solutions. It doesn't require Excel to be installed on the server, offers better performance for large datasets, and provides an intuitive API that mimics Excel's native functionality while being optimized for .NET applications.

Can I export spreadsheets with freeze panes to different file formats?

Yes, after adding freeze panes using IronXL's CreateFreezePane method, you can export your spreadsheet to various file formats while maintaining the freeze pane functionality. The library preserves these settings when saving to supported Excel formats.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Ready to Get Started?
Nuget Downloads 1,753,501 | Version: 2025.12 just released