Jquery+php to implement the method of exporting DataTables plug-in data to Excel _php skills

Source: Internet
Author: User
Tags php programming

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.

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.