How to lock certain range of cells in an Excel document using C#, VB.NET?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
XlsIO supports locking and unlocking cells by using the cell’s Locked property of CellStyle. This article explains how to lock certain range of cells in an Excel document using
Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Use the following code snippet for locking certain range of cells in an Excel document.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputData.xlsx"), FileMode.Open, FileAccess.ReadWrite);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];
//Unlock cells in the worksheet
worksheet.UsedRange.CellStyle.Locked = false;
//Lock specific cells (A1:A5)
worksheet.Range["A1:A5"].CellStyle.Locked = true;
//Protect worksheet to apply lock
worksheet.Protect("password", ExcelSheetProtection.All);
#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("LockedCells.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion
//Dispose streams
outputStream.Dispose();
inputStream.Dispose();
}
VB.NET
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputData.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Unlock cells in the worksheet
worksheet.UsedRange.CellStyle.Locked = False
'Lock specific cells (A1:A5)
worksheet.Range("A1:A5").CellStyle.Locked = True
'Protect worksheet to apply lock
worksheet.Protect("password", ExcelSheetProtection.All)
'Save the workbook
workbook.SaveAs("Output.xlsx")
End Using
You can get the complete sample for locking certain range of cells in an Excel document from here.
I hope you enjoyed learning about how to lock certain range of cells in an Excel document using
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.