codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:

Home » ASP » Article

Creating a Dynamic Reports using ASP and Excel

Article by:  Jeff Anderson  ( 1362 ) (1/9/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Summary: A simple way to generate Excel reports from a database using Excel.
Viewed: 322373 times Rating (174 votes): 
 4.4 out of 5
 Rate this Article  Read Comments  Post Comments

Creating a Dynamic Reports using ASP and Excel



For some reason, there aren't a hold load of ways to create online reports as far as I know. There's Crystal Reports Enterprise which has a good attempt, but could be a lot better. Then there's not much else (recommendations please post below!). But one easy way of generating neat reports is to generate Excel files on the fly direct from the database, with a little ASP. It's surprisingly easy to do.

The first step is to modify the mime type in the header, so the browser knows this is an Excel file, not an HTML page:

Select All Code


Note this needs to be at the very top of the page, before anything else.

Once you've done that it's simply a case of reading through the fields in the database and printing them out to a standard HTML table:

Select All Code


It's as simple as that. You can even include simple Excel function like SUM - just put the same text into a table field as you would into the Excel field - as in :

Select All Code


That would show in a table field the total of rows B2 to B6 in the Excel field. To achieve this of course, you need to know the name and letter of the fields that will appear - this may require a bit of trial and error, but it's easy enough to achieve and can produce excellent results.

Thanks to a few of our members here on CodeToad for the following additional suggestions.

Preceding zeros



You can maintain preceding zeros in a box by placing a non-breaking space character ( ) in front of the number. As in:
&nbsp;<%= objrs(i) %>


Currency and other formats

Currency formatting can be set on an Excel cell by preceding values with the currency symbol. For Example, precede the value with a dollar sign.

<TD>$<% = objrs(i) %></TD>

More currency formatting is available with the Visual Basic FormatCurrency function. FormatCurrency has several parameters which are optional.

FormatCurrency(Expression[,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]])

The following will cause a cell to be formatted with a preceding dollar sign, have two decimal digits, precede values less than one dollar with a zero, place parentheses around negative values, and use commas to group thousands.

<td><%= FormatCurrency(objrs(i),2,vbTrue,vbTrue,vbTrue) %></td>

Other Visual Basic functions may offer other formatting for the Excel cells.





CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Creating a Dynamic Reports using ASP and Excel'
Posted by :  Archive Import (Ray Levasseur) at 11:33 on Tuesday, April 08, 2003
I was doing a Google search on just this subject, and of all the references this one has been absolutely the most useful of all. I have had multiple requests from users asking if some of our web reports (Crystal) could instead be output to Excel files.

By using some formatting of dates and numerics in asp, plus applying a few style sheet elements I was able to provide a rather complex spreadsheet in exactly the format the user wanted.

Thanks again for this killer tip

Ray
Posted by :  Archive Import (y2s) at 00:22 on Wednesday, April 09, 2003
dun use control to loop recordset
use below function is much more faster..
sStr=tmprs.GetString(,,"</td><td>","</td></tr><tr><td>"," ")
Posted by :  Archive Import (Anandha Subha T) at 05:03 on Tuesday, August 05, 2003
Hai all,
This was the query I posted previously:
If i run with SQL Server 2000, First time when i call through browser i will get empty excel sheet. Close and call through browser then also the same.

with out closing the browser if u run with another window then the result will display in the browser. Could u help in this.

I was able to find another way to come around this.By adding the following line to your code makes the display fine at the very first time.

Response.AddHeader "Content-Disposition","attachment;filename=somename.xls"
Posted by :  dipsnaik at 06:11 on Thursday, October 09, 2003
hi all,

after reading this article i got a clbetter idea of how to work with asp and excel but now i want to write in an exsiting excel sheet which is on server side. its a template..
and has a standard format.. so i want to write in that excel sheet in a paricualr
fields and then open that file to save on the client side.. can i do that?..
i visited many pages/sites.. but all that they are doing is.. creating a new excel
sheet,writing on it and then user can save it on his/her side.. but i want to
use a template that is already existing.
please help .......
Thanx in advance..
Deepali

