Jquery+php implements the method of exporting DataTables plug-in data to Excel, Jquerydatatables
The example in this article describes how jquery+php implements the export of DataTables plug-in data to Excel. Share to everyone for your reference. Specific as follows:
DataTables is a jquery form plugin. This is a highly flexible tool based on a gradual enhancement, which will add advanced interactive controls that support any HTML table. Main Features:
1. Automatic page pagination processing
2. Instant Tabular Data filtering
3. Data sorting and automatic data type detection
4. Automatically handle column widths
5. Customizable styles via CSS
6. Support for hidden columns
7. Ease of Use
8. Scalability and flexibility
9. Internationalization
10. Create a table dynamically
11. Free
Plugin address http://www.datatables.net/
But unfortunately, the official website Forms data export method uses the Tabletools plugin, uses Flash to export the data, and does not support the Chinese data, finds the official API and the material, the use jquery and the PHP Export data method.
JavaScript functions for exporting data
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 is empty}); CSV + = Headers.join (', ') + "\ n"; Get table Data if (ExportMode = = "full") {//all 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 div is already open, delete it if ($ ('. Csv-data '). Length) $ ('. Csv-data '). Remove (); Open a div with a download link $ (' body '). Append ('); } function Strip_tags (HTML) {var tmp = document.createelement ("div"); tmp.innerhtml = html; return tmp.textcontent| | Tmp.innertext; }
function supports exporting all data and current page data
Export only the What's 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 ();
Where #spdata is the ID of the table
Background PHP export Excel code
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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1027494.html www.bkjia.com true http://www.bkjia.com/PHPjc/1027494.html techarticle jquery+php implements the method of exporting DataTables plug-in data to Excel, Jquerydatatables This article describes the jquery+php implementation method of exporting DataTables plug-in data to Excel. To share with you ...