FIROZ ANSARI

FIROZ ANSARI

  • NA
  • 52
  • 5.7k

Exporting chart and gridiew to Excel

Apr 28 2016 1:39 AM
I have a situation where I have to export gridview and chart in the same excel file without using Interop (MicroSoft). Here is my code
 
 
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Prosperity.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grdPremiumAnalysis.AllowPaging = false;
BindReportInGridView();
grdPremiumAnalysis.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int a = 0; a < grdPremiumAnalysis.HeaderRow.Cells.Count; a++)
{
grdPremiumAnalysis.HeaderRow.Cells[a].Style.Add("background-color", "#507CD1");
}
int j = 1;
foreach (GridViewRow gvrow in grdPremiumAnalysis.Rows)
{
gvrow.BackColor = Color.White;
if (j <= grdPremiumAnalysis.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
grdPremiumAnalysis.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
string imgPath1 = (Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + newPremChart));
string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + premIncome);
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=ebs1.xls;");
Response.AddHeader("Content-Disposition", "attachment; filename=ebs2.xls;");
StringWriter stringWriter1 = new StringWriter();
StringWriter stringWriter2 = new StringWriter();
HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWriter1);
HtmlTextWriter htmlWrite2 = new HtmlTextWriter(stringWriter2);
string headerTable1 = @"<Table><tr><td><img src='" + imgPath1 + @"'/></td></tr></Table>";
string headerTable2 = @"<Table><tr><td><img src='" + imgPath2 + @"'/></td></tr></Table>";
Response.Write(headerTable1);
Response.Write(headerTable2);
Response.Write(stringWriter1.ToString());
Response.Write(stringWriter2.ToString());
Response.End();
 
this is my Code Where i m going wrong 

Answers (3)