Adobe
Prodotti
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Altri prodotti
Soluzioni
Marketing digitale
Media digitale
Istruzione
Servizi finanziari
Pubblica Amministrazione
Gestione dell'esperienza web
Altre soluzioni
Formazione Assistenza Download Società
Acquista
Uso domestico a scopo personale e per l’ufficio di casa
Settore dell'istruzione per studenti, docenti e personale tecnico-amministrativo
Business per piccole e medie imprese
Programmi multilicenza per aziende, istituti scolastici ed enti della Pubblica Amministrazione
Altre modalità di acquisto
Offerte speciali
Cerca
 
Info Accedi
Benvenuto, Il mio carrello Le mie ordinazioni Il mio supporto
Il mio account
Esci
A cosa serve l'accesso?L'accesso consente di gestire il proprio account e di usufruire di download ed estensioni di prodotti, aree della community e altro ancora.
Adobe
Prodotti Sezioni Acquista   Cerca  
Soluzioni Società
Assistenza Formazione
Accedi Esci Le mie ordinazioni Il mio supporto
Date Date
Qty:
Subtotal
Checkout
Adobe Developer Connection / Centro per sviluppatori ColdFusion /

Working with spreadsheets in ColdFusion

Per Ben Forta

Ben Forta
  • forta.com

Content

  • Example 1: Reading a spreadsheet and dumping the contents
  • Example 2: Working with formulas and formatting in spreadsheets
  • Where to go from here

Created

4 April 2011

Strumenti pagina

Condividi su Facebook
Condividi su Twitter
Condividi su LinkedIn
Segnalibro
Stampa

Tags

Requisiti

Conoscenza dei prerequisiti

Basic knowledge of CFML, databases, spreadsheets, and ColdFusion Builder.

Livello utente

Intermedio

Prodotti richiesti

  • ColdFusion (Download trial)

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 the CFReport tag 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.

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. This tag has 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.

Example 1: Reading a spreadsheet and dumping the contents

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).

Example 2: Working with formulas and formatting in spreadsheets

We'll start with a simple Application.cfc to define the data source needed for this app:

<cfcomponent> <cfset this.datasource="cfartgallery"> </cfcomponent>

Now to create a spreadsheet using a database query, use the following code:

<!--- Get data ---> <cfquery name="ArtOrders"> SELECT orderid, customerfirstname, customerlastname, total FROM orders ORDER BY orderid </cfquery> <!--- Save it ---> <cfspreadsheet action="write" query="ArtOrders" filename="Orders.xls" overwrite="true">

That's all it takes. The cfspreadsheet tag creates a spreadsheet populated with the retrieved data, using the query column names as column headers in the first spreadsheet row, and the data starting in the second row. Nice, huh?

But what if you want more control? What if you want to explicitly control data placement? What if you needed to provide row or column or cell level formatting? And what if you needed a total cell containing a formula to SUM the total column?

Here's a complete example showing you how to use the tag to control formulas and format in your spreadsheet:

<!--- Get data ---> <cfquery name="ArtOrders"> SELECT orderid, customerfirstname, customerlastname, total FROM orders ORDER BY orderid </cfquery> <!--- Create new spreadsheet ---> <cfset sObj=SpreadsheetNew()> <!--- Create header row ---> <cfset SpreadsheetAddRow(sObj, "Order,First Name,Last Name,Amount")> <cfset SpreadsheetFormatRow(sObj, {bold=TRUE, alignment="center"}, 1)> <!--- Add orders from query ---> <cfset SpreadsheetAddRows(sObj, ArtOrders)> <!--- Figure out row for formula, 2 after data ---> <cfset rowDataStart=2> <cfset rowDataEnd=ArtOrders.recordCount+1> <cfset rowTotal=rowDataEnd+2> <cfset totalFormula="SUM(D#rowDataStart#:D#rowDataEnd#)"> <!--- Add total formula ---> <cfset SpreadsheetSetCellValue(sObj, "TOTAL:", rowTotal, 3)> <cfset spreadsheetSetCellFormula(sObj, totalFormula, rowTotal, 4)> <!--- Format amount column as currency ---> <cfset SpreadsheetFormatColumn(sObj, {dataformat="$00000.00"}, 4)> <!--- Save it ---> <cfspreadsheet action="write" name="sObj" filename="Orders.xls" overwrite="true">

