There are tools for exporting tables directly to excel in Access and MSSQLSERVER, but when customers need to generate Excel tables in the database tables in the program, it's hard to do it with the usual T-SQL statements! Here's what I wrote with the online profile and the customer's needs. For learning and communication only.
The
<%
set Rs=server. CreateObject ("Adodb.recordset")
sql= "SELECT * from Stu_signup ORDER by ID ASC"
rs.open sql,conn,1,1
Set fs = Ser Ver. CreateObject ("Scripting.FileSystemObject")
'--let's say you want the generated Excel file to be stored as follows
filename = Server.MapPath ( "Users.xls")
'--If the original Excel file exists, delete it
if FS. FileExists (filename) then
fs. DeleteFile (filename)
end if
'--Create an Excel file
Set myfile = fs. CreateTextFile (filename,true)
Dim ttxt,file,filepath,writefile
Ttxt= "User.xls" ' Take a filename for the file to be written, suffix can be txt,xls, here I use CSV, this kind of file opens also Excel table
Set file = CreateObject ("Scripting.FileSystemObject")
Application.Lock ' write the file's storage path, be sure to open read and write permissions under this path
Filepath=server.mappath (ttxt)
Set WriteFile = file. CreateTextFile (filepath,true)
' writes the first row in the table, the field description, which is written according to your actual data table field
if not (rs.eof and RS.BOF) then
Dim trline,responsestr
Strline= ""
' If you don't need to change the list name
For each x in Rs. Fields
Strline=strline &x.name& chr (9)
Next
' If the fields in your data table are not what the user wants to see, here you can put your field in the array to write the first line in Excel
Arr=array ("number", "Student Name", "Student gender", "Birthday", "school", "Grade", "Class", "English Age of Enlightenment", "whether learning English in extra-curricular institutions", "extracurricular institution name", "Enrollment Time", "Parent's name", "Parental title", "Parent cell phone" , "Parent work Unit", "Parent occupation", "education", "Registration date")
For i=0 to Ubound (arr)
StrLine = strLine & Arr (i) & Chr (9)
Next
MyFile.WriteLine StrLine
Do and not Rs. Eof
Strline= ""
J=0
For each x in Rs. Fields
StrLine = StrLine &x.value& chr (9)
Next
MyFile.WriteLine StrLine
Rs. MoveNext
Loop
Myfile.close
Rs.close
Set rs=nothing
End If
' Finish writing
%>
Tools for exporting tables directly to excel in Access and MSSQLSERVER