Monday, March 22, 2010    
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 (3) [RSS]
 • Adobe (90) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (219) [RSS]
 • Appearances (191) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1382) [RSS]
 • Data Services (34) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (198) [RSS]
 • Flex (499) [RSS]
 • Home Automation (5) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (120) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (21) [RSS]
 • SQL (40) [RSS]
 • Stuff (536) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (162) [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).

TrackBacks
?????? ? ????????? Excel ? ColdFusion 9
Вчора Бен Форта розкрив ще одну новинку з CF Centaur.
Цей пост є вільним перекладом оригінальної замітки Working With Spreadsheets In ColdFusion.
Tracked by Coldfusion User Group Ukraine | Tracked on 6/19/09 10:50 AM

No trackback URL. Trackbacks are only allowed via interactive form.

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

  © Copyright 1997-2009 Ben Forta, All Rights Reserved