About Me

My photo
a Dynamic and Energetic guy.....

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();
        }

No comments:

My Masters