About Me

My photo
a Dynamic and Energetic guy.....
Showing posts with label Export to EXCEL. Show all posts
Showing posts with label Export to EXCEL. Show all posts

Wednesday, August 17, 2011

How to Open a DataSet using Microsoft Excel in a web application

protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            DataSet ds = CurrentApproverService.GetReportData(depID, UserID);

            DataGrid dg = new DataGrid();
            dg.DataSource = ds;
            dg.DataBind();

            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + "ReportData.xls");
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            dg.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();

            dg = null;
            dg.Dispose();
        }

My Masters