Posted by :  lrumley at 11:13 on Tuesday, October 14, 2003
i got the excel output to work A-OK, but am unable to apply CSS to the table cells - it just spits out a big arial mess...any advice??

upon further messing with it, it accepts old-school formatting, such the font tag with size and face attributes, but no CSS...
Posted by :  saravanan at 00:15 on Sunday, February 08, 2004
Fine i got what i am searching. But i want to do something more with this. I want to save the SQL query result automatically to a excel file and i like to give only the link for that excel file so that the user click and download it.
Posted by :  mr_zxx at 08:33 on Thursday, February 19, 2004
I can't get this to work could someone please send me a working html file so that i can edit the database source!! I would be very grateful!
Posted by :  tmbabu_78 at 01:52 on Friday, March 12, 2004
I used the above MIME type.
Response.ContentType = "application/vnd.ms-excel"

But when I ask the page to generate excel report it is moving to the next page in excel format without displaying anything.
ie when i call through browser i get empty excel sheet.

can you explain why it happens

Lakhs of Thanks in advance,
TMBabu

Posted by :  tmbabu_78 at 02:19 on Friday, March 12, 2004
thanks for the additional comments Anandha subha, it works excellent now using the code that you specified.
Posted by :  satheesh at 04:58 on Friday, May 07, 2004
i hav generated a table report in web page using asp code
nw i need to do is tht whn i click a button to export to excel thn the report in the web page shud automatically export to excel file and it shud save itself with filename having currentdate as like "572004.xls".
can u able to send the code for this requirement ?
one thing it shud not prompt the user to give the file name to save in excel.
pls make it ASAP.
i too hav used this --
Response.ContentType = "application/vnd.ms-excel"
but system prompting user to enter the file name to save the file in excel.
Posted by :  varunms at 12:00 on Wednesday, May 19, 2004
Hi There,
This article has given a good information on the concerned topic. But I had a problem while setting the font size and width of the columns on the Excel spreadsheet. When I have given the column width as 22 in my ASP code, its not the font that I am getting on Excel spread sheet. Its the default 12 I see on my Excel. Below is part of the code.


for intCounter = 0 to ObjRS.fields.Count-1

Response.Write "<th align=""left"" font-size:""22pt"" bgcolor=""blue"" width=""100px"">" & ObjRS.fields(intCounter).Name & "</th>" & vbCRLF

next

Is there any specific reason of why its doing? Could you please give an explanation or any solution of setting the column width and the font size of the excel spread sheet on the ASP side??

I would really appreciate your ideas.

Thanks,
Varun
Posted by :  ggonzalez45 at 07:37 on Tuesday, May 25, 2004
Ok, I have an asp page that sends a report to excel and works fine, I have a button in my page named "Excel", but when I click on this button the browser ask me two times if I want to open or save the file. The question is, is there a way to receive the question one time instead of two. The problem is that this behavior only happen in some Internet Explorer browsers, maybe there is something I have to configure in the browser
Thank, and excuse my poor english
Posted by :  ajlkirin at 06:03 on Tuesday, July 06, 2004
I like the article on display html table information in excel and being able to use formulas, but, is ther a way to use the relative addressing scheme (RC..) rather than the absolute (A1..)

Thanks

Andy
Posted by :  e1 at 07:52 on Thursday, August 05, 2004
Hi everybody!

I liked a lot this article!

Just one question, I'm starting with ASP .NET and I have to generate an Excel from a recordset like in the article example, is there an easy way to do it in .NET or it works exactly like in normal ASP.

Thanks in advance!

Posted by :  pradeep at 08:08 on Friday, August 06, 2004
Hi

I have done an <B>Export</B> feature for my web page . I have assigned a name for the file before download through response.addheader...

