Friday, November 7, 2014

Export in excel in mvc asp.net


In this example we have shown how to  use the export the data in excel in mvc. I  have create method “ExportToExcelInMVC” in controller. I have  create dummy class as “PushPinModel” as shown below
 
 
public class PushPinModel
{
    public string latittude { getset; }
    public string longitude { getset; }
    public string title { getset; }
    public string type { getset; }
}
 
  public ActionResult ExportToExcelInMVC()
        {

            //get loaction or latitude longitude according to databases            
            List<PushPinModel> pushPinCollection = new List<PushPinModel>();
            pushPinCollection.Add(new PushPinModel { latittude = "43.4155817799037", longitude = "-123.345552938182", title = "tejjs", type = "Atm" });
            pushPinCollection.Add(new PushPinModel { latittude = "44.0504273726889", longitude = "-121.354689398906", title = "help", type = "Atm" });


            var grid = new GridView();
            grid.DataSource = pushPinCollection;
            grid.DataBind();

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition""attachment; filename=address.xls");
            Response.ContentType = "application/ms-excel";

            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

            return View("ExportToExcelInMVC");
        }
 
 
 In view
 
<input type="button"  id="btnExport" value="export" />
<script type="text/javascript">

    $(function() {
   
        $("#btnExport").click(function() {
            windoow.location.href== '@Url.Action("ExportToExcelInMVC""home")';
        })
     
       
    } );

</script>

No comments:

Post a Comment

http://blogsiteslist.com