Search code examples
web-controls

How to Export Webcontrol table to excel in asp.net mvc


List<Table> t = (List<Table>)GenerateLayout(dtDPList, dtBOList, dpl);

In the list i am getting the 3 Webcontrol table and i want to export those webconrol tables to excel.. so please help me..


Solution

  • List list = GenerateLayout(dt1,dt2,dpl); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment;filename=DeliveryPickList.xls"); Response.AddHeader("Content-Type", "application/vnd.ms-excel");

            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    list[0].RenderControl(htw);
                    TextWriter output= Response.Output;
                    output.Write(sw.ToString());
                }
            }
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    list[1].RenderControl(htw);
                    TextWriter output = Response.Output;
                    output.Write(sw.ToString());
                }
            }
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    list[2].RenderControl(htw);
                    TextWriter output = Response.Output;
                    output.Write(sw.ToString());
                }
            }
            Response.End();