How ASP exports data to Excel tables

Source: Internet
Author: User
Tags first row


<%
Rem Initialization excelapplication Working environment
Dim Excelapp,ebook,esheet
Set excelapp = CreateObject ("Excel.Application") ' establishes an Excel object
Excelapp.displayalerts=false ' does not show warning
Excelapp.application.visible=false ' does not display interface
Rem initialization of Excel data
' ExcelApp.Workbooks.Open (Server.MapPath ("Zzz.xls")) ' opens the Excel work book, which replaces the following line
Set ebook=excelapp.workbooks.add ' new Excel work Ben
Set ebook=excelapp.workbooks (1) ' references the first working book
Set esheet = ebook.worksheets (1) ' References first worksheet
Rem Data Import
Dim i,img
i = 1
For I=1 to 5
Esheet.cells (i,1). value= "Field One" &i
Esheet.cells (i,2). value= "Field two" &i
Esheet.cells (i,3). value= "Field three" &i
Esheet.cells (i,4). Select ' Selects cell 4th cells in row I
Set Img=esheet.pictures.insert (Server.MapPath ("people.jpg")) ' Inserts a picture in the above position and gets a reference to the picture
Img. Top=img. Top+2 ' Adjusts the picture position, the same below, otherwise it will press the table sideline
Img. Left=img. Left+2 ' units are pounds
Esheet.rows (i). Rowheight=img. Height+4 ' Adjusts the height of the current line so that it automatically is the same height as the picture
Next
Rem preservation of the work done above
' Ebook.save ' If you are opening an existing Excel file, you can use this line instead of the following line
Ebook.saveas Server.MapPath ("Zzz.xls")
Set esheet=nothing
Set ebook=nothing
' Excelapp.quit ' must exit, otherwise Excel's process stays in the operating system.
Set excelapp = Nothing
%>


<%
Dim Conn,rs,sql
Sub dbopen ()
Dim Db:db=server.mappath ("Zzz.xls")
Set conn=server.createobject ("Adodb.connection")
On Error Resume Next
Conn. Open "Provider=Microsoft.Jet.OLEDB.4.0; Extended properties= "" Excel 8.0; Hdr=yes ";D ata source=" & DB
Rem HDR defaults to Yes, which means the first row is the field name, otherwise it is considered content
Rem for Excel2007, but should be: "provider=microsoft.ace.oledb.12.0; Extended properties=excel 12.0;data source=xxx.xlsx; "
If Err.number<>0 Then
Err.Clear
Response.Write ("Response.End ()
End If
On Error GoTo 0
End Sub
Sub dbclose ()
IF Isnotblank (conn) Then
Conn. Close ()
Set conn=nothing
End If
End Sub
Function Isnotblank (ByRef TempVar)
IsBlank = True
Select case VarType (TempVar)
Case 0,1 ' Empty & Null
IsBlank = False
Case 9 ' Object
If TypeName (TempVar) = "Nothing" Or TypeName (TempVar) = "Empty" Then
IsBlank = False
End If
End Select
End Function
Call Dbopen ()
Sql= "SELECT * from [sheet1$]" "Note the table name, you need to add a symbol after the sheet name $
Set Rs=conn. Execute (SQL)
While not Rs. Eof
Response.Write (RS (0) & ",")
Response.Write (RS (1) & ",")
Response.Write (RS (2) & "<br/>" &vbcrlf)
Rs. Movenext
Wend
Rs. Close:set rs=nothing
Call Dbclose ()
%>

Related Article

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.