Four ways to export data readings to Excel files using ASP

Source: Internet
Author: User
Tags client
excel| Export excel| Data author: Tonny from: wei-net power http://www.weiw.com/
If you want to reprint, please specify the source.

Author Preface: This kind of knowledge point, there are many on the net, here I just make a summary.

First, with OWC
What is OWC?
OWC is the abbreviation for Office Web compent, Microsoft's Office Web Component, which provides for drawing graphics in the Web

A flexible and most basic mechanism. In an intranet environment, if you can assume that a specific browser exists on the client and a

Powerful software, such as IE5 and Office 2000, you can use the Office Web Component to provide an interactive graphics open

Hair environment. In this mode, the client workstation will share a significant proportion of the entire task.
Detailed information on this site can also be found.
<%option Explicit
Class Excelgen
Private Objspreadsheet

Private Icoloffset

Private Irowoffset
Sub Class_Initialize ()
Set Objspreadsheet = Server.CreateObject ("OWC. Spreadsheet ")
Irowoffset = 2
Icoloffset = 2
End Sub

Sub Class_Terminate ()
Set objspreadsheet = Nothing ' clean up
End Sub

Public Property Let ColumnOffset (Icoloff)
If Icoloff > 0 Then
Icoloffset = Icoloff
Else
Icoloffset = 2
End If
End Property

Public Property Let RowOffset (Irowoff)
If Irowoff > 0 Then
Irowoffset = Irowoff
Else
Irowoffset = 2
End If
End Property Sub Generateworksheet (objRS)
' Populates the EXCEL worksheet based on a Recordset ' s contents
' Start by displaying the titles
If objRS.EOF then Exit Sub
Dim Objfield, Icol, IRow
Icol = Icoloffset
IRow = Irowoffset
For Each objfield in Objrs.fields
Objspreadsheet.cells (IRow, Icol). Value = objField.Name
Objspreadsheet.columns (Icol). Autofitcolumns
' Set the font in Excel
Objspreadsheet.cells (IRow, Icol). Font.Bold = True
Objspreadsheet.cells (IRow, Icol). Font.Italic = False
Objspreadsheet.cells (IRow, Icol). Font.Size = 10
Objspreadsheet.cells (IRow, Icol). HAlignment = 2 ' centered
Icol = Icol + 1
Next ' Objfield
' Display all ' of the data
Do as Not objrs.eof
IRow = IRow + 1
Icol = Icoloffset
For Each objfield in Objrs.fields
If IsNull (Objfield.value) Then
Objspreadsheet.cells (IRow, Icol). Value = ""
Else
Objspreadsheet.cells (IRow, Icol). Value = Objfield.value
Objspreadsheet.columns (Icol). Autofitcolumns
Objspreadsheet.cells (IRow, Icol). Font.Bold = False
Objspreadsheet.cells (IRow, Icol). Font.Italic = False
Objspreadsheet.cells (IRow, Icol). Font.Size = 10
End If
Icol = Icol + 1
Next ' Objfield
Objrs.movenext
Loop
End Sub Function saveworksheet (strFileName)

' Save the worksheet to a specified filename
On Error Resume Next
Call ObjSpreadsheet.ActiveSheet.Export (strFileName, 0)
Saveworksheet = (Err.Number = 0)
End Function
End Class

Dim objRS
Set objRS = Server.CreateObject ("ADODB. Recordset ")
Objrs.open "SELECT * from xxxx", "PROVIDER=SQLOLEDB.1;" Persist Security

Info=true; User id=xxxx; Password=xxxx;initial catalog=xxxx;data source=xxxx; "
Dim Savename
Savename = Request.Cookies ("Savename") ("name")
Dim Objexcel
Dim Excelpath
Excelpath = "Excel\" & Savename & ". xls"
Set objexcel = New Excelgen
Objexcel.rowoffset = 1
Objexcel.columnoffset = 1
Objexcel.generateworksheet (objRS)
If Objexcel.saveworksheet (Server.MapPath (excelpath)) Then
' Response.Write '
<a href= ' "& Server. UrlEncode (Excelpath) & "' > Download </a>"
Else
Response.Write "There are errors in the save process!"
End If
Set Objexcel = Nothing
Objrs.close
Set objRS = Nothing
%>


Exporting to Excel or Word in the client with Excel application components
Note: "Data" in two functions is the ID of the table to be exported in the Web page
<input type= "hidden" name= "Out_word" onclick= "Vbscript:builddoc" value= "Export to Word class=" Notprint ">
<input type= "hidden" name= out_excel "onclick=" Automateexcel (); "value=" Export to Excel class= "Notprint" >

Export to Excel code
<script language= "JavaScript" >
<!--
function Automateexcel ()
{
Start Excel and get Application object.
var oXL = new ActiveXObject ("Excel.Application");
Get a new workbook.
var owb = OXL.Workbooks.Add ();
var osheet = Owb.activesheet;
var table = Document.all.data;
var hang = table.rows.length;

var lie = table.rows (0). Cells.length;

ADD table headers going cell by cell.
for (i=0;i{
for (j=0;j<lie;j++)
{
Osheet.cells (i+1,j+1



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.