Question

I'm using <cfspreadsheet> to output a database query to an Excel spreadsheet. When these spreadsheets are created manually it is possible to highlight all the cells and 'Format as table'. This means when the table header is clicked, the user can sort the table ascending and descending.

Is it possible to specify this formatting in the ColdFusion code when generating the Excel file?

Was it helpful?

Solution

If you are on Coldfusion 9, you can use SpreadSheetNew, then SpreadSheetAddRow, SpreadSheetFormat functions to style an excel spreadsheet from a data set

 <cfset sObj = spreadsheetNew("myreport","yes")>
 <cfset SpreadsheetAddRow(sObj, "Column_1, ... , Column27")>

 <cfset SpreadsheetFormatRow(sObj, {bold=TRUE, alignment="center"}, 1)>

 <cfset spreadsheetAddRows(sObj, qMyQuery)>
    <cfheader name="content-disposition" value="attachment; filename=report_#Dateformat(NOW(),"MMDDYYYY")#.xlsx">

BE WARNED however, this can be extremely taxing to the JVM, I had a query i was creating an xls with, applying only two styles (bold, text-center) to the header row, and any query over 700 rows would shut down the entire server via JVM memory loss...here is my SO question about it, with related code/answer SpreadsheetAddRows failing on moderate size query

It has been documented with adobe as being a bug

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top