如何通过 jQuery/JavaScript 在 HTML 页面中将 html 表导出为 excel 或 pdf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18180536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to export html table to excel or pdf in a HTML page through jQuery/JavaScript?
提问by Shubham
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.
我有一个 html 页面,其中包含一个动态填充的表格。我需要在其下方放置 2 个按钮,以便将表结构数据导出到 Excel 和 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 !
我更喜欢一些 Javascript/jQuery 解决方案。我看过DocRaptor,但我认为它是一项付费服务。我需要类似的东西,它适用于 IE8 浏览器。请帮忙 !
回答by Zaheer Ahmed
TableToolsis a plug-in for the DataTables HTML table enhancer, which adds a highly customisable button toolbar to a DataTable. Key features include:
TableTools是 DataTables HTML 表格增强器的插件,它为 DataTable 添加了高度可定制的按钮工具栏。主要功能包括:
- 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
- 复制到剪贴板
- 将表格数据保存为 CSV、XLS 或 PDF 文件
- 清洁打印的打印视图
- 行选择选项
- 易于使用的预定义按钮
- 按钮的简单定制
- 用于高级控制的定义明确的 API
回答by Zaheer Ahmed
$("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));
});