Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Setting SQLDataSource parameter from the code-behindUsing PostSharp and log4net to Set Up Controller Logging in ASP.net MVC »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

For a long time I have had to make web reports for our company that could be exported to excel. This is a fairly easy process. All you have to do is put your report in an HTML table and add the following 2 lines of code:

  1. Response.ContentType = "application/ms-excel";
  2. Response.AddHeader("content-disposition", "attachment; filename=test.xls");

However, when the excel file is generated it has a nasty side effect of having all the gridlines hidden. They can easily be turned back on in excel by the following: Tools > Options > click gridlines checkbox.

Until today I put up with the gridlines being hidden. When I tried to search for a solution via google, most people suggested that it just wasn't possible to generate the excel report with gridlines. Other people offered solutions that required you to run a COM object on the server to start an instance of excel in the background to create the file. However, after searching through a bunch of garbage and piecing together bits and pieces of non-working solutions, I finally got it to work. The trick is to set up your own custom XML settings, and add the "Panes" worksheet option. Here was the working solution:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. namespace excelGridlineTest
  13. {
  14.    public partial class _Default : System.Web.UI.Page
  15.    {
  16.       protected void Page_Load(object sender, EventArgs e)
  17.       {
  18.          Response.ContentType = "application/ms-excel";
  19.          Response.AddHeader("content-disposition", "attachment; filename=test.xls");
  20.          Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
  21.          Response.Write("<head>");
  22.          Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\">");
  23.          Response.Write("<!--[if gte mso 9]>");
  24.          Response.Write("<xml>");
  25.          Response.Write("<x:ExcelWorkbook>");
  26.          Response.Write("<x:ExcelWorksheets>");
  27.          Response.Write("<x:ExcelWorksheet>");
  28.          //this line names the worksheet
  29.          Response.Write("<x:Name>gridlineTest</x:Name>");
  30.          Response.Write("<x:WorksheetOptions>");
  31.          //these 2 lines are what works the magic
  32.          Response.Write("<x:Panes>");
  33.          Response.Write("</x:Panes>");
  34.          Response.Write("</x:WorksheetOptions>");
  35.          Response.Write("</x:ExcelWorksheet>");
  36.          Response.Write("</x:ExcelWorksheets>");
  37.          Response.Write("</x:ExcelWorkbook>");
  38.          Response.Write("</xml>");
  39.          Response.Write("<![endif]-->");
  40.          Response.Write("</head>");
  41.          Response.Write("<body>");
  42.          Response.Write("<table>");
  43.          Response.Write("<tr><td>ID</td><td>Name</td><td>Balance</td></tr>");
  44.          Response.Write("<tr><td>1234</td><td>Al Bundy</td><td>45</td></tr>");
  45.          Response.Write("<tr><td>9876</td><td>Homer Simpson</td><td>-129</td></tr>");
  46.          Response.Write("<tr><td>5555</td><td>Peter Griffin</td><td>0</td></tr>");
  47.          Response.Write("</table>");
  48.          Response.Write("</body>");
  49.          Response.Write("</html>");
  50.       }
  51.    }
  52. }

Got a web related question? Discuss it in the forums: http://forum.lessthandot.com/

4458 views
asp, asp.net, excel
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

9 comments

Comment from: SQLDenis [Member] Email
*****
Sweet, will have to try this out soon.

Congratz on your first blog post :-)
25/06/09 @ 14:20
Comment from: chaospandion [Member] Email
****-
You should wrap that into a simple reusable object.
25/06/09 @ 22:03
Comment from: ca8msm [Member] Email
*****
Great post, thanks!
26/06/09 @ 01:11
Comment from: onpnt [Member] Email
*****
Nice post kaht! I'm having my developers read it today or they are fired! :P

looking forward to more like this
26/06/09 @ 06:00
Comment from: Annelies van Gent [Visitor]
*****
Thanks!!! You really helpt me with this post.
07/07/09 @ 08:41
Comment from: Rohit [Visitor]
*****
Great help man...I've looking for something for this since a long time and you gave me what I wanted exactly...
Cheers!!
17/08/09 @ 23:38
Comment from: fatih [Visitor] Email
****-
Hi,

Thanks for the post. But I have a question?

How can we add another sheet and add some data to that sheet?
Creating another sheet in the xml part is easy. But how can we assing the data in the body part?

I add another worksheet to the upper xml part and add another table in the body. It did not work.
Do u have any idea?

Thanks
24/08/09 @ 04:00
Comment from: Saroj Roy [Visitor]
*****
Great post. Thank you. I’m currently working with PHP and I was facing problems with exporting my report data to excel with the grid lines visible. Now I can easily export my report to excel from the print-friendly view.
25/08/09 @ 21:34
Comment from: Josh [Visitor] · http://www.joshfitz.com
*****
I spent all morning looking for a solution for this, then stumbled on this post. This worked perfectly. Thank you!
24/09/09 @ 11:03

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)