I use Microsoft.Office.Interop.Excel in winform, where I read a single excel file, processing the data and outputting a new excel file. However, I am having problems writing to cells - in particular, to add column headers. Here is the code:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet ws = (Worksheet)wb.Worksheets[1]; for (int i = 0; i < dt.Columns.Count; i++) { for (int j = 0; j < dt.Rows.Count; j++) { ws.Cells[j + 1, i] = dt.Rows[j][i].ToString(); } } ws.Cells[0, 0] = "Ticket Number"; ws.Cells[0, 1] = "Transit"; ws.Cells[0, 2] = "Outage Start Date"; ws.Cells[0, 3] = "Outage End Date"; ws.Cells[0, 4] = "Business Impact"; wb.Worksheets.Add(ws);
where "dt" is my DataTable. The nested for loop does not generate a run-time error, but the code following it. The error simply says: COM Exception was unhandled, exception from HRESULT: 0x800A03EC.
Any advice is appreciated.
Sincerely.
Kevin source share