Tools for exporting tables directly to excel in Access and MSSQLSERVER

Source: Internet
Author: User
Tags mssqlserver first row

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

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.