Admiral often outputs a variety of query results into Excel, so make the following small program, used to output the result of a SQL SELECT query to an Excel format file, this program you only need to set it Since obtaining a record set SQL SQL SELECT query statement and a file name, the program can output the Excel format file, this program consists of three files, the first file name is: TOEXCEL.ASP is the main file, The content is as follows:
<%
'The front is to link to the database, please write the relevant statement yourself, this is slightly
SQL = session ("TOEXCELSQL") 'This is the query statement to output Excel, such as "Samplect * form cai where gender =' female '" filename = "excel.xls"' to output the file name of the Excel file, you only Change the above two sentences, don't change anything else.
'You just modify the above two variables. Others I have done it.
Call toExcel (filename, sql) set conn = Nothing
Function ReadText (filename) 'This is a function set adf = server.createObject ("adoDb.stream") with adf .type = 2 .LineseParator = 10 .open .loadFromfile (server.mappath (filename) ) .Charset = "GB2312" .position = 2 readtext = .readtext .cancel () .close () end with set ads = nothingend function
Sub Savetext (filename, data) 'This is a function of writing files set fs = createObject ("scripting.FilesystemObject") set ts = fs.createtextfile (server.mappath (filename), true) Ts.writeline (data) Ts.Close set ts = nothing set fs = nothingend Sub
Sub toExcel (filename, sql) 'This is a SQL statement and filename generating an Excel file set RS = Server.createObject ("AdoDb.Recordset") RS.Open SQL, CONN, 1, 3 TOEXCELLR = "
p> td> "end if else toexcellr = toExcellr &" | "& rs (fieldname (i)) &" td> "end if Next TOEXCELLR = TOEXCELLR & " tr>" rs.movenext loop toExcellr = TOEX Cellr & " Table>" Tou = ReadText ("Tou.txt") Di = ReadText ("Di.txt") TOEXCELLR = Tou & toExcellr & Di Call Savetext (FileName, TOEXCELLR) End Sub%>
|