Tuesday, December 31, 2019

Datatable to excel Download

public static void ExcelPrintWithSubReport(string SpName, DataTable dataSource)
        {

            GridView gv = new GridView();
            gv.DataSource = dataSource;
            gv.DataBind();

            SpName = SpName + ".xls";

            HttpContext context = System.Web.HttpContext.Current;
            context.Response.ClearContent();
            context.Response.Buffer = true;
            context.Response.AddHeader("content-disposition", "attachment; filename= CheckData.xls");
            context.Response.ContentType = "application/ms-excel";
            context.Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            context.Response.Output.Write(sw.ToString());
            context.Response.Flush();
            htw.Close();
            sw.Close();
            context.Response.End();

        }// End of Method

No comments:

Post a Comment

Screen Record

 Windows Screen Record WindowsKey+ Alt + R Recording Starts.