考試大編輯整理:ACCESS和EXECL結(jié)合的簡(jiǎn)單說明
引用EXCEL
Dim xlApp As Excel.Application
Dim xlBook As Workbook, xlSheet As Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(EXECL文件名) '打開要輸出的EXECL文件
Set xlSheet = xlBook.Worksheets(1) '打開工作表,這里是第一個(gè),可以設(shè)其他
xlApp.Visible = True '顯示
xlSheet.CELLS(3, 3) = "'" & 要輸出的內(nèi)容 'CELLS(行,列) 單元格
xlSheet.PrintPreview '如果是要打印,只要把 PrintPreview 改為 PrintOut
xlBook.Close SaveChanges:=False
xlApp.Quit '退出
****************
For iCols = 0 to rs.Fields.Count - 1
ws.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
ws.Range(ws.Cells(1, 1), _
ws.Cells(1, rs.Fields.Count)).Font.Bold = True
ws.Range("A2").CopyFromRecordset rs
*****************************
Private Sub CommandButton1_Click()
Dim CNN As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim Stpath, strSQL As String
Stpath = ThisWorkbook.Path & Application.PathSeparator & "學(xué)生檔案.mdb"
CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" & Stpath '& ";Jet OLEDB:Database Password=" & "123"
If ComboBox3.Value = "" Then
strSQL = "Select * from 檔案 WHERE 籍貫 LIKE '" & ComboBox2.Value & "'"
ElseIf ComboBox2.Value = "" Then
strSQL = "Select * from 檔案 WHERE 性別 LIKE '" & ComboBox3.Value & "'"
Else
strSQL = "Select * from 檔案 WHERE 性別 LIKE '" & ComboBox3.Value & "'" & "AND 籍貫 LIKE '" & ComboBox2.Value & "'"
End If
RST.Open strSQL, CNN
Sheet1.Range("A2:G10000").ClearContents '刪除原內(nèi)容
Sheet1.Cells(2, 1).CopyFromRecordset RST
RST.Close
Set RST = Nothing '示放RST 的內(nèi)存
Set CNN = Nothing '示放CNN的內(nèi)存
End Sub
引用EXCEL
Dim xlApp As Excel.Application
Dim xlBook As Workbook, xlSheet As Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(EXECL文件名) '打開要輸出的EXECL文件
Set xlSheet = xlBook.Worksheets(1) '打開工作表,這里是第一個(gè),可以設(shè)其他
xlApp.Visible = True '顯示
xlSheet.CELLS(3, 3) = "'" & 要輸出的內(nèi)容 'CELLS(行,列) 單元格
xlSheet.PrintPreview '如果是要打印,只要把 PrintPreview 改為 PrintOut
xlBook.Close SaveChanges:=False
xlApp.Quit '退出
****************
For iCols = 0 to rs.Fields.Count - 1
ws.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
ws.Range(ws.Cells(1, 1), _
ws.Cells(1, rs.Fields.Count)).Font.Bold = True
ws.Range("A2").CopyFromRecordset rs
*****************************
Private Sub CommandButton1_Click()
Dim CNN As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim Stpath, strSQL As String
Stpath = ThisWorkbook.Path & Application.PathSeparator & "學(xué)生檔案.mdb"
CNN.Open "provider=Microsoft.jet.OLEDB.4.0;data source=" & Stpath '& ";Jet OLEDB:Database Password=" & "123"
If ComboBox3.Value = "" Then
strSQL = "Select * from 檔案 WHERE 籍貫 LIKE '" & ComboBox2.Value & "'"
ElseIf ComboBox2.Value = "" Then
strSQL = "Select * from 檔案 WHERE 性別 LIKE '" & ComboBox3.Value & "'"
Else
strSQL = "Select * from 檔案 WHERE 性別 LIKE '" & ComboBox3.Value & "'" & "AND 籍貫 LIKE '" & ComboBox2.Value & "'"
End If
RST.Open strSQL, CNN
Sheet1.Range("A2:G10000").ClearContents '刪除原內(nèi)容
Sheet1.Cells(2, 1).CopyFromRecordset RST
RST.Close
Set RST = Nothing '示放RST 的內(nèi)存
Set CNN = Nothing '示放CNN的內(nèi)存
End Sub