Hi am looking for the best way on how export to excel does in ASP.NET MVC
Now i got this one from billsternberger.net
Export to Excel or CSV from ASP.NET MVC with C#
//Export to excel
public ActionResult Download()
{
List<Lookup> lookupList = data,GetLookupList();
var grid = new System.Web.UI.WebControls.GridView();
grid.DataSource = lookupList;
grid.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=YourFileName.xlsx");
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
return View();
}
which is working from binding to datagrid and export to excel.
Now what i need to do is get the my html table and export it to excel where i used jquery datatable on manipulating table data so it will be more light weight because it is done on client side.
I tried using jquery and ajax where i pass my html table to my entities on my controller
function Export()
{
var details = {};
details.LookupName = $("#tblLookup").html();
//Validate details
var url_ = generateURL("/Home/Download"); //Call Save Controller and pass details entities
$.ajax({
type: "POST",
url: url_,
data: details, //details will act as the Entities Model
traditional: true,
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error: " + XMLHttpRequest.responseText);
},
dataType: 'json'
});
};
But it throws me A potentially dangerous Request.Form value was detected from the client
etc,..
How is it done on MVC? I already look for some similar topic but it always drop me to my first working sample.
Thanks in Regards
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…