'┏〓〓〓〓〓〓〓〓〓 GetExcelRs,start 〓〓〓〓〓〓〓〓〓┓
'[簡介]:
'VB讀取EXCEL工作薄某個表中數(shù)據(jù)
Function GetExcelRs(ByVal sFile As String, Optional ExcelSheetName As String = "sheet1", Optional ErrInfo As String) As ADODB.Recordset
'VB源碼,幫你寫函數(shù),幫你寫代碼,幫你寫模塊,幫你設計軟件
'--需要什么函數(shù)或功能,可以聯(lián)系我。
'版權所有,請保留作者信息.QQ:1085992075
'如需商業(yè)用途請聯(lián)系作者
On Error GoTo Err
Dim RS As ADODB.Recordset
Set RS = New ADODB.Recordset
Dim ConnStr As String
ConnStr = "DRIVER=Microsoft Excel Driver (*.xls);" & "DBQ=" & sFile & ";ReadOnly=False"
RS.Open "SELECT * FROM [" & ExcelSheetName & "$]", ConnStr, 1, 3
Set GetExcelRs = RS
Set RS = Nothing
Exit Function
Err:
ErrInfo = Err.Description
MsgBox ErrInfo
End Function
'┗〓〓〓〓〓〓〓〓〓 GetExcelRs,end 〓〓〓〓〓〓〓注意:本例題程序必須引用 Microsoft Excel 11.0 Object Library
Visual Basic code
?1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'Form = Command1
'引用 Microsoft Excel 11.0 Object Library
'完成效果:在D盤下建立一個名字為 2的 excel文件 文件中的第5列第5行有“成功字樣”
'*************下面是代碼***************
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Private Sub Command1_Click()
Set xlapp = CreateObject("Excel.Application") '創(chuàng)建EXCEL對象
Set xlbook = xlapp.Workbooks.Add()
Set xlsheet = xlbook.Worksheets("Sheet1") '設置活動工作表
xlsheet.Cells(5, 5) = "成功" '向第五行第五列寫入該數(shù)據(jù)
xlbook.SaveAs "D:.xls" '另存文件
xlbook.Close '關閉工作簿
Set xlbook = Nothing '從內存中清除
xlapp.Quit '關閉excel
Set xlapp = Nothing '從內存中清除
End Sub