Export to Excel not working when deployed

2019-03-02 04:55发布

问题:

This is the code I used to create a data table with export functionality.

oTable = $("#tblSearch").DataTable({
    "jQueryUI": true,
    "sPaginationType": "full_numbers",
    "iDisplayLength": 10,
    "bSort": true,
    "aaSorting": [[0, "desc"]],
    "lengthMenu": [[5, 10, 25, 50], [5, 10, 25, 50]],
    "autoWidth": true,
    "scrollCollapse": true,
    "dom": 'T<"clear">lfrtip',
    "tableTools": {
        "sSwfPath": "../../swf/copy_csv_xls.swf",
        "aButtons": ["xls"]
    }
});

Export is working in my local but when I deployed in server, the button does not appear.

回答1:

Change the sSwfPath to an absolute path.

"tableTools": {
    "sSwfPath": "http://cdn.datatables.net/tabletools/2.2.3/swf/copy_csv_xls_pdf.swf",
    "aButtons": ["xls"]
}

here using the dataTables CDN. The problem is, that the relative path messes up when you deploy to your IIS or whatever windows platform you are using. This is a very common problem.