It is working fine on Windows O.S.
But in Macintosh browsers like Safari and Mozilla, it gives a problem.
In Macintosh machine if we open the downloaded file, it opens as a Work book and will ask the user to save the file.
How can I make the file to download as a Worksheet
How can I solve this issue?

Regards
Pradeep


Posted by :  shou at 07:43 on Tuesday, September 07, 2004
Hello,

I have read the article and the codes. I need to do something like this however i want to know whether we can create excel headers and footers by this system or not.I will be using ASP as my scripting language.

If it is possible then if somebody can help me with code will be very much appriciated.

Regards,

shoubhik
Posted by :  tofocsend at 11:01 on Tuesday, September 07, 2004
Another thing: by default, IE opens Excel pages inside a browser window, which can cause difficulties if the user wants to save the Excel page locally or make changes to it. To force IE to open the dynamic page in Excel, add this line of ASP below the Content-Type response:

Response.AddHeader "Content-Disposition", "attachment;filename=myfilename.xls"

(change myfilename to whatever you want the Excel file to be called).

Cheers,
Ryan
Posted by :  DSR35802 at 09:14 on Wednesday, September 08, 2004
To create very robust and professional looking Excel reports. I first create a layout of the Column Headers, Page Layout, Color, fonts, etc. in the Excel product itself. I then save it as a web page using the File Save As... [Save as type:] [u]Web page[/u]

I then use a text editor to copy and paste the source code into an asp page. From there, I can add any looping logic to render the results of a dataset.

You can use asp code in many areas of the Excel source code to print a user's name, etc. on the Excel document itself.
Posted by :  jamguitar at 03:45 on Thursday, September 16, 2004

I have a question for a preceding zeros

" <%= objrs"(i) %>

but the front has space before the number some of the system cant space it will be a different meaning of space 01 and 01.

How to code with printing only number 01 on excel ? I would like to use ASP to control the excel column formatting.

Best Regards,
Brian
Posted by :  hchimata at 13:53 on Friday, October 29, 2004
I am able to download data from SQL server to Excel. But my problem is with text field. I am capuring the Enter key and converting it to br
replace(x.value,chr(13)& chr(10)," <br>")
but excel takes each line break as seperate record. Is there a way to display that in one single record ?
Thanks for the help.

Haritha.
Posted by :  freemantle at 09:45 on Thursday, November 04, 2004
Hello,

I have a similar script for creating an Excel spread sheet. How ever I need to display five different tables from my database in Excel. Does anyone know of any way of creating new sheets in the workbook, one for each table? Thank you.
Posted by :  Jael at 14:39 on Monday, January 17, 2005
Hi,

This article is the closest thing I have been able to find for what I am trying to do. What I need to do is not to import to excel from a DB, but vice versa. From excel (using ASP ... not .NET) import data to a table into sql server 2000 with the click of a form button. Thanks for the help! This article has put me on the right path. If anyone has any good ideas on a way to do this, I am open to suggestions.

Thanks again,

Johnny
Posted by :  leen at 23:17 on Tuesday, April 05, 2005
How to make the report into [b]PIE[/b] or [b]BAR[/b] chart?

Pls help!
Posted by :  illusions77 at 14:20 on Monday, April 25, 2005
Nice way to begin coding. I found this link to other who might be wanting more info:
[b][u]http://support.microsoft.com/default.aspx?scid=kb;en-us;Q271572[/u][/b]
Posted by :  ajith at 02:48 on Saturday, April 30, 2005
Hi All

I need a help im doing a project in asp. the problem is i have a search criteria and on clicking a go button i need to generate a excel sheet with data in it accessed from sql database. Plz send me some sample codes which will help me out and plz do it soon bcoz im in greate trouble

My mail id is ajithevn@mail.com

Thanks
Posted by :  sunu1008 at 06:20 on Monday, June 27, 2005
Hi Every Body !

Export to excel data from ultrawebgrid rows, Each data row contain one worksheet in excel sheet like as
row1 data contain sheet1
row2 data contain sheet2
row3 data contain sheet3
like should i want from web form......to Excel sheet

