Author: Unknown
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. Skose SQL = session ("TOEXCELSQL")' This is the query statement to output Excel, such as "SSECT * form cai where gender = 'Female' "FileName =" Excel.xls "'The file name of the Excel file to output, you can change the two sentences, don't change anything else.' You only need to modify the above two variables. Other I It is done. 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 set fs = createObject ("scripting.filesystemObject") set ts = fs.createtextfile (Server.Mappath (filename), true) Ts.writeline (data) Ts.close Set TS = Nothing set fs = nothingnd sub sub toExcel (filename, sql) 'This is a SQL statement and filename generates an Excel file set = serv Er.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 = toExcellr &" table> "Tou = readtext (" tou.txt ") Di = readtext (" di.txt ") TOEXCELLR = Tou & ToExcellr & Di Call Savetext (FileName, TOEXCELLR) End Sub%>
|