Here I share Export to Grid view in excel format using render control :
Steps :
1 : create common function Gridview export to excel
2: call common function where you want :
1 : common function for Grid view export to excel with image :
public static void ExportToExcel(string FileName, string HtmlWrite, string Descr)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "filename=" + FileName + ".zip");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/zip";
ZipFile zip = new ZipFile(HttpContext.Current.Response.OutputStream);
HtmlWrite = HtmlWrite.Replace("[$Logo$]", "<img src='image1.png' />");
zip.AddStringAsFile(FileIO.OptimizeHtml(HtmlWrite), FileName + ".xls", "");
zip.Save();
HttpContext.Current.Response.End();
}
2 : Call this function in your aspx.cs page :
protected void btnexptoexl_Click(object sender, EventArgs e) {
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
htmlWrite.Write("<table><tr><td rowspan='4'><img src="title.jpg'/></td><td></td><td></td><td></td></tr>");
htmlWrite.Write("<tr><td></td></tr><tr><td></td></tr>");
htmlWrite.Write("<tr><td></td><td></td>");
htmlWrite.Write("<td style='font-weight:bold;font-size:20px;color:Orange' align='Center' colspan='3'> Report</td>");
htmlWrite.Write("</tr><tr><td></td><td></td><td></td><td align='center' style='font-weight:bold;font-size:15px;' colspan='3'>From " + dateFrom + " To " + dateTo + "</td></tr>");
htmlWrite.Write("</table>");
gvmandate.RenderControl(htmlWrite);
CommonHelper.ExportToExcel("REPORT", htmlWrite.InnerWriter.ToString(), "");
}
0 comments:
Post a Comment