来自HRESULT的异常:excel应用程序中的0x800a03ec [英] Exception from HRESULT: 0x800a03ec in excel application
问题描述
我正在尝试在VB中保存excel工作表,但它给我的错误
xlWorkBook = xlApp.Workbooks.Add(MissValue)
此行为错误:
HRESULT异常:0x800A03EC
Public Sub ExportToExcelFromDGView(ByVal dgView As DataGridView,ByVal strFileName As String,_
ByVal ParamArray strSkippedCol()As String)
尝试
Dim strOutput(dgView .RowCount + 2,dgView.ColumnCount - 1)As String
Dim str As String =
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim MissValue = System.Reflection.Missing.Value
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(MissValue )
xlWorkSheet = xlWorkBook.Sheets.Add(Sheet1)
'****对于列标题(添加列名称)
Dim intColDisp As Integer = 0
Dim flgCol As Boolean = False
for intCol As Integer = 0 to dgView.ColumnCount - 1
'检查跳过的记录
flgCol = False
对于iArrLength As Integer = 0到strSkippedCol.Length - 1
如果是dgView.Columns(intCol)。 Name.Trim.ToUpper = strSkippedCol.GetValue(iArrLength).ToString.Trim.ToUpper然后
flgCol = True
退出
结束如果
下一个
如果flgCol那么
继续
结束如果
strOutput(0,intCol)= CStr(dgView.Columns(intCol).HeaderText.Trim)
intColDisp = intColDisp + 1
下一个
'****用于记录(添加记录文本)
对于intRow As Integer = 0到dgView.RowCount - 1
intColDisp = 0
对于intCol As Integer = 0到dgView.ColumnCount - 1
flgCol = False
For iArrLength As Integer = 0 to strSkippedCol.Length - 1
如果dgView.Columns(intCol).Name.Trim.ToUpper = strSkippedCol.GetValue(iArrLength).ToString.Trim.ToUpper那么
flgCol = True
退出
结束如果
下一个
如果flgCol然后
继续
结束如果
strOutput(intRow + 1,intCol)= CStr(dgView.Item(intCol,intRow).Value.ToString.Trim)
intColDisp = intColDisp + 1
'结束如果
下一个
下一个
xlWorkShe et.Range(A1)。Resize(dgView.RowCount + 1,dgView.ColumnCount).Value = strOutput
xlWorkBook.SaveAs(strFileName)
xlWorkBook.Close()
xlApp.Quit()
ReleaseObjects(xlApp)
ReleaseObjects(xlWorkBook)
ReleaseObjects(xlWorkSheet)
ReleaseObjects(MissValue)
Catch ex As Exception
MsgBox(ex.Message)
结束尝试
结束子
这是我的全部代码,请帮帮我
我尝试过:
我尝试添加
xlWorkBook = xlApp.Workbooks.Add()
您确定添加工作簿时发生错误吗?
我想这是在添加工作表时发生的事情
xlWorkSheet = xlWorkBook.Sheets.Add( Sheet1)
第一个参数指定添加新工作表之前的工作表(参见 Worksheets.Add方法(Microsoft.Office.Interop.Excel) [ ^ ])。但是如果没有名为Sheet1的工作表,则调用将失败并返回0x800A03EC(NAME_NOT_FOUND)。
使用
xlWorkSheet = xlWorkBook.Sheets.Add()而不是。
Workbooks.Add方法(Microsoft.Office.Interop.Excel) [ ^ ]除非要从模板创建新工作簿,否则不需要参数。
这也应该有效:
xlWorkBook = xlApp.Workbooks.Add()
欲了解更多详情,请参阅:
如何:以编程方式创建新工作簿 [ ^
I am trying to save excel worksheet in VB but it gives me error on
xlWorkBook = xlApp.Workbooks.Add(MissValue)
this line as error:Exception from HRESULT: 0x800A03EC
Public Sub ExportToExcelFromDGView(ByVal dgView As DataGridView, ByVal strFileName As String, _ ByVal ParamArray strSkippedCol() As String) Try Dim strOutput(dgView.RowCount + 2, dgView.ColumnCount - 1) As String Dim str As String = "" Dim xlApp As New Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet Dim MissValue = System.Reflection.Missing.Value xlApp = New Excel.Application xlWorkBook = xlApp.Workbooks.Add(MissValue) xlWorkSheet = xlWorkBook.Sheets.Add("Sheet1") '****For Column Heading(Adding column Names) Dim intColDisp As Integer = 0 Dim flgCol As Boolean = False For intCol As Integer = 0 To dgView.ColumnCount - 1 'Check for skipped Records flgCol = False For iArrLength As Integer = 0 To strSkippedCol.Length - 1 If dgView.Columns(intCol).Name.Trim.ToUpper = strSkippedCol.GetValue(iArrLength).ToString.Trim.ToUpper Then flgCol = True Exit For End If Next If flgCol Then Continue For End If strOutput(0, intCol) = CStr(dgView.Columns(intCol).HeaderText.Trim) intColDisp = intColDisp + 1 Next '****For Records(Adding record text) For intRow As Integer = 0 To dgView.RowCount - 1 intColDisp = 0 For intCol As Integer = 0 To dgView.ColumnCount - 1 flgCol = False For iArrLength As Integer = 0 To strSkippedCol.Length - 1 If dgView.Columns(intCol).Name.Trim.ToUpper = strSkippedCol.GetValue(iArrLength).ToString.Trim.ToUpper Then flgCol = True Exit For End If Next If flgCol Then Continue For End If strOutput(intRow + 1, intCol) = CStr(dgView.Item(intCol, intRow).Value.ToString.Trim) intColDisp = intColDisp + 1 'End If Next Next xlWorkSheet.Range("A1").Resize(dgView.RowCount + 1, dgView.ColumnCount).Value = strOutput xlWorkBook.SaveAs(strFileName) xlWorkBook.Close() xlApp.Quit() ReleaseObjects(xlApp) ReleaseObjects(xlWorkBook) ReleaseObjects(xlWorkSheet) ReleaseObjects(MissValue) Catch ex As Exception MsgBox(ex.Message) End Try End Sub
This is my whole code ,Please help me out
What I have tried:
I tried addingxlWorkBook = xlApp.Workbooks.Add()解决方案Are you sure that the error occurs when adding the workbook?
I guess it is happening when adding the sheet with
xlWorkSheet = xlWorkBook.Sheets.Add("Sheet1")The first parameter specifies the sheet before which the new sheet is added (see Worksheets.Add method (Microsoft.Office.Interop.Excel)[^]). But if there is no sheet named "Sheet1", the call will fail with 0x800A03EC (NAME_NOT_FOUND).
Use
xlWorkSheet = xlWorkBook.Sheets.Add()instead.
Workbooks.Add method (Microsoft.Office.Interop.Excel)[^] does not require parameter unless you want to create new workbook from template.
This should works as well:
xlWorkBook = xlApp.Workbooks.Add()
For further details, please see:
How to: Programmatically Create New Workbooks[^]
这篇关于来自HRESULT的异常:excel应用程序中的0x800a03ec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!