Re: lock first row

Hi,

The latest versions (e.g., 4.5, 4.4.3 etc.) provide an API i.e.., Cells.ApplyRowStyle method so you may use it for your task.

May the following sample code help you for your need:

Sample code:

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];
Style style;
StyleFlag flag;

//First unlock all the cells.
style = wb.Styles[wb.Styles.Add()];
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
wb.DefaultStyle = style;

//Now lock the first row in the worksheet only.
Style style1 = wb.Styles[wb.Styles.Add()];
style1.IsLocked = true;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.ApplyRowStyle(0,style1, flag);

sheet.Protect(ProtectionType.All);

wb.Save("f:\\test\\lockarow.xls");

Thank you.

Hi,

The latest versions (e.g., 4.5, 4.4.3 etc.) provide an API i.e.., Cells.ApplyRowStyle method so you may use it for your task.

May the following sample code help you for your need:

Sample code:

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];
Style style;
StyleFlag flag;

//First unlock all the cells.
style = wb.Styles[wb.Styles.Add()];
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
wb.DefaultStyle = style;

//Now lock the first row in the worksheet only.
Style style1 = wb.Styles[wb.Styles.Add()];
style1.IsLocked = true;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.ApplyRowStyle(0,style1, flag);

sheet.Protect(ProtectionType.All);

wb.Save("f:\\test\\lockarow.xls");

Thank you.

Hi,

If I lock the first row by the code you mentioned, users cannot insert or delete any row in the spreadhseet they download.

Is there any way that only first row is locked while the remaining spreadsheet remains editable.

Hi,

Thanks for considering Aspose.

I think you should try some advanced protection options for your need. I have revised the code I wrote, kindly consult it:

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];
Style style;
StyleFlag flag;

style = wb.Styles[wb.Styles.Add()];
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
wb.DefaultStyle = style;

Style style1 = wb.Styles[wb.Styles.Add()];
style1.IsLocked = true;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.ApplyRowStyle(0,style1, flag);

sheet.Protect(ProtectionType.All);
sheet.Protection.IsInsertingRowsAllowed = true;
sheet.Protection.IsDeletingRowsAllowed = true;

wb.Save("f:\\test\\lockarow.xls");

Thank you.