DataTables is a jQuery table plug-in. This is a highly flexible tool based on progressive enhancements that will add advanced interactive controls and support for any HTML form. Main features:
However, it is a pity that the official website table data export method uses the tabletools plug-in, which uses flash to export data, and does not support Chinese data. By searching the official API and information, I found the use of jquery and php export data method.
Javascript function to export data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | function table2csv(oTable, exportmode, tableElm) {
var csv = & #39;';
var headers = [];
var rows = [];
$(tableElm+& #39; thead').find('th').each(function() {
var $th = $( this );
var text = $th.text();
var header = & #39;"' + text + '"';
if (text != "" ) headers.push(header);
});
csv += headers.join(& #39;,') + "\n";
if (exportmode == "full" ) {
var total = oTable.fnSettings().fnRecordsTotal()
for (i = 0; i < total; i++) {
var row = oTable.fnGetData(i);
row = strip_tags(row);
rows.push(row);
}
} else {
$(tableElm+& #39; tbody tr:visible').each(function(index) {
var row = oTable.fnGetData( this );
row = strip_tags(row);
rows.push(row);
})
}
csv += rows.join( "\n" );
if ($(& #39;.csv-data').length) $('.csv-data').remove();
$(& #39;body').append('<p class="csv-data"><form enctype="multipart/form-data" method="post" action="/csv.php"><textarea class="form" name="csv">'+csv+'</textarea><input type="submit" class="submit" value="Download as file" /></form></p>');
}
function strip_tags(html) {
var tmp = document.createElement( "p" );
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}
|
Copy after login
The function supports exporting all data and the current page data
1 2 3 4 5 6 7 8 9 10 11 12 | $(& #39;#export_visible').click(function(event) {
var oTable;
oTable= $(& #39;#spdata').dataTable();
event.preventDefault();
table2csv(oTable, & #39;visible', '#spdata'); })
$(& #39;#export_all').click(function(event) {
var oTable;
oTable= $(& #39;#spdata').dataTable();
event.preventDefault();
table2csv(oTable, & #39;full', '#spdata'); })
|
Copy after login
where #spdata is the id of the table
Backend php export excel code
1 2 3 4 5 6 7 8 | 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 ;
|
Copy after login
The above is the detailed content of Detailed explanation of jquery export data to excel code example. For more information, please follow other related articles on the PHP Chinese website!