我想用ADO+SQL语句查询EXCEL,但得不到结果,大侠帮忙查找一下原因,谢谢
Public xlApp As Excel.Application '定义EXCEL类Public xlBook As Excel.Workbook '定义工件簿类
Public xlsheet As Excel.Worksheet '定义工作表类
Private Sub Command1_Click()
FileName = "仓库.xls"
Set xlApp = GetObject(, "Excel.Application") '判断Excel是否打开
xlApp.WindowState = xlMaximized
Set xlBook = xlApp.Workbooks.Open(App.Path & "\" & FileName) '打开工件簿文件
xlApp.Visible = True
Set cnn = CreateObject("ADODB.Connection")
Set rst2 = CreateObject("ADODB.Recordset")
' Set xlsheet = xlBook.Worksheets(4)
' xlsheet.Unprotect "123abc"
' xlsheet.Range("J:J").ClearContents
cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties=Excel 8.0;data source=" & App.Path & "\" & FileName
' StrSQL = "SELECT * FROM [" & Sh.Name & "$]"
StrSQL = "SELECT * FROM [结存$] where 物料名称='JACK IN THE BOX'"
StrSQL = "SELECT * FROM [结存$] "
' Set rst2 = cnn.Execute("SELECT 物料名称,规格型号,出库地点,出库人 FROM [出库$] where 物料名称 =''and 出库方式='采购入库'")
Set rst2 = cnn.Execute(StrSQL)
Set xlsheet = xlBook.Worksheets(2)
xlsheet.Range("a5").CopyFromRecordset rst2
For i = 1 To 2 'rst2.Fields.Count '获得SQL结果的列标题
' MsgBox rst2.Fields(i - 1).Name '字段名,
' MsgBox rst2.Fields(i - 1) '字段名,
Next
End Sub
跟踪没有提示错误,我错了哪里?
SQLCX.zip
(21.09 KB)