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

Tags: asp

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/

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

Comments and Feedback