Click here to Skip to main content
15,885,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Datatable which contains some data.I want to save that data in to Excel.Please help
Posted
Comments
Nandakishore G N 20-Jun-14 0:53am    
What have you done till now paste it. First, search in google you will get many examples, try it if there is any problem then request for suggestion here.

use gridview to excel file

bind your datatable to gridview and export using below mention code.


C#
Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + yourfilename);
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
 
Share this answer
 
v2
Hi,

Try this one. Hope it will solve your problem

C#
public void ExportToExcel(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                string filename = "Report.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();

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


Thanks
 
Share this answer
 
protected void btnSave_Click(object sender, EventArgs e)
        {

            try
            {

             if (ViewState["Excel"] != null && ViewState["Excel"].ToString() != "")
                {
                    DataTable dt = (DataTable)ViewState["Excel"];

                    //  Create a dummy GridView
                    GridView GridView1 = new GridView();
                    GridView1.AutoGenerateColumns = false;

                    BoundField bfield = new BoundField();
                    bfield.DataField = "First_Name";
                    bfield.HeaderText = "Name";
                    GridView1.Columns.Add(bfield);


                    GridView1.AllowPaging = false;
                    GridView1.DataSource = dt;
                    GridView1.DataBind();



                    // GridView1.Page.EnableViewState = false;
                    StringWriter sWriter = new StringWriter();
                    HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
                    GridView1.RenderControl(hWriter);
                    string HtmlInfo = sWriter.ToString().Trim();
                    FileStream fStream = new FileStream("e:\\datagjobs.xls", FileMode.Create);
                    BinaryWriter BWriter = new BinaryWriter(fStream);
                    BWriter.Write(HtmlInfo);
                    BWriter.Close();
                    fStream.Close();
                    //Label1.Text = "File write to c:\\data.xls ";
                }
            }
            catch (Exception ex)
            {
                // Label1.Text = ex.ToString();
            }
        }
 
Share this answer
 
Visit this link

Excel Shet as Database
You can take data from excel sheet in Dataset and then send it to your database
 
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