Articles in this section
Category / Section

Unlock cells and protect the worksheet for formatting columns in C#, VB.NET

3 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can unlock a range of cells in worksheet and then protect it for formatting cells.

Locking and unlocking of cells will be effective only when the worksheet is protected. But to unlock the cells with protection for formatting columns, the range of cells must first be unlocked and then the sheet must be protected with Select locked cells, Select unlocked cells options along with Format columns option.

Steps to unlock a range of cells in which the sheet is protected for formatting columns, programmatically:

Step 1: Create a new C# console application project.

Create a new C# console application project

Create a new C# console application project

Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install NuGet package to the project

Install NuGet package to the project

Step 3: You can unlock the cells in a worksheet by disabling the Locked property available under CellStyle.

C#

//Unlock a range of cells in worksheet
worksheet.Range["A1:A10"].CellStyle.Locked = false;

 

VB.NET

'Unlock a range of cells in worksheet
worksheet.Range("A1:A10").CellStyle.Locked = False

 

Step 4: You can protect a worksheet using the Protect method, with different sheet protection options available under ExcelSheetProtection enum.

C#

//Protect the worksheet
worksheet.Protect("syncfusion", ExcelSheetProtection.LockedCells | ExcelSheetProtection.UnLockedCells | ExcelSheetProtection.FormattingColumns);

 

VB.NET

'Protect the worksheet
worksheet.Protect("syncfusion", (ExcelSheetProtection.LockedCells Or (ExcelSheetProtection.UnLockedCells Or ExcelSheetProtection.FormattingColumns)))

 

Step 5: Include the following namespaces in Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

Step 6: Include the following code snippet in main method of Program.cs file to unlock cells in a worksheet and then protect it for formatting columns.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate excel application object
    IApplication application = excelEngine.Excel;
 
    //Create a new Excel workbook
    IWorkbook workbook = application.Workbooks.Create(1);
 
    //Get the first worksheet in workbook into IWorksheet
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //Unlock a range of cells in worksheet
    worksheet.Range["A1:A10"].CellStyle.Locked = false;
 
    //Protect the worksheet for formatting columns
    worksheet.Protect("syncfusion", ExcelSheetProtection.LockedCells | ExcelSheetProtection.UnLockedCells | ExcelSheetProtection.FormattingColumns);
 
    //Save the Excel workbook
    workbook.SaveAs("Output.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate excel application object
    Dim application As IApplication = excelEngine.Excel
 
    'Create a new Excel workbook
    Dim workbook As IWorkbook = application.Workbooks.Create(1)
 
    'Get the first worksheet in workbook into IWorksheet
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Unlock a range of cells in worksheet
    worksheet.Range("A1:A10").CellStyle.Locked = False
 
    'Protect the worksheet for formatting columns
    worksheet.Protect("syncfusion", (ExcelSheetProtection.LockedCells Or (ExcelSheetProtection.UnLockedCells Or ExcelSheetProtection.FormattingColumns)))
 
    'Save the Excel workbook
    workbook.SaveAs("Output.xlsx")
End Using

 

A complete working sample to unlock cells in a worksheet and then protect it for formatting columns can be downloaded from Unlock-Cells.zip.

By executing the program, you will get the output Excel file with editable range from A1 to A10 and formatting option available for any column in the worksheet.

Output Excel document

Output Excel document

Take a moment to peruse the documentation, where you will find other options like protect and unprotect workbook, protect and unprotect a worksheet, protect and unprotect a chart sheet and cell protection.

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied