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  ( 1361 ) (1/9/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: A simple way to generate Excel reports from a database using Excel.
Viewed: 154614 times Rating (135 votes): 
 4.5 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


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

 



RELATED ARTICLES
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.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
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.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
MagicGrid
by Abhijeet Kaulgud
MagicGrid is an all-in-one grid for ASP programmers. It is a 3 Level Hierarchial Grid. You can Add, Edit, Delete Items under all the three levels. You can also cut-copy-paste Items from one level to other, It happens just by drag & drop!
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
ASP.NET Forum Source Code
by ITCN
Complete open source website Forum and Discussion Board programmed in Microsoft dot Net 1.1 Framework with Visual Basic.
The asp:listbox control
by David Sussman, et al
The next HTML server control that we'll look at, <asp:listbox>, is very much related to <asp:dropdownlist>.








Recent Forum Threads
• IE page Redirect
• Re: Javascript problem with document.write and accented characters
• Re: sorting and Linked list
• Re: need help linked list
• Re: Help with arrays
• Re: Reading from a file
• Re: Why Use Method?
• Re: Help with a simple program
• Re: need help with quiz


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005