I am using Microsoft.Office.Interop.Excel in a winform where I am reading one excel file, processing the data, and outputting a new excel file. However I am having trouble writing to the cells -- specifically to add column headings. Here's the code:
我用Microsoft.Office.Interop。在winform中读取一个Excel文件,处理数据,输出一个新的Excel文件。然而,我在写单元格时遇到了麻烦——特别是要添加列标题。这是代码:
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 doesn't throw a runtime error but the code following it does. The error just says: COM Exception was unhandled, Exception from HRESULT: 0x800A03EC.
其中“dt”是我的数据表。嵌套的for循环不会抛出运行时错误,但是后面的代码会抛出错误。错误只是说:COM异常未被处理,HRESULT: 0x800A03EC中的异常。
Any advice is appreciated.
任何建议都是感激。
Regards.
的问候。
1 个解决方案
#1
8
Cells[]
is 1-based, not zero-based.
cell[]是基于1的,而不是基于0的。
#1
8
Cells[]
is 1-based, not zero-based.
cell[]是基于1的,而不是基于0的。