This article mainly introduces how to export data from the datatables plug-in to excel using jquery + php. The example shows how to use the jquery Ables plug-in jquery and how to export data from the datatables to Excel using php.
This article mainly introduces how to export data from the datatables plug-in to excel using jquery + php. The example shows how to use the jquery Ables plug-in jquery and how to export data from the datatables to Excel using php.
This example describes how to export data from the datatables plug-in to excel using jquery + php. Share it with you for your reference. The details are as follows:
DataTables is a jQuery table plug-in. This is a highly flexible tool that is based on a step-by-step enhancement that will add advanced interactive control and support for any HTML table. Main features:
1. Automatic Paging
2. Real-time table data filtering
3. Data Sorting and automatic data type detection
4. automatically process column width
5. You can customize styles through CSS.
6. columns can be hidden.
7. ease of use
8. scalability and flexibility
9. Internationalization
10. dynamically create a table
11. Free
Plug-in address
Unfortunately, the table data export method on the official website uses the tabletools plug-in to export data using flash, and does not support Chinese data. By searching official APIs and materials, find the data export method using jquery and php.
Javascript function for data export
Function table2csv (oTable, exportmode, tableElm) {var csv = ''; var headers = []; var rows = []; // Get header names $ (tableElm + 'thead '). find ('th '). each (function () {var $ th = $ (this); var text = $ th. text (); var header = '"' + text + '"'; // headers. push (header); // original codeif (text! = "") Headers. push (header); // actually datatables seems to copy my original headers so there ist an amount of TH cells which are empty}); csv + = headers. join (',') + "\ n"; // get table dataif (exportmode = "full") {// total datavar total = oTable. fnSettings (). fnRecordsTotal () for (I = 0; I <total; I ++) {var row = oTable. fnGetData (I); row = strip_tags (row); rows. push (row) ;}} else {// visible rows only $ (tableElm + 'tbody tr: visable '). each (function (index) {var row = oTable. fnGetData (this); row = strip_tags (row); rows. push (row) ;}} csv + = rows. join ("\ n"); // if a csv p is already open, delete itif(('.csv-data '). length) values ('.csv-data '). remove (); // open a p with a download link $ ('body '). append ('
');} Function strip_tags (html) {var tmp = document. createElement ("p"); tmp. innerHTML = html; return tmp. textContent | tmp. innerText ;}
Function supports exporting all data and current page data
// Export only what is visible right now (filters & paginationapplied) $ ('# export_visible '). click (function (event) {var oTable; oTable = $ ('# spdata '). dataTable (); event. preventDefault (); table2csv (oTable, 'visable', '# spdata');}) // export all table data $ (' # export_all '). click (function (event) {var oTable; oTable = $ ('# spdata '). dataTable (); event. preventDefault (); table2csv (oTable, 'full', '# spdata ');})
Where # spdata is the table id
Export excel Code using php in the background
Header ("Content-Type: application/vnd. ms-execl "); header (" Content-Disposition: attachment; filename=myExcel.csv "); header (" Pragma: no-cache "); header (" Expires: 0 "); $ buffer = $ _ POST ['csv']; $ buffer = str_replace (", \ t", $ buffer); $ buffer = mb_convert_encoding ($ buffer, "GB2312", "UTF-8"); echo $ buffer;
I hope this article will help you with php programming.