Once again, we start with a database query. Then, you use the SpreadsheetNew() function to create a new spreadsheet object.

The code then creates the header row. Use the SpreadsheetAddRow() function to add a specific row, the column headers (as no row number was specified, SpreadsheetAddRow() adds the row to the next free row, the first). Use the SpreadsheetFormatObject() function to format row 1, this function accepts a structure (which here is implicitly created inline).

Next comes the data. SpreadsheetAddRows() adds the entire query to the spreadsheet (again, as no row was specified the data gets added to the next free row).

Now for the formula that will total all orders. So as to not hard code the row, use a few <cfset> statements to calculate the first and last data row and the row for the total (second row after the end of the data, so leaving one empty row in between data and total). The formula can't be hard coded either, so instead of SUM(D2:D24) , the row variables are used to build the formula string dynamically. Use the SpreadsheetSetCellValue()function to add a title, and the SpreadsheetSetCellFormula() inserts the formula.

Next, the entire fourth column, containing the order amounts and the calculated total, is formatted to display as currency using the SpreadsheetFormatColumn() function.

All that is left to do then is to save the file using the cfspreadsheet tag.

The cfspreadsheet tag and its 30+ supporting functions can do lots more, but this should give you a taste of just what's possible using this innovative new feature.

Where to go from here

Next, check out my Adobe TV video (9:27 minute). Ben shows how ColdFusion 9 adds powerful and sophisticated spreadsheet support. Follow along as Ben Forta explains how to work with new and existing spreadsheets.

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Moving from Dreamweaver to ColdFusion Builder 2
  • What's new in ColdFusion Builder 2
  • Getting started with ColdFusion Builder 2
  • Using ColdFusion 8 and Flex 3 to export visual components
  • Accessing ColdFusion Services from Flex applications
  • A beginner's guide to using stored procedures with ColdFusion
  • Using ColdFusion 8 Ajax security features
  • Customizing the Eclipse IDE for robust ColdFusion application development
  • Extending ColdFusion Builder
  • Developing Adobe AIR offline applications using the ColdFusion 9.0.1 ActionScript ORM Library

Prodotti

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Applicazioni mobili
  • Photoshop
  • Touch Apps

Soluzioni

  • Marketing digitale
  • Media digitale
  • Gestione dell'esperienza web

Settori

  • Istruzione
  • Servizi finanziari
  • Pubblica Amministrazione

Assistenza

  • Centri di assistenza dei prodotti
  • Ordini e resi
  • Download e installazione
  • Il mio Adobe

Formazione

  • Adobe Developer Connection
  • Adobe TV
  • Formazione e certificazione
  • Forum
  • Design Center

Modalità di acquisto

  • A scopo personale e per l’ufficio di casa
  • per studenti, docenti e personale tecnico-amministrativo
  • Per piccole e medie imprese
  • Per aziende, istituti scolastici ed enti della Pubblica Amministrazione
  • Offerte speciali

Download

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Società

  • Sala stampa
  • Iniziative per partner commerciali
  • Responsabilità sociale d'impresa
  • Opportunità di lavoro
  • Relazioni con gli investitori
  • Eventi
  • Settore legale
  • Sicurezza
  • Contatti Adobe
Scegli il paese Italia (Cambia)
Scegli il paese Chiudi

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • ?eská republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • España
  • France
  • Deutschland
  • Hrvatska
  • Ireland
  • Israel - English
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Magyarország
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Österreich - Deutsch
  • Polska
  • Portugal
  • România
  • Россия
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Slovenija
  • Slovensko
  • Srbija
  • Suomi
  • Sverige
  • Türkiye
  • Укра?на
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Termini di utilizzo | Informativa sulla privacy e cookie (Aggiornato)