Many times we need to convert tabular data into execl in the form of a user, and there are several ways to do it, and I'll introduce a method that takes advantage of ASP, which allows the server to dynamically create EXECL reports without consuming any server space. The method also allows multiple users to receive the data at the same time. But the method requires at least EXECL 97 support.
Needless to say, the most important thing to do in this job is to tell the browser HTTP headers to use the following code:
<%
Response.ContentType = "Application/vnd.ms-excel"
%>
Let's take a look at an example, assuming that you now have the following form of data:
Flavor qty_baked Qty_eaten Qty_sold Price
Boston 24 2 10 0.5
Jelly 24 1 12 0.5
Strawberry 36 1 15 0.5
Chocolate 24 2 6 0.75
Maple 12 1 6 0.75
Customer requirements are expressed in the form of execl and want to add some other calculated totals
Use the following code:
......
<%
Response.ContentType = "Application/vnd.ms-excel"
Set Conntemp=server.createobject ("Adodb.connection")
Cnpath= "dbq=" & Server.MapPath ("/stevesmith/data/timesheet.mdb")
Conntemp. Open "Driver={microsoft Access DRIVER (*.mdb)};" & Cnpath
Set Rs=conntemp.execute ("SELECT * from Donut")
%>
<table border=1>
<TR>
<%
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'% Loop through Fields Names and print out the Field Names
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
j = 2 ' Row counter
For i = 0 to RS. Fields.count-1
%>
<td><b><% = RS (i). Name%></b></td>
<% Next%>
<td><b>on Hand (Calculated) </B></TD>
<td><b>gross (Calculated) </B></TD>
</TR>
<%
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'% Loop through rows, displaying each field
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Do but not RS. Eof
%>
<TR>
<% for i = 0 to RS. Fields.count-1
%>
<TD valign=top><% = RS (i)%></td>
<% Next%>
<TD>=b<%=j%>-c<%=j%>-d<%=j%></TD>
<TD>=d<%=j%>*e<%=j%></TD>
</TR>
<%
Rs. MoveNext
j = j + 1
Loop
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'% make sure to close '-the result Set and the Connection object
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rs. Close
%>
<tr bgcolor=red>
<TD>Totals</TD>
<td>=sum (B2:B6) </TD>
<td>=sum (C2:C6) </TD>
<td>=sum (D2:D6) </TD>
<TD>n/a</TD>
<td>=sum (F2:F6) </TD>
<td>=sum (G2:G6) </TD>
</TABLE>
......
This allows us to achieve the goal, the user can open it in the browser window for simple operation, or can be saved to the hard disk for other operations. I'll also introduce a way to take advantage of FileSystemObject operations. Please wait a moment. :)