How to lock the top row of a spreadsheet

Hello, I have tried all of the examples of locking the top row that I found in your forum, but none of them work. For example:

Many of the examples used deprecated code. Please provide me with an up-to-date working example.

Also, we do NOT want our workbook to be locked/protected. We only want the top row inside the spreadsheet to be locked to prevent scrolling.

Many thanks and kind regards,
Sheridan

@workshare,

You may please use Worksheet.FreezePanes() function to freeze first row.

// Opening the Excel file through the file stream
Workbook workbook = new Workbook();

// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];

// Applying freeze panes settings
worksheet.FreezePanes(1, 0, 1, 0);

// Saving the modified Excel file
workbook.Save("file.xls");

Thank you for this much simpler method. :+1:

I also need to verify that the top row is locked in a unit test now. Can you please show me how I can detect whether the top row is locked or not?

@workshare,

You can use the Worksheet.GetFreezedPanes() to check if the worksheet contains frozen cells (in rows/cols). GetFreezedPanes method would return a Boolean value whether the specified cell (with the underlying row/column indices, etc.) are locked as frozen panes. See the sample code for your reference:
e.g
Sample code:

// Opening the Excel file through the file stream
            Workbook workbook = new Workbook();

            // Accessing the first worksheet in the Excel file
            Worksheet worksheet = workbook.Worksheets[0];

            // Applying freeze panes settings
            worksheet.FreezePanes(1, 0, 1, 0);

            int row, col, numrows, numcols;
            bool ch = worksheet.GetFreezedPanes( out row, out col, out numrows, out numcols);

            Console.WriteLine(row);
            Console.WriteLine(col);
            Console.WriteLine(numrows);
            Console.WriteLine(numcols);

..........

Hope, this helps a bit.

Thank you, that was exactly what I was after. :slight_smile:

@workshare,

Good to know that your issue is sorted out by the suggested code. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.