Program code
<%
'================================================== =========
'Export records as (.csv) file
'Date of production: March 2003
'Author: Snow in Saibei
'================================================== =========
Class Export_CSV
private FSO 'File system object
private fExport
dim Exp_Type
private sub Class_Initialize()
set FSO= Server.CreateObject("Scripting.FileSystemObject") 'Create a file system object instance
Exp_Type=1 'Export type 1 is append mode to write a file 2 is to create a new file and then write
end sub
'Close the object
private sub Class_Terminate()
fExport.Close
set FSO=nothing
end sub
'Export the main program (use array to specify fields)
public Function ExportFile(fullpath,rsSet, HeaderArray)
if Exp_type=1 then
if not ExistFile(fullpath) then
if CreateFile(fullpath) then
WriteHeaderHeaderArray
WriteBody rsSet,HeaderArray
else
err_Show "An error occurred while creating the file!"
end if
else
if OpenFile(fullpath) then
WriteBody rsSet,HeaderArray
else
err_Show "An error occurred while opening the file!"
end if
end if
elseif Exp_type=2 then
if ExistFile(fullpath) then
err_Show "File name conflict!"
else
if CreateFile(fullpath) then
WriteHeaderHeaderArray
WriteBody rsSet,HeaderArray
else
err_Show "An error occurred while creating the file!"
end if
end if
end if
if err=0 then
ExportFile=true
else
ExportFile=false
end if
end function
'Write the specified title
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
'Output of field information specified by the record set
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
'Export the main program (complete record set)
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 "An error occurred while creating the file!"
end if
else
if OpenFile(fullpath) then
WriteBody2 rsSet
else
err_Show "An error occurred while opening the file!"
end if
end if
elseif Exp_type=2 then
if ExistFile(fullpath) then
err_Show "File name conflict!"
else
if CreateFile(fullpath) then
WriteHeader2 rsSet
WriteBody2 rsSet
else
err_Show "An error occurred while creating the file!"
end if
end if
end if
if err=0 then
ExportFile2=true
else
ExportFile2=false
end if
end function
'Write the field name as the title
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
'Output record set information
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
'Output error message
private sub Err_Show(errDesc)
Response.Write errDesc
Response.End
end sub
'Create a file
public Function CreateFile(fullpath)
Set fExport = FSO.CreateTextFile(FullPath, false)
if err=0 then
CreateFile=true
else
CreateFile=false
end if
end function
'Open a file
public Function OpenFile(fullpath)
set fExport=FSO.OpenTextFile(fullPath,8,false)
if err=0 then
OpenFile=true
else
OpenFile=false
end if
end function
'Determine whether the file with the specified file name exists
public function ExistFile(fullpath)
if FSO.FileExists(fullpath) then
ExistFile=true
else
ExistFile=false
end if
end function
endClass
%>