跳至页脚内容
使用 IRONXL

如何在 Excel 中冻结行(初学者指南)

1. Introduction

Excel, with features like freezing panes, simplifies viewing content from different areas within a workbook. By freezing rows or columns, specific cells remain visible while scrolling through content. This can be achieved using the IronXL library, which offers advanced Excel operations, including a freeze-pane option.

2. Freeze and Unfreeze Rows and Columns using Excel

2.1 Freeze Rows

In your worksheet, you might want to permanently display a specific row or column, particularly the header cells. You can scroll over your material while still viewing the frozen cells by freezing rows or columns in place.

Step 1: Select the row you want to freeze or, if you want to freeze the top row, select row 1. In this case, choose row 1 to freeze it to see the header at all times.

How to Freeze a Row in Excel (Beginner Guide), Figure 1: The Excel data demonstrated for freezing rows The Excel data demonstrated for freezing rows

Step 2: Choose Freeze Panes from the drop-down menu by clicking the Freeze Panes command on the View tab.

How to Freeze a Row in Excel (Beginner Guide), Figure 2: Navigate to the Freeze Panes function in Microsoft Excel Navigate to the Freeze Panes function in Microsoft Excel

Step 3: The gray line indicates that the rows are frozen in place. The frozen rows can still be viewed at the top of the worksheet as you scroll down. In this example, scroll down to row 17, but row 1 still can be seen on the screen because it has been frozen as the top row.

How to Freeze a Row in Excel (Beginner Guide), Figure 3: Gray indication for frozen rows Gray indication for frozen rows

2.2 Freeze Columns

Step 1: Choose the column to the right of the column or columns you wish to freeze. In this case, choose column B to freeze column A.

How to Freeze a Row in Excel (Beginner Guide), Figure 4: The Excel data demonstrated for freezing columns The Excel data demonstrated for freezing columns

Step 2: Choose Freeze Panes from the drop-down menu by clicking the Freeze Panes command on the View tab.

How to Freeze a Row in Excel (Beginner Guide), Figure 5: Navigate to the Freeze Panes function in Microsoft Excel Navigate to the Freeze Panes function in Microsoft Excel

Step 3: Then, select the column to freeze, which will be shown in a gray line. The gray line indicates where the column will be frozen in place. When navigating through the worksheet, the frozen column remains visible on the left-hand side. In this example, scroll down to column G and can still see the frozen column on the left.

How to Freeze a Row in Excel (Beginner Guide), Figure 6: Gray indication for frozen columns Gray indication for frozen columns

2.3 Unfreezing Panes

If you want to select a different view option, you may need to reset the spreadsheet by unfreezing the panes. To do this, click the Freeze Panes command and then select Unfreeze Panes from the drop-down menu to unfreeze rows or columns.

How to Freeze a Row in Excel (Beginner Guide), Figure 7: Navigate to the Unfreeze Panes function in Microsoft Excel Navigate to the Unfreeze Panes function in Microsoft Excel

3. Freeze and Unfreeze Rows and Columns using IronXL

3.1 IronXL Features

IronXL is a .NET library that enables the reading and editing of Microsoft Excel documents in C#. It is a standalone .NET software library that can read multiple spreadsheet formats. The installation of Microsoft Excel or Interop is not required.

With IronXL's user-friendly C# API, it is easy to read, modify, and create Excel spreadsheet files in the .NET environment. The library provides comprehensive support for .NET Core, .NET Framework, Xamarin, Mobile, Linux, macOS, and Azure, and is among the best Excel spreadsheet libraries for these platforms.

3.2 IronXL Features

  • Data from XLS, XLSX, CSV, and TSV can be edited, read, and loaded.
  • It supports storing and exporting data to CSV, TSV, JSON, XLS, and XLSX.
  • Ranges can be easily utilized with the WorkSheet["A1:B10"] syntax. Ranges can be mixed logically.
  • Columns, rows, and ranges can be sorted.
  • Examples of styling include cell visual styles, font, size, border, alignment, lock, freeze cells, hide/unhide rows, and number formats can be applied.

3.3 Freeze Panes using IronXL

Freezing rows and columns in Excel allows them to remain fixed in the specified location on the screen while scrolling, making it easier to keep track of columns and refer to them. Below is a sample code for freezing rows:

using IronXL;

// Load the existing Excel file
WorkBook wb = WorkBook.LoadExcel("sample1.xlsx");

// Get a specific worksheet from the workbook
WorkSheet ws = wb.GetWorkSheet("Sheet1");

// Create a freeze pane at the top of the worksheet
// Parameters are column index and row index; here, it's freezing the top row
ws.CreateFreezePane(0, 1);

// Save the modified Excel file
wb.SaveAs("sample1.xlsx");
using IronXL;

// Load the existing Excel file
WorkBook wb = WorkBook.LoadExcel("sample1.xlsx");

