How can I access Excel objects in ASP!
---------------------------------------------------------------
ASP Excel Technical Summary
Directory
I. Environment Configuration
Ii. Basic ASP operations on Excel
3. Generate a data table using ASP Excel
Iv. Generate a chart using ASP Excel
V. Solutions for viewing, downloading and deleting Excel files on the server
Vi. Appendix
Body
I. Environment Configuration
Server-side environment configuration from the perspective of reference materials, Microsoft series configuration should be all done, that is:
1. Win9x + PWS + Office
2. Win2000 professional + PWS + Office
3. Win2000 Server + IIS + Office
Currently, the test is successful in the latter two environments. There are no special requirements for the Office version. Considering the uncertainty and compatibility of the client configuration, it is recommended that the Office version on the server end be too high to prevent the client from being correctly displayed after download.
Two accidental discoveries of server-side environment configuration are as follows:
1. The author developed the wps2002 with Kingsoft installed on the machine. As a result, the Excel Object creation problem always occurs. After wps2002 is uninstalled, the error disappears.
2. I developed ASPCodeI like to use FrontPage. It turns out that if the FrontPage is opened (on the server side), the object creation may be unstable, and sometimes it may fail. After the extension test, we found that if the Office series software runs on the server side, it is difficult to create an Excel object.
The operation permission of the COM component must also be set on the server side. Type "dcomcnfg" in the command line to go to the COM component configuration page. select Microsoft Excel and click the Properties button. All three single options are customized. In the editing window, add everyone to all permissions. Restart the server after saving the file.
Client environment configuration does not find anything special, as long as the office and IE are installed, the general version seems to be OK.
II. Basic ASP operations on Excel
1. Create an Excel Object
set objexcelapp = Createobject ("Excel. application ")
objexcelapp. displayalerts = false: no warning is displayed.
objexcelapp. application. visible = false: the interface is not displayed.
2. Create an Excel file.
objexcelapp. workbooks. add
set objexcelbook = objexcelapp. activeworkbook
set objexcelsheets = objexcelbook. worksheets
set objexcelsheet = objexcelbook. sheets (1)
3. Read existing Excel files
straddr = Server. mappath (". ")
objexcelapp. workbooks. open (straddr & "\ templet \ table.xls")
set objexcelbook = objexcelapp. activeworkbook
set objexcelsheets = objexcelbook. worksheets
set objexcelsheet = objexcelbook. sheet (1)
4. save an Excel file
objexcelbook. saveas straddr & "\ temp \ table.xls"
5. Save the Excel file
objexcelbook. save (saved successfully during the test. An error is reported on the page .)
6. Exit Excel
objexcelapp. Quit must be exited.
set objexcelapp = nothing
3. Create a data table using ASP excel.
1. Insert data within a specified range.
objexcelsheet. range ("B3: K3 "). value = array ("67", "87", "5", "9", "7", "45", "45", "54", "54 ", "10")
2. Insert data into a cell
objexcelsheet. cells (3, 1 ). value = "Internet Explorer"
3. Select a range
4. Draw a rough line on the left of the cell
5. Draw a rough line on the right of the cell
6. Draw a thick line on the top of the cell. rough lines
7. Draw a thick line below a cell
8. Set the background color of a cell
9. Merge cells
10. Insert rows
11. Insert columns
4. Create a chart using ASP Excel
1. Create a chart
objexcelapp. charts. add
2. Set the chart type
objexcelapp. activechart. charttype = 97
Note: Two-dimensional line chart, 4; two-dimensional pie chart, 5; two-dimensional column chart, 51
3. Set the chart title
objexcelapp. activechart. hastitle = true
objexcelapp. activechart. charttitle. TEXT = "A test chart"
4. Use table data to set a chart
objexcelapp. activechart. setsourcedata objexcelsheet. range ("A1: K5"), 1
5. directly set graphic data (recommended)
objexcelapp. activechart. seriescollection. newseries
objexcelapp. activechart. seriescollection (1 ). name = "=" "333" "
objexcelapp. activechart. seriescollection (1 ). values = "= {, 2}"
6. Bind a chart
objexcelapp. activechart. location 1
7. display the data table
objexcelapp. activechart. hasdatatable = true
8. Display legend
objexcelapp. activechart. datatable. showlegendkey = true
V. Solutions for viewing, downloading and deleting Excel files on the server
There are many solutions for browsing, "location. href = "," navigate "," Response. redirect "can be implemented. We recommend that you use the client method, because it gives the server more time to generate an Excel file.
The download implementation requires some trouble. It is a good solution to download components from a ready-made server on the Internet or develop a component by yourself. Another method is to operate the Excel component on the client, and save the Excel file on the client to the client. This method requires the client to enable the operation permission of the insecure ActiveX Control. Considering the trouble of notifying each customer to set the server as a trusted site, we recommend that you use the first method to save trouble.
The deletion scheme consists of three parts:
A: an Excel file generated by the same user is composed of the same file name. The file name can contain user ID or sessionid to ensure that strings are not repeated. In this way, the previous file is automatically overwritten when a new file is generated.
B: When the session_onend event is set to be triggered in the global. Asa file, delete the Excel temporary file of this user.
C: delete all files under the temporary directory when the application_onstart event is set in the global. Asa file.
Note: It is recommended that the directory structure \ SRC code directory \ templet template directory \ Temp temporary directory
Vi. Appendix
When an error occurs, it is a headache to see an Excel dead process. Adding "on error resume next" to each file will help improve this situation, because it will always execute "application. Quit" regardless of whether the file produces errors.ProgramNo dead process is left after execution.
Add two points:
1. Other Excel operations can be solved by recording macros.
2. Opening the SQL enterprise manager on the server also causes problems.
VII. Example
<%
On Error resume next
Straddr = server. mappath (".")
Set objexcelapp = Createobject ("Excel. application ")
Objexcelapp. displayalerts = false
Objexcelapp. application. Visible = false
Objexcelapp. workbooks. Open (straddr & "\ templet \ null.xls ")
Set objexcelbook = objexcelapp. activeworkbook
Set objexcelsheets = objexcelbook. worksheets
Set objexcelsheet = objexcelbook. Sheets (1)
Objexcelsheet. range ("B2: K2 "). value = array ("week1", "week2", "week3", "week4", "week5", "week6", "week7", "week8", "week9 ", "week10 ")
Objexcelsheet. range ("B3: K3 "). value = array ("67", "87", "5", "9", "7", "45", "45", "54", "54 ", "10 ")
Objexcelsheet. range ("B4: K4 "). value = array ("10", "10", "8", "27", "33", "37", "50", "54", "10 ", "10 ")
Objexcelsheet. range ("B5: K5 "). value = array ("23", "3", "86", "64", "60", "18", "5", "1", "36 ", "80 ")
Objexcelsheet. cells (3, 1). value = "Internet Explorer"
Objexcelsheet. cells (4, 1). value = "Netscape"
Objexcelsheet. cells (5, 1). value = "other"
Objexcelsheet. Range ("B2: K5"). Select
Objexcelapp. charts. Add
Objexcelapp. activechart. charttype = 97
Objexcelapp. activechart. barshape = 3
Objexcelapp. activechart. hastitle = true
Objexcelapp. activechart. charttitle. Text = "visitors log for each week shown in browsers percentage"
Objexcelapp. activechart. setsourcedata objexcelsheet. Range ("A1: K5"), 1
Objexcelapp. activechart. Location 1
'Objexcelapp. activechart. hasdatatable = true
'Objexcelapp. activechart. able. showlegendkey = true
Objexcelbook. saveas straddr & "\ temp \ excel.xls"
Objexcelapp. Quit
Set objexcelapp = nothing
%>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Meta name = "generator" content = "Microsoft FrontPage 5.0">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<Body>
</Body>
</Html>
I will try again here. Save the table on the webpage.
For example, table id = mytable
<! -- Generate an Excel file -->
Sub btnexport_onclick ()
Dim objexcel
On Error resume next
Set objexcel = Createobject ("Excel. application ")
With objexcel
. Visible = true
. Workbooks. Add
. Sheets ("sheet1"). Select
End
M_row = "0"
For a = 0 to document. All. mytable. Rows. Length-1
M_row = CSTR (INT (m_row) + 1)
For B = 0 to document. All. mytable. Rows (a). cells. Length-1
M_col = CHR (ASC ("A") + B)
Objexcel. Range (m_col & m_row). Select
M_value = Document. All. mytable. Rows (a). cells (B). innertext
Objexcel. activecell. value = CSTR (m_value)
Next
Next
Objexcel. Visible = true
Objexcel. Range ("A1"). Select
End sub