将过滤后的 HTML 表数据的 JavaScript 数组导出到 MS Excel 或 CSV
我再次需要做一些听起来很简单但实际上令人沮丧地逃避我的事情。
在我公司的 Intranet 站点上,我们有一个大型数据表,其中应用了 JavaScript 过滤器,以便经理和其他感兴趣的各方可以快速找到与他们相关的行。我正在使用的过滤器可以在 http://tablefilter.free.fr/ 找到。
当我需要一个按钮将筛选结果导出到 Excel 以便经理可以离线访问数据时,我的问题就出现了。有许多直接的选项可用于将 HTML 表导出到 Excel,但我一直无法弄清楚如何仅导出过滤后的结果。 (注意:此 Intranet 站点只能通过 IE 访问)
有一个函数作为 javascript 表过滤器的一部分,GetFilteredData(),它将抓取过滤后的数据单元格并将其输入到数组中,我认为称为filteredData[]。该数组的格式如下:[rowindex,[value1,value2,value3...]]。
那么如何将此数组放入 Excel 或 csv 文件中呢?同样,这只能由 IE 访问,因此 activeX 对象和控件是可以接受的。
另外,我可能应该注意到,我无法使用服务器端技术,因此请将您的响应限制在 HTML、javascript 和 activeX 的范围内。谢谢!
I once again need to do something that sounds simple but is infact frustratingly evading me.
On my company's intranet site we have a large table of data that has a javascript filter applied to it so that managers and other interested parties can quickly locate the rows that are relevant to them. The filter I am using can be found at http://tablefilter.free.fr/ .
My issue arises when I need to have a button to export the filtered results to Excel so that the managers can access the data offline. There are many straight forward options for exporting the HTML table to excel but I have been unable to figure out how to get JUST the filtered results to export. (Note: This intranet site will only be accessed via IE)
There is a function as part of the javascript table filter, GetFilteredData(), that will grab the filtered data cells and input these into an array, i think called filteredData[]. This array is formated as such: [rowindex,[value1,value2,value3...]].
So how do I get this array into an Excel or csv file? Again, this will be accessed only by IE so activeX objects and controls are acceptable.
Also I should probably note that I cannot use server-side technologies so please limit your responses to the confines of HTML, javascript and activeX. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅供参考:DataTables 有一个很好的插件,称为 TableTools,它可以在客户端将表导出为 csv。它是使用Flash 实现的。如果您对DataTables的过滤器感到满意,我认为这将是一个很好的解决方案。
http://www.datatables.net/extras/tabletools/
FYI: DataTables has a nice plugin called TableTools which can export table to csv on client-side. It's achieved using Flash. If you are satisfied with the filter of DataTables, I think this would be a good solution.
http://www.datatables.net/extras/tabletools/
如果您从版本 8 开始使用
IE
,则可以使用data:
网址。只需生成 URL 并使用location.href
将浏览器指向那里即可。 CSV中的数据可以通过javascript和base64编码生成。If you are using
IE
starting at version 8, you can usedata:
URLs. Just generate the URL and point the borwser there usinglocation.href
. The data in CSV can be generated by javascript and base64 encoded.您可能需要考虑一种依赖于字符串操作的方法。
获得此数组后,您可以将其转换为 JSON 字符串。尝试这个流行的轻量级库(json2.js):
https://github.com/douglascrockford/JSON-js< /a>
示例:
您应该有类似的内容
此时,您可以在字符串上使用一些正则表达式替换来将其格式化为任一样式。
You might want to consider an approach that relies on string manipulation.
Once you have this array, you can turn it into a JSON string. Try this popular lightweight library (json2.js):
https://github.com/douglascrockford/JSON-js
Example:
You should have something like
At this point you could use some regex replacement on the string to format it to either style.