怎么样将记录集输出到EXCEL表格中,请给出例子!谢谢!
解决方案 »
- SHFileOperation函数问题
- ----------------------------------菜鸟,数据库连接的问题--------------------------------
- 急求帮助!大家帮忙看看我的程序错在哪了,ADO SQL Server的,库应该连上了,查巡报424错!
- Activebar控制一些问题请教...
- 求助vb数据库连接问题
- 关于程序备份的问题,各位大哥帮忙,急!!!!
- 请帮帮忙吧!
- 谁知道Microsoft Message Queue 2.0的使用指南(50分)
- //请教ComboBox中增加内容///
- DCOMCNFG是个什么东东????
- inet 能否实现http文件上传功能??
- 超难问题,关于注册表的?
VB6 中将数据导出到 Excel 提速之法
李洪根
--------------------------------------------------------------------------------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"))
With xlQuery
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
End With
xlQuery.FieldNames = True '显示字段名
xlQuery.Refresh
With xlSheet
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Name = "黑体"
'设标题为黑体字
.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True
'标题字体加粗
.Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous
'设表格边框样式
End With
With xlSheet.PageSetup
.LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" ' & Gsmc
.CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:"
.RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"
.LeftFooter = "&""楷体_GB2312,常规""&10制表人:"
.CenterFooter = "&""楷体_GB2312,常规""&10制表日期:"
.RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"
End With
xlApp.Application.Visible = True
Set xlApp = Nothing '"交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
End Function注::在程序中引用'Microsoft Excel 9.0 Object Library'和ADO对象,机器必装Excel 2000
本程序在Windows 98/2000,VB 6 下运行通过。
^_^
Private Sub Command1_Click()
Dim acApp As Access.Application
Dim strSourcePath As String
Dim strReportPath As String
Dim strObjectName As String
strSourcePath = Text2.Text
strReportPath = Text1.Text
strObjectName = Text3.Text
Set acApp = GetObject(strSourcePath, "Access.Application")
'打开数据库
acApp.DoCmd.OutputTo acOutputTable, strObjectName, acFormatXLS, strReportPath
'转换指定表为Excel文件并存储到指定的目录
acApp.CloseCurrentDatabase
Set acApp = Nothing
End Sub
strSQL = "SELECT * INTO [Excel 8.0;Database=" & App.Path & _
"\book1.xls].[Sheet1] FROM Customers"
cnSrc.Execute strSQL
另外大家知道MS Excel Object Library对象的使用方法吗?有没有帮助文档?给我一份!
EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c -q -S"GNETDATA/GNETDATA" -U"sa" -P""'
Private Sub Command3_Click()
Dim objFileSystem As Object
Dim objExcelText As Object
Dim strTableString As String, i As Integer, strFileName As String
Dim pubConn As New ADODB.Connection
Dim rsTable As New ADODB.Recordset
Dim strConn As String
Dim strSQL As String strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=develop; password=12345;Data Source=ServerNmae"
pubConn.Open strConn
rsTable.CursorLocation = adUseClient
strSQL = "select top 10 * from gate_register"
rsTable.Open strSQL, pubConn, adOpenDynamic, adLockOptimistic
For i = 0 To rsTable.Fields.Count - 1
strTableString = strTableString & rsTable.Fields(i).Name & Chr(9) '獲取字段名
Next
strTableString = strTableString & rsTable.GetString '字段名+數據庫的記錄
cmDialog.CancelError = False
cmDialog.FileName = "FileName" '默認生成的文件名
cmDialog.DialogTitle = "Save Export File"
cmDialog.Filter = "Excel (*.xls)|*.xls|文本文件(*.DBF)|*.DBF|檔案文件(*.doc)|*.doc|所有文件(*.*)|*.*"
cmDialog.DefaultExt = "*.xls"
cmDialog.ShowSave
strFileName = cmDialog.FileName
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objExcelText = objFileSystem.createtextfile(strFileName, True)
objExcelText.writeline (strTableString)
objExcelText.Close
Set objFileSystem = Nothing
End Sub
//安装office后有帮助,里面应该比较全面了!
另外http://www.excelhome.net/index.asp 资料也挺多的还有老郭(郭安定)的,这个家伙挺有意思,看看再说!没仔细看过
http://www.oiio.com/
假设定义的记录名为myres
Dim myexcel As New Excel.Application
Dim mybook As New Excel.Workbook
Dim mysheet As New Excel.Worksheet
Set mybook = myexcel.Workbooks.Add '添加一个新的BOOK
Set mysheet = mybook.Worksheets.Add '添加一个新的SHEET
myexcel.visible=true
mysheet.Cells.CopyFromRecordset myres
mybook.SaveAs (m_ExcelName) '保存文件
下面是我常用的:
Private Sub Command3_Click()
On Error GoTo err1
Dim i As Long
Dim j As Long
Dim objExl As Excel.Application ‘声明对象变量
Me.MousePointer = 11 ‘改变鼠标样
Set objExl = New Excel.Application ‘初始化对象变量
objExl.SheetsInNewWorkbook = 1 ‘将新建的工作薄数量设为1
objExl.Workbooks.Add ‘增加一个工作薄
objExl.Sheets(1).Name = "book1" ‘改变新工作薄的名称
objExl.Sheets.Add ‘再次增加一个工作薄
objExl.Sheets(1).Name = "book2" ‘修改工作薄名称
objExl.Sheets("book2").Select ‘选中工作薄<book2>
For i = 1 To 5 ‘循环写入数据
For j = 1 To 5
objExl.Cells(i, j) = i & j
Next
Next
objExl.Sheets("book1").Select ‘选中工作薄<book1>
For i = 5 To 10 ‘循环写入数据
For j = 5 To 10
objExl.Cells(i, j) = i & j
Next
Next
objExl.Sheets("book1").Select ‘选中工作薄<book1>
objExl.ActiveWindow.View = xlPageBreakPreview ‘设置显示方式
objExl.ActiveWindow.Zoom = 100 ‘设置显示大小
objExl.Sheets("book2").Select ‘选中工作薄<book2>
objExl.ActiveWindow.View = xlPageBreakPreview ‘设置显示方式
objExl.ActiveWindow.Zoom = 100 ‘设置显示大小
objExl.Visible = True ‘使EXCEL可见
objExl.Application.WindowState = xlMaximized ‘EXCEL的显示方式为最大化
objExl.ActiveWindow.WindowState = xlMaximized ‘工作薄显示方式为最大化
objExl.SheetsInNewWorkbook = 3 ‘将默认新工作薄数量改回3个
Set objExl = Nothing ‘清除对象
Me.MousePointer = 0 ‘修改鼠标
Exit Sub
err1:
objExl.SheetsInNewWorkbook = 3
objExl.DisplayAlerts = False ‘关闭时不提示保存
objExl.Quit ‘关闭EXCEL
objExl.DisplayAlerts = True ‘关闭时提示保存
Set objExl = Nothing
Me.MousePointer = 0
End Sub