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 { get; set; }
public string longitude { get; set; }
public string title { get; set; }
public string type { get; set; }
}
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