本人才疏学浅,用惯了连接sql和access 现在实在搞不定excel了。用data控件可以建立好对于excel文件的连接,只是在操作时 不知道怎么把excel中的数据进行sql语句操作。希望各位高手赐教~
一个excel文件中可能有多个sheet表,在data中RecordSource属性好象是用sheet加$(即sheet1$)来表示的,我便理解为用sheet1$来表示表的名字 便这样写在了sql里
(例:
Set rcs = Data1.Database.OpenRecordset("SELECT * FROM SHEET2$", dbOpenDynaset, dbReadOnly)
)可是编译时总是告诉我from附近语法有错误 删掉$后,又找不到sheet2 请问该如何连接excel文件 用sql进行查询操作啊`~回答问题便给分~~
一个excel文件中可能有多个sheet表,在data中RecordSource属性好象是用sheet加$(即sheet1$)来表示的,我便理解为用sheet1$来表示表的名字 便这样写在了sql里
(例:
Set rcs = Data1.Database.OpenRecordset("SELECT * FROM SHEET2$", dbOpenDynaset, dbReadOnly)
)可是编译时总是告诉我from附近语法有错误 删掉$后,又找不到sheet2 请问该如何连接excel文件 用sql进行查询操作啊`~回答问题便给分~~
ÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉ
不是说查询语句不完整,就是说无效的SQL语句...
Set conn = Server.CreateObject("ADODB.Connection")
Driver = "Driver={Microsoft Excel Driver (*.xls)};"
DBPath = "DBQ=" & Server.MapPath( "Sample.xls" ) ' 调用Open 方法连接数据库
conn.Open Driver & DBPath Set rs = Server.CreateObject("ADODB.Recordset")
' 打开数据源,参数二为Connection对象
rs.Open "Select * From [成绩单$]", conn, 2, 2
能否用VB的ADO的方法访问Excel文件,如果能该怎样实现?Dim adoConnection As New ADODB.Connection
Dim adoRecordset As New ADODB.Recordset
'OLE DB + ODBC Driver 方式:
'adoConnection.Open "Data Provider=MSDASQL.1;driver=Microsoft Excel Driver (*.xls);DBQ=e:\temp\book2.xls"
'Microsoft.Jet.OLEDB.4.0 方式,(建议)
adoConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=e:\temp\book2.xls;Extended Properties='Excel 8.0;HDR=Yes'"
adoRecordset.Open "select * from [sheet1$]", adoConnection, adOpenKeyset, adLockOptimistic
Debug.Print adoRecordset.RecordCount
Dim i As Integer
Do Until adoRecordset.EOF
For i = 0 To adoRecordset.Fields.Count - 1
Debug.Print adoRecordset.Fields.Item(0).Name
Debug.Print adoRecordset.Fields.Item(0).Value
Next i
adoRecordset.MoveNext
Loop
'注: OLE DB + ODBC Driver 方式不支持以下语句,但 Microsoft.Jet.OLEDB.4.0 方式支持!
adoConnection.Execute "insert into [sheet1$](F1) values(3)"
adoRecordset.AddNew Array("f1", "f2", "f3", "f4"), Array(1, 2, 3, 4)
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q278/9/73.asp&NoWebContent=1