简体   繁体   中英

C#/Javascript datatable export to Excel and PDF

I took over a C# Entity Framework project from an ex coworker. He set up a few tables in ways I am not to familiar using Javascript. I need to get the contents of this data table exported to an Excel and PDF file (seperate button for each). I am open to using C#, Javascript, or JQuery.

I have been doing ColdFusion recently, and haven't even looked at anything .NET related in almost a year, so I am trying to get a refresher as well as figure out stuff I have never seen before, so my mind fried.

Javascript Code used to fill the data table:

              $(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>");
              }

Datatable used for the headers

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

PDF Button:

I thank you in advance for the help, and if this is already in a post somewhere I do apologize for overlooking it.

Since you are open in using any of the three platforms, you might want to check on Office Interoperability wherein you can maximize the use of MS Office in your C# code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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