You must Sign In to post a response.
  • Category: ASP.NET

    Export to excel from web user control gridview is not working.

    Hi,
    i want export gridview data to excel,i am trying to export and it's working in aspx page but when i am using same code in web user control(ascx) page , it's not working.

    even i tried to override VerifyRenderingInServerForm in my aspx page where i am binding user control also not working.
    public override void VerifyRenderingInServerForm(Control control) { }


    protected void ExporttoExcel_Click(object sender, EventArgs e)
    {
    ExporttoExcel();
    }
    private void ExporttoExcel()
    {
    BindGridDetails();
    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);
    GridView1.AllowPaging = false;
    // BindGridDetails(GridView1);
    form.Attributes["runat"] = "server";
    form.Controls.Add(GridView1);
    this.Controls.Add(form);
    form.RenderControl(hw);
    string style = @"<style> .textmode { mso-number-format:\@;}</style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
    }
    protected void BindGridDetails()
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("StudentID", typeof(Int32));
    dt.Columns.Add("Student_Name", typeof(string));
    dt.Columns.Add("City", typeof(string));
    DataRow dtrow = dt.NewRow();
    dtrow["StudentID"] = 1;
    dtrow["Student_Name"] = "Rakesh";
    dtrow["City"] = "Delhi";
    dt.Rows.Add(dtrow);
    dtrow = dt.NewRow();
    dtrow["StudentID"] = 2;
    dtrow["Student_Name"] = "Suresh";
    dtrow["City"] = "Mumbai";
    dt.Rows.Add(dtrow);
    dtrow = dt.NewRow();
    dtrow["StudentID"] = 3;
    dtrow["Student_Name"] = "Samir";
    dtrow["City"] = "Bangalore";
    dt.Rows.Add(dtrow);

    GridView1.DataSource = dt;
    GridView1.DataBind();

    }
    protected void BindingGridView_Click(object sender, EventArgs e)
    {
    BindGridDetails();
    }

    let me know if you need more information.

    Thanks
  • #765128
    Hi,
    Please go through following links:
    https://code.msdn.microsoft.com/office/Export-GridView-to-07c9f836
    http://stackoverflow.com/questions/14040707/export-user-controls-gridview-to-excel
    http://forums.asp.net/t/1358354.aspx?gridview+export+to+excel+usercontrol
    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #765132
    Hi santosh,

    I think it's not because of UserControl, it might be some different problem I guess, request you to create dummy UserControl and use the same code and check it again.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765135
    I got some code, see if its useful for you
    public class ExcelUtility
    {
    public static void ToExcel(object dataSource)
    {
    GridView grid = new GridView { DataSource = dataSource };
    grid.DataBind();

    StringBuilder sb = new StringBuilder();
    foreach (TableCell cell in grid.HeaderRow.Cells)
    sb.Append(string.Format("\"{0}\",", cell.Text));
    sb.Remove(sb.Length - 1, 1);
    sb.AppendLine();

    foreach (GridViewRow row in grid.Rows)
    {
    foreach (TableCell cell in row.Cells)
    sb.Append(string.Format("\"{0}\",", cell.Text.Trim().Replace(" ", string.Empty)));
    sb.Remove(sb.Length - 1, 1);
    sb.AppendLine();
    }
    ExportToExcel(sb.ToString());
    }

    private static void ExportToExcel(string data)
    {
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Report.csv");
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    HttpContext.Current.Response.ContentType = "text/csv";
    HttpContext.Current.Response.Write(data);
    HttpContext.Current.Response.End();
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765145
    Following are some the tips.
    1. Are you getting any exception. If yes post that so the we will get more idea.
    2. If you are not getting any exception. There should be data issue. Debug and check somewhere you may lose your data because of post back.
    3. You can create the button for upload in the user control itself.(Create delicate for handling the event)

    By Nathan
    Direction is important than speed


Sign In to post your comments