gpt4 book ai didi

c# - 在 asp.net MVC2 中将 html 表导出到 excel

转载 作者:行者123 更新时间:2023-11-30 16:20:37 26 4
gpt4 key购买 nike

您好,我正在寻找有关如何在 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();
}

从绑定(bind)到数据网格到导出到 excel。

现在我需要做的是获取我的 html 表并将其导出到 excel 中,我在其中使用 jquery datatable 来处理表数据,因此它会更轻量级,因为它是在客户端完成的。

我尝试使用 jquery 和 ajax,将 html 表传递给 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'
});

};

但它向我抛出 A potentially dangerous Request.Form value was detected from the client 等等,..

它是如何在 MVC 上完成的?我已经在寻找一些类似的主题,但它总是让我转到我的第一个工作示例。

致谢

最佳答案

最简单的解决方案是将 HTML 表格导出为 CSV 文件并发送到服务器。让我们举个例子。假设我们已经定义了一个 View 模型:

public class ExportViewModel
{
[AllowHtml]
public string Csv { get; set; }
}

和一个 Controller :

public class HomeController : Controller
{
public ActionResult Index()
{
return View(new ExportViewModel());
}

[HttpPost]
public ActionResult Export(ExportViewModel model)
{
var cd = new ContentDisposition
{
FileName = "YourFileName.csv",
Inline = false
};
Response.AddHeader("Content-Disposition", cd.ToString());
return Content(model.Csv, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}

现在在相应的 View 中,我们假设我们已经生成了一些 <table> (这个表的生成方式在这里真的没什么意思):

@model ExportViewModel

<table id="myTable">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Foo</td>
</tr>
<tr>
<td>2</td>
<td>Bar</td>
</tr>
</tbody>
</table>

@using (Html.BeginForm("Export", null, FormMethod.Post, new { id = "export" }))
{
@Html.HiddenFor(x => x.Csv)
<button type="submit">Export to Excel</button>
}

<script type="text/javascript" src="http://www.kunalbabre.com/projects/table2CSV.js"></script>
<script type="text/javascript">
$('#export').submit(function () {
$('#Csv').val($('#myTable').table2CSV({ delivery: 'value' }));
});
</script>

我们正在使用 table2CSV jQuery plugin 将 HTML 表格转换为 CSV 格式。生成的 CSV 将在表单提交到服务器之前存储在隐藏字段中。

如果你想构建原生 XLSX 文件,你将不得不使用 OpenXML SDK在服务器上。您不能只使用 HTML 表格并将其转换为 native Excel 文件。该解决方案将更难付诸实践,因为您只需将数据发送到服务器,但它允许您对生成的 Excel 文件进行更大程度的自定义。

关于c# - 在 asp.net MVC2 中将 html 表导出到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14038811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com