Translate

Wednesday, 18 November 2015

How to export List Data to EXCEL,Word,PDF..

Export List data to Excel, Word, Pdf is shown below.

In Controller:
Getting data :

public ActionResult jQGRID()
        {
            Sample sample = new Sample();
            List<Sample> lst = new List<Sample>();
            lst = sample.GetList();
            Session["Export"] = lst;
            return View(lst);
        }



in view:



<input type="button" id="Exporttopdf" value="Exporttopdf" />
<input type="button" id="ExporttoExcel" value="Export to Excel" />
<input type="button" id="ExporttoWord" value="Export to Word" />

onclick functions using Jquery:

<script>
    $(document).ready(function () {
        $("#Exporttopdf").click(function () {
            window.location = '@Url.Action("ExportGridToPDF","JqGrid")';
        });
        $("#ExporttoExcel").click(function () {
            window.location = '@Url.Action("ExportToExcel","JqGrid")';
        });
        $("#ExporttoWord").click(function () {
            window.location = '@Url.Action("ExportToWord","JqGrid")';
        });
        
    });
</script>

Methods when click on buttons to export in controller:


public void ExportToExcel()
        {
            if (Session["Export"] != null)
            {
                GridView gv = new GridView();
                gv.DataSource = Session["Export"];
                gv.DataBind();
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment; filename=Kavitha.xls");
                Response.ContentType = "application/ms-excel";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
                Response.Charset = "";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                gv.RenderControl(htw);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }

        public void ExportGridToPDF()
        {
            if (Session["Export"] != null)
            {
                GridView gv = new GridView();
                gv.DataSource = Session["Export"];
                gv.DataBind();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Kavitha.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                gv.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();

            }

public void ExportToWord()
        {
            if (Session["Export"] != null)
            {
                GridView gv = new GridView();
                gv.DataSource = Session["Export"];
                gv.DataBind();
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition",
                "attachment;filename=Kavitha.doc");
                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-word ";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                gv.RenderControl(hw);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }

        }

No comments:

Post a Comment