Skip to main content
Inspiring
Jul 25, 2013 12:18 PM UTC
Question

Useful code for exporting to Excel xlsx

I wanted to "give back" by posting this little code sample for exporting a query into Excel xlsx

Kudos to Charlie Arehart for helping me increase the server memory and java heap size.

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.

Need help? I recommend you contact Charlie Arehart on server issues: charlie@carehart.org

Code:

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#">    

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

James Moberg
Inspiring
Jul 26, 2013 2:33 PM UTC

Here's a link to a ColdFusion 9+ Custom Tag that I wrote to convert a query object to an Excel file.
https://gist.github.com/JamoCA/6089292

I added some features to deal with dates, values that look like numbers, column borders/colors/widths and freeze rows.  It also uses a download technique that doesn't make the file permanently available to the public.

Translate
tclaremont
Inspiring
Jul 25, 2013 4:16 PM UTC

I gave up on that approach and now use jquery Data Tables almost exclusively.

http://www.datatables.net/

Now my users are not limited as to the format they want to see the data in, and can sort and filter however they want.

I include references to the jquery code, surround the table in the appropriate tags, and the rest takes care of itself.

Translate
James Moberg
Inspiring
Jul 26, 2013 2:20 PM UTC

An inline DataTable is not the same thing as exporting data in a standardized XLSX format.  (I'll post the script I use in another comment.

Regarding inline tables, here are some other worth scripts:


Handsontable - minimalistic Excel-like data grid editor

http://handsontable.com/


TableSorter 2 (forked) - sorting, filtering, grouping, pagination, inline editing, sticky header

http://mottie.github.io/tablesorter/docs/

jQuery Grid Plugin

http://paramquery.com/

jQuery table to CSV

http://www.kunalbabre.com/projects/table2CSV.php

jQuery Pivot - presenting table data in pivot form
http://metalogic.dk/jquery.pivot/demo.htm

Translate