this opertaion getting at a time on pressing one button.
we have pressing one button ultrawebgrid rows data to export excel sheet, In excel sheet each worksheet contain one grid row deatails, for example grid have 10 rows ,pressing any button, In excel sheet 10 worksheets will occure and each worksheet contain 1 row data of grid,

pls send me a solution for this example
Posted by :  losbiznatch at 10:40 on Wednesday, August 03, 2005
Does anyone know how to freeze cells using the example posted above. I am having issues in doing so.

Thank you in advance,

Curt
Posted by :  girija at 18:39 on Wednesday, September 14, 2005
does anyone know how to put the values in multiple worksheet in the same Excel workbook.
Specifying hte worksheet name before html row tag doesn't seem to work !!
Posted by :  srinivasulu at 07:08 on Friday, September 23, 2005
Thank you

i have used the copde in this way

<%
Response.ContentType ="Application/vnd.excel"
Response.AddHeader "Content-Disposition", "attachment; filename=srinu.xls"
Response.Write "srinivas"
%>

now could you please tell me how to auto fit the coloumns in the excel sheet that i am going to get using this method, i need excelsheet whose autofit happened on the fly

please help me in this regard

Posted by :  srinivasulu at 08:56 on Friday, September 23, 2005
autofit of the excelsheet usign MIMS can be done by using XML but it works only in excel-2000 version onwards
but i am using some old versions , so i need some other way that will hlp me in autofit the excel sheet on fly

Thanks in advance
Posted by :  nhnguyen at 15:05 on Friday, February 17, 2006
ASP export to excel,
I had problem as tmbabu_78. I used sample code above but It could not get the result. when I use the link export to the file had code like the sample above included
<%Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment; filename=Export_to_excel.xls"
%>
but it not show any data in excel sheet, if you have any hint please tell me thank you very much.

Regards,
Posted by :  vikkyzkool at 12:03 on Tuesday, May 09, 2006
toi create multiple worksheets without using any come or third party component go to http://www.codetoad.com/asp_excel.asp

<Added>

to create multiple worksheets in excel doc without using any COM or third party component go to
http://www.codetoad.com/asp_excel.asp
Posted by :  vikkyzkool at 12:06 on Tuesday, May 09, 2006
to create multiple worksheets in excel doc without using any COM or third party component go to

http://www.codeproject.com/dotnet/ExportToExcel.asp
Posted by :  surya_sree at 04:19 on Saturday, June 10, 2006
i want code download option so that i can test the code my self i can learn who to write the code with extra feature.
Posted by :  ravichella at 00:30 on Tuesday, July 04, 2006
When we convert and dump things to an excel, what happens when the number of rows exceeds the permissible limits? I guess an excel sheet cannot hold more than 65000 odd entries. What exception, if any, is thrown? How do we handle this scenario?

Thanks and Regards,
Ravi
Posted by :  alonso86 at 22:43 on Sunday, August 06, 2006
wat if we hav a certain criteria for the page, for example that page in onli showin all recored for company A, but in the DB there are recored of company B. C , D oso..
Posted by :  nksoral at 03:45 on Tuesday, October 31, 2006
It's great
Posted by :  nksoral at 03:51 on Tuesday, October 31, 2006
I have to authorize someone to edit my Excel web page on net, any suggestion.
Posted by :  mahbub422 at 05:04 on Thursday, May 03, 2007
I am getting blank Excel sheet. How can I sove this?

<Added>

I am getting blank Excel sheet. How can I solve this?
Posted by :  BruceG at 11:18 on Friday, June 22, 2007
Hello,
Let me add that I have the same question as mahbub22. On most users machines, the Excel option works fine, but on some, we get the Excel headings only, with no data. We are all using IE 6. I am guessing a browser setting must be adjusted, but have not been able to find it. Any help is appreciated.
Thank you.
Posted by :  asprajesh at 01:05 on Thursday, October 04, 2007
Hi all i am very new member of this group .. I have used same code which is shown in the article *Creating a Dynamic Reports using ASp and Excel*. It works very fine but i am facing problems . when i export 5000 recods into excel it work fine but when i export more than 30000 records in excel it give me Error like

