Home » excel » excel – how to create xlsx files using coldfusion

excel – how to create xlsx files using coldfusion

Posted by: admin March 9, 2020 Leave a comment


Disclaimer: I am new into coldfusion.
I am trying to create an Excel 2010 document with images and multiple tabs.
I have been able to get this to output into XLS, but I cannot get the image into the file.

I have not been able to find a complete example of how to Properly create an XLSx file.
I would prefer to learn the right way and develop my own bad habits later, rather than just have bad habits.

Here is an example:

<!--- Make CF export to Excel --->
<!--- This will create a XLS file --->
<!--- <cfheader name="Content-Disposition" value="attachment; filename=#URL.TRNo#_image.xls">
<cfcontent type="application/vnd.msexcel"> --->

<!--- This does not work to create an XLSX file --->
<cfheader name="Content-Disposition" value="inline; filename=#URL.TRNo#_image.xlsx">
<cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"> 

<cfparam name="URL.TRNo" default="AD0310">

    <?xml version="1.0"?>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
            <ss:Style ss:ID="Default" ss:Name="Normal">
                <ss:Font ss:Size="11" ss:FontName="Calibri"/>
            <ss:Style ss:ID="Left">
                <ss:Alignment ss:Horizontal="Left"/>
            <ss:Style ss:ID="Center">
                <ss:Alignment ss:Horizontal="Center"/>
            <ss:Style ss:ID="Right">
                <ss:Alignment ss:Horizontal="Right"/>

        <Worksheet ss:Name="#URL.TRNo# page 1">
            <ss:Table ss:DefaultColumnWidth="15" ss:DefaultRowHeight="15">
                <Row ss:Height="51"><!--- Start Row 1 --->
                    <Cell ss:Index="1" ss:MergeAcross="9">
                        <Data ss:Type="String">Final Test Report</Data>
                    <Cell ss:Index="11" ss:MergeAcross="10" ss:StyleID="Center">
                        <Data ss:Type="String"><!--- <img src="http://wwwdev.elmsweb.ford.com/elmsGEN3/SafetyLab/FMVSS/ReportWriter/img/fordLogo_transparent_small.png" height="68" width="181" alt="13"> ---></Data>
                    <Cell ss:Index="22" ss:MergeAcross="9" ss:StyleID="Right">
                        <Data ss:Type="String">Confidential</Data>
                </Row><!--- End Row 1 --->
                <Row/><!--- Row 2 Blank --->
                <Row><!--- Start Row 3 --->
                    <Cell ss:Index="1" ss:MergeAcross="1" ss:StyleID="Right">
                        <Data ss:Type="String">To:</Data>
                    <Cell ss:Index="3" ss:MergeAcross="12">
                        <Data ss:Type="String"></Data>
                    <Cell ss:Index="16" ss:MergeAcross="10" ss:StyleID="Right">
                        <Data ss:Type="String">Test Order:</Data>
                    <Cell ss:Index="27" ss:MergeAcross="4">
                        <Data ss:Type="String">#URL.TRNo#</Data>
                </Row><!--- End Row 3 --->
How to&Answers:

I would suggest using the built-in spreadsheet functions of ColdFusion (first introduced in version 9). Here is the documentation for the SpreadsheetNew function. If you set the xmlformat parameter to ‘true’ it will create an .xlsx file.

There are several examples on the web (and here on SO) of how to use these ColdFusion functions. Raymond Camden has a nice example here of how to generate a spreadsheet and deliver it directly to the user using the cfcontent tag. Here is another example from Raymond that builds on the first example.


I’ve been working on this all week and wanted to share the results. This code works for me using CF 9 on Windows Server 2008 R2. By the way, CFSpreadsheet seems to consume a lot of memory on larger exports. For this reason, we increased the server’s physical memory then used CF Administrator to increase the jvm heap size in the Administrator -> Java and JVM settings. My max java heap size is now 3072 Gig.
Need help? I recommend you contact Charlie Arehart on server issues: [email protected]


Example query is called “Myquery” Spreadsheet is called “Myspreadsheet” I export the spreadsheet to a subdirectory called “xlsx” I use cflocation to direct the browser to the spreadsheet. The browser prompts the user to open, save or cancel.

<cfset var_filenameis = "Myspreadsheet.xlsx">
<cfset SpreadsheetObj = spreadsheetNew("true")>
<cfset SpreadsheetObj = spreadsheetNew("#var_filenameis#","yes")>

<cfspreadsheet action="write" filename="./xlsx/#var_filenameis#" query="Myquery" overwrite="true"> 

<cflocation url = "./xlsx/#var_filenameis#">  

Dave Kraft