bonson
April 12, 2018, 8:54pm
1
Hi,
I would like one cell locked and the other unlocked:
style = m_WorkSheet.Cells[rowIndex, 0].GetStyle();
style.Font.IsBold = true;
style.IsLocked = true;
m_WorkSheet.Cells[0, 0].SetStyle(style);
m_WorkSheet.Cells[0, 0].PutValue("Title");
m_WorkSheet.Protect(ProtectionType.All);
style = m_WorkSheet.Cells[rowIndex, 1].GetStyle();
style.Font.IsBold = true;
m_WorkSheet.Cells[0, 1].SetStyle(style);
m_WorkSheet.Cells[0, 1].PutValue("Edit prices below");
m_WorkSheet.Protect(ProtectionType.All);
But when I download the spreadsheet the entire sheet is locked, even though I set isLocked on one cell only?
@bonson
Thanks for using Aspose APIs.
Please see the following code and its output Excel file as well as screenshot for a reference. The code unlocks the cells B5, C5 and D5. You can edit only these three cells and all other cells are protected and cannot be edited.
Download Link:
Output Excel File.zip (5.6 KB)
C#
//Create workbook
Workbook wb = new Workbook();
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Put some values in cells
ws.Cells["B5"].PutValue("Unlocked");
ws.Cells["C5"].PutValue("Unlocked");
ws.Cells["D5"].PutValue("Unlocked");
//Unlock the cells B5, C5, D5
Cell c = ws.Cells["B5"];
Style s = c.GetStyle();
s.IsLocked = false;
//Unlocking
ws.Cells["B5"].SetStyle(s);
ws.Cells["C5"].SetStyle(s);
ws.Cells["D5"].SetStyle(s);
//Set protection that we allow selecting unlocked cells only
ws.Protection.AllowSelectingUnlockedCell = true;
//Protect the worksheet
ws.Protect(ProtectionType.All);
//Save the output Excel file
wb.Save("output.xlsx");
Screenshot :
@bonson ,
We also recommend you to kindly see the documents in the section for your complete reference:
Class Libraries & REST APIs for the developers to manipulate & process Files from Word, Excel, PowerPoint, Visio, PDF, CAD & several other categories in Web, Desktop or Mobile apps. Develop & deploy on Windows, Linux, MacOS &...