Mam tabelę HTML na stronie widoku MVC programu ASP.NET. Teraz muszę wyeksportować tę tabelę do programu Excel.Eksportuj tabelę HTML do Excela
(1) Użyłem częściowy widok (Inquiries.ascx), aby wyświetlić dane tabeli z bazy danych (przy użyciu LINQ do podmiotu) (2) Ja również używali UITableFilter plugin do filtrowania rekordów (np http://gregweber.info/projects/demo/flavorzoom.html)
(3) W dowolnym momencie muszę filtrować widoczne rekordy do programu Excel.
Doceń odpowiedzi.
Dzięki
Rita
Oto moim zdaniem:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Mvc.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content2" ContentPlaceHolderID="cphHead" runat="server">
<script src="../../Scripts/jquery.tablesorter.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.uitablefilter.js" type="text/javascript"></script>
<script type="text/javascript">
//Load Partial View
$('#MyInquiries').load('/Home/Inquiries');
// To Apply Filter Expression using uiTableFilter plugin
$("#searchName").keyup(function() {
$.uiTableFilter($("#tblRefRequests"), this.value);
$("#tblRefRequests").tablesorter({ widthFixed: true, widgets: ['zebra'] });
});
//Export the HTML table contents to Excel
$('#export').click(function() {
//Code goes here
});
</script>
</asp:Content>
//Main Content
<asp:Content ID="Content1" ContentPlaceHolderID="cphContent" runat="server">
<h2 class="pageName">View All Inquiries</h2>
<input type="submit" value="Export to Excel" id="export" />
<div id='MyInquiries'></div>
</asp:Content>
silnie typami Częściowy widok kontrola użytkownika (Inquiries.ascx) w celu wygenerowania tabeli:
<table>
<tr><td valign ="middle">Filter Expression: <%= Html.TextBox("searchName")%></td></tr>
</table>
<table id="tblRefRequests" >
<thead>
<tr>
<th>Tx_ID</th>
<th>TX Date</th>
<th>Name</th>
<th>Email Address </th>
<th>Products</th>
<th>Document Name</th>
</tr>
</thead>
<tbody>
<% foreach (var item in Model) { %>
<tr>
<td visible =false><%= item.RequestID %></td>
<td><%= String.Format("{0:d}", item.RequestDate) %></td>
<td><%= item.CustomerName %></td>
<td><%= Html.Encode(item.Email) %></td>
<td><%= item.ProductName %></td>
<td><%= Html.Encode(item.DocDescription)%></td>
</tr>
<% } %>
</tbody>
</table>
Oto mój kod kontrolera, aby załadować widok częściowy zapytania:
[HttpGet]
public PartialViewResult Inquiries()
{
var model = from i in myEntity.Inquiries
where i.User_Id == 5
orderby i.TX_Id descending
select new {
RequestID = i.TX_Id,
CustomerName = i.CustomerMaster.FirstName,
RequestDate = i.RequestDate,
Email = i.CustomerMaster.MS_Id,
DocDescription = i.Document.Description,
ProductName = i.Product.Name
};
return PartialView(model);
}
próbowałem. Przychodzi jako ciąg w nowym oknie. Ale chcę to w pliku Excel. – Rita
Dodałem przykładowy kod. Sprawdź to. I nie wiem, w jaki sposób jesteś ustawiony na eksportowanie programu Excel. To cała inna bestia. Sugeruję eksportowanie CSV. O wiele prostszy, szybszy i lżejszy i elegancki jest eksport pliku CSV w porównaniu z plikiem Excel. Możesz wystawić rachunek jako "plik CSV dla programu Excel". To jest to co robię. –
Wygląda na to, że idziesz do pracy. Spróbuję teraz wkrótce zaktualizować. – Rita