5

ASP.NET MVC ビュー ページに HTML テーブルがあります。次に、このテーブルを Excel にエクスポートする必要があります。

(1) 部分ビュー (Inquiries.ascx) を使用してデータベースからテーブル データを表示しました (LINQ to Entity を使用) (2) UITableFilter プラグインを使用してレコードをフィルター処理しました (例: http://gregweber.info/ projects/demo/flavorzoom.html )

(3) いつでも、表示されているレコードを Excel にフィルター処理する必要があります。

あなたの応答に感謝します。

ありがとう

リタ

これが私の見解です:

<%@ 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>

テーブルを生成する厳密に型指定された部分ビュー ユーザー コントロール (Inquiries.ascx):

<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>

Inquiries 部分ビューをロードするコントローラー コードを次に示します。

[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);
        }
4

2 に答える 2

5

jQuery プラグインtable2csvを試してください。csv を文字列として返すには、引数 delivery:'value' を使用します。

実装は次のとおりです。

  1. ページに通常の html 入力ボタンと .NET HiddenField を追加します。
  2. 「エクスポート」というボタンに onclick イベントを追加します。
  3. table2CSV() の戻り値を非表示フィールドに格納し、ポストバックする JavaScript 関数 Export を作成します。
  4. サーバーはhiddenfield投稿データ(文字列としてのcsv)を受け取ります
  5. サーバーは文字列をcsvファイルとしてブラウザに出力します

.

// javascript  
function Export()  
{    
    $('#yourHiddenFieldId').val() = $('#yourTable').table2CSV({delivery:'value'});  
    __doPostBack('#yourExportBtnId', '');  
}

// c#  
if(Page.IsPostBack)  
{
    if(!String.IsNullOrEmpty(Request.Form[yourHiddenField.UniqueId]))  
    {  
        Response.Clear();  
        Response.ContentType = "text/csv";  
        Response.AddHeader("Content-Disposition", "attachment; filename=TheReport.csv");  
        Response.Flush();  
        Response.Write(Request.Form[yourHiddenField.UniqueID]);  
        Response.End();  
    }  
}
于 2010-02-04T06:04:52.707 に答える
0

コンポーネントのダウンロード:npm install table-to-excel

https://github.com/ecofe/tabletoexcel

var tableToExcel=new TableToExcel();
document.getElementById('button1').onclick=function(){

    tableToExcel.render("table");

};
document.getElementById('button2').onclick=function(){
    var arr=[
        ['LastName','Sales','Country','Quarter'],
        ['Smith','23','UK','Qtr 3'],
        ['Johnson','14808','USA','Qtr 4']
    ]
    tableToExcel.render(arr,[{text:"create",bg:"#000",color:"#fff"},{text:"createcreate",bg:"#ddd",color:"#fff"}]);
};

于 2016-10-18T01:27:58.657 に答える