错误hresult 0x800a03ec试图给excel单元赋值

我想通过使用下面的代码将DataGridView的内容导出到Excel工作表

Dim xlApp As New Microsoft.Office.Interop.Excel.Application Dim xlBook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Add Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(xlBook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet) xlSheet.Name = "MySheet" xlApp.Visible = True With xlSheet For C As Integer = 0 To DGV_IntCalc.Columns.Count - 1 .Range(.Cells(1, C + 1)).Value = DGV_IntCalc.Columns(C).HeaderText For R As Integer = 0 To DGV_IntCalc.RowCount - 1 .Range(.Cells(R + 2, C + 1)).Value = DGV_IntCalc.Rows(R).Cells(C).Value Next Next End With 

.Range(.Cells(1, C + 1)).Value = DGV_IntCalc.Columns(C).HeaderText

我有Option Strict On所以我不能使用.Cells(1, C + 1).Value = DGV_IntCalc.Columns(C).HeaderText因为后期绑定

我错在哪里?

即使范围只有一个单元格,也需要插入范围的开始和结束单元格。

解决办法是改变:

 .Range(.Cells(1, C + 1)).Value = DGV_IntCalc.Columns(C).HeaderText 

有:

 .Range(.Cells(1, C + 1), .Cells(1, C + 1)).Value = DGV_IntCalc.Columns(C).HeaderText