Creating Multiple Tabs in Excel with Coldfusion

Posted By : todd sharp Posted At : July 19, 2006 2:48 PM Posted In: ColdFusion

20

I know I've seen this before, but I thought it couldn't hurt to throw it up for those who haven't seen it. Beginning with Office 2002 (XP) you can create Excel spreadsheets using the XML Spreadsheet schema. The documentation is pretty good. As with any XML it must be well formed. To get a taste of the schema check out the documentation, or save any spreadsheet as .xml and check out the saved file. Here's a quick example of how to create a multiple tabbed workbook in Excel.

<cfsetting showdebugoutput="false">
<cfheader name="content-disposition" value="inline;filename=ExcelWithMultipleWorksheets.xls">
<cfcontent type="application/msexcel">
<?xml version="1.0"?>
<Workbook
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">


    <Worksheet ss:Name="Worksheet 1">
        <Table>
            <Row>
                <Cell ss:Index="1"><Data ss:Type="String">Test Data 1</Data></Cell>
            </Row>
        </Table>
    </Worksheet>
    
    <Worksheet ss:Name="Worksheet 2">
        <Table>
            <Row>
                <Cell ss:Index="1"><Data ss:Type="String">Test Data 2</Data></Cell>
            </Row>
        </Table>
    </Worksheet>
        
</Workbook>

Here is what you get...

Comments (20)

Rich Rein's Gravatar I had tried this before, and found that with large spreadsheets, the xml file was often still far too large (and took far too long to create). For smaller sheets this works fine, for anything much larger than 100 rows or so, we wrote a custom tag to use the poi project with excellent results.

todd's Gravatar Rich:

I used this method yesterday to create a workbook with around 12 tabs populated from a storedproc that returns 5 resultsets. All told, the final product is around 9mb. I'm not sure if that's going to work...

The weird thing is that when it's saved as .xls (instead of the prompted .xml) it drops down to around 1.5mb. Still haven't figured that out.

What's up with the poi project and your custom tag? Can you share?

Rich Rein's Gravatar Drop me an email, we can discuss this and I don't mind you sharing once you get it to work...

Terrence's Gravatar With my limited knowledge of CF.. I can't seem to loop over a query and get this to work.

Suggestion?

Basically:

<Table>
    <cfoutput query="test">
<Row>
<Cell ss:Index="1"><Data ss:Type="String">#empID#</Data></Cell>
          <Cell ss:Index="2"><Data ss:Type="String">#ReportType_ID#</Data></Cell>
</Row>
       </cfoutput>
</Table>

todd's Gravatar Seems like that should work Terrence, what error are you getting?

Derek Brigner's Gravatar Is it possible to loop over an include with this? I would like to loop over an include of a table that i need to display multiple times but don't want to recreate that table in XML.

Thank you,

Derek

todd's Gravatar Derek: You should absolutely be able to loop over a table or row in this method. Let me check my code that I have in production tomorrow at work and I'll post an example here in the comments.

In the meantime, post your code here if it's not working for you and I'll see if I can see a problem.

se's Gravatar were you guys able to loop through it ?

Do you have that code (Example ?) can you post it ?

se's Gravatar this is how it works...

--------------------------------------------

<cfoutput query="topAllDataQry">
<cfset topAllData = topAllData
   & ' <Row>
    <Cell ss:Index="2" ss:StyleID="s92"><Data ss:Type="Number">#num#</Data></Cell>
<Cell ss:StyleID="s94"><Data ss:Type="String">#txtype#</Data></Cell>
<Cell ss:StyleID="s93"><Data ss:Type="String">&##160;#productCode#</Data></Cell>
<Cell ss:StyleID="s94"><Data ss:Type="String">#name#</Data></Cell>
<Cell ss:StyleID="s94"><Data ss:Type="String">#region#</Data></Cell>
<Cell ss:StyleID="s92"><Data ss:Type="Number">#cnt#</Data></Cell>
</Row>
'>
</cfoutput>

js's Gravatar I am having problems getting Excel to read the xml file..... I have it formatted as such:

<?xml version='1.0'?>
<workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'>
<worksheet ss:Name='Sheet1 - Month DD, YYYY'>
<table>
<Row>
<Cell><Data ss:Type='String'>Stuff</Data></Cell>
...repeat 5 times...
</Row></table></worksheet>
...Repeat 7-8 times...
</workbook>

And I get an "unable to open" error in Excel..... What am I doing wrong?

James's Gravatar I get a similiar error if I add additional rows to a table....Thoughts?

Marc Ehrenkrantz's Gravatar Your example works great. How can I modify your example to auto-resize the cells and change fonts, colors, etc.? Thanks

Joe's Gravatar i am also having problems getting the loop code to work. i was wondering if you ever figured out the solution.

Joe's Gravatar I got the loop to work but now have the same question as Marc. Is there any way to format the cells and text that is being displayed. I would like to make some text bold and a larger font and choose the size of the cells. Let me know. Thanks!!

Joe's Gravatar I have figured it all out. Thanks for the great starting point!

Steve Durette's Gravatar Actually if you want to see how to do specific things with the excel. Create an excel spreadsheet with what you want done to it and save as Excel 2003 xml.

It will give you all the xml settings you need to make the changes. I use this all the time now for my spreadsheet exports at work.

todd sharp's Gravatar If you upgraded to CF9 you could use cfspreadsheet, but rumor has it you just got around to CF8 ;)

Steve Durette's Gravatar Yeah, just upgraded one server to cf8 this weekend. Can't wait for the maintenance agreement to kick in! Other server was already CF8!

phil's Gravatar I am using POI Utilities coldfusion to generate excel files, the intial size of the generated file is huge
compared to when i save same file. Any suggestion

phil's Gravatar sorry I meant i am using XML Spreadsheet schema example you have on the top of this page to generate excel files, the intial size of the generated file is huge compared to when i save same file. Any suggestion