Server side excel export with DataTable

2019-08-08 11:27发布

问题:

I have a DataTables with server side processing. On server side I have functions retrieving filters from the ajax request and fetching data from database.

Now I want export data into excel file with filters.

I assume I have to design an excel file creator on server side and send the file through HTTP response.

I would like use already-defined functions to handle export request.

My question is: how can I send DataTables filter as same format as in the ajax request to my server ?

回答1:

SOLUTION

You can use ajax.params() to get the data submitted by DataTables to the server in the last Ajax request.

Then you can redirect to script that generates Excel file with these parameters using jQuery helper function $.param that converts array into query string.

For example:

window.location = '/getFile.php?' + $.param($('#example').DataTable().ajax.params());

where example is table ID.

NOTES

There is also TableTools extension with Download button but with recent jQuery DataTables 1.10.8 release, TableTools extension is now deprecated.