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

        }

Check Login session before every action hitting in MVC



Here is checking the session values every time when before hitting to every action.



  protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Session["User"] != null)
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                TempData["TimeOut"] = "Please login again.";
                filterContext.Result = RedirectToAction("Index", "Home", new { area = "" });
            }

        }

Ajax and getJSon functions to call a method in MVC



Ajax and getJSON functions to call a method in mvc


Ajax function:

<script>
    $(document).ready(function () {
            $.ajax({
                url: '@Url.Action("test","JqGrid")', type: "POST", datatype: "JSON", data: { id: $("#txtMobile").val(), Name: $("#txtName").val() }, success: function (data) {
                    if (data != null) {
                        alert("success");
                        //$.each(data, function (sample, i) {
                        //});
                    }
                }
            });
           
        });
</script>


getJSON function

<script>
    $(document).ready(function () {
           
            $.getJSON('@Url.Action("test","JqGrid")', { id: $("#txtMobile").val(), Name: $("#txtName").val() }, function (data) {
                if (data != null) {
                    alert("success");
                }
            });
        });

</script>