0

Hello i got 2 pages with exporting to excel , i use something like this :

      Response.ClearContent()
            Response.Buffer = True
            Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "poView.xls"))
            Response.ContentEncoding = Encoding.UTF8
            Response.ContentType = "application/ms-excel"
            Dim tw As New IO.StringWriter()
            Dim htw As New HtmlTextWriter(tw)
            budgetPlanGrid.RenderControl(htw)
            Response.Write(tw.ToString())
            Response.[End]()

Now i do manage to export into excel and see the needed grid, but on the more complex page which has alot more stuff in it for some reason beside the gridview i see parts from my masterpage! and it yells at me that i am missing the path for my style.css which has nothing to do with the gridview i am trying to export!. I used 2 panels to make everything up untill / from the gridview visible = false, I am kinda clueless what else could have caused that.

I am hoping maybe someone had this sort of issue while using this code and can advice me on what to look for? Thanks in advanced!

kresa
  • 135
  • 2
  • 3
  • 13
  • Please use native .NET libraries for exporting to excel. You will have all sorts of problems with this html table to excel hack. see http://stackoverflow.com/a/9569827/351383 – Antonio Bakula Sep 03 '14 at 12:48

1 Answers1

0

I had that issue using RenderControl - kept asking for missing style sheets and other nonsense.

I used the datatable I would have used to bind the gridview and did this:

HttpContext.Current.Response.Clear();
HttpContacts.Current.Response.AddHeader("content-disposition", attachment):
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

string sTab = "";

foreach (DataColumn dc in dt.Columns)
{
HttpContext.Current.Response.Write(sTab + dc.ColumnName);
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");

int i;
foreach (DataRow dr in dt.Rows)
{
sTab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
HttpContext.Current.Response.Write(sTab + dr[i].ToString());
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
}

HttpContext.Current.Response.End();
Martin Smellworse
  • 1,702
  • 3
  • 28
  • 46