anshad ali

anshad ali

  • NA
  • 7
  • 141

Export excel with multiple sheets using html content

Jan 3 2018 12:13 AM
I have a C# code to export excel using html string for one data table. The code shown below.Using this code created single excel sheet with html string for one data table.But I don't know how to create multiple sheets with html string with these method for multiple data tables. Is there any way to add multiple sheets using this method or any other method?
 
private void ExportToExcel(string fileName, string contentType, DataTable dt, string RptName)
{
var ColumnCount = dt.Columns.Count;
GridView gv = new GridView();
var reportName = fileName;
var reportPritedOn = DateTime.Now.ToString("dd/MM/yyyy");
StringBuilder reportHtml = new StringBuilder();
gv.DataSource = dt;
gv.DataBind();
reportHtml.Append("<html dir='ltr'><meta http-equiv='content-type' content='application/xhtml+xml; charset=UTF-8'/><body>" +
"<table dir='ltr' id='Table1' cellspacing='0' cellpadding='0' width='1000' border='1' borderColor='GhostWhite' align='left'>");
reportHtml.Append("<tr dir='ltr'><td align='left' width ='150' colspan='" + ColumnCount + "'><b><font size='3'>" + RptName + "</font></b><br></td></tr>");
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", reportName));
Response.ContentType = contentType;
StringWriter objSW = new StringWriter();
HtmlTextWriter objTW = new HtmlTextWriter(objSW);
Control parent = gv.Parent;
int GridIndex = 0;
if (parent != null)
{
GridIndex = parent.Controls.IndexOf(gv);
parent.Controls.Remove(gv);
}
gv.RenderControl(objTW);
if (parent != null)
{
parent.Controls.AddAt(GridIndex, gv);
}
reportHtml.Append("</table>");
reportHtml.Append(objSW.ToString());
reportHtml.Append("</body></html>");
Response.Output.Write(reportHtml.ToString());
Response.Flush();
Response.End();
}
 

Answers (1)