Click here to Skip to main content
15,915,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

here i am developing web application and in it i have gridview, and excel sheet having same column names as gridview and i want export the content of gridview into excel sheet with respect to the field name of girdview, please help me out friends,
Posted
Comments
Nespokill 21-Mar-16 9:25am    
Refer to this. The article shows how you can export GridView or DataGrid controls into a spreadsheet by using this C# component for excel.

Try this tool
Export To Excel
 
Share this answer
 
You should be use this method on your exceldownload button click event
 public void excelgridview()

{

        Response.Clear();

        Response.AddHeader("content-disposition", "attachment; filename=FileName.xls");

        Response.Charset = "";

        // If you want the option to open the Excel file without saving than

        // comment out the line below

        // Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite =
        new HtmlTextWriter(stringWrite);

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End();

    }

    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {

        // Confirms that an HtmlForm control is rendered for the
        //  specified ASP.NET server control at run time.

    }
 
Share this answer
 
 
Share this answer
 
private void ExportToExcel(string strFileName, DataGrid dg)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

try this
just pass name and that datagrid and called this function.
may this help you!
 
Share this answer
 
v2
Comments
RaviRanjanKr 24-Dec-11 17:15pm    
[Code is wrapped in pre tag[/Edited]
NekoNao 22-Sep-14 2:03am    
do you have answer for this?
Export to Excel : Two DataGridView[^
Google Please
export gridview into excel file in asp.net [^]
better to search in google first and try to make program. please try to revert back with specific question.
 
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