*** internet explorer can not Download xyz.xls file from localhost . Internet Explorer was not able to open this internet site . The requested site is either unavailable or cannot be found. Please try again later.***

How i can resole this problem ..
If is there any other method to export into excel then let me know..

Looking forward to your reply n suggetion ..

Kindly Do somthing ..

Thankyou in advance.

Regard
asprajesh

Posted by :  eunice at 23:15 on Friday, October 05, 2007
Thanks for sharing. I have a problem here.
How could i do if i want it auto export into excel file and save in a path without any button click?
Posted by :  xavito at 18:05 on Thursday, November 01, 2007
In the Excel sheet generated there is always a blank line at the top.

I don't know how to get rid of it.

Can you help me ?

Thanks.
Posted by :  shonk at 04:37 on Thursday, February 21, 2008
Hi,
I'm using a very similar script and have run in to a problem that someone else mentioned a while back and I was wondering if anyone could help.
I've got a text field with line breaks in it and I'd like to translate these to the cell data that's being output.
If I use Replace(jobbie,Chr(10),"<br />") the excel sheet is output with an extra row for each of these line breaks. I've also tried Replace(jobbie,Chr(10),vbCrLf) and this doesn't work either...
Can anyone help?
Thanks
Posted by :  The Stifler at 06:44 on Thursday, June 05, 2008
Hi
i used similar script but it dosent work on HTTPS can u suggest me what to do for using on HTTPS
Posted by :  divya at 05:04 on Friday, October 10, 2008
Hi all

I have a record set having 20000 records. I want to export 10000 records in the first worksheet and the rest to the next worksheet. How do I go about it.

Looking forward to your reply n suggestion ..

Thanks
Divya
Posted by :  aashishnakra at 08:48 on Friday, February 06, 2009
hii
any idea how can i generate a chart in excel...after downloading data from a asp page
Posted by :  hemasr15 at 01:29 on Wednesday, February 11, 2009
Hi all


i want to create a new excel sheet dynamically while downloading data from oracle database to excel sheet if

<Added>

Hi all

I am downloading data from oracle database to an excel sheet.if the records are more that 65555 then i want to download the remaining records to new excel sheet,this new sheet should be created dynamiclly if records become more than 65555.

any one can help Pls..

Thanks in advance
Posted by :  sriramavinash at 04:14 on Monday, March 16, 2009
Hi Every body


I need to generate an excel sheet using asp

i have an page with drop down list in search column,when i select a value from the drop down and i click search i am getting records
in the bottom of the page i have a button to generate excel.if i click i need to generate excel sheet with all the records inside the sheet.

can any one solve this bug.





To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
ASP FilesystemObject
by Jeff Anderson
An introduction to the Filesystemobject
ASP GetTempName
by Jeff Anderson
Use the GetTempName method to create a randomly generated temporary file on the server.
ASP OpenTextFile
by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
ASP FileExists
by Jeff Anderson
An introduction to the FileExistsMethod of the FileSystemObject
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).
Add or Subtract Hours in SQL or ASP using DateAdd
by Jeff Anderson
A beginners guide to using the SQL DATEADD function to add or subtract hours. Particularly useful when setting the time displayed on the ASP page to a different time zone (eg when the server is in the US, and the site is for a UK audience).
ASP CreateTextFile
by Jeff Anderson
An explanation of the CreateTextFile Method, part of the ASP FileSystemObject








Recent Forum Threads
• C++
• Re: refresh parent after closing pop up window
• Dynamic Insertion
• Date and Time function around the world???
• Significant Factors
• Perl array access
• Re: huffman encoding and decoding in C++...
• Perl One Liner: Replace {(
• Re: html including php, accessing the functions


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009