Tuesday, June 2, 2015

How to Export DataTable to Excel file in C#.net.

 In this method we pass only datatable and file name.This will export whole datatable to excel file with given name.
Try this......................

 public void ExportToExcel(DataTable dt, string filename)
        {


            Response.Clear();
            Response.Charset = "";
            //Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            Response.AddHeader("content-disposition", "attachment; filename=" + filename + "");
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView g = new GridView();

            DataTable d = new System.Data.DataTable();


            g.AllowPaging = false;

            DataTable dtExport = new DataTable();
            dtExport = dt;

            g.DataSource = dtExport;
            g.DataBind();
            if (dtExport.Rows.Count > 0)
            {
                //Change the Header Row back to white color
                g.HeaderRow.Style.Add("background-color", "#FFFFFF");
                //Applying stlye to gridview header cells
                for (int i = 0; i < g.HeaderRow.Cells.Count; i++)
                {
                    g.HeaderRow.Cells[i].Style.Add("background-color", "#00ab96");
                    g.HeaderRow.Cells[i].Style.Add("color", "#FFFFFF");
                }
                int j = 1;
                //This loop is used to apply stlye to cells based on particular row
                foreach (GridViewRow gvrow in g.Rows)
                {
                    gvrow.BackColor = Color.White;
                    if (j <= g.Rows.Count)
                    {
                        if (j % 2 != 0)
                        {
                            for (int k = 0; k < gvrow.Cells.Count; k++)
                            {
                                gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                                //gvrow.Cells[k].Style.Add("color", "#FFFFFF");
                            }
                        }
                    }
                    j++;
                }
                g.RenderControl(htmlWrite);
                Response.Write(stringWrite.ToString());
                Response.End();
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "+filename+"));
                Response.ContentType = "application/ms-excel";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
            }
            else
            {
                //
            }
        }

 

 

No comments:

Post a Comment