0

Wondering if anyone can assist. I have an .ashx file which creates an excel document which is populated with a C# DataGrid. This all works well the only problem is when I open the excel document the automatic gridlines are turned off. Is there a way to enable them?

Thanks in advance, Air

HttpResponse response = HttpContext.Current.Response;

            response.Clear();
            response.Charset = "";
            response.ContentEncoding = System.Text.Encoding.Default;
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"dataImportTemplate.xls\"");
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
                    dg.DataSource = ds.Tables[0];
                    dg.ShowHeader = false;
                    dg.DataBind();                    
                    dg.RenderControl(htw);
                    response.Write(sw.ToString());
                }
            }
Jebanisa
  • 111
  • 2
  • 15

1 Answers1

0

If you export to a csv it keeps the gridlines. This is a pretty simple workaround that a lot of people opt to use.

See similear post Here

That post includes a helper function that supposedly can add the gridlines to your export.

Hope this helps.

Community
  • 1
  • 1
danielpiestrak
  • 5,279
  • 3
  • 30
  • 29