一般是在ACCESS或是SQLSERVER中查尋,或是匯總,然后生成一個(gè)‘記錄集’可以顯示在GRID里,也可以將這個(gè)記錄集導(dǎo)出到磁盤中。
下面可以導(dǎo)出Xls,DBF,DB,MDB(表),這些功能是由ISAM數(shù)據(jù)庫(kù)接口實(shí)現(xiàn),為了導(dǎo)出各種版本的文件,我在MS網(wǎng)站了最新的JET4和MDAC6。前者到用于桌面數(shù)據(jù)庫(kù)如ACCESS,F(xiàn)OXPRO的組件,后者是實(shí)現(xiàn)新版本ADO組件。分別在:
http://download.microsoft.com/download/access2000/SP/4.0/NT5/EN-US/Jet40SP5_W2K.exe
http://download.microsoft.com/download/dasdk/install/2.60.6526.3/WIN98Me/CN/mdac_typ.exe
這些是標(biāo)準(zhǔn)的SQL導(dǎo)出語(yǔ)句:
select * into [Excel 8.0;database=導(dǎo)出目錄].導(dǎo)出表名 from 表
select * into [FoxPro 2.6;database=導(dǎo)出目錄].導(dǎo)出表名 from 表
select * into [FoxPro 2.5;database=同上].導(dǎo)出表名 from 表
select * into [dBase III;database=同上].導(dǎo)出表名 from 表
select * into [Paradox 4.X;database=同上].導(dǎo)出表名 from 表
select * into [;database=C:\temp\xxx.mdb].導(dǎo)出表名 from 表
下面程序?yàn)閷?shí)現(xiàn)用戶自定議文件名用變量代替一部分。
http://go.163.com/~chunpeng/project/export.jpg
http://go.163.com/~chunpeng/project/Export.zip 點(diǎn)這里原程序文件?! ?
'請(qǐng)先引用ADODB類庫(kù)。
Dim Export_Str, mdbTable As String
Dim rsExport As New ADODB.Recordset
Dim conn As New ADODB.Connection
Private Sub Close_cmd_Click()
Unload Me
End Sub
Private Sub EXport_cmd_Click()
Dim myPath, myStr As String, myPos As Integer
'******************處理選擇的各種表的導(dǎo)出
With Dialog1
If myOption(2).Value Then
.FilterIndex = 1
.ShowSave
myStr = StrReverse(.FileName) '串取反
myPos = InStr(myStr, "\") '在反字符串中,找從左開始第一個(gè)\的位置
On Error GoTo myError '防FILENAME為空,MID出錯(cuò)
myPath = StrReverse(Mid(myStr, myPos)) '取目錄部分,并還原.
myStr = StrReverse(Left(myStr, myPos - 1)) '取文件名
Export_Str = "select * into [dBase III;database=" & myPath & "]." & myStr & " from Customers"
.DefaultExt = "*.DBF"
ElseIf myOption(3).Value Then
mdbTable = InputBox("請(qǐng)給導(dǎo)出到MDB文件的表確定表名")
.FilterIndex = 2
.ShowSave
Export_Str = "select * into [;database=" & .FileName & "]." & mdbTable & " from Customers"
.DefaultExt = "*.MDB"
ElseIf myOption(4).Value Then
.FilterIndex = 3
.ShowSave
Export_Str = "select * into [Excel 8.0;database=" & .FileName & "].Customers from Customers"
.DefaultExt = "*.XLS"
ElseIf myOption(5).Value Then
.FilterIndex = 4
.ShowSave
myStr = StrReverse(.FileName) '串取反
myPos = InStr(myStr, "\") '在反字符串中,找從左開始第一個(gè)\的位置
On Error GoTo myError '防FILENAME為空,MID出錯(cuò)
myPath = StrReverse(Mid(myStr, myPos)) '取目錄部分,并還原.
myStr = StrReverse(Left(myStr, myPos - 1)) '取文件名
Export_Str = "select * into [Paradox 4.X;database=" & myPath & "]." & myStr & " from Customers"
.DefaultExt = "*.DB"
End If
End With
'*****生成文件
Debug.Print Export_Str
If rsExport.State = 1 Then
rsExport.Close
End If
If Dir(Dialog1.FileName) <> "" Then
On Error GoTo myError '防用戶沒選文件
If Dialog1.FilterIndex <> 2 Then
Kill (Dialog1.FileName)
End If
rsExport.Open Export_Str, conn, adOpenStatic, adLockOptimistic
Else
rsExport.Open Export_Str, conn, adOpenStatic, adLockOptimistic
End If
myError:
Exit Sub
End Sub
Private Sub Form_Load()
'聯(lián)接數(shù)據(jù)庫(kù)并打開記錄集
conn.CursorLocation = adUseServer
conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\NWind.mdb;"
rsExport.Open "select *from Customers", conn, adOpenStatic, adLockOptimistic
Set Grid1.DataSource = rsExport
'初始化對(duì)話筐
With Dialog1
.Filter = "FoxBase/FoxPro (*.DBF) *.DBF Access 8.0(*.MDB) *.MDB Excel 8.0(*.XLS) *.XLS Paradox 4.x(*.DB) *.DB"
.DialogTitle = "導(dǎo)出文件為"
.CancelError = False
End With
End Sub