Originally adapted from the https://github.com/teamcfadvance/cfspreadsheet-railo extension, this is a standalone library for reading, creating and formatting spreadsheets in Lucee Server which does not require installation as an extension.
Unlike Adobe ColdFusion, Lucee doesn't support spreadsheet functionality out of the box. Extensions exist for both Lucee 4.5 and Lucee 5, but I decided to create a standalone library which doesn't depend on customisation of the engine.
- No installation required, either at the server or individual web context level.
- Invoking the library doesn't create a workbook instance (a.k.a. Spreadsheet Object), meaning:
- a blank workbook isn't created unnecessarily when reading an existing spreadsheet
- the library can be stored as a singleton in application scope
read()method offers all the features of the<cfspreadsheet action="read">tag in script in addition to the basic options ofSpreadsheetRead().- Offers a number of additional functions and options (see below)
- Fixes various outstanding bugs/omissions.
- No dependency on Lucee within the included jar files.
- Written entirely in CFML script.
- Existing code needs adapting to invoke the library. Existing CFML spreadsheet functions and the
<cfspreadsheet>tag won't work with it.
Note that this is not a Lucee extension, so does not need to be installed. To use it, simply copy the files/folders to a location where Spreadsheet.cfc can be called by your application code.
The following example assumes the file containing the script is in the same directory as the folder containing the spreadsheet library files, i.e.:
/root/
/spreadsheetLibrary/
Spreadsheet.cfc
etc.
script.cfm
<cfscript>
spreadsheet = New spreadsheetLibrary.Spreadsheet();
data = QueryNew( "First,Last", "VarChar, VarChar", [ [ "Susi", "Sorglos" ], [ "Frumpo", "McNugget" ] ] );
workbook = spreadsheet.new();
spreadsheet.addRows( workbook, data );
</cfscript>
You will probably want to place the spreadsheet library files in a central location with an application mapping, and instantiate the component using its dot path (e.g. New myLibrary.spreadsheet.Spreadsheet();).
How to create mappings (StackOverflow).
- addColumn
- addFreezePane
- addImage
- addInfo
- addRow
- addRows
- addSplitPane
- autosize, implemented as autoSizeColumn
- createSheet
- deleteColumn
- deleteColumns
- deleteRow
- deleteRows
- formatCell
- formatCellRange
- formatColumn
- formatColumns
- formatRow
- formatRows
- getCellComment
- getCellFormula
- getCellValue
- getColumnCount
- info
- isSpreadsheetFile
- isSpreadsheetObject
- mergeCells
- new
- read
- readBinary
- removeSheet
- setActiveSheet
- setActiveSheetNumber
- setCellComment
- setCellFormula
- setCellValue
- setColumnWidth
- setFooter
- setHeader
- setRowHeight
- shiftColumns
- shiftRows
- write
- clearCell
- clearCellRange
- hideColumn
- isBinaryFormat
- isXmlFormat
- renameSheet
- removeSheetNumber
- setRepeatingColumns
- setRepeatingRows
- setCellRangeValue
- setSheetPrintOrientation
- setReadOnly
- showColumn
- binaryFromQuery
- csvToQuery
- download
- downloadCsvFromFile
- downloadFileFromQuery
- newXls
- newXlsx
- workbookFromCsv
- workbookFromQuery
- writeFileFromQuery
In Adobe ColdFusion, the SpreadsheetRead() script function is limited to just returning a spreadsheet object, whereas the <cfspreadsheet action="read"> tag has a range of options for reading and returning data from a spreadsheet file.
The read() method in this library allows you to read a spreadsheet file into a query and return that instead of a spreadsheet object. It includes all of the options available in <cfspreadsheet action="read">.
<cfscript>
myQuery = spreadsheet.read( src=mypath, format="query" );
</cfscript>
The read() method also features the following additional options not available in ColdFusion or the Spreadsheet Extension:
fillMergedCellsWithVisibleValueincludeHiddenColumnsincludeRichTextFormattingpasswordto open encrypted XML (only) spreadsheets
The following international date masks are used by default to read and write cell values formatted as dates:
- DATE =
yyyy-mm-dd - DATETIME =
yyyy-mm-dd HH:nn:ss - TIME =
hh:mm:ss - TIMESTAMP =
yyyy-mm-dd hh:mm:ss
Each of these can be overridden by passing in a struct including the value(s) to be overridden when instantiating the Spreadsheet component. For example:
<cfscript>
spreadsheet = New spreadsheetLibrary.spreadsheet( dateFormats={ DATE: "mm/dd/yyyy" } );
</cfscript>
Although primarily intended for Lucee, the library can be run under ColdFusion 11 or higher. This may be useful where you want to your codebase to be cross-compatible between the two engines.
Please note though that reading or writing password-protected files only works with Lucee.
If you are using Lucee 4.5 or Adobe ColdFusion, Mark Mandel's JavaLoader is required and the bundled version will be used by default.
JavaLoader is not required if using Lucee 5 or later.
For more details and options see: Loading the POI java libraries
You can also download this library through CommandBox.
box install cfsimplicity/lucee-spreadsheet
It will download the files into a modules directory and can be used just the same as downloading the files manually.
If using ColdBox you can use either of the WireBox bindings like so:
spreadsheet = wirebox.getInstance("Spreadsheet@lucee-spreadsheet");
spreadsheet = wirebox.getInstance("LuceeSpreadsheet");
The automated tests require TestBox 2.1 or later. You will need to create an application mapping for /testbox
The code is very largely based on the work of TeamCfAdvance, to whom credit and thanks are due. Ben Nadel's POI Utility was also used as a basis for parts of the read functionality.
JavaLoader is by Mark Mandel.
Copyright (c) 2015-17 Julian Halliwell
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.