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

    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

    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/

    32736 views
    asp, asp.net, excel
    Instapaper

    16 comments

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

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

    looking forward to more like this
    06/26/09 @ 06:00
    Comment from: Annelies van Gent [Visitor]
    *****
    Annelies van Gent Thanks!!! You really helped me with this post.
    07/07/09 @ 08:41
    Comment from: Rohit [Visitor]
    *****
    Rohit Great help man...I've looking for something for this since a long time and you gave me what I wanted exactly...
    Cheers!!
    08/17/09 @ 23:38
    Comment from: fatih [Visitor] Email
    ****-
    fatih 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
    08/24/09 @ 04:00
    Comment from: Saroj Roy [Visitor]
    *****
    Saroj Roy 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.
    08/25/09 @ 21:34
    Comment from: Josh [Visitor] · http://www.joshfitz.com
    *****
    Josh I spent all morning looking for a solution for this, then stumbled on this post. This worked perfectly. Thank you!
    09/24/09 @ 11:03
    Comment from: Brandon Lewis [Visitor]
    Brandon Lewis Great solution. I have to export a web page to Excel and I thought I was going to have to build the excel file from scratch. This solved the problem.
    03/24/10 @ 09:33
    Comment from: Webdiyer [Visitor] · http://en.webdiyer.com
    Webdiyer Solved my problem, thank you very very much!:)
    09/08/10 @ 20:30
    Comment from: Bob [Visitor]
    Bob Thanks for the info. You confirmed what I suspected: some of the magic is done via XML. You saved me some experimenting. But for those who want to experiment more, try creating a VERY simple .xls file in excel. Then save it AS an xml file. You'll see that lots of stuff gets generated for you. Change one item, like color a cell, and then save again as a different xml file. You'll be able to see how your change was rendered my excel. So, if you want to script a more complex spreadsheet, including multiple worksheets, gradually add more items and check out the generated XML. It's not TOO difficult if you take it slow.
    04/25/11 @ 19:50
    Comment from: kris [Visitor]
    kris Hi I generate html code from source code and i want to export it in an excel file, any suggestions?
    06/21/11 @ 02:18
    Comment from: Godfrey [Visitor]
    Godfrey Great Post, how do you create another worksheet and add another html table to it?
    01/09/12 @ 06:40
    Comment from: Nisha [Visitor]
    Nisha Thanks a lot. Actually I was looking for and searching a lot for my asp application. It works.......... Thanks once again.
    03/14/12 @ 22:15
    Comment from: simflex [Visitor] · http://www.keniginc.com
    simflex Man, I spent two days looking for this.

    Excellent article!!

    Thank you very much.
    04/27/12 @ 13:09

    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.)