1. 程式人生 > >vB程式設計VB原始碼 VB讀取EXCEL工作薄某個表中資料 ADODB.Recordset

vB程式設計VB原始碼 VB讀取EXCEL工作薄某個表中資料 ADODB.Recordset

 
Private Sub Form_Load()
Dim RS As ADODB.Recordset
Set RS = GetExcelRs(App.Path & "\book1.xls")
MsgBox RS.RecordCount
MsgBox RS(0)
RS(0) = Timer
RS.Update
RS.Close
End Sub

'┏〓〓〓〓〓〓〓〓〓 GetExcelRs,start 〓〓〓〓〓〓〓〓〓┓
'[簡介]:
'VB讀取EXCEL工作薄某個表中資料
Function GetExcelRs(ByVal sFile As String, Optional ExcelSheetName As String = "sheet1", Optional ErrInfo As String) As ADODB.Recordset
   '[mycode_id:2025],edittime:2011-9-7 下午 02:15:41
   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  〓〓〓〓〓〓〓〓〓┛