开发者

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

开发者 https://www.devze.com 2023-03-23 12:58 出处:网络
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

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!


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/


If you are using IE starting at version 8, you can use data: URLs. Just generate the URL and point the borwser there using location.href. The data in CSV can be generated by javascript and base64 encoded.


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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消