Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I take one gridview.in that 7 columns..in 7th column i take another gidview.but problem is i want to show this gridview in excel..but in excel it shows only outer grid nag 7 th column is 0..so i want to show 7 column grid also..in 7th column gridview i take three columns and show related data..plz help me
Posted

hi
check it out
public void ExportToExcel(DataTable dt)
    {
            con.Open();
            string sql = "select *from test";
            cmd = new SqlCommand(sql, con);
            dt = new DataTable();
            da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            cmd.Dispose();


            string filename = "DownloadTest.xls";
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            DataGrid dgGrid = new DataGrid();
            dgGrid.DataSource = dt;
            dgGrid.DataBind();

            dgGrid.RenderControl(hw);
            
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
            this.EnableViewState = false;
            Response.Write(tw.ToString());
            Response.End();


            con.Close();
    }
 
Share this answer
 
Hey, check this article, you may find answer here:
9 Solutions to Export Data to Excel for ASP.NET[^]
 
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