Other API Samples Lock and Protect Cells SpreadsheetGear API SampleRange Samples Operations Lock and Protect CellsDescriptionLock and unlock cells and enable worksheet protection on two worksheets, protecting with and without a password.Sample Source Code// Create a new workbook. SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(); // SAMPLE 1: Protect Sheet1 without a password. { // Get reference to sheet one and local variable to cells. SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"]; SpreadsheetGear.IRange cells = worksheet.Cells; // Add a couple of labels and AutoFit the column. cells["A1"].Value = "Row 1 is not locked - you can edit this cell."; cells["A2"].Value = "All other rows are locked - you cannot edit this cell."; cells["A3"].Value = "This sheet is not protected with a password."; cells["A1:A2"].Columns.AutoFit(); // Get a reference to row 1. SpreadsheetGear.IRange row1 = cells["1:1"]; // Unlock the range of cells. (NOTE: Cells are locked by default) row1.Locked = false; // Enable protection for the worksheet. worksheet.ProtectContents = true; } // SAMPLE 2: Protect Sheet2 with a password. { // Add a second worksheet to the workbook and get local variable to cells. SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets.Add(); SpreadsheetGear.IRange cells = worksheet.Cells; // Add a couple of labels and AutoFit the column. cells["A1"].Value = "Row 1 is not locked - you can edit this cell."; cells["A2"].Value = "All other rows are locked - you cannot edit this cell."; cells["A3"].Value = "This sheet is protected with a password."; cells["A1:A2"].Columns.AutoFit(); // Get a reference to row 1. SpreadsheetGear.IRange row1 = cells["1:1"]; // Unlock the range of cells. (NOTE: Cells are locked by default) row1.Locked = false; // Enable protection for the worksheet. worksheet.Protect("MyPassword"); }