POSTED BY Paul Kukiel on 12:02 PM under
A few moths ago I write a post on extracting data from a flex datagrid see: HERE

I had several people contact me about this regarding extending functionality or how to get it into a file. At the time I was asked to implement the function the customer wanted it into the clipboard hence the copy/paste method I produced but I didn't think about how easy it would be to extend this to a downloadable file.

John Gag mentioned a method he uses which didn't quite work when I had date formatters. We currently generate many excel spreadsheets using HTML formatting to color code excel but I wanted a simpler version with no formatting/colors. Excel will automatically populate cells based on TAB delimited strings so I used my original method with a few changes and simply posted this back to Coldfusion ( or in this case Railo ) or PHP. ( I tested this in OSX with Excel, Windows with Excel and Ubuntu with Open Office and they all worked ).



View source is enabled but because of this bug: http://bugs.adobe.com/jira/browse/FB-13194 you have to goto http://code.kukiel.net/blog/datagridexcel/ to view it.

And below is the CFML and PHP.
CFML:


PHP:


Now I'm sure this method could apply to asp, pl or any other server side language I just made it for the 2 I have available. The great thing about this method is that I can send any tab delimited data from a grid as a POST to my server without having to customize server side code in anyway.

The data being displayed in the grid comes directly from Coldfusion Bloggers. Thanks Ray ;)
11 comments so far:
    John Gag January 6, 2009 3:09 PM , said...

    Cool, I am going to have to check this out. Thanks!

     
    Mike March 30, 2009 2:31 PM , said...

    I have tried to change your test application to accommodate an arraycollection instead of an RSS feed and I keep getting in the excel sheet that the headers a shifted one cell to the right. Enabling the clipboard functionality and pasting directly into excel does not have this problem.

    How can I correct this?

     
    Paul Kukiel March 30, 2009 2:34 PM , said...

    Perhaps the first Item in the arrayCollection is empty? Hence its putting in an empty TAB

     
    Mike March 30, 2009 2:52 PM , said...

    theData = new ArrayCollection([
    {name:"John Smith",isManager:false,age:34,sex:"M"},
    {name:"Jenny Zurich",isManager:true,age:45,sex:"F"},
    {name:"Jason Ruins",isManager:false,age:22,sex:"M"},
    {name:"Tim Johnson",isManager:false,age:21,sex:"M"},
    {name:"Mary Winn",isManager:false,age:20,sex:"F"},
    {name:"Joe Rock",isManager:true,age:31,sex:"M"}
    ]);

     
    Paul Kukiel March 30, 2009 3:35 PM , said...

    Without seeing all the code I'm just not sure. I know I have a copy from arrayCollection mod myself and it is working fine. If you want to pastebin the code I can take a look.

     
    Paul Kukiel April 1, 2009 1:40 PM , said...

    The code looks fine. Perhaps its a server setting. In the Coldfusion / Railo admin there is "Suppress white space" try turn that on see if it helps.

     
    Mike April 1, 2009 1:51 PM , said...

    That is exactly the the solution. In the Web Admin of Railo under the Settings menu, there is a sub-item called Output and on that page is a checkbox as follows:

    "Removes all white spaces in the output that follow a white space"

    I checked that and all was fine.

    For Coldfusion 7.02...

    In the Administrator, Server Settings menu, Settings sub-menu check the checkbox:

    "Enable Whitespace Management
    Reduces the file size of the pages that ColdFusion returns to the browser by removing many of the extra spaces, tabs, and carriage returns that ColdFusion might otherwise persist from the CFML source file."

    Much thanks to Paul for helping me troubleshoot this problem and find the solution.

     
    Mike April 13, 2009 12:02 PM , said...

    Another issue that I had was printing descriptions that were text areas and included tabs, new lines and carriage returns.

    In the copyToDatagrid function where 'Remove all " as it messes with the tab delimited string' is, I added this to remove those characters from the the strings:

    data = data.replace(/\r/g, " ");
    data = data.replace(/\n/g, " ");
    data = data.replace(/\t/g, " ");

     
    Madhava June 8, 2009 11:03 AM , said...

    When I try to implement the excel.php wiht excel.jsp, I get the content in a single cell. The content is not getting delimeted. This is what my code in the jsp. Please throw some light into it:

    page contentType="application/vnd.ms-excel"

    response.setHeader("Content-Disposition", "attachment; filename=\"Workbook.xls\"");

    out.print(request.getParameter("htmltable"));

     
    Paul June 8, 2009 12:16 PM , said...

    Hi Madhava, I would say its something to do with the issue Mike had where additional whitespace was being introduced somewhere breakign the tab delimitation. I'm not sure how to over come this with jsp ( assume tomcat ) but you could wrap the data in html table code which excel will also will open and display fine. You can find this example at this link: http://www.saskovic.com/blog/?p=3

     
    Madhava June 8, 2009 12:52 PM , said...

    Thanks a lot and it helped me a lot.

     

Copyright A little bit of what interests me - A tech blog by Paul Kukiel. | Using the GreenTech Theme | Bloggerized by Falcon Hive.com