Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i want to export data from a datagridview to a excel sheet. i already add an reference
of excel sheet to my project.
but i m unable to do futhur coding.
Can anybody help me out n give me knowledge about the flow.
kudos
neaS
Posted
Comments
Virendra Mutkekar 7-Jan-13 3:35am    
Want code in VB6: Datagrid to Excel export and excel file protection. Can anyone help?

 
Share this answer
 
Use
C#
DataSet ds = new DataSet();
        
        ds = GetData(ID);
        grdGridView.AllowPaging = false;
        grdGridView.PageIndex = 0;
        grdGridView.DataSource = ds.Tables[0];
        grdGridView.DataBind();
        if (grdGridView.Rows.Count > 0)
        {
            grdGridView.GridLines = GridLines.Both;
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=MyFile.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.xls";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            grdGridView.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.Flush();
            Response.End();
        }
 
Share this answer
 
 
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