Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code debug with no errors but not getting exported Excel file. Please Help Me

C#
protected void btnExport_Click(object sender, EventArgs e)
{
            dgvCreditSettle.DataSource = Session["Grid"]; // For data binding to gridview
            dgvCreditSettle.DataBind();
            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Charset = "";
            string FileName = "Credit Settle Details " + DateTime.Now.ToShortDateString() + ".xls";
            StringWriter strwritter = new StringWriter();
            HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
            dgvCreditSettle.GridLines = GridLines.Both;
            dgvCreditSettle.HeaderStyle.Font.Bold = true;
            dgvCreditSettle.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.End(); 
}

   public override void VerifyRenderingInServerForm(Control control)
        {
            /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
               server control at run time. */
        }
Posted
Updated 18-Mar-15 20:21pm
v4
Comments
F-ES Sitecore 18-Mar-15 7:44am    
What do you get? Have you tried a Response.Flush before Response.End? Also I hope you're aware your code doesn't actually generate an Excel document, it only generates an html table, but the content headers are telling the client browser that if the client as Excel installed then show the html table in Excel as a worksheet with rows and columns rather than showing it in the browser as an html table.
SyberBrain 18-Mar-15 23:39pm    
nothing :(, just execute code without any output file
I already installed Microsoft Excel
King Fisher 18-Mar-15 8:59am    
Do you have Update Panel on this Page?
SyberBrain 18-Mar-15 23:37pm    
Nop I don't have Update Panel
Arkadeep De 18-Mar-15 14:28pm    
if you have put this btnExport button within Updat Panel make a trigger with this id. Otherwise it wouldn't work.

1 solution

As per the above discussion, I believe that you are not using the Update Panel. I figured it out that you are not binding gridview before export. Make sure you load all the data and bind it to gridview before export.
C#
protected void btnExport_Click(object sender, EventArgs e)
{
  dgvCreditSettle.AllowPaging = false;
  //call your bind method here..
  LoadGridData();
  //now your export code here..
}

Let me know if still any issues.
 
Share this answer
 
Comments
SyberBrain 19-Mar-15 2:09am    
Thank you :)

but still i have a same problem :(
then i use this code but still have same problem

HtmlForm form = new HtmlForm();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "Student.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
dgvCreditSettle.AllowPaging = false;
dgvCreditSettle.DataSource = Session["Grid"]; // For data binding to gridview
dgvCreditSettle.DataBind();
//BindGridDetails(dgvCreditSettle);
form.Attributes["runat"] = "server";
form.Controls.Add(dgvCreditSettle);
this.Controls.Add(form);
form.RenderControl(hw);
string style = @"<!--mce:2-->";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
Modi Mayank 19-Mar-15 4:56am    
Here is the working tutorial on how to export excel data using c# example that might helpful to you. If this doesn't work, please update your question with .aspx and .cs code(keep related code, remove extra code).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900