Unlock specific column from protected excel sheet using C#.NET

Hi Team,

I want to unlock only specific column from protected worksheet from excel.
But from following I am unable to lock specific column.

Platform : .Net

------------------------Dummy Code -------------------------
this.worksheet.Protect(ProtectionType.All, β€œtest”, null);
Style editstyle = workbook.CreateStyle();
StyleFlag editFlag = new StyleFlag { Locked = false };
this.worksheet.Cells.Columns[7].ApplyStyle(editstyle, editFlag);

Thanks :slight_smile:
Happy Coding
aspose_unloack_col.png (7.4 KB)

You need to set the Style.IsLocked = true to set the style as locked and StyleFlag.Locked = true to cconsider this feature among all the styles. Also for column β€˜G’ use index 6 instead of 7.

Here is a complete example that unlocks the desired column while rest of the cells are protected. Please give it a try and share the feedback.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET

// Create a new workbook.
Workbook wb = new Workbook();

// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[0];

// Define the style object.
Style style;

// Define the styleflag object.
StyleFlag flag;

// Protect all the cells in the sheet
style = sheet.Cells.Style;
style.IsLocked = true;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.ApplyStyle(style, flag);

// Get the target column style.
style = sheet.Cells.Columns[6].Style; //For Columnn G as shown in your image

// Unlock it.
style.IsLocked = false;

// Instantiate the flag.
flag = new StyleFlag();

// Set the lock setting.
flag.Locked = true;

// Apply the style to the seventh column.
sheet.Cells.Columns[6].ApplyStyle(style, flag);

// Protect the sheet.
sheet.Protect(ProtectionType.All,"test", null);

// Save the excel file.
1 Like

Resolved Thanks :slight_smile:

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.