Wasp Four ways to export Excel data

Source: Internet
Author: User
Tags range
First, the use of OWC

What is OWC?

OWC is the abbreviation for Office Web compent, Microsoft's Office Web Component, which provides a flexible and basic mechanism for drawing graphics on the web. In an intranet environment, if you can assume that there are specific browsers and powerful software (such as IE5 and Office 2000) on the client, you have the ability to use the Office Web Component to provide an interactive graphical development environment. In this mode, the client workstation will share a significant proportion of the entire task.

<%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 '

download "
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

Export to Excel code

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

ADD table headers going cell by cell.
for (i=0;i {
for (j=0;j {
Osheet.cells (i+1,j+1). Value = Table.rows (i). Cells (j). innertext;
}

}
oXL.Visible = true;
Oxl.usercontrol = true;
}
-->

Export to Word code

Set Objworddoc = CreateObject ("Word.Document")

ObjWordDoc.Application.Documents.Add Thetemplate, False
Objworddoc.application.visible=true

Dim TheArray (20,10000)
For I=0 to Row-1
For J=0 to Column-1
TheArray (j+1,i+1) = Table.rows (i). Cells (j). innertext
Next
Next
ObjWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore ("Consolidated query result set")//Show table title

ObjWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore ("")
Set Rngpara = objWordDoc.Application.ActiveDocument.Paragraphs (1). Range
With Rngpara
. Bold = True//set title to Bold
. ParagraphFormat.Alignment = 1//Center the title
. Font.Name = "Official script"//Set Title font
. Font.Size = 18//Set Title font size
End With
Set rngcurrent = objWordDoc.Application.ActiveDocument.Paragraphs (3). Range
Set tabcurrent = OBJWORDDOC.APPLICATION.ACTIVEDOCUMENT.TABLES.ADD (rngcurrent,row,column)

For i = 1 to column

ObjWordDoc.Application.ActiveDocument.Tables (1). Rows (1). Cells (i). Range.InsertAfter TheArray (i,1)
ObjWordDoc.Application.ActiveDocument.Tables (1). Rows (1). Cells (i). Range.paragraphformat.alignment=1
Next
For I =1 to column
For j = 2 to Row
ObjWordDoc.Application.ActiveDocument.Tables (1). Rows (j). Cells (i). Range.InsertAfter TheArray (I,J)
ObjWordDoc.Application.ActiveDocument.Tables (1). Rows (j). Cells (i). Range.paragraphformat.alignment=1
Next
Next

End Sub

Open in IE directly, then save as Excel file

read out the data in a

format, displayed in the Web page, at the same time, add the next sentence can be displayed in the Guest client Excel table.

<%response. ContentType = "Application/vnd.ms-excel"%>

Note: In the displayed page, only the

output, preferably do not output other forms of information.

Iv. exporting CSV separated by half-width commas

Method of generating a text file with the FSO method, generating an extension of CSV file. This file, a row is a row of the datasheet. Generate a data table field separated by a half-width comma. (for the FSO to generate a text file, this does not introduce the method)

CSV file Introduction (comma-delimited file)

Selecting the system will create a downloadable CSV file; CSV is the most common file format that can be easily imported into a variety of PC tables and databases.

Note that even if you select a table as the output format, you can still download the results to the CSV file. At the bottom of the table output screen, the CSV file option appears, and you can download the file by clicking it.

If you configure your browser to associate your spreadsheet software with a text (TXT)/comma-separated file (CSV), the file will automatically open when you download the file. After downloading, if you have Excel installed locally, click this file to automatically open the file with Excel software.




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.