用ASP将数据转为XML文件下载

发表于:2007-06-30来源:作者:点击数: 标签:
前一段日子在用ASP 开发 PDM系统.系统开发就涉及一些数据导入导出的程序!开始自己试过写成了CSV格式的导入导出程序.效果也不错,不过仍不满足,因为有些数据用CSV无法满足,比如有 数据库 中含有多个","号.就没办法了.求助了一些参考书,呵呵,终于找到了更方便的

  前一段日子在用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 & "\loadfile\file_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 & "\data\syste_@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
 @#如果你的服务器采用较老版本Aclearcase/" target="_blank" >ccess驱动,请用下面连接方法
 @#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
%>

原文转自:http://www.ltesting.net