异常是VB.Net Framework v1.1中的Excel应用程序 [英] Exception is coming Excel application in VB.Net Framework v1.1
问题描述
您好,
我正在尝试在我的vb windows应用程序中读取一个excel文件。这是我读取Excel的代码
Imports System.IO
Imports System.Runtime.InteropServices
私有 Sub Button1_Click( ByVal sender 作为系统。对象, ByVal e As System.EventArgs)句柄 Button1.Click
Dim xlApp 作为 Excel.Application
Dim xlWorkBook 作为 Excel.Workbook
Dim xlWorkSheet 作为 Excel .Worksheet
Dim 范围作为 Excel.Range
Dim rCnt 作为 整数
Dim cCnt 作为 整数
Dim Obj 作为 对象
xlApp = 新 Excel.ApplicationClass()
xlWorkBook = xlApp.Workbooks.Open( C:\StatusReport.xls)
xlWorkSheet = xlWorkBook.Worksheets( sheet1)
range = xlWorkSheet.UsedRange
对于 rCnt = 1 range.Rows.Count
对于 cCnt = 1 至 range.Columns.Count
Obj = CType (range.Cells(rCnt,cCnt),Excel.Range)
MsgBox(Obj.value)
下一步
下一步
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject (xlWorkBook)
releaseObject(xlWorkSheet)
结束 Sub
< span class =code-keyword>私有 Sub releaseObject( ByVal obj 作为 对象)
尝试
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex 作为例外
obj = 没什么
最后
GC.Collect()
结束 尝试
结束 Sub
当我执行此操作时,获得例外
ExceltoHtml1.exe中发生未处理的System.Runtime.InteropServices.COMException类型异常
其他信息:CLSID为{00024500-0000-0000-C000-000000000046}的COM对象无效或未注册。行
< pre lang =vb> xlApp = 新 Excel.ApplicationClass()
我在谷歌搜索但是dint得到任何有用的信息。
任何帮助都会得到满足。
谢谢。
替换:
xlApp = 新 Excel.ApplicationClass()
with
xlApp = 新 Excel.Application()
,一切都应该没问题。
BTW:ApplicationClass
与C#一起使用。
Hello,
I am trying to read an excel file in my vb windows application.This is my code for reading Excel
Imports System.IO
Imports System.Runtime.InteropServices
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim range As Excel.Range
Dim rCnt As Integer
Dim cCnt As Integer
Dim Obj As Object
xlApp = New Excel.ApplicationClass()
xlWorkBook = xlApp.Workbooks.Open("C:\StatusReport.xls")
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
range = xlWorkSheet.UsedRange
For rCnt = 1 To range.Rows.Count
For cCnt = 1 To range.Columns.Count
Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
MsgBox(Obj.value)
Next
Next
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
When I am executing this , getting an exception
An unhandled exception of type ''System.Runtime.InteropServices.COMException'' occurred in ExceltoHtml1.exe
Additional information: COM object with CLSID {00024500-0000-0000-C000-000000000046} is either not valid or not registered. at line
xlApp = New Excel.ApplicationClass()
I searched in Google but dint get any usefull info.
Any help will be appriciated.
Thanks.
Replace:
xlApp = New Excel.ApplicationClass()
with
xlApp = New Excel.Application()
and everything should be OK.
BTW:ApplicationClass
is used with C#.
这篇关于异常是VB.Net Framework v1.1中的Excel应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!