locked
Export Dataset To Excel? RRS feed

  • Question

  • User-817808371 posted

    Hi,

    Here i need to export dataset to excel. please show me a link or way

    Wednesday, July 9, 2008 7:14 AM

Answers

  • User-595745061 posted

     private void Button3_Click(object sender, System.EventArgs e)
            {
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                
                DataGrid dgGrid = new DataGrid();
            

                dtgPCA.HeaderStyle.Font.Bold = true;
                dtgPCA.DataBind();
                dtgPCA.RenderControl(hw);

                Response.AddHeader("content-disposition", "attachment;filename=d:\\documents\\PCAADetails.xls");
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);

                Response.ContentType = "application/vnd.ms-excel";
                this.EnableViewState = false;
                Response.Write(tw.ToString());
                Response.End();    


            }



    =========================================================

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 9, 2008 7:25 AM
  • User-583891309 posted

     public class ExcelExport
    {
        public static void ExportDataSetToExcel(DataSet ds, string filename)
        {
            HttpResponse response = HttpContext.Current.Response;

            // first let's clean up the response.object
            response.Clear();
            response.Charset = "";

            // set the response mime type for excel
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

            // create a string writer
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    // instantiate a datagrid
                    DataGrid dg = new DataGrid();
                    dg.DataSource = ds.Tables[0];
                    dg.DataBind();
                    dg.RenderControl(htw);
                    response.Write(sw.ToString());
                    response.End();
                }
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 9, 2008 7:29 AM

All replies

  • User-595745061 posted

     private void Button3_Click(object sender, System.EventArgs e)
            {
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                
                DataGrid dgGrid = new DataGrid();
            

                dtgPCA.HeaderStyle.Font.Bold = true;
                dtgPCA.DataBind();
                dtgPCA.RenderControl(hw);

                Response.AddHeader("content-disposition", "attachment;filename=d:\\documents\\PCAADetails.xls");
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);

                Response.ContentType = "application/vnd.ms-excel";
                this.EnableViewState = false;
                Response.Write(tw.ToString());
                Response.End();    


            }



    =========================================================

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 9, 2008 7:25 AM
  • User-583891309 posted

     public class ExcelExport
    {
        public static void ExportDataSetToExcel(DataSet ds, string filename)
        {
            HttpResponse response = HttpContext.Current.Response;

            // first let's clean up the response.object
            response.Clear();
            response.Charset = "";

            // set the response mime type for excel
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

            // create a string writer
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    // instantiate a datagrid
                    DataGrid dg = new DataGrid();
                    dg.DataSource = ds.Tables[0];
                    dg.DataBind();
                    dg.RenderControl(htw);
                    response.Write(sw.ToString());
                    response.End();
                }
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 9, 2008 7:29 AM