当我试图将我的datagridview组件保存到excel时出现错误
Exception from HRESULT: 0x800A03EC
围绕这一行:xlWorkSheet = xlWorkBook.Sheets.Add("[Sheet1]")
我的代码中有一部分:
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets.Add("[Sheet1]")
For i = 0 To DataGridView1.RowCount - 1
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value
Next
Next
Next
xlWorkSheet.SaveAs("c:\users\500998877\documents\bg\dtgv1.xlsx")
xlWorkBook.Close()
xlApp.Quit()
Process.Start("c:\users\500998877\documents\bg\dtgv1.xlsx")
答案 0 :(得分:1)
未经测试但请尝试以下操作:
xlWorkSheet = xlWorkBook.Sheets.Add
xlWorksheet.Name = "MyNewSheet"
请注意,可能还有默认的" Sheet1"使用Excel的defaut实例。
您可能希望使用this MSDN reference page
开始研究