Hi,
Following code is copying validations to all the cells with similar settings.
Eg, in the code below; I am over-riding validation to "Test Content" at cell "E8" only, and at design time we have validations added till E27 with content as "Design Time Validation".
Unfortunately the validations are getting overridden in all the cells. Is there anything missing in the code?
I have attached template and output workbooks.
Page-Load
Workbook workbook = new Workbook();
workbook.Open(@"C:\Users\vdwivedi\Desktop\Archives\Book1.xls", FileFormatType.Excel97To2003);
this.AddValidation(sheet, "Test Test", "Test Content", sheet.Cells["E8"]);
workbook.Save(@"C:\Users\vdwivedi\Desktop\Archives\Book2.xls");
//Methods
private void AddValidation(Worksheet localSheet, String hText, String content, Cell dCell)
{
try
{
Validation ValCell = GetCurrentValidation(localSheet, dCell.Name);
if (ValCell == null) { ValCell = localSheet.Validations[localSheet.Validations.Add()]; }
ValCell.InputTitle = hText;
ValCell.InputMessage = content;
ValCell.ShowInput = true;
CellArea area;
area.StartColumn = dCell.Column;
area.EndColumn = dCell.Column;
area.StartRow = dCell.Row;
area.EndRow = dCell.Row;
ValCell.AreaList.Add(area);
}
catch { throw; }
}private Validation GetCurrentValidation(Worksheet localSheet, String cellID)
{
Validation v = null;
Cell dCell = localSheet.Cells[cellID];
try
{
for (int i = 0; i < localSheet.Validations.Count; i++)
{
Validation localVal = localSheet.Validations[i];
for (int j = 0; j < localVal.AreaList.Count; j++)
{
CellArea ca = (CellArea)localVal.AreaList[j];
if (dCell.Row >= ca.StartRow && dCell.Row <= ca.EndRow
&& dCell.Column >= ca.StartColumn && dCell.Column <= ca.EndColumn)
{
v = localVal;
localVal = null;
break;
}
}
}
}
catch { throw; }
return v;
}
Regards, Vinay