VB 實現(xiàn) 數(shù)據(jù)快速導(dǎo)入EXCEL
'***********************************************************************/
'* Function Name: ToExcel */
'* Input Arguments: */
'* Out Arguments : */
'* : */
'* Description : */
'* Author : by yarno QQ:84115357 */
'* Date : 2005-11-25 */
'***********************************************************************/
Public Function ToExcel()
On Error GoTo ErrorHandler
Dim exlapp As Excel.Application
Dim exlbook As Excel.Workbook
Set exlapp = CreateObject("Excel.Application")
Set exlbook = exlapp.Workbooks.Add
exlapp.Caption = "數(shù)據(jù)正在導(dǎo)出......"
exlapp.Visible = True
exlapp.DisplayAlerts = False
Dim exlsheet As Excel.Worksheet
Set exlsheet = exlbook.Worksheets.Add
exlsheet.Activate
Set exlsheet = exlsheet
exlsheet.Name = "我導(dǎo)出的數(shù)據(jù)"
'設(shè)置列寬
exlapp.ActiveSheet.Columns(1).ColumnWidth = 10
exlapp.ActiveSheet.Columns(2).ColumnWidth = 20
StrSql = "你的SQL語句"
Set exl_rs = PubSysCn.Execute(StrSql)
exlsheet.Range("A2").CopyFromRecordset exl_rs
exl_rs.Close
Set exl_rs = Nothing
exlapp.Worksheets("sheet1").Delete
exlapp.Worksheets("sheet2").Delete
exlapp.Worksheets("sheet3").Delete
exlapp.DisplayAlerts = True
exlapp.Caption = "數(shù)據(jù)導(dǎo)出完畢??!"
exlapp.Visible = True
Set exlapp = Nothing
Set exlbook = Nothing
Set exlsheet = Nothing
Exit Function
ErrorHandler:
MsgBox "EXCEL : " & err.Number & " : " & err.Description
End Function