【发布时间】:2017-01-04 01:23:09
【问题描述】:
由于某种原因,当我将数据表写入 excel 时,我收到以下错误消息。看来错误发生在我的第二个 for 循环中。
HRESULT 异常:0x800A03EC
Public Shared Sub ExportExcel(ByVal dt As DataTable)
Try
Dim strFile As String = MYFilelocation
Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
wBook.SaveAs(strFile)
wBook.Close()
Catch ex As Exception
MessageBox.Show("there was an issue Exporting to Excel" & ex.ToString)
End Try
End Sub
【问题讨论】:
-
那么当您单步执行代码时,调试器会告诉您什么?
-
您的数据表包含多少行?
-
数据表包含的数据少于 1500。调试器在为每个循环单步执行第二个之后给我这个错误 HRESULT: 0x800A03EC。