积极答复者
VB.net读取Excel 文件的问题...?

问题
-
在下面两句的代码中,
xlApp = GetObject(, "Excel.Application")
xlBook = xlApp.Workbooks.Open("C:\123.xls")
很多的子对象都是 {System.__ComObject} 的样子。
office 是 2003 版本; VS是 2008 正版。
问题到底在哪里呢?=============================================
引用如下:
代码如下,
Imports Microsoft.Office.Interop.Excel
Private Sub StartPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartPrint.Click
On Error GoTo errorline
Dim xlApp As New Application
Dim xlBook As Workbook
Dim xlSheet As Worksheet
On Error Resume Next
xlApp = GetObject(, "Excel.Application")
If Err.Number() <> 0 Then
Err.Clear()
xlApp = CreateObject("Excel.Application")
If Err.Number() <> 0 Then
MsgBox("Have not installed Excel")
End
End If
End IfOn Error GoTo errorline
xlBook = xlApp.Workbooks.Open("C:\123.xls")
xlApp.DisplayAlerts = False
xlApp.Visible = True
xlBook.Activate()
xlBook.Parent.Windows(1).Visible = True
xlSheet = xlBook.Worksheets(WORKSHEETNAME)
...........................
问题到底在哪里呢?
答案
-
你好,
在.NET框架中,对COM对象使用gettype的返回值确实是system.__comobject,但是你可以使用Information.typename(http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.information.typename.aspx) 来获取到它的真实类型。
另外,一些题外的建议,请不要再使用OnError了。在vb.NET里面,有一种极其强大的try..catch语句(http://msdn.microsoft.com/zh-cn/library/fk6t46tz.aspx)可供使用。
如果我对这个帖子理解有误,请告诉我,谢谢!
Regards,
Shanks Zen
MSDN Community Support | Feedback to us
- 已标记为答案 zwm136200 2012年6月7日 2:36
全部回复
-
你好,
在.NET框架中,对COM对象使用gettype的返回值确实是system.__comobject,但是你可以使用Information.typename(http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.information.typename.aspx) 来获取到它的真实类型。
另外,一些题外的建议,请不要再使用OnError了。在vb.NET里面,有一种极其强大的try..catch语句(http://msdn.microsoft.com/zh-cn/library/fk6t46tz.aspx)可供使用。
如果我对这个帖子理解有误,请告诉我,谢谢!
Regards,
Shanks Zen
MSDN Community Support | Feedback to us
- 已标记为答案 zwm136200 2012年6月7日 2:36
-
我被打败了,改为使用下面的方法了。
但是我觉得应该是配置问题。在下面的方法中有关于 x64, x32 位的设置,上面的问题会不会 也是如此呢。
Public Function excelToDataSet(ByVal fileName As String) As DataSet
Dim DS As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=" & fileName & ";" & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [Print$]", MyConnection)
DS = New System.Data.DataSet()
MyCommand.Fill(DS)
MyConnection.Close()
Return DS
End Function