vb.net 将工作表添加到 Excel 工作簿时出现 HRESULT 异常:0x800A03EC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28062153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception from HRESULT: 0x800A03EC when adding a sheet to Excel workbook
提问by Gábor Boros
When i'm trying to save my datagridview component to excel i got an error
当我尝试将 datagridview 组件保存到 excel 时出现错误
Exception from HRESULT: 0x800A03EC
Exception from HRESULT: 0x800A03EC
around this line: xlWorkSheet = xlWorkBook.Sheets.Add("[Sheet1]")
围绕这条线: xlWorkSheet = xlWorkBook.Sheets.Add("[Sheet1]")
There is the part of my code:
我的代码有一部分:
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:\users0998877\documents\bg\dtgv1.xlsx")
xlWorkBook.Close()
xlApp.Quit()
Process.Start("c:\users0998877\documents\bg\dtgv1.xlsx")
采纳答案by barryleajo
Untested but try the following:
未经测试,但请尝试以下操作:
xlWorkSheet = xlWorkBook.Sheets.Add
xlWorksheet.Name = "MyNewSheet"
Be aware that there may also be a default "Sheet1" with a defaut instance of Excel.
请注意,可能还有一个带有默认 Excel 实例的默认“Sheet1”。
You may wish to start your research with this MSDN reference page
您可能希望从此 MSDN 参考页面开始您的研究

