Using parallel task library to write to excel worksheet

Jun 17, 2013 at 2:41 PM
Hi all,
I am getting a data table back from a query. I then want to just write this data to an excel work sheet and save the file. I know there is
worksheet.Cells["A1"].LoadFromDataTable(dt, true);
But I have dates that get incorrectly formatted when i use that.
So I am trying to iterate through the rows and cells myself. However, I wanted to speed it up (I have 49,000 rows and 350+ columns), so I thought I'd trying using paraellelism since each row is indpendent of others. is this possible? When I try i get out of bounds exceptions. Here is my code.
int rowCount = dt.Rows.Count;
            System.Threading.Tasks.Parallel.For(1, rowCount, x =>
            {
                    {
                    dr = dt.Rows[x];
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            //if (dr[i] is DateTime)
                            ExcelRange cell = worksheet.Cells[x + 1, i + 1];
                            if (isDates[i])
                                cell.Value = ((DateTime)dr[i]).ToShortDateString();
                            else
                                cell.Value = dr[i];
                            cell.Dispose();
                        }
                    }
                    }
                });
thanks,
Dave