title: Export DataTable to Excel in ASP.NET
body: <p>When calling this method, the whole page with html design ported to excel along with data.
  I wants export data only, should not come with html design. I couldn't find the solution, Please some one help this to resolve.</p>
  
  <pre><code> public void ExportToExcel(DataTable dtExportData)
          {
              string strReportName = string.Empty;
              //StringWriter oStringWriter = null;
              //Html32TextWriter oHtmlTextWriter = null;
              //TableOneDayPassReport.Visible = true;
              ExcelGrid.EnableViewState = false;
              ExcelGrid.DataSource = dtExportData;
              ExcelGrid.DataBind();
              Response.Clear();
              Response.ClearHeaders();
              Response.Cache.SetCacheability(HttpCacheability.Private);
              Response.AddHeader("content-disposition", "attachment;filename=OwnTransportDetails.xls");
              Response.Charset = "";
              Response.ContentType = "application/vnd.ms-excel";
              System.IO.StringWriter stringWrite = new System.IO.StringWriter();
              System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
              //TableOneDayPassReport.RenderControl(htmlWrite);
              Response.Write(stringWrite.ToString());
              //Response.End();
  
  
              // strReportName = dtExportData.TableName;
              // Report_Month.InnerHtml = "&lt;b&gt;" + strReportName + " Report&lt;/b&gt;&lt;br /&gt;";
              // ReportHead.Attributes.Add("style", "DISPLAY: block; VISIBILITY: visible");
              // TableOneDayPassReport.Visible = true;
              // //oStringWriter = new StringWriter();
              // //oHtmlTextWriter = new Html32TextWriter(oStringWriter);
              // ExcelGrid.EnableViewState = false;
              // ExcelGrid.DataSource = dtExportData;
              // ExcelGrid.DataBind();
              // Response.Clear();
              // Response.Buffer = true;
              // Response.AppendHeader("Content-Disposition", "attachment; filename=" + strReportName + ".xls");
              // Response.ContentType = "application/vnd.ms-excel";
              // TableOneDayPassReport.RenderControl(oHtmlTextWriter);
              // Response.Write(oStringWriter.ToString());
              // HttpContext.Current.ApplicationInstance.CompleteRequest();
              //// Response.End();
          }
  </code></pre>