先引用ExportToExcel
Option Explicit
Dim DBToExcel As ExportToExcel.ToExcel
Dim Adoconn As New ADODB.Connection
Private Sub Command1_Click()
Set DBToExcel = New ExportToExcel.ToExcel
Dim StrSql As String
StrSql = "select * from Customers"
'StrSql ="select companyname as 公司名,contactname as 联系人 from Customers" '测试2
DBToExcel.QueryToExcel Adoconn, StrSql
Set DBToExcel = Nothing
End Sub
Private Sub Form_Load()
Dim StrConnect As String
StrConnect = "driver={sql server};server=127.0.0.1;uid=SA;pwd=;database=NorthWind"
Adoconn.ConnectionString = StrConnect
Adoconn.Open
End Sub
Option Explicit
Dim Adoconn As New ADODB.Connection '定义ADO连接
Public Function QueryToExcel(ByVal Strcnn As ADODB.Connection, ByVal StrOpen As String)
'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL
'* 用法:ExporToExcel(sql查询字符串)
'* 注:须在程序中引用'Microsoft Excel 9.0 Object Library'和ADO对象,机器必装Excel 2000
'*********************************************************
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Long
Dim Icolcount As Long
Dim xlApp As New Excel.Application '定义Excel对象
Dim xlBook As Excel.Workbook '定义工作薄
Dim xlSheet As Excel.Worksheet '定义工作表
Dim xlQuery As Excel.QueryTable
On Error GoTo err:
If Adoconn.State = 1 Then Adoconn.Close
Set Adoconn = Strcnn
With Rs_Data
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = Adoconn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = StrOpen
.Open
End With
With Rs_Data
If .RecordCount < 1 Then
MsgBox "没有找到指定的记录!", vbInformation, "查询"
Exit Function
End If
'记录总数
Irowcount = .RecordCount
'字段总数
Icolcount = .Fields.Count
End With
Set xlApp = CreateObject("Excel.Application")
Set xlBook = Nothing
Set xlSheet = Nothing
Set xlBook = xlApp.Workbooks().Add
Set xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
'添加查询语句,导入EXCEL数据
Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("a1")) '从第几行,第几列开始显示
Excel 是一个非常优秀的报表制作软件,用VBA可以控制其生成优秀的报表,本文通过添加查询语句的方法,即用Excel中的获取外部数据的功能将数据很快地从一个查询语句中捕获到EXCEL中,比起往每个CELL里写数据的方法提高许多倍。
在程序中引用Microsoft Excel 9.0 Object Library,将下文加入到一个模块中,窗体中调用如下ExporToExcel("select * from table")。则实现快速将数据导出到EXCEL中。
Public Function ExporToExcel(strOpen As String)
'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL
'* 用法:ExporToExcel(sql查询字符串)
'*********************************************************
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Integer
Dim Icolcount As Integer
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlQuery As Excel.QueryTable
With Rs_Data
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = Cn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = strOpen
.Open
End With
With Rs_Data
If .RecordCount < 1 Then
MsgBox ("没有记录!")
Exit Function
End If
'记录总数
Irowcount = .RecordCount
'字段总数
Icolcount = .Fields.Count
End With
Set xlApp = CreateObject("Excel.Application")
Set xlBook = Nothing
Set xlSheet = Nothing
Set xlBook = xlApp.Workbooks().Add
Set xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
'添加查询语句,导入EXCEL数据
Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("a1"))