如何在C#中创建Excel文件

How to Add Freeze Pane

This article was translated from English: Does it need improvement?
Translated
View the article in English

In a large data sheet table, it can be challenging to view the data in 50+ rows or columns beyond the 'Z' column while keeping the corresponding headers in view. The Freeze Pane functionality provides a clever solution to this issue.

Quickstart: Lock Header Rows and Columns in One Line

Use the simple CreateFreezePane(colSplit, rowSplit) method to freeze rows or columns in seconds. No setup fuss — 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


Add Freeze Pane Example

Freeze panes is an option to lock rows and columns in place, allowing them to remain visible while scrolling. It is a very useful feature for keeping the header column or row in place while quickly comparing information.

CreateFreezePane(int column, int row)

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) will create a freeze pane starting from column A and rows 1 to 4.

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

Demonstration

Freeze Pane in Action

Remove Freeze Pane

Use the RemovePane method to quickly remove all existing freeze panes from your spreadsheet.

: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

Advanced Freeze Pane Example

The CreateFreezePane method offers an advanced option to create freeze panes with pre-scrolling functionality.

CreateFreezePane(int column, int row, int subsequentColumn, int subsequentRow)

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.

For instance, by using workSheet.CreateFreezePane(5, 2, 6, 7), you can create a freeze pane that spans columns A-E and rows 1-2. It includes a 1-column and 5-row scroll. When the worksheet is first opened, it will display 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

Demonstration

Advanced Freeze Panes Demonstration

请注意Only 1 setting of freeze pane 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).

常见问题解答

如何使用C#向电子表格添加冻结窗格?

要在C#中向电子表格添加冻结窗格,可以使用IronXL的CreateFreezePane方法。指定起始列和行,例如workSheet.CreateFreezePane(1, 4),这将锁定第1到4行和第A列。

在Excel中使用冻结窗格的目的是什么?

冻结窗格用于将特定的行或列锁定在原位,使其在滚动电子表格的其他部分时仍可见。这在导航大型数据集时特别有用,以保持标题或关键数据的可见性。

如何以编程方式从电子表格中移除冻结窗格?

要以编程方式使用IronXL移除冻结窗格,可以使用RemovePane方法,这将清除电子表格中的所有现有冻结窗格。

冻结窗格可以应用于旧版Excel文件格式吗?

不,使用IronXL创建的冻结窗格不适用于版本97-2003(.xls)的Microsoft Excel文件格式。

在单个工作表中可以创建多个冻结窗格吗?

不,在一个工作表中只能一次应用一个冻结窗格。任何新的冻结窗格创建都会覆盖之前的。

如何创建具有预滚动功能的冻结窗格?

IronXL允许您使用具有四个参数的CreateFreezePane方法创建带有预滚动的冻结窗格。例如,workSheet.CreateFreezePane(5, 2, 6, 7)创建一个冻结窗格,从列A-E和行1-2开始,在打开电子表格时滚动从列G和行8开始。

添加冻结窗格后可以导出哪些文件格式?

使用IronXL添加冻结窗格后,您可以使用IronXL的导出功能将电子表格导出为各种文件格式,如XLSX、CSV等。

如何安装必要的库以在C#中使用冻结窗格?

您需要从NuGet安装IronXL库,以使用C#中的冻结窗格功能。访问NuGet网站并搜索IronXL.Excel以下载并安装它。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布