SoFunction
Updated on 2025-03-07

C# implements freezing Excel windows to lock rows or unfreeze

When working with large Excel workbooks, sometimes we need to freeze the pane in the worksheet so that we can keep some rows or columns fixed while scrolling to view the data. Freezing the pane can help us navigate and understand complex datasets more easily. Instead, when you don't need to freeze panes, you may need to thaw them to get a full view.

How to use it is described belowFree .NET libraryFreeze Excel windows through C# to lock rows and columns, and how to unfreeze.

Free .NET Excel library

The implementation solution provided in this article requiresFree for .NETThis free library. This library can quickly implement various operations on Excel documents in .NET applications. Can be passedNugetInstall directly, ordownloadAfter manually referring to Dll.

PM> Install-Package 

C# Freeze Excel window (freeze first row, first column, multiple rows and multiple columns)

Free for .NET free library provides(int rowIndex, int columnIndex)Method to freeze Excel rows and columns. Two of these parameters:

  • rowIndex: Indicates the index of the row (index starts at 1), all rows above the row will be frozen.
  • columnIndex: Indicates the index of the column (index starts at 1), all columns to the left of the column will be frozen.

Therefore, to freeze the first lineFreezePanes(2, 1), freeze the first column asFreezePanes(1, 2), and at the same time, the first column of the first row isFreezePanes(2, 2), and so on.

The following is a C# sample code for freezing Excel rows or columns:

using ;
 
namespace FreezeTopRow
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Excel document            Workbook workbook = new Workbook();
            (@"C:\Users\Administrator\Desktop\Test.xlsx");
 
            //Get the first worksheet            Worksheet sheet = [0];
 
            //Frozen the first line            (2, 1);
 
            //Frozen the first column            //(1, 2);
 
            //Frozen the first row first column           //(2, 2);
 
            //Frozen the first three lines            //(4, 1);
 
            //Save the file            ("Frozen Excel ranks.xlsx", ExcelVersion.Version2016);
        }
    }
}

C# Unfreeze Excel rows and columns

To unfreeze, use it directly()The method is just the following example code:

using ;
namespace UnfreezeExcelPane
{
    class Program
    {
 
        static void Main(string[] args)
        {
            //Load Excel file            Workbook workbook = new Workbook();
            ("Frozen ranks.xls");
 
            //Get the first worksheet            Worksheet sheet = [0];
 
            //Unfreeze rows or columns in the worksheet            ();
 
            //Save the file            ("Thaw Excel ranks.xlsx", ExcelVersion.Version2016);
 
        }
    }
}

This is the article about C# freezing Excel window to lock rows or unfreeze. For more related content on C# freezing Excel window, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!