// Get a specific worksheet from the workbook
WorkSheet ws = wb.GetWorkSheet("Sheet1");

// Create a freeze pane at the top of the worksheet
// Parameters are column index and row index; here, it's freezing the top row
ws.CreateFreezePane(0, 1);

// Save the modified Excel file
wb.SaveAs("sample1.xlsx");
Imports IronXL

' Load the existing Excel file
Private wb As WorkBook = WorkBook.LoadExcel("sample1.xlsx")

' Get a specific worksheet from the workbook
Private ws As WorkSheet = wb.GetWorkSheet("Sheet1")

' Create a freeze pane at the top of the worksheet
' Parameters are column index and row index; here, it's freezing the top row
ws.CreateFreezePane(0, 1)

' Save the modified Excel file
wb.SaveAs("sample1.xlsx")
$vbLabelText   $csharpLabel

The above code is a sample that freezes the top row of the Excel sheet.

  1. Load the Existing Excel Sheet:
    Use the method WorkBook.LoadExcel to load the Excel file into the wb object, which can perform various types of Excel processes.

  2. Select the Worksheet:
    Use GetWorkSheet to select the required worksheet by name (in this case, "Sheet1").

  3. Create Freeze Panes:
    The method CreateFreezePane is used with two parameters - one for the column position and the other for the row position. It freezes the specified columns and rows in place.

  4. Save the Workbook:
    Use the SaveAs method to save changes to a file.

Changing the values in CreateFreezePane can modify which rows and columns are frozen.

How to Freeze a Row in Excel (Beginner Guide), Figure 8: The Excel file with freeze panels function from IronXL The Excel file with freeze panes function from IronXL

3.4 Unfreezing Panes using IronXL

The RemovePane function can be used to unfreeze rows and columns in the Excel sheet.

4. Conclusion

The IronXL library is a development library that provides all the advanced capabilities required for a complex Excel application. One great thing about it is that it offers developers and users a free trial, making it easy to determine if it meets their needs. IronXL is one of the fastest libraries available, and with just a few lines of code, a developer can easily learn how to create an Excel document and perform a variety of Excel operations. To learn more about IronXL, click on this IronXL main page, or follow this tutorial about reading Excel files for additional examples.

常见问题解答

如何使用 C# 在 Excel 表中冻结行?

您可以使用 IronXL 通过加载 Excel 文件,选择工作表,然后使用 CreateFreezePane 方法来指定要冻结的行,从而在 Excel 表中冻结行。

在 Excel 中使用冻结窗格功能有什么好处?

Excel 中的冻结窗格功能允许用户在滚动时保持特定行或列可见,从而更轻松地查看和引用工作表的重要部分。IronXL 使开发人员能够在 .NET 应用程序中以编程方式实现此功能。

如何使用 C# 在 Excel 中解除冻结窗格?

要在 Excel 中使用 C# 解除冻结窗格,您可以使用 IronXL 的 RemovePane 方法。此方法将删除所有冻结的行或列,恢复工作表的默认滚动行为。

IronXL 的关键特性是什么?

IronXL 是一个强大的 .NET 库,允许读取、编辑和创建 Excel 文件,而不需要 Microsoft Excel。它支持多种文件格式,如 XLS、XLSX、CSV 和 TSV,并提供冻结窗格和数据导出等功能。

我可以免费试用 IronXL 吗?

是的,IronXL 提供免费的试用版本,让开发人员在购买前探索其功能和能力。

如何使用 IronXL 冻结 Excel 文件的第一行?

要使用 IronXL 冻结第一行,首先使用 WorkBook.LoadExcel 加载您的 Excel 文件。然后,使用 GetWorkSheet 选择您的工作表,并应用 CreateFreezePane 指定您要冻结的行索引。

IronXL 支持哪些平台?

IronXL 支持广泛的平台,包括 .NET Core、.NET Framework、Xamarin、移动设备、Linux、macOS 和 Azure。

IronXL 如何在处理 Excel 文件时提高生产力?

IronXL 通过允许开发人员以编程方式自动化 Excel 任务,如冻结窗格、编辑数据和导出文件,而不需要 Microsoft Excel,从而简化 .NET 应用程序的工作流程,从而提高生产力。

Excel 中如何指示已冻结的行或列?

在 Excel 中,灰线表示已冻结的行或列。这个视觉提示帮助用户识别在导航过程中保持可见的工作表部分。IronXL 可以通过 C# 以编程方式复制这种效果。

如果我的冻结窗格在 Excel 中不起作用,可以采取哪些故障排除步骤?

如果冻结窗格不起作用,请确保您在 IronXL 中使用了正确的方法调用。检查您是否使用 GetWorkSheet 加载了正确的工作表,并使用 CreateFreezePane 应用了正确的索引。此外,请验证您的 Excel 文件是否与您执行的操作兼容。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。