您好,我正在寻找有关如何在ASP.NET MVC中导出为excel的最佳方法
现在我从billsternberger.net得到了这个
//Export to excel
public ActionResult Download()
{
List 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();
}
从绑定到datagrid并导出到excel。
现在,我需要做的是获取我的html表并将其导出到excel,在这里我在处理表数据时使用了jquery
datatable,因此它的重量更轻,因为它是在客户端完成的。
我尝试使用jquery和ajax将html表传递给控制器上的实体
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'
});
};
但这把我扔了,A potentially dangerous Request.Form value was detected from the
client等等。
如何在MVC上完成?我已经在寻找类似的话题,但是它总是使我陷入第一个工作样本中。
在此致谢