简体   繁体   English

如何在ASP.NET MVC 5中将HTML表从局部视图导出到excel

[英]How can I export an HTML table from a partial view to excel in ASP.NET MVC 5

I have spent hours searching all over the internet but it seems no one has a current answer. 我已经花了数小时在整个互联网上进行搜索,但似乎没有人得到当前的答案。 I have a simple table set up in a PARTIAL view and want to export the data in the html table not from the database. 我在PARTIAL视图中设置了一个简单的表,并希望从数据库中导出html表中的数据。 I do not have the option of using any plugins either. 我也没有选择使用任何插件的选择。 I am open to doing it through JavaScript/Jquery but I am fairly new to MVC and I have exhausted other solutions on the internet. 我愿意通过JavaScript / Jquery做到这一点,但是我对MVC还是很陌生,并且在互联网上已经用尽了其他解决方案。

Address Book Summary (Partial View) : 通讯簿摘要(部分视图)

<table class="table table-bordered table-responsive table-striped" id="tblAddressBook">
    <thead>
        <tr>
            <th> @Html.DisplayNameFor(model => model.Name)</th>
            <th> @Html.DisplayNameFor(model => model.Address)</th>
         <tr>
     </thead>
     <tbody>
     @foreach (var item in Model)
        {
        <td class="text-right">@Html.DisplayFor(modelItem => item.Name)</td>
        <td class="text-right">@Html.DisplayFor(modelItem => item.Address)</td>
     }
     </tbody>
     <tfoot>
        <td class="text-right>Model.Sum(model => model.TotalPeople))</td>
        <td class="text-right>Model.Sum(model => model.Addresses))</td>
     </tfoot>

Address Book 地址簿

<button type="button" class="btn btn-primary">
    Export
</button>
<div id="summaryAddressBook">
            @Html.Partial("AddressBook/_Summary", Model.Peoples)
</div>

Address Book Controller 地址簿控制器

public ActionResult Export() {
   Response.ContentType = "application/x-msexcel"; 
   Response.AddHeader("Content-Disposition", "attachment;filename=ExcelFile.xls");
   return View();
}

Personally, here is how I would get it done. 就个人而言,这就是我将如何完成它。 I would use JQuery to gather up the data from the table through this: 我将使用JQuery通过以下方法从表中收集数据:

var tableData = {}
$('#tblAddressBook > tbody  > tr').each(function() {...add each row's columns to tableData...});

Then make an AJAX call to an MVC Controller method. 然后,对MVC Controller方法进行AJAX调用。 See Here: Making a Simple Ajax call to controller in asp.net mvc 请参阅此处: 在asp.net mvc中对控制器进行简单的Ajax调用

With your data now in the controller method, you can go ahead and create your excel file with the data. 现在在控制器方法中使用数据,您可以继续使用数据创建excel文件。 I would probably go with .csv file, rather than .xls if you can, considering you most likely need a third party library to export to .xls. 考虑到您最有可能需要第三方库导出到.xls,我可能会选择.csv文件,而不是.xls。

Edit: Alternatively, you can skip the jquery and pass the table's model straight to the controller, see here: Pass Table Value from View to Controller MVC 编辑:或者,您可以跳过jquery,并将表的模型直接传递给控制器​​,请参见此处:将表值从View传递到Controller MVC

If "Address Book Summary"(PartialView) contains only the table then change the code :- 如果“通讯簿摘要”(PartialView)仅包含表,则更改代码:-

public ActionResult Export() {
    Response.ContentType = "application/x-msexcel"; 
    Response.AddHeader("Content-Disposition", "attachment;filename=ExcelFile.xls");
    return View();
}

to :- 至 :-

public ActionResult Export()
{
    Response.ContentType = "application/x-msexcel"; 
    Response.AddHeader("Content-Disposition", "attachment; filename=ExcelFile.xls");
    return PartialView("Address_Book_Partial_View_FileName_Here_Without_Extension");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM