rst.Open "select c1,c2,c3,...,c255 from table1", conn, adOpenKeyset, adLockOptimistic
Dim xls As New Excel.Application
Dim book As New Excel.Workbook
Dim sheet As New Excel.Worksheet
Set book = xls.Workbooks.Open("c:\test.xls")
Set sheet = book.Sheets(1)
sheet.Range(sheet.Cells(1, 1), sheet.Cells(1, 1)).CopyFromRecordset rst
rst.Close
rst.Open "select c256,c257,... from table1", conn, adOpenKeyset, adLockOptimistic
Set sheet = book.Sheets(2)
sheet.Range(sheet.Cells(1, 1), sheet.Cells(1, 1)).CopyFromRecordset rst
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 = g_strDbConn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = strOpen
.Open
End With
With Rs_Data
If .RecordCount < 1 Then
MsgBox "数据库中没有查询到符合条件的记录!", vbExclamation, App.Title
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"))
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb;Persist Security Info=False"
rst.Open "select * from table1", conn, adOpenKeyset, adLockOptimistic
Dim xls As New Excel.Application
Dim book As New Excel.Workbook
Dim sheet As New Excel.Worksheet
Set book = xls.Workbooks.Open("c:\test.xls")
Set sheet = book.Sheets(1)
sheet.Range(sheet.Cells(1, 1), sheet.Cells(1, 1)).CopyFromRecordset rst
xls.Visible = True
'......
End Sub
rst.Open "select * from table1 where ......", conn, adOpenKeyset, adLockOptimistic
Dim xls As New Excel.Application
Dim book As New Excel.Workbook
Set book = e.Workbooks.Open("c:\test.xls")
xls.Visible = True
book.Sheets(1).Range(s.Cells(1, 1), s.Cells(1, 1)).CopyFromRecordset rst
Set objExlBok = objExlApp.Workbooks.Add
Set objExlSht = objExlBok.Sheets(1)
With aa
For i = 0 To aa.Rows - 1
For j = 0 To aa.Cols - 1
objExlSht.Cells(i + 1, j + 1) = .TextMatrix(i, j)
Next j
ccrpProbar.Value = i
Next i
End With
这样很慢的,有更好更快的方法吗?谢谢。