在VB6中导出EXCEL,FOXPRO,PRODOX格式的表

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 cocoboy79 的 blog

 

mis系统在月末由于业务的需要总要汇总当月业务情况,并且导出报盘,我把我的程序中的这一部分功能单拿出来,做成一个小的程序,仅供参考。

一般是在access或是sqlserver中查寻,或是汇总,然后生成一个‘记录集’可以显示在grid里,也可以将这个记录集导出到磁盘中。

下面可以导出xls,dbf,db,mdb(表),这些功能是由isam数据库接口实现,为了导出各种版本的文件,我在ms网站下载了最新的jet4和mdac6。前者到用于桌面数据库如access,foxpro的组件,后者是实现新版本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

这些是标准的sql导出语句:
select * into [excel 8.0;database=导出目录].导出表名 from 表
select * into [foxpro 2.6;database=导出目录].导出表名 from 表
select * into [foxpro 2.5;database=同上].导出表名 from 表
select * into [dbase iii;database=同上].导出表名 from 表
select * into [paradox 4.x;database=同上].导出表名 from 表
select * into [;database=c:\temp\xxx.mdb].导出表名 from 表
下面程序为实现用户自定议文件名用变量代替一部分。
http://go.163.com/~chunpeng/project/export.jpg

http://go.163.com/~chunpeng/project/export.zip 点这里下载原程序文件。


'请先引用adodb类库。
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

'******************处理选择的各种表的导出
with dialog1
if myoption(2).value then
.filterindex = 1
.showsave
mystr = strreverse(.filename) '串取反
mypos = instr(mystr, "\")      '在反字符串中,找从左开始第一个\的位置
on error goto myerror  '防filename为空,mid出错
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("请给导出到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, "\")      '在反字符串中,找从左开始第一个\的位置
on error goto myerror  '防filename为空,mid出错
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()
'联接数据库并打开记录集
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

'初始化对话筐
with dialog1
.filter = "foxbase/foxpro (*.dbf)|*.dbf|access 8.0(*.mdb)|*.mdb|excel 8.0(*.xls)|*.xls|paradox 4.x(*.db)|*.db"
.dialogtitle = "导出文件为"
.cancelerror = false
end with
end sub

 


 

本文关键:在VB6中导出EXCEL,FOXPRO,PRODOX格式的表
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top