Can I lock a perticular row?



I need to lock a few cells in a perticular row, for example, In the first row, i have 10 merged cells (0 to 9), and it displays a notification that should not be edited. How can I set only these cells in the row to be locked?



Thanks for considering Aspose

Please follow the procedure:

1. Open your template(source file) in excel.

2. Select all the cells in your worksheet. or press CTRL+A.

3. Click Format menu to select Cells... to open Format Cells dialog box.

4. Click its Protection tab and uncheck the Locked option and press OK.

5. Now select those 10 cells in the first row whom you want to protect, click Format menu to select Cells... to open Format Cells dialog box again and click its protection tab and checked the locked option and press OK.

6. Now please protect your worksheet using Worksheet.Protect() method of Aspose.Cells APIs.

If you still have some queries ... feel free to contact us


Amjad Sahi

Aspose Nanjing Team


I understand that I can protect the worksheet using Worksheet.Protect() APi, but is it not possible to do steps 1 through 5 using any Aspose Api’s? I mean is it not possible through code?



Well, at the moment if you want to protect only a range of cells in a worksheet, you need to have a template file


Amjad Sahi

Aspose Nanjing Team


Locked the row with code is supported in the latest version. Following is the sample code. However, using a template file will be simpler.

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];
Style style;
StyleFlag flag;
for(int i = 0; i <= 255; i ++)
style = sheet.Cells.Columns[(byte)i].Style;
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);

style = sheet.Cells.Rows[0].Style;
style.IsLocked = true;

flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Rows[0].ApplyStyle(style, flag);

Aspose.Cells.Protection protection = sheet.Protection;

wb.Save("d:\\test\\abc.xls", FileFormatType.ExcelXP);