程式碼
<%
'================================================== =========
'將紀錄輸出為(.csv)文件
'製作日期:2003年3月
'作者 :塞北的雪
'================================================== =========
Class Export_CSV
private FSO '檔案系統對象
private fExport
dim Exp_Type
private sub Class_Initialize()
set FSO= Server.CreateObject("Scripting.FileSystemObject") '建立檔案系統物件實例
Exp_Type=1 '匯出類型 1為追加方式寫入一個檔案 2為新建一個新檔案再寫入
end sub
'關閉對象
private sub Class_Terminate()
fExport.Close
set FSO=nothing
end sub
'匯出主程式(用陣列指定欄位)
public Function ExportFile(fullpath,rsSet, HeaderArray)
if Exp_type=1 then
if not ExistFile(fullpath) then
if CreateFile(fullpath) then
WriteHeader HeaderArray
WriteBody rsSet,HeaderArray
else
err_Show "建立檔案時發生錯誤!"
end if
else
if OpenFile(fullpath) then
WriteBody rsSet,HeaderArray
else
err_Show "開啟檔案時發生錯誤!"
end if
end if
elseif Exp_type=2 then
if ExistFile(fullpath) then
err_Show "檔案名稱衝突!"
else
if CreateFile(fullpath) then
WriteHeader HeaderArray
WriteBody rsSet,HeaderArray
else
err_Show "建立檔案時發生錯誤!"
end if
end if
end if
if err=0 then
ExportFile=true
else
ExportFile=false
end if
end function
'指定的標題書寫
public sub WriteHeader( HeaderArray)
sLine = ""
for i=0 to ubound(HeaderArray,2)
sLine=sLine & "," & HeaderArray(0,i)
next
if sLine<>"" then
sLine=right(sLine,len(sLine)-1)
fExport.WriteLine(sLine)
end if
end sub
'記錄集指定的欄位資訊輸出
public sub WriteBody(rsSet, HeaderArray)
rsSet.movefirst
Do Until rsSet.EOF
sLine = ""
for i=0 to ubound(HeaderArray,2)
sLine=sLine & "," & rsSet(HeaderArray(1,i))
next
if sLine <> "" then
sLine = right(sLine, Len(sLine)-1)
fExport.WriteLine(sLine)
end if
rsSet.MoveNext
Loop
end sub
'匯出主程式(完全記錄集)
public Function ExportFile2(fullpath,rsSet)
if Exp_type=1 then
if not ExistFile(fullpath) then
if CreateFile(fullpath) then
WriteHeader2 rsSet
WriteBody2 rsSet
else
err_Show "建立檔案時發生錯誤!"
end if
else
if OpenFile(fullpath) then
WriteBody2 rsSet
else
err_Show "開啟檔案時發生錯誤!"
end if
end if
elseif Exp_type=2 then
if ExistFile(fullpath) then
err_Show "檔案名稱衝突!"
else
if CreateFile(fullpath) then
WriteHeader2 rsSet
WriteBody2 rsSet
else
err_Show "建立檔案時發生錯誤!"
end if
end if
end if
if err=0 then
ExportFile2=true
else
ExportFile2=false
end if
end function
'以欄位名為標題書寫
public sub WriteHeader2(rsSet)
sLine = ""
for i=0 to rsSet.fields.count-1
sLine=sLine & "," & rsSet.fields(i).name
next
if sLine<>"" then
sLine=right(sLine,len(sLine)-1)
fExport.WriteLine(sLine)
end if
end sub
'輸出記錄集中訊息
public sub WriteBody2(rsSet)
rsSet.movefirst
Do Until rsSet.EOF
sLine = ""
for i=0 to rsSet.fields.count-1
sLine=sLine & "," & rsSet.fields(i).value
next
if sLine <> "" then
sLine = right(sLine, Len(sLine)-1)
fExport.WriteLine(sLine)
end if
rsSet.MoveNext
Loop
end sub
'輸出錯誤訊息
private sub Err_Show(errDesc)
Response.Write errDesc
Response.End
end sub
'建立一個文件
public Function CreateFile(fullpath)
Set fExport = FSO.CreateTextFile(FullPath, false)
if err=0 then
CreateFile=true
else
CreateFile=false
end if
end function
'開啟一個文件
public Function OpenFile(fullpath)
set fExport=FSO.OpenTextFile(fullPath,8,false)
if err=0 then
OpenFile=true
else
OpenFile=false
end if
end function
'判斷指定檔案名稱的檔案是否存在
public function ExistFile(fullpath)
if FSO.FileExists(fullpath) then
ExistFile=true
else
ExistFile=false
end if
end function
end Class
%>