C# 在asp.net MVC2中导出html表格到excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14038811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Export html table to excel in asp.net MVC2
提问by BizApps
Hi am looking for the best way on how export to excel does in ASP.NET MVC
嗨,我正在寻找有关如何在 ASP.NET MVC 中导出到 excel 的最佳方法
Now i got this one from billsternberger.net
现在我从 billsternberger.net 得到了这个
Export to Excel or CSV from ASP.NET MVC with C#
使用 C# 从 ASP.NET MVC 导出到 Excel 或 CSV
//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.
这是从绑定到数据网格并导出到 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.
现在我需要做的是获取我的 html 表并将其导出到 excel 中,我在其中使用 jquery 数据表来操作表数据,因此重量会更轻,因为它是在客户端完成的。
I tried using jquery and ajax where i pass my html table to my entities on my controller
我尝试使用 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'
});
};
But it throws me A potentially dangerous Request.Form value was detected from the client
etc,..
但它让我A potentially dangerous Request.Form value was detected from the client
等,..
How is it done on MVC? I already look for some similar topic but it always drop me to my first working sample.
在MVC上是怎么做的?我已经在寻找一些类似的主题,但它总是让我想到我的第一个工作示例。
Thanks in Regards
致谢
采纳答案by Darin Dimitrov
The easiest solution would be to export the HTML table as CSV file and send to the server. Let's take an example. Suppose that we have defined a view model:
最简单的解决方案是将 HTML 表导出为 CSV 文件并发送到服务器。让我们举个例子。假设我们已经定义了一个视图模型:
public class ExportViewModel
{
[AllowHtml]
public string Csv { get; set; }
}
and a 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");
}
}
Now inside the corresponding view we suppose that we have generated some <table>
(the way this table is generated is really not interesting here):
现在在相应的视图中我们假设我们已经生成了一些<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>
We are using the table2CSV jQuery plugin
to convert the HTML table into a CSV format. The resulting CSV will then be stored inside a hidden field just before the form is submitted to the server.
我们正在使用table2CSV jQuery plugin
将 HTML 表格转换为 CSV 格式。在将表单提交到服务器之前,生成的 CSV 将存储在隐藏字段中。
If you want to build native XLSX files you will have to use the OpenXML SDKon the server. You cannot just take an HTML table and turn it into a native Excel file. This solution will be more difficult to put into practice as you will have to send only the data to the server but it will allow you far greater customization over the resulting Excel file.
如果要构建本机 XLSX 文件,则必须在服务器上使用OpenXML SDK。您不能仅将 HTML 表格转换为本地 Excel 文件。此解决方案将更难实施,因为您只需将数据发送到服务器,但它允许您对生成的 Excel 文件进行更大程度的自定义。