Link to home
Start Free TrialLog in
Avatar of Kiran Sonawane
Kiran SonawaneFlag for India

asked on

Export excel asp.net

I have data table. I am able to export the data table to excel. I need to export the excel from 5th ( 6th, 7th ...should have control of mine) column.

Help me out!!!
SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kiran Sonawane

ASKER

I want to export the grid to excel from E1 excel cell

 protected void Page_Load(object sender, EventArgs e)
        {

            DataTable dt = new DataTable();


            DataColumn[] adcCols = new DataColumn[3];
            for (int i = 0; i < adcCols.Length; i++)
            {
                adcCols[i] = new DataColumn();
            }

            dt.Columns.AddRange(adcCols);


            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(int));

            DataRow dr = dt.NewRow();
            dr["Name"] = "AAAA";
            dr["Age"] = 21;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Name"] = "BBBBB";
            dr["Age"] = 22;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Name"] = "CC";
            dr["Age"] = 33;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Name"] = "DDDDD";
            dr["Age"] = 32;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Name"] = "EEEEE";
            dr["Age"] = 43;
            dt.Rows.Add(dr);

            ExportToExcel(dt);


        }


        public void ExportToExcel(DataTable dt)
        {

            
            
            
            if (dt.Rows.Count > 0)
            {
                string filename = "DownloadMobileNoExcel.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.ItemDataBound += new DataGridItemEventHandler(dgGrid_ItemDataBound);
                dgGrid.DataSource = dt;
                dgGrid.DataBind();

                //Get the HTML for the control.
                dgGrid.RenderControl(hw);
                //Write the HTML back to the browser.
                //Response.ContentType = application/vnd.ms-excel;
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                this.EnableViewState = false;
                Response.Write(tw.ToString());
                Response.End();
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial