protected void ExportToExcel(DataTable dt)
{
this.lblMsg.Text = ""; NOT WORKING
string sFileName = "MyFile_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + ".xls";
GridView gv = new GridView();
gv.AllowPaging = false;
gv.DataSource = dt;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", sFileName));
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType = "application/vnd.openxmlformats";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//Change the Header Row Foreground to white color
gv.HeaderRow.Style.Add("color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
{
//Change to Blue
gv.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in gv.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gv.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
Response.Flush();
}
<asp:Content ID="Content2" ContentPlaceHolderID="cphBody" Runat="Server">
<script type="text/javascript">
function ClearTextbox() {
document.getElementById('<%=lblMsg.ClientID%>').value = '';
return false;
}
</script>
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
OnClientClick="return ClearTextbox();" />
</asp:Content>