ASP export Excel file simple implementation code

Source: Internet
Author: User
The code is as follows Copy Code


<!--#include file= "conn.asp"-->

<%
Set xlapplication = Server.CreateObject ("Excel.Application") ' calls the Excel object
xlapplication.visible = False ' Do not need to open Excel
Xlapplication.sheetsinnewworkbook=1 ' to specify the number of tables in Excel
XlApplication.Workbooks.Add ' Add Workbook
Set Xlworksheet = xlapplication.worksheets (1) ' generates child objects for worksheet 1th
xlworksheet.name= Statistics ' specified sheet name
' Specifies the width of the column and alignment 1 left-aligned 2-aligned 3 Centered
XlApplication.ActiveSheet.Columns (1). columnwidth=5 
XlApplication.ActiveSheet.Columns (1). horizontalalignment=3    
XlApplication.ActiveSheet.Columns (2). columnwidth=10
XlApplication.ActiveSheet.Columns (2). Horizontalalignment=3
XlApplication.ActiveSheet.Columns (3). Columnwidth=20
XlApplication.ActiveSheet.Columns (3). Horizontalalignment=3

' XlApplication.ActiveSheet.Rows (i). Height of RowHeight = 30 ' line
' Specify the height of the column and the specific column
Xlworksheet.range (Xlworksheet.cells (1,1), Xlworksheet.cells (1,3)). MergeCells =true ' Merge columns
Xlworksheet.range ("A1"). Value= "2005 Statistics"
Xlworksheet.range ("A1"). Font. Size=14 ' font size
Xlworksheet.range ("A1"). Font.bold=true ' Bold
Xlworksheet.range ("A1"). Horizontalalignment=3 ' Horizontal alignment
Xlworksheet.range ("A1"). Verticalalignment=3 ' Vertical alignment

Xlworksheet.cells (2,1). Value = "Number"
Xlworksheet.cells (2,2). Value = "Name"
Xlworksheet.cells (2,3). Value = "unit"


' Xlworksheet.range ("A1:c1"). Borders.linestyle=1 ' Set row style

'--------------------------------------------------can do loop i=i+1 (database data)

I=1
strSQL = "SELECT * from Excel"
Set rs =conn.execute (strSQL)
If not rs.eof then
Do as not rs.eof
Xlworksheet.cells (2+i,1). Value = RS (0)
Xlworksheet.cells (2+i,2). Value = RS (1)
Xlworksheet.cells (2+i,3). Value = RS (2)
I=i+1
Rs.movenext
Loop
End If

'--------------------------------------------------

Set fs = CreateObject ("Scripting.FileSystemObject")
Tfile=server.mappath ("Test.xls")
If Fs. FileExists (Tfile) Then
Set f = fs. GetFile (Tfile)
F.delete true
Set F = Nothing
End If
Set fs = Nothing
Xlworksheet.saveas tfile ' Save file
Xlapplication.quit ' Release object
Set xlworksheet = Nothing
Set xlapplication = Nothing
%>

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.