xml|資料|下載
前一段日子在用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
'如果你的伺服器採用較老版本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
%>