Free Support Forum -

Need to protect specific row in a sheet


I need to validate some cells on a worksheet using custom validation. In the validation I want to make sure that some cells content are'nt changed. I'm posting the code which is supposed to that for a single cell ('A1') but its not working. When I open the excel I can't see the formula value on that cell. Even if I manually craete the custom validation in Excel and try to read the formula with aspose I get null value in 'validations[0].Formula' content.

I'm using version of aspose.cells.dll.

string fileName = @"C:\\temp\\TestProtection.xls";

Workbook workBook = new Workbook();

FileStream fileStream = null;



using (fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))




Worksheet workSheet = workBook.Worksheets[0];

Validations validations = workSheet.Validations;

//Creating a Validation object

Validation validation = validations[validations.Add()];

//Setting the validation type to whole number

validation.Type = ValidationType.Custom;

validation.AlertStyle = ValidationAlertType.Stop;

validation.ErrorMessage = "This cell cannot be changed";

validation.Formula1 = String.Format("={0}", "A1");

validation.ShowError = true;

CellArea area = new CellArea();

area.StartRow = 0;

area.EndRow = 0;

area.StartColumn = 0;

area.EndColumn = 0;


// Save the excel file.

workBook.Save(@"c:\\temp\\TestProtection.xls", FileFormatType.Excel2003);


catch (Exception ex)




What am I missing?

Thank you



Well, all validation types (decimal, list, any value, whole number, date, time, text length etc. ) are supported except custom. We will support it soon.

Thank you.


Please try the attached version. We have supported to set Custom validation now for your requirement.

Note: In the fix, you may create / set custom validation at runtime. But retrieving the Formula attributes for the custom validation in the template file is not yet supported.

Thank you.