不能使用jQuery导出html表到excel [英] Can not export html table to excel using jQuery

查看:146
本文介绍了不能使用jQuery导出html表到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约5000行(tr)内部的HTML表,每列有10列(td)。现在我试图将这个整个HTML表导出到excel中,使用以下jQuery代码:

I have around 5000 rows(tr) inside HTML table each having 10 columns(td). Now I am trying to export this whole HTML table to excel using following jQuery code:

var test = $('#data');
window.open('data:application/vnd.ms-excel,' + encodeURIComponent(test.html()));

还尝试过许多其他jQuery插件,如DataTables.net,jqWidgets和jqGrid,但每次浏览器得到崩溃,必须重新加载页面。

Have also tried with many other jQuery plugins like DataTables.net, jqWidgets and jqGrid but each time my browser gets crashed and have to reload the page again.

推荐答案

window.open()具有其范围并且在这篇文章中已经很好解释的限制:导出为CSV使用jQuery和html

window.open() has its scope and limitations that has been well explained in this post: Export to CSV using jQuery and html

对于您的关注,我测试了2500行,它工作正常。 (我不能在jsfiddler上上传这么多数据,但我相信它也可以工作5000行)。

For your concern, I tested with 2500 rows and it works fine. (I can't upload so much data on jsfiddler but I am sure it will work for 5000 rows also.)

另外,我怀疑你是否正在包装你的html表进入容器DIV。只需将你的html表放入DIV并使用这个 -

Also, I suspect if you are wrapping your html table into a container DIV. Just put your html table into a DIV and use like this -

$("[id$=myButtonControlID]").click(function(e) {
    window.open('data:application/vnd.ms-excel,' + encodeURIComponent( $('div[id$=divTableDataHolder]').html()));
    e.preventDefault();   });

http://jsfiddle.net/AnilAwadh/wJyWm/

encodeURIComponent()是一个Javascript函数,用于编码特殊字符,如果您的数据有任何,并且其使用是可选的。

encodeURIComponent() is a Javascript function that is used to encode special characters if you data has any and its use is optional.

这篇关于不能使用jQuery导出html表到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