【发布时间】:2012-12-27 09:24:30
【问题描述】:
您好,我正在寻找如何在 ASP.NET MVC 中导出到 excel 的最佳方法
现在我从 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();
}
从绑定到datagrid并导出到excel。
现在我需要做的是获取我的 html 表并将其导出到 excel 中,我在其中使用 jquery 数据表来处理表数据,因此它会更轻,因为它是在客户端完成的。
我尝试使用 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 上完成的?我已经在寻找一些类似的主题,但它总是让我进入我的第一个工作示例。
在此致谢
【问题讨论】:
-
对于这种类型的错误,我们通常在控制器上使用 validateRequest="false" attr...试试看..
标签: c# jquery ajax asp.net-mvc-2 export-to-excel