Using jquery + php to export datatables plug-in data to excel, jquerydatatables

Source: Internet
Author: User

Using jquery + php to export datatables plug-in data to excel, jquerydatatables

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 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 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-data" 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 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.