This article describes the jquery+php implementation of the export DataTables plug-in data to Excel method. Share to everyone for your reference. Specifically as follows:
DataTables is a jquery form plugin. This is a highly flexible tool, based on a step-by-step enhancement, which will add advanced interactive controls that support any HTML table. Main Features:
1. Automatic Paging processing
2. Instant Tabular Data filtering
3. Data sorting and data type automatic detection
4. Automatically handle column widths
5. Can be customized through the CSS style
6. Support for hidden columns
7. Easy to use
8. Scalability and flexibility
9. Internationalization
10. Create a table dynamically
11. Free
Plugin address http://www.datatables.net/
Unfortunately, the official website Forms data export method uses the Tabletools plug-in, uses the flash to export the data, and does not support the Chinese data, by searches the official API and the material, finds uses the jquery and the PHP to export the data method.
JavaScript function to export 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 I original headers so there ist a amount of TH cells which are});
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 = Otab
Le.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 (' <div class= "csv-data" ><form enctype= "Multipart/form-dat A "method=" post "action="/csv.php "><textarea class=" form "name=" csv "> ' +csv+ ' </textarea><input
Type= "Submit" class= "Submit" value= "Download as File"/></form></div> ");
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 what are 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 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 will help you with your PHP programming.