Hi,I am trying to delete sheets in a work book if the sheet name is not equal to certain value but I am getting the error as "System.InvalidOperationException: Collection was modified; enumeration operation may not execute.". Below is my code, please check and let me know what is wrong.
Workbook
wb = new Workbook();
wb.Open(FileName);
Worksheet ws = wb.Worksheets[VersionName];
Workbook
wb = new Workbook();
wb.Open(FileName);
Worksheet ws = wb.Worksheets[VersionName];
foreach
(Worksheet sheet in wb.Worksheets)
{
if (!sheet.Name.Equals(VersionName, StringComparison.InvariantCultureIgnoreCase))
{
wb.Worksheets.RemoveAt(sheet.Name);
}
}
foreach
(Worksheet sheet in wb.Worksheets)
{
if (!sheet.Name.Equals(VersionName, StringComparison.InvariantCultureIgnoreCase))
{
wb.Worksheets.RemoveAt(sheet.Name);
}
}
Thanks,
Jyotshna