关于VB6.0中用DataGrid显示Excel数据的问题
如题,在VB6.0中用DataGrid不能显示Excel数据,代码如下:
str = CommonDialog1.FileName '贾军修改
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=" & str & ";Extended Properties='Excel 8.0;imes=1'"
rs.Open "select * from [Sheet1$]", cn, adOpenKeyset, adLockOptimistic
rs.CursorLocation = adUseClient
Set DataGrid1.DataSource = rs
MsgBox "共读取了" & rs.RecordCount & "个数据"
注解:之前添加了一个对话框用于打开一个Excel文件,
MsgBox "共读取了" & rs.RecordCount & "个数据"
这句都能执行,不提示错误,就是DATAGRID里面什么没有,不显示EXCEL中内容,也不闪,DATAGRID控件均采用默认方式,几乎没有改动
还希望高手赐教,急死了 --------------------编程问答-------------------- 另外把DATAGRID控件换成MSFLEXGRID控件问题还是一样 --------------------编程问答-------------------- 经测试,下面代码完全正确
Private Sub Command1_Click()
Dim sFile As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Tdlg.ShowOpen
sFile = Tdlg.FileName '贾军修改
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=" & sFile & ";Extended Properties='Excel 8.0;imes=1'"
rs.CursorLocation = adUseClient
rs.Open "select * from [Sheet1$]", cn, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs
End Sub
--------------------编程问答-------------------- Tdlg就是你的CommonDialog1
补充:VB , 控件