简体   繁体   English

将过滤的HTML表格数据的JavaScript数组导出到MS Excel或CSV

[英]Export JavaScript Array of Filtered HTML Table data to MS Excel or CSV

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. 在我公司的Intranet网站上,我们有一个很大的数据表,上面应用了一个javascript过滤器,以便经理和其他有关方面可以快速找到与其相关的行。 The filter I am using can be found at http://tablefilter.free.fr/ . 我正在使用的过滤器可以在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. 当我需要一个按钮将过滤的结果导出到Excel,以便经理可以脱机访问数据时,就会出现我的问题。 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. 有很多直接的选项可以将HTML表导出到excel,但是我一直无法弄清楚如何仅将过滤后的结果导出。 (Note: This intranet site will only be accessed via IE) (注意:此Intranet网站只能通过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[]. 作为javascript表格过滤器的一部分,有一个函数Ge​​tFilteredData(),它将抓取过滤后的数据单元并将它们输入到数组中,我认为这称为filteredData []。 This array is formated as such: [rowindex,[value1,value2,value3...]]. 该数组的格式如下:[rowindex,[value1,value2,value3 ...]]。

So how do I get this array into an Excel or csv file? 那么如何将该数组放入Excel或csv文件中? Again, this will be accessed only by IE so activeX objects and controls are acceptable. 同样,只能通过IE访问此控件,因此可以接受activeX对象和控件。

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. 另外,我可能应该注意,我不能使用服务器端技术,因此请限制您对HTML,javascript和activeX的响应。 Thanks! 谢谢!

FYI: DataTables has a nice plugin called TableTools which can export table to csv on client-side. 仅供参考:DataTables有一个名为TableTools的插件,可以将表导出到客户端的csv。 It's achieved using Flash. 它是使用Flash实现的。 If you are satisfied with the filter of DataTables, I think this would be a good solution. 如果您对DataTables的筛选器感到满意,我认为这将是一个很好的解决方案。

http://www.datatables.net/extras/tabletools/ http://www.datatables.net/extras/tabletools/

If you are using IE starting at version 8, you can use data: URLs. 如果从版本8开始使用IE ,则可以使用data: URL。 Just generate the URL and point the borwser there using location.href . 只需生成URL并使用location.href将浏览器指向那里即可。 The data in CSV can be generated by javascript and base64 encoded. CSV中的数据可以通过javascript和base64编码生成。

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. 一旦有了此数组,就可以将其转换为JSON字符串。 Try this popular lightweight library (json2.js): 试试这个流行的轻量级库(json2.js):

https://github.com/douglascrockford/JSON-js https://github.com/douglascrockford/JSON-js

Example: 例:

text = JSON.stringify(['e', {pluribus: 'unum'}]);
// text is '["e",{"pluribus":"unum"}]'

You should have something like 你应该有这样的东西

var dataString = '["rowindex",["value1","value2","value3"]]'

At this point you could use some regex replacement on the string to format it to either style. 此时,您可以在字符串上使用一些正则表达式替换以将其格式化为任何一种样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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