473,848 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export to Excel

I have an ASP page that calls ASP routines that I created that execute
a database query and return the results to a recordset. I then iterate
through the recordset and display the data in a table. Before I iterate
through the recordset I instruct the browser that the content type is
Excel using the following line:

(Response.Conte ntType = "applicatio n/vnd.ms-excel")

This works fine with Excel 2003 but with older versions (I tested Excel
97) the HTML included within the include files (on Page One) is
displayed in an Excel worksheet.

Page One - Begins with Include files (which contain my subroutines and
databse connection information). Then I have a Select statement to
determine which export users want. When I hit the case I need I then
call a Sub routine that exports the data to Excel.

Page Two - this page contains all of my custom functions and
procedures.

Page One Example Code:
<!-- #INCLUDE FILE="../../../includes/common_settings .asp" -->
<!-- #INCLUDE FILE="../includes/composite_setti ngs.asp" -->
<!-- #INCLUDE FILE="../includes/composite_funct ions.asp" -->

<%
sSymbol = Request("Symbol ")
sExportType = Request("Export Type")

'Determine Selected Export Type
Select Case lcase(sExportTy pe)
' '
'QUARTERLY EXPORT
Case "quarter"
' '
'Execute the Display Quarter Procedure
call DisplayQuarterE xport(sSymbol)
' '
'ANNUAL EXPORT
Case "annual"
' '
'Execute the Display Annual Procedure
call DisplayAnnualEx port(sSymbol)
' '
'RAW EXPORT
Case "raw"
' '
'Execute the Display Annual Procedure
call DisplayRawExpor t(sSymbol)
' '
End Select
%>

Page Two Procedure:
<%
Sub DisplayQuarterE xport(pSymbol)
'Execute Quarter Function
set pRSQuarter = QuarterExportQu ery(pSymbol)
' '
'Tell the Browser to redirect the output to Excel
Response.Conten tType = "applicatio n/vnd.ms-excel"
' '
'Check is Recordset contains data
If pRSQuarter.Reco rdCount > 0 then
' '
'Move to the first record in the recordset
pRSQuarter.Move First
' '
'Display the title bar
Response.Write "<table border=1>"
Response.Write "<tr>"
' '
'Iterate through the fields collection
for each field in pRSQuarter.Fiel ds
Response.Write "<td>"
Response.Write "<b>" & replace(field.n ame,"_"," ") & "</b>"
Response.Write "</td>"
next
' '
Response.Write "</tr>"
' '
'Move to the first record in the recordset
pRSQuarter.Move First
' '
'Display data
Do until pRSQuarter.EOF
Response.Write "<tr>"
' '
'Composite Code
Response.Write "<td>"
Response.Write trim(pRSQuarter .Fields("Compos ite").Value)
Response.Write "</td>"
'Composite Date
Response.Write "<td>"
Response.Write pRSQuarter.Fiel ds("Composite_D ate").Value
Response.Write "</td>"
'Gross Return
Response.Write "<td>"
Response.Write Round(pRSQuarte r.Fields("Gross ").Value,2) & "%"
Response.Write "</td>"
'Gross UV
Response.Write "<td>"
Response.Write Round(pRSQuarte r.Fields("Gross _UV").Value,2)
Response.Write "</td>"
'Net Return
Response.Write "<td>"
Response.Write Round(pRSQuarte r.Fields("Net") .Value,2) & "%"
Response.Write "</td>"
'Net UV
Response.Write "<td>"
Response.Write Round(pRSQuarte r.Fields("Net_U V").Value,2)
Response.Write "</td>"
'Total Equity
Response.Write "<td>"
Response.Write pRSQuarter.Fiel ds("Total_Equit y").Value
Response.Write "</td>"
'Total Fixed
Response.Write "<td>"
Response.Write pRSQuarter.Fiel ds("Total_Fixed ").Value
Response.Write "</td>"
'Cash
Response.Write "<td>"
Response.Write pRSQuarter.Fiel ds("Cash").Valu e
Response.Write "</td>"
' '
Response.Write "</tr>"
pRSQuarter.Move Next
Loop
' '
'Close Quarter Export Recordset
CloseRS(pRSQuar ter)
' '
'Close Database Connection
CloseDBConnecti on()
' '
Response.Write "</table>"
' '
'Display Error Message is no results were found
Else
Response.Write "Your Query returned (0) results."
' '
End If
' '
End Sub
%>

Again this works great if I have Excel 2003 but for older versions of
Excel it displays the HTML that is contained within the include files
on page one.

Here is my assumption but I have no clue why Excel 2003 works. I am
assuming that the include file content is written to the screen (behind
the scenes) and when I instruct the browser that the content is Excel,
the include file data is included in the "content" and is displayed in
Excel.

Please help, I am racking my head against the wall with this one.

Jul 22 '05 #1
1 5045
I figured out a solution.

Before I set the content type (Response.Conte ntType) I clear the buffer
by executing the following line.

Response.Clear

This cleared the existing content from the buffer.

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3793
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
2
4001
by: Siu | last post by:
Hi, I use the following code to export and import a file Excel from resp. into a Web page with the following code: //EXPORT Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; this.EnableViewState = false;
6
4071
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is this possible? If so, I really need the code immediately. Thank you, Elena
13
13266
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to save it the Default File Type is Web Page(*.htm; *.html)
5
31931
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
9782
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping to export two Tables (or queries...not sure which to use - they both seem to have the same data) in...
1
10520
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am trying to export to Excel using a command in an Access Form. RowID strFY AccountID CostElementWBS 1 2008 1 7 2 2008 1 7 I want to...
3
7171
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this file but save it as an excel file. The data in this excel file will be imported into an Access database. The
2
6427
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the method.... it creates 6 sheets # region Namespaces using System;
0
9892
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10997
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10661
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10347
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7067
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3172
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.