Skip to main content
This topic has been closed for replies.

1 reply

Participant
February 10, 2012

I was looking for a way to alternate the row color from a query. This is what I got to work:

<!-We need an absolute path, so get the current directory path.->

<!-You need to create a format structure.->

Hope this is useful for somebody else. It took a lot of searching to come across this much.

Participant
February 10, 2012

Well I see the code didn't paste.  I was trying to be helpful...

We are in the process of upgrading to ColdFusion 9 and I was testing out cfspreadsheet tag.

I was trying to figure out how to alternate row colors.  This is what I found to work:

<cfquery datasource="myDatasource" name="getQuery">

Select colA,colB,colE as emailAddress,colN as fullName

From this_table

where 0=0

</cfquery>

<!---We need an absolute path, so get the current directory path.--->

<cfset filelocation=getdirectoryfrompath(getbasetemplatepath())>

<cfset filename="myfilename.xlsx">

<!---You need to create a format structure.  Set foreground color to gray.--->

<cfset fmat=StructNew()>

<cfset fmat.fgcolor="gray">

<cfscript>

theFile=(filelocation & filename);

//Create a new Excel spreadsheet object and add the query data.

theSheet = SpreadsheetNew("Data","true");

SpreadsheetAddRows(theSheet,getQuery);

//Loop through the query and alternate the foreground color of that row.
for (x = 1; x <= getQuery.RecordCount; x++) {

      if(x % 2) {

      SpreadSheetFormatRow(theSheet,fmat,x);

      }

}

</cfscript>

<cfspreadsheet action="write" filename="#theFile#" name="theSheet" overwrite="1">

You could set other properties of rows or cells in the fmat structure

<cfscript>

// Define a format for the column.

fmat  = StructNew();

fmat.font="Arial";

fmat.fontsize="10";

fmat.color="dark_blue;";

fmat.italic="true";

fmat.bold="true";

fmat.alignment="left";

fmat.textwrap="true";

fmat.fgcolor="pale_blue";

fmat.bottomborder="thick";

fmat.bottombordercolor="blue_grey";

fmat.topbordercolor="blue_grey";

fmat.topborder="thick";

fmat.leftborder="dotted";

fmat.leftbordercolor="blue_grey";

fmat.rightborder="dotted";

fmat.rightbordercolor="blue_grey";
</cfscript>

Here's a link to POI Palette colors : http://poi.apache.org/apidocs/org/apache/poi/hssf/util/HSSFColor.html