locked
exporting Gridview To Excel RRS feed

  • Question

  • User2033107836 posted

    Hello

    I'm using GridViewHelper my gridview show like this

    when i export to excel it showing like this not in same as gridview

    need proper code and i excel also it should look like same as gridview

    Sunday, January 29, 2017 10:05 AM

All replies

  • User409696431 posted

    You have to post the code you are using before someone can tell you what is wrong.

    Sunday, January 29, 2017 11:10 AM
  • User2033107836 posted

    On button click i used this query to export gridview

     Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=Receipe_Report.xls");
            Response.Charset = "";
            this.EnableViewState = false;
    
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
    
            grdtanview.RenderControl(htw);
    
            Response.Write(sw.ToString());
            Response.End();

    Sunday, January 29, 2017 11:38 AM
  • User2117486576 posted

    I think your problem is that the RenderControl() method actually creates an HTML file not an Excel file.

    Here is a link with very clear instructions for a better approach using the open source EPPlus package.

    http://www.mikesdotnetting.com/article/278/a-better-way-to-export-gridviews-to-excel

    Sunday, January 29, 2017 2:08 PM
  • User-1838255255 posted

    Hi asp.ambur,

    According to your description, please try this method then modify your code to export gridview to excel. Also I am not clear you how to write Article Name:Product1 and Color: Green, so I hope you could give us more explanation and code. Please refer to this sample:

    protected void btnExport_Click(object sender, EventArgs e)
    {       
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.Charset = "";
    
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    
        GridView1.RenderControl(oHtmlTextWriter);
    
        Response.Write(oStringWriter.ToString());
        Response.End();
    }

    Also you could refer to these links:

    Export GridView to Excel in ASP.Net with Formatting using C# and VB.Net:

    http://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx 

    Export GridView To Word Excel PDF CSV Formats in ASP.Net:

    http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx 

    Best Regards,

    Eric Du

    Monday, January 30, 2017 6:22 AM
  • User2033107836 posted

    i tried this code not working 

    its already in gridivew ArticleName and Color i used Gridviewhelperclass

    Please do let me know why excel is not the same as gridview looks after exporting..

    Thanking You

    Monday, January 30, 2017 9:44 AM
  • User1324658857 posted

    Hi,

    I think this article may be can help you:

    http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm

    Wednesday, February 8, 2017 6:04 AM