简体   繁体   中英

How to export html table to excel or pdf in a HTML page through jQuery/JavaScript?

I have a html page with a table which is populated dynamically. I need to put 2 buttons below it which enables exporting the table structure data to Excel and PDF.

What is the best/easiest approach to implement this?

I will prefer some Javascript/jQuery solution. I have seen DocRaptor , but I think its a paid service. I need something similar which works on IE8 browser. Please Help !

TableTools is a plug-in for the DataTables HTML table enhancer, which adds a highly customisable button toolbar to a DataTable. Key features include:

  • Copy to clipboard
  • Save table data as CSV, XLS or PDF files
  • Print view for clean printing
  • Row selection options
  • Easy use predefined buttons
  • Simple customisation of buttons
  • Well defined API for advanced control
 $("button").click(function () {
  var str="";
  $('tr').each(function() {

        $(this).find('td').each(function() {

         str=str+$(this).html()+"\t";
  });
      str=str+"\n";

  });

  alert(str);

        window.open('data:application/vnd.ms-excel,' + encodeURIComponent(str));
    });

http://jsfiddle.net/4Vr89/1/

FPDF是一个允许生成PDF文件的PHP类

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM