可以快速导出使用excel 就有该功能
Public Function ExportToExcel(ByVal strOpen As String, Title As String, dizhi As String, con As ADODB.Connection)'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL'* 用法:ExporToExcel(strOpen查询字符串,titile
'*excel标题,dizhi 保存路径,con 数据库连接地址)
'*********************************************************
lok: On Error GoTo er
Screen.MousePointer = 11
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Long
Dim Icolcount As Long
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 = con
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = strOpen
DoEvents
' Debug.Print strOpen
.Open
End With
Debug.Print strOpen
' Set Rs_Data = Open_rst_from_str(strOpen)
With Rs_Data
If .RecordCount < 1 Then
MsgBox ("没有记录!")
Screen.MousePointer = 0
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")
'添加查询语句,导入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
Dim i As Integer, Zd As String
With xlSheet
For i = 1 To 6
Zd = .Range(.Cells(1, 1), .Cells(1, Icolcount)).item(1, i)
' .Range(.Cells(1, 1), .Cells(1, Icolcount)).Item(1, i) = Lm_YwToZw(Zd)
Next
.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
' .Range(.Cells(Irowcount + 2, Icolcount)).Text = Zje
'设表格边框样式
End With
XlApp.Visible = True
XlApp.Application.Visible = True
' xlBook.SaveAs dizhi
Set XlApp = Nothing '"交还控制给Excel
Set xlbook = Nothing
Set xlSheet = Nothing
Screen.MousePointer = 0
Exit Function
er:
' Dispose_Err
MsgBox err.Description & " 从新导报表,请等待!"
GoTo lok:
End Function
使用这个模块就可以,你可以看看引用的函数即可
VB读取数据库并显示出来的方法可参考下面用控件的实现方法:
ACCESS数据库,用VB6里的Data控件,选中它,设置属性:
DatabaseName 属性:连接到数据库文件
RecordSource 属性:连接数据表(可以用Select语句返回记录集)
然后使用它的:data1.Recordset.Fields(“字段名”),就可以返回获得你说的内容了。
privatesub
search()
dim
str
as
string
dim
res
as
adodb.recordset
dim
cnn
as
ADODB.Connection
//数据库的连接,根据实际情况设置
Set
cnn
=
New
ADODB.Connection
cnn.Provider
=
"Microsoft.Jet.OLEDB.4.0"
cnnFile
=
"Data
Source="
&
App.Path
&
"\data.mdbPersist
Security
Info=False
cnn.Open
cnnFile
str="
select
*
from
authorityInfo
where
编号='"&
trim(text1)
&"'"
set
res.Open
str,
cnn,
adOpenStatic,
adLockOptimistic
MSHFlexGrid1.datasource=res
end
sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)