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, for more information about how to export data from the datatables plug-in to excel using jquery + php, see the following example. 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 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 http://www.datatables.net/
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 code if(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 data if (exportmode == "full") { // total data var 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:visible').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 it if($('.csv-data').length) $('.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, 'visible', '#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.