【问题标题】:C#/Javascript datatable export to Excel and PDFC#/Javascript 数据表导出到 Excel 和 PDF
【发布时间】:2014-12-05 03:16:20
【问题描述】:

我从一位前同事那里接手了一个 C# 实体框架项目。他使用 Javascript 以我不熟悉的方式设置了一些表格。我需要将此数据表的内容导出到 Excel 和 PDF 文件(每个单独的按钮)。我愿意使用 C#、Javascript 或 JQuery。

我最近一直在做 ColdFusion,并且几乎一年都没有看过任何与 .NET 相关的东西,所以我试图复习一下,并找出我以前从未见过的东西,所以我的脑子里一片空白.

用于填充数据表的Javascript代码:

              $(document).ready(function () {

                  var active = true;

                  GetList(active);
              });

              function ChangeList() 
              {
                  GetList($("#ddlRiderType").val());
              }

              function GetList(isActive) {

                  $('#dataTable').dataTable({
                      "sDom": '<"top"lf>rt<"bottom"ip><"clear">',
                      "bAutoWidth": false,
                      "bProcessing": true,
                      "bSort": true,
                      "bDestroy": true,
                      "sPaginationType": "full_numbers",
                      "bServerSide": true,
                      "sAjaxSource": "Instructor.aspx/GetAllInstructors",
                      "fnServerData": function (sSource, aoData, fnCallback) {

                          var jsonData = "{jsonAOData : '" + JSON.stringify(aoData) + "', isActive: '" + isActive + "'}";

                          $.ajax({
                              //dataType: 'json', 
                              contentType: "application/json; charset=utf-8",
                              type: "POST",
                              url: sSource,
                              data: jsonData,
                              complete: function () { Init(); },
                              success: function (msg) {
                                  if (msg.d != null)
                                      fnCallback(msg.d);
                              },
                              error: function (XMLHttpRequest, textStatus, errorThrown) {
                                  ntfy_push("Error", ServerFail());
                              }
                          });
                      },
                      "aoColumnDefs": [

                { "sName": "Cipher",
                    "aTargets": [0],
                    "bVisible": false,
                    "bSearchable": false
                },
                { "sName": "firstName",
                    "aTargets": [1]
                },
                { "sName": "lastName",
                    "aTargets": [2]
                },
                { "sName": "org",
                    "aTargets": [10]
                },
                { "sName": "workPhone",
                    "aTargets": [4]
                },
                { "sName": "altPhone",
                    "aTargets": [5],
                    "bVisible": false,
                    "bSearchable": false
                },
                { "sName": "cellPhone",
                    "aTargets": [6],
                    "bVisible": false,
                    "bSearchable": false
                },
                 { "sName": "email",
                     "aTargets": [7]
                 },
                { "sName": "riderCoach",
                    "aTargets": [8],
                    "bVisible": false,
                    "bSearchable": false
                },
                { "sName": "rangeAid",
                    "aTargets": [9],
                    "bVisible": false,
                    "bSearchable": false
                },
                 { "sName": "admin",
                     "aTargets": [3]
                 },
                { "sName": "seniorInst",
                    "aTargets": [11],
                    "bVisible": false,
                    "bSearchable": false
                },
                { "sName": "dateAdded",
                    "aTargets": [12],
                    "bVisible": false,
                    "bSearchable": false
                },
                 { "sName": "dateMod",
                     "aTargets": [13],
                     "bVisible": false,
                     "bSearchable": false
                 },
                { "sName": "modBy",
                    "aTargets": [14],
                    "bVisible": false,
                    "bSearchable": false
                },
                { "sName": "active",
                    "aTargets": [15],
                    "bVisible": false,
                    "bSearchable": false
                },
                { "sName": "delete",
                    "aTargets": [16],
                    "bVisible": false,
                    "bSearchable": false
                },
                { "fnRender": function () {
                    return "Edit";
                },
                    "aTargets": [17]
                },
                { "fnRender": function () {
                    return "Delete";
                },
                    "aTargets": [18]
                }
            ],
                      "sDom": '<"toolbar">flrtip'
                  });

                  $("div.toolbar").html("<div class = 'tableOption'><img src = 'image/button/new-icon.png'/ alt= 'Add New Class' style = 'cursor:pointer' onclick = 'Show_New_Instructor()'></div>");
              }

用于标题的数据表

<table id = "dataTable">
<thead>
    <tr>
        <th>Instructor ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Admin</th>
        <th>Work Phone</th>
        <th>Alt Phone</th>
        <th>Cell Phone</th>
        <th>Email Address</th>
        <th>Rider Coach</th>
        <th>Range Aid</th>
        <th>Organ</th>
        <th>Senior Inst</th>
        <th>Date Added</th>
        <th>Date Modified</th>
        <th>Mod By</th>
        <th>Active</th>
        <th></th>
    </tr>
</thead>

<tbody></tbody>
</table>

Excel 按钮:

PDF 按钮:

我提前感谢你的帮助,如果这已经在某个地方的帖子中,我为忽略它而道歉。

【问题讨论】:

    标签: javascript c# .net excel entity-framework-4


    【解决方案1】:

    由于您可以使用这三个平台中的任何一个,您可能需要检查 Office 互操作性,您可以在 C# 代码中最大限度地使用 MS Office

    http://msdn.microsoft.com/en-ca/library/dd264733.aspx

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 2021-10-24
      • 1970-01-01
      • 2010-12-12
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多