Import SQL query results into Excel

Source: Internet
Author: User
Tags chr flush
<%
Databasename= "Db.mdb"
Apath=server.mappath (".") & "/xmllover.xls"

Set Conn=server.createobject ("Adodb.connection")
Constr= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & Server.MapPath (DatabaseName)
Conn.Open Constr
Set Rs=server.createobject ("Adodb.recordset")
' Orderly
Sql= "SELECT * FROM Employees ORDER BY EmployeeID ASC"
' Reverse order
' sql= ' SELECT * FROM Employees ORDER BY EmployeeID DESC "
Rs.Open Sql,conn
Msg= "No." & Chr (9) & "Name" & vbCrLf
Do as not rs.eof
Msg=msg & RS ("EmployeeID") & Chr (9) & RS ("LastName") & vbCrLf
Rs.movenext
Loop
Set F=server.createobject ("Scripting.FileSystemObject")
Set Myfile=f.createtextfile (apath,true)
MyFile.WriteLine msg
Myfile.close
Rs.close
Set rs=nothing
%>

Another way is to directly output the file in CSV format, and then use response. For output,

Sub CSV ()
Response.Buffer = True
Response.AddHeader "Content-disposition", "attachment"; Filename=member "&formatdatetime" (date,2) & ". csv"
Response.Charset = "Utf-8"
Response.ContentType = "Application/octet-stream"
' Add table column name output code here
Response.Flush ()
' Add the data output code here
Response.Flush ()
End Sub

Let's take a look at an ASP tutorial instance that generates an Excel document

<%dim excelstring
Dim excelstr
Dim excelstr2
Dim head
excelstring = ""
Head = "Sn" & "," & "Reason" & "," & "Empid" & "," & "EmpName" & "," & "Ext" & "," &am P "Depname"
Excel= "Creat"
If excel<> "" Then
Set rs2 = Server.CreateObject ("ADODB. Recordset ")
SQL2 = "SELECT * FROM Value_table"
Rs2.open sql2,conn,1,3
Do as not rs2.eof
Excelstr = "" & Rs2 (0) & "" "" "" & Rs2 (1) & "" "," "& Rs2 (2) &" "", "" & Rs2 (3) & "" "," "" & Rs2 (4) & "" "," "" "& Rs2 (5) &" "" "
EXCELSTR2 = excelstr2 & Chr (a) & Excelstr
Rs2.movenext
Loop
excelstring = Head &AMP;CHR & EXCELSTR2
' Response. Write (excelstring)
' Rs2.close
Set rs2=nothing
Set fs = CreateObject ("Scripting.") FileSystemObject ")
Set exc = fs. OpenTextFile (server. MapPath ("Excel.csv"), 2,true)
Exc.write (excelstring)
If Fs. FileExists (Server.MapPath ("Excel.csv")) Then
Response.Write ("<a href=excel.csv> report has been generated, click View </a>")
' Response.Write (excelstring)
Else
Response.Write ("Generate Report failed!") ")
End If
End If%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.