Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a grid view like the following:
XML
<form id="form21">
  <asp:GridView ID="GVcandidates" runat="server" AllowPaging="true" AllowSorting="True" AutoGenerateColumns="False" CssClass="tabLayout_Fixed" OnPageIndexChanging="GVcandidates_PageIndexChanging" OnRowCommand="GVcandidates_RowCommand" OnRowDataBound="GVcandidates_RowDataBound" OnSorting="GVcandidates_Sorting" PageSize="50" Width="100%">
</form>


I modified the page header like:
EnableEventValidation="false"


And onclick of an button I write the following in code behind:
C#
protected void exclexp_Click(object sender, EventArgs e)
{
   Response.ClearContent();
   Response.Buffer = true;
   Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
   Response.ContentType = "application/ms-excel";
   StringWriter sw = new StringWriter();
   HtmlTextWriter htw = new HtmlTextWriter(sw);
   GVcandidates.AllowPaging = false;
   //Change the Header Row back to white color
   GVcandidates.HeaderRow.Style.Add("background-color", "#FFFFFF");
   //Applying stlye to gridview header cells
   for (int i = 0; i < GVcandidates.HeaderRow.Cells.Count; i++)
   {
      GVcandidates.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 GVcandidates.Rows)
   {
      //gvrow.BackColor = Color.White;
      if (j <= GVcandidates.Rows.Count)
      {
         if (j % 2 != 0)
         {
            for (int k = 0; k < gvrow.Cells.Count; k++)
            {
               gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
            }
         }
      }
      j++;
   }
   GVcandidates.RenderControl(htw);
   Response.Write(sw.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. */
}


When I click the button nothing is happening, please help I am very new to asp.net
Posted
Updated 21-Apr-14 23:21pm
v2
Comments
Honey Jagyasi 22-Apr-14 5:27am    
Try to debug the above code first. Always maintain ur code in Try catch block. If you find any error during debugging,Paste here.
srikanta.mondol 22-Apr-14 5:30am    
I have done a debugging, but not getting any error, it is executing successfully and landing to the browser, with no result.

The Excel file format is not HTML, so you either need to use a library for creating true Excel files like http://epplus.codeplex.com/[^] or you should create a html representation of what you want.
 
Share this answer
 
Use this code for export to excel gridview data. Make sure you pass correct datasource to dt(You can provide same datasource which you provide to gridview).

onbuttonclick()
{
datatable dt= new datatable();
dt= obj.getdataforexcel(); // here you can use datasource with which gridview is bind.
ExportToExcel(dt);
}

//export to excel function
  public void ExportToExcel(DataTable dt)
        {
            string XlsPath = "Sheet1";
            string attachment = string.Empty;
            HttpContext context = HttpContext.Current;
            if (XlsPath.IndexOf("\\") != -1)
            {
                string[] strFileName = XlsPath.Split(new char[] { '\\' });
                attachment = "attachment; filename=" + strFileName[strFileName.Length - 1] + ".xls";
            }
            else
                attachment = "attachment; filename=" + XlsPath + ".xls";
            try
            {
                context.Response.ClearContent();
                context.Response.AddHeader("content-disposition", attachment);
                context.Response.ContentType = "application/vnd.ms-excel";
               
                string tab = string.Empty;

                foreach (DataColumn datacol in dtRecords.Columns)
                {
                    context.Response.Write(tab + datacol.ColumnName);
                    tab = "\t";
                }
                context.Response.Write("\n");

                foreach (DataRow dr in dtRecords.Rows)
                {
                    tab = "";
                    for (int j = 0; j < dtRecords.Columns.Count; j++)
                    {
                        context.Response.Write(tab + Convert.ToString(dr[j]));
                        tab = "\t";
                    }

                    context.Response.Write("\n");
                }
                context.Response.End();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
Share this answer
 

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