Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
560
Excel Export Grid
posted

Hi,

I am exporting 3 grids into an excel workbook, 3 different worksheets
Each time if a worksheet already exists, I have to erase worksheet and write grid contents. I am doing as below and export works good first time but runs into issues second or third time

I get an exception with error "Index was out of range. Must be non-negative and less than the size of the collection"

Workbook workbook = Workbook.Load(FilePath);
if (workbook.Worksheet.Exists(WorksheetName))
{
 Worksheet worksheet = workbook.Worksheets[WorksheetName];

  workbook.Worksheets.Remove(worksheet);
  workbook.Worksheets.Add(WorksheetName);
}
else
{
   workbook.Worksheets.Add(WorksheetName);
}

workbook.WindowOptions.SelectedWorksheet = workbook.Worksheets[WorksheetName];

ultraGridExcelExporter.Export(grid, workbook);

Any suggestion will be helpful

Thx