用ASP将数据转为XML文件下载
类别: ASP教程
前一段日子在用ASP开发PDM系统.系统开发就涉及一些数据导入导出的程序!开始自己试过写成了CSV格式的导入导出程序.效果也不错,不过仍不满足,因为有些数据用CSV无法满足,比如有数据库中含有多个","号.就没办法了.求助了一些参考书,呵呵,终于找到了更方便的方法,就是用XML 导入导出数据库了!现在就把代码贴出,与大家分享!呵呵
总共需要三个文件:
conn.asp用于数据库连接!
download.asp下载页面
data_to_xml.asp转数据页面
文件名:
data_to_xml.asp
-----------------------------------------------
<!--#include file="download.asp"-->
<!--#include file="conn.asp"-->
<%
set rs=server.CreateObject("adodb.recordset")
set fso=server.CreateObject("Scripting.FileSystemObject")
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
xml_filepath=root_path & "loadfilefile_class.xml"
\'用SQL查出要导出的数据!
sql="select * from file_class"
rs.open sql,conn,1,3
if fso.fileexists(xml_filepath) then
fso.deletefile xml_filepath
end if
rs.save xml_filepath,1
\'\'----------------------------------------------
call transferfile(xml_filepath,"file_class.xml")
response.end
%>
conn.asp
-----------------------------------------------
<%
db_path=root_path & "datasyste_@k#ksks.asa"
\'response.write db_path
\'response.end
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db_path
\'如果你的服务器采用较老版本Access驱动,请用下面连接方法
\'connstr="driver={Microsoft Access Driver (*.mdb)};dbq=" & db_path
conn.Open connstr
%>
download.asp
------------------------------------------------------
<%
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
\'\' 文档作用:下载组件
\'\' 创建时间:2005-8-19
\'\' 修改情况:
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
const forreading=1
const tristatetrue=-1
const file_transfer_size=16384
response.Buffer=true
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
\'\' 用于文件下载!
\'\' f_path:文件的绝对路径,f_filename:要保存的文件名
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
function transferfile(f_path,f_filename)
dim path,mimetype,filename
dim objfilesystem,objfile,objstream
dim char
dim sent
path=f_path
filename=f_filename
send=0
transferfile=true
set objfilesystem=server.CreateObject("scripting.filesystemobject")
set objfile=objfilesystem.getfile(path)
mimetype=objfile.type
set objstream=objfile.openastextstream(forreading,tristatetrue)
response.AddHeader "content-disposition","attachment;filename=" & filename
response.AddHeader "content-length",objfile.size
do while not objstream.atendofstream
char = objstream.read(1)
response.BinaryWrite(char)
sent=sent+1
if(sent mod file_transfer_size)=0 then
response.Flush()
if not response.IsClientConnected then
transferfile=false
exit do
end if
end if
loop
response.flush
if not response.IsClientConnected then transferfile=false
objstream.close
set objstream=nothing
set objfilesystem=nothing
end function
%>
总共需要三个文件:
conn.asp用于数据库连接!
download.asp下载页面
data_to_xml.asp转数据页面
文件名:
data_to_xml.asp
-----------------------------------------------
<!--#include file="download.asp"-->
<!--#include file="conn.asp"-->
<%
set rs=server.CreateObject("adodb.recordset")
set fso=server.CreateObject("Scripting.FileSystemObject")
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
xml_filepath=root_path & "loadfilefile_class.xml"
\'用SQL查出要导出的数据!
sql="select * from file_class"
rs.open sql,conn,1,3
if fso.fileexists(xml_filepath) then
fso.deletefile xml_filepath
end if
rs.save xml_filepath,1
\'\'----------------------------------------------
call transferfile(xml_filepath,"file_class.xml")
response.end
%>
conn.asp
-----------------------------------------------
<%
db_path=root_path & "datasyste_@k#ksks.asa"
\'response.write db_path
\'response.end
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db_path
\'如果你的服务器采用较老版本Access驱动,请用下面连接方法
\'connstr="driver={Microsoft Access Driver (*.mdb)};dbq=" & db_path
conn.Open connstr
%>
download.asp
------------------------------------------------------
<%
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
\'\' 文档作用:下载组件
\'\' 创建时间:2005-8-19
\'\' 修改情况:
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
const forreading=1
const tristatetrue=-1
const file_transfer_size=16384
response.Buffer=true
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
\'\' 用于文件下载!
\'\' f_path:文件的绝对路径,f_filename:要保存的文件名
\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
function transferfile(f_path,f_filename)
dim path,mimetype,filename
dim objfilesystem,objfile,objstream
dim char
dim sent
path=f_path
filename=f_filename
send=0
transferfile=true
set objfilesystem=server.CreateObject("scripting.filesystemobject")
set objfile=objfilesystem.getfile(path)
mimetype=objfile.type
set objstream=objfile.openastextstream(forreading,tristatetrue)
response.AddHeader "content-disposition","attachment;filename=" & filename
response.AddHeader "content-length",objfile.size
do while not objstream.atendofstream
char = objstream.read(1)
response.BinaryWrite(char)
sent=sent+1
if(sent mod file_transfer_size)=0 then
response.Flush()
if not response.IsClientConnected then
transferfile=false
exit do
end if
end if
loop
response.flush
if not response.IsClientConnected then transferfile=false
objstream.close
set objstream=nothing
set objfilesystem=nothing
end function
%>
- 上一篇: 一段打开光驱的ASP代码
- 下一篇: 菜鸟十天学会ASP教程之第一天:安装调试ASP的环境
-= 资 源 教 程 =-
文 章 搜 索