Matt, that's exactly what I was thinking! It will work for even user modifiable tables!
If the table is not modifiable you could just write the data for the table to a file, and have the table built via ajax'ing the xls file and doing a simple parse (much easier for a tabbed file or the new excel xml format). This cuts back the bandwidth and most good browsers should serve the download right from the cache. On 2/19/07, Matt Kruse <[EMAIL PROTECTED]> wrote: > > I have a table populated by a php/mysql script. I've created an excel export > > option which changes the header to ms-excel and refreshes the current page > > by making another call to the mysql db to fetch the data. > > So I thought, the table data is already embedded in the current html page; > > why make another call to the mysql db to fetch the same data again? is not > > possible to use javascript to scan the current html page for <table> and > > </table> and create an excel file on the fly therefore saving bandwith ? > > Yes, it's possible - I do it all the time. The benefit of this approach is > that > if you allow client-side sorting, filtering, column hiding, etc, the user can > download exactly what they see because the manipulated client-side table is > exactly what they want. > > The problem is it can't be done _purely_ client-side, because there is no way > for js to generate a document of a specific mime type. Because of this, you > need to write a simple server-side component in your language of choice that > accepts a form value containing content to echo back. It slaps the excel mime > type on it and returns it as-is, and the user's browser (hopefully) opens > excel > to handle the response. > > My solution is for an IE-only webapp, so the code isn't very cross-browser, > but > I suppose you could generalize the approach. I do these steps: > > 1) Get the outerHTML of the table > 2) Create a hidden iframe and write out the table as part of it > 3) Get a reference to the new table and manipulate it using standard dom to > clean it into something that excel will like. For example, > a) Convert text inputs into plain text > b) Convert checkboxes into an "X" if checked, else blank > c) Convert select lists into their selected option's visible text > d) Convert links to plain text > e) Replace images with their [alt] text > f) Make sure borders are on > g) Put nowrap on every cell > h) etc... > 4) Get the outerHTML of the resulting table and set it into a textarea in the > iframe > 5) Submit the iframe form to the server, which echoes back the submitted > content with the excel mime type > > It all happens very quickly and the resulting Excel output matches exactly to > what the user sees on screen. Depending on your data, you may need to scrub > your tables more to get dates in the right formats that excel will parse > correctly or to remove other markup that will ugly up the excel. > > Once written, though, it becomes pretty reusable on almost any table with very > good results. > > With the power of jQuery, I imagine that 'cleaning' the table would be > extremely trivial and the resulting js file would be pretty small. I would add > it to my general table library (which I'm going to release as a jQuery plugin > soon) but since it requires a server-side component I think it needs to be > separate. > > Hope that helps! > > Matt > > > _______________________________________________ > jQuery mailing list > [email protected] > http://jquery.com/discuss/ > -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ _______________________________________________ jQuery mailing list [email protected] http://jquery.com/discuss/
