Wednesday, May 23, 2012    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Jun 2009 >>
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        

Search

Categories
 • Acrobat (5) [RSS]
 • Adobe (117) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (34) [RSS]
 • AdobeMAX11 (28) [RSS]
 • AdobeMAX13 (1) [RSS]
 • AIR (299) [RSS]
 • Appearances (217) [RSS]
 • Books (86) [RSS]
 • CFEclipse (15) [RSS]
 • Cloud (1) [RSS]
 • ColdFusion (1483) [RSS]
 • ColdFusion Builder (23) [RSS]
 • Data Services (43) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (368) [RSS]
 • Flex (565) [RSS]
 • Home Automation (5) [RSS]
 • HTML5 (36) [RSS]
 • JavaScript (3) [RSS]
 • Jobs (133) [RSS]
 • jQuery (15) [RSS]
 • JRun (14) [RSS]
 • Labs (63) [RSS]
 • LiveCycle (37) [RSS]
 • MAX (285) [RSS]
 • Mobile (257) [RSS]
 • PhoneGap (17) [RSS]
 • Regular Expressions (19) [RSS]
 • RIA (21) [RSS]
 • SQL (45) [RSS]
 • Stuff (554) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (167) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
June 18, 2009

Working With Spreadsheets In ColdFusion

Spreadsheets are key to just about all businesses and organizations, and ColdFusion developers have long sought a way to access and manipulate spreadsheet data programmatically. The truth is, ColdFusion has supported spreadsheet access for a while in a variety of ways, it's been possible to access Excel files from an ODBC driver, and it's been possible to generate these files using CFReport as well as by generating HTML or CSV content and then setting the appropriate MIME type to force the browser to display the data using Excel.

But ColdFusion developers have been asking for more, greater and more control, and the ability to read and write specific parts of spreadsheet files. And as I demoed in my usergroup presentations last week, this is indeed planned for ColdFusion "Centaur".

Just like we did with the ColdFusion image manipulation functionality, spreadsheets in ColdFusion are manipulated using a tag or functions, or some combination thereof. The <CFSPREADSHEET> tag is used to read a sheet from a spreadsheet file (as a spreadsheet object, a query, a CSV string, or HTML), write sheets to XLS files, and add sheets to XLS files. The over 30 supporting functions like SpreadsheetSetCellValue(), SpreadsheetAddRow(), and SpreadsheetSetCellFormula() allow for more granular spreadsheet manipulation, and can be used in conjunction with supporting functions like SpreadsheetNew() to create a new spreadsheet object, and SpreadsheetInfo() which returns title, subject, sheet names, last saved date and time, and more.

Here are some of the examples I used at my presentations. This first example reads an entire spreadsheet as a query and dumps the contents:

<!--- Read spreadsheet --->
<cfspreadsheet action="read"
                src="Sales.xls"
                query="myQuery">

<cfdump var="#myQuery#">

This next example reads a specific cell on a specific sheet and returns it in a variable:

<!--- Read a spreadsheet cell --->
<cfspreadsheet action="read"
                src="Sales.xls"
                name="myVar"
                sheet="1"
                rows="C"
                columns="3">

<cfdump var="#myVar#">

<CFSPREADSHEET> is also used to write (or overwrite) a spreadsheet, as seen here:

<!--- Write spreadsheet --->
<cfspreadsheet action="write"
    overwrite="true"
    filename="Sales.xls"
    name="sObj" />

To update a specific cell, you need to read, update, and save, like this:

<!--- Read spreadsheet --->
<cfspreadsheet action="read"
        src="Sales.xls"
        name="sObj" />

<!--- Set cell value --->
<cfset spreadsheetSetCellValue(sObj, FORM.sales, FORM.row, FORM.col)>
<!--- Write spreadsheet --->
<cfspreadsheet action="write" overwrite="true"
        filename="Sales.xls"
        name="sObj" />

This example uses form fields to specify the row, column, and value.

There's a lot more to it, but you get the idea. <CFSPREADSHEET> supports Excel and Open Office spreadsheet files.

Oh, and this one came up several times, so worth noting. Just like any other file access (for example, using <CFFILE> or <CFIMAGE>), you'd not want concurrent access, and so you'd want to use <CFLOCK> calls when accessing and manipulating spreadsheet files, read locks when reading, exclusive locks when updating, using named locks (perhaps with the spreadsheet file name as the name).

Comments
That's is very sweet :)
# Posted By Emily | 6/18/09 12:24 PM
*sigh*, and I just finished an auto report e-mailer, using HTML saved as .xls, and had to explain Excel's 2007 security warning to the customer. This would have been great.

a.) What about support for ODF?
b.) Support for Office 2K7 (xlsx) format?
# Posted By Paul | 6/18/09 12:59 PM
Ben, what support for SharePoint 3 will be built it? Will CF be able to read events directly out a SharePoint 3 database? Thanks.
# Posted By Joe Meboe | 6/18/09 2:37 PM
Will you be able to add and read formula results as well?
# Posted By Ian | 6/18/09 3:16 PM
Very sweet! I've definitely got some projects that could use this. Any chance something similar is planned for Powerpoint? I know about CFPRESENTATION, but I have users that specifically want to download .ppt files.
# Posted By 6dust | 6/18/09 4:24 PM
Paul, ODF is not supported yet, but we may add that. XSLX is indeed supported.

Joe, I'll post some specifics on Sharepoint integration in the next week or so, stay tuned.

Ian, yep, SpreadSheetGetCellValue() will retrieve the value after evaluating the formula (if applicable). SpreadSheetGetCellFormula() will get the actual formula.

6dust, enhancements to other Office document types are works in progress.

--- Ben
# Posted By Ben Forta | 6/18/09 4:41 PM
Can you read/write Excel xls and xlsx files on a Linux server? Or does this only work on Windows?
# Posted By Grant | 6/18/09 7:24 PM
Supported on all platforms supported by ColdFusion.

---- Ben
# Posted By Ben Forta | 6/18/09 7:28 PM
This is fantastic! I can finally turn off that pesky, expensive SQL server and store all my data in spreadsheets!

I kid, I kid...
# Posted By Hamish | 6/18/09 9:27 PM
Um, dumb question:

Is file extension the only way CF knows what kind of file it's working with?

Shouldn't there be a format or MIME type attribute or something?
# Posted By Paul | 6/18/09 10:11 PM
Paul, no, CF does not rely on the extension, so long as it is a valid spreadsheet file it'll work.

--- Ben
# Posted By Ben Forta | 6/18/09 10:48 PM
would be a killer feature for our shop ... if it is working really well.

(in other words: please do not make it a second cfreport or cfdocument or ... :) )
# Posted By Thilo Hermann | 6/19/09 6:28 AM
This is huge. Here's an important item - how about spreadsheet styles? And the ability to import a template and write to it?
# Posted By Jeff | 6/19/09 9:13 AM
Do the spreadsheet need to be on the cfmc server or can it be on the network (intranet)?
# Posted By Patrick Whittingham | 6/19/09 1:08 PM
Thilo, I absolutely hope it'll work well. When public beta starts make sure to test it thoroughly.

Jeff, no, I don't believe that stylesheets or templates are support, but we're not done yet.

Patrick, so long as CF can access the network drive then the spreadsheet functionality will work, too.

--- Ben
# Posted By Ben Forta | 6/19/09 8:11 PM
I am trying to get CF to access a mapped network drive. Aren't there security implications to changing the account that the Application service uses? Are there better ways around this? Thanks so much!
# Posted By Christine Cruz | 8/10/09 4:15 PM
I have a spreadsheet that has formulas that look at other sheets. Is there a way to get the values from the cells rather than the formulas when reading the sheer? thanks.
# Posted By chris l. | 1/20/10 3:43 PM
Does spreadsheets functions use apache POI ?
Does anyone know if a formula containing the "FORECAST" excel function can be evaluate ?
# Posted By beg | 2/11/10 4:36 AM
Ben,

I need to read a spreadsheet which has both images (charts) and data and write it to a pdf or powerpoint. I'm not having any success retrieving the images from the excel spreadsheet. If I can write it to a pdf, then I could always use "thumbnail" to write it to a powerpoint. Do you have any ideas.
# Posted By Patrick Whittingham | 5/11/10 7:56 AM
This is so great. I've had a request to be able to interact with excel for an existing spreadsheet that is to be used in the automotive area.
The trick will be to lock the cells once a cell update from a data query has been done.
Sad for me that we're only on 8 and haven't upgraded to 9 yet.
# Posted By Lisa Walker | 6/7/10 3:50 PM
The big problem I am finding when working with CFSPREADSHEET is that if you have a large dataset, it times out the server if your time out value isn't high enough.

Large being anything over 500 records.

Because of my DB schema, I am reading and creating a query on the fly, creating a temp query in memory, and then dumping that info to the spreadsheet, it basically hangs the server.

This line - <cfset SpreadsheetAddRows(schedRpt, myQuery)>

Pushes the server to 100% utilization.

Anybody come up with a way to improve performance for this yet?

Thanks.
# Posted By Doug | 1/17/11 8:13 PM
is there a way/formating-function to remove the gridlines when we create a cfspreadsheet using coldfusion.
# Posted By suchet | 3/17/11 12:44 PM
This is a great tag, except for one small issue. I have an excel spread sheet where the column headers have spaces in them. I see that in the dump the headers are correctly named, but I cant figure out how to reference them in my code. Here is an example:

<cfspreadsheet
action = "read"
src="TDAT_85G.xls"
query="excelquery" headerrow="1" columns="1-9" rows="1-9">
<cfdump var="#excelquery#" />

<cfoutput query="excelquery">
#excelquery.'Pay Period'#
</cfoutput>


Any Ideas?
# Posted By Anthony O | 5/24/11 2:26 PM
Have you tried #excelquery['Pay Period']# type notation?

--- Ben
# Posted By Ben Forta | 5/24/11 2:29 PM
I had tried that, but it just occured to me that It is a multi dimensional array:
#excelquery['PAY Period'][2]#

That fixed it. Thanks for your help
# Posted By Anthony O | 5/24/11 3:33 PM
Ben, Any ideas why I am getting a Structure returned from my <CFSpreadSheet> tag?

The following line should return the value of one cell, but it is showing me a structure contain information about the spreadsheet, with no cell value in sight.

<cfspreadsheet action="read" sheet="1" src="#path_attachments##files.name#" name="a4" columnnames="A" rows="4">
# Posted By Andrew | 7/13/11 1:41 PM
I think you want COLUMNS, not COLUMNNAMES.

--- Ben
# Posted By Ben Forta | 7/13/11 4:12 PM
is it possible to return a query/run SQL against a sheet when using CFSPREADSHEET within a CFC?

ie
<cfspreadsheet action="read" src = "D:/farmers.xls" query="qObj" />

but in cfscript syntax?
# Posted By ben | 8/1/11 3:27 PM
I had to use the cfspreadsheet tag in CF9 for the first time today. I LOVE it! Very powerful, borderline orgasmic! 12 years of programming in CF and it never stops being fun!
# Posted By Kurt Bunge | 11/15/11 2:47 PM
Which of your books contain the instructions of how to use the CFSpreadsheet?
# Posted By Kofi | 1/9/12 1:21 PM
3rd one, in the Office integration chapter.

--- Ben
# Posted By Ben Forta | 1/9/12 1:40 PM
Thanks Ben. I got the book. One question though, how would you get the spreadsheet created to be saved on a client's local drive? Thanks.
# Posted By Kofi | 1/13/12 1:51 PM
You can't create files on the client side, no. But you can push it which will prompt the user to download it, look at the <cfcontent> tag.

--- Ben
# Posted By Ben Forta | 1/13/12 2:00 PM
I get this error while using the cfspreadsheet. I do not get the error in the development environment but I get it in the staging environment.

The error -- "coldfusion.excel.ExcelProcessException: An exception occurred while using action=write."
# Posted By Kofi | 3/9/12 2:52 PM
I get the following error when using more than 65,000 records in CFSPREADSHEET:

java.lang.IllegalArgumentException: Invalid row number (65536)

How do we add more than 65,536 rows?
# Posted By J Christopher | 5/2/12 2:41 PM

  © Copyright 1997-2009 Ben Forta, All Rights Reserved