Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 853a03e6a3a16b6664aad3616b8422bf2f115a85 @nmische committed Apr 22, 2011
Showing with 121 additions and 0 deletions.
  1. +32 −0 README.md
  2. +46 −0 customtags/table.cfm
  3. +5 −0 customtags/td.cfm
  4. +6 −0 customtags/th.cfm
  5. +5 −0 customtags/tr.cfm
  6. +27 −0 index.cfm
32 README.md
@@ -0,0 +1,32 @@
+# CF Spreadsheet Taglib
+
+This is a very basic set of custom tags that allow you to create Excel files from ColdFusion 9 using syntax similar to HTML tables.
+
+##Example
+
+ <cfimport taglib="customtags" prefix="ss" />
+ <ss:table variable="mySpreadSheet">
+ <ss:tr>
+ <ss:th width="20">Col One</ss:th>
+ <ss:th>Col Two</ss:th>
+ <ss:th>Col Three</ss:th>
+ </ss:tr>
+ <ss:tr>
+ <ss:td width="30">This column has a lot of data which will wrap. Blah, blah, blah.</ss:td>
+ <ss:td>Blah</ss:td>
+ <ss:td>Blah</ss:td>
+ </ss:tr>
+ <ss:tr>
+ <ss:td>Blah</ss:td>
+ <ss:td>Blah</ss:td>
+ <ss:td width="20">
+ Blah, Blah
+ Blah, Blah
+ </ss:td>
+ </ss:tr>
+ </ss:table>
+
+ <cfheader name="content-disposition" value="attachment; filename=test.xls" />
+ <cfspreadsheet action="write" filename="#ExpandPath('temp.xls')#" name="mySpreadSheet" overwrite="true" />
+ <cfcontent file="#ExpandPath('temp.xls')#" reset="true" type="application/msexcel" deletefile="true" />
+
46 customtags/table.cfm
@@ -0,0 +1,46 @@
+<cfif thisTag.executionMode eq "end">
+ <cfparam name="attributes.sheetName" default="Sheet1" />
+ <cfparam name="attributes.variable" default="spreadsheet" />
+ <cfparam name="attributes.format" default="xls" />
+ <cfparam name="thisTag.rows" default="#ArrayNew(1)#" />
+ <cfscript>
+ ssObj = SpreadsheetNew(attributes.sheetName, (attributes.format eq "xlsx") );
+
+ // track column widths
+ colWidths = [];
+
+ for (i = 1; i lte ArrayLen(thisTag.rows); i++) {
+ row = thisTag.rows[i];
+ values = "";
+ headers = [];
+ for (j = 1; j lte ArrayLen(row.cells); j++) {
+ cell = row.cells[j];
+ SpreadsheetSetCellValue(ssObj,cell.value,i,j);
+
+ format = {textwrap = true, verticalalignment="vertical_center"};
+
+ // is it a header
+ if(StructKeyExists(cell,"isHeader") and cell.isHeader) {
+ format.bold = true;
+ }
+ SpreadsheetFormatCell(ssObj,format,i,j);
+
+ // get the width for the column
+ currWidth = (ArrayIsDefined(colWidths,j)) ? colWidths[j] : 0 ;
+ newWidth = StructKeyExists(cell,"width") ? cell.width : 0 ;
+ colWidths[j] = Max(newWidth,currWidth);
+ }
+ }
+
+ // set column width
+ for (k = 1; k lte ArrayLen(colWidths); k++) {
+ width = colWidths[k];
+ if (width neq 0) {
+ SpreadSheetSetColumnWidth(ssObj,k,width);
+ }
+ }
+
+ caller[attributes.variable] = ssObj;
+ </cfscript>
+
+</cfif>
5 customtags/td.cfm
@@ -0,0 +1,5 @@
+<cfif thisTag.executionMode eq "end">
+ <cfparam name="attributes.value" default="#Trim(thisTag.generatedContent)#" />
+ <cfset thisTag.generatedContent = "" />
+ <cfassociate basetag="CF_TR" datacollection="cells" />
+</cfif>
6 customtags/th.cfm
@@ -0,0 +1,6 @@
+<cfif thisTag.executionMode eq "end">
+ <cfparam name="attributes.value" default="#Trim(thisTag.generatedContent)#" />
+ <cfset attributes.isHeader = true />
+ <cfset thisTag.generatedContent = "" />
+ <cfassociate basetag="CF_TR" datacollection="cells" />
+</cfif>
5 customtags/tr.cfm
@@ -0,0 +1,5 @@
+<cfif thisTag.executionMode eq "end">
+ <cfparam name="thisTag.cells" default="#ArrayNew(1)#">
+ <cfset attributes.cells = thisTag.cells />
+ <cfassociate basetag="CF_TABLE" datacollection="rows" />
+</cfif>
27 index.cfm
@@ -0,0 +1,27 @@
+<cfimport taglib="customtags" prefix="ss" />
+<cfoutput>
+<ss:table variable="mySpreadSheet">
+ <ss:tr>
+ <ss:th width="20">Col One</ss:th>
+ <ss:th>Col Two</ss:th>
+ <ss:th>Col Three</ss:th>
+ </ss:tr>
+ <ss:tr>
+ <ss:td width="30">Value One. This column has a lot of data which I hope will wrap. We shall see.</ss:td>
+ <ss:td>Value Two</ss:td>
+ <ss:td>Value Three</ss:td>
+ </ss:tr>
+ <ss:tr>
+ <ss:td>
+ - Value Four Value Four Value Four
+ - Value Four
+ </ss:td>
+ <ss:td>Value Five</ss:td>
+ <ss:td width="20">Value Six</ss:td>
+ </ss:tr>
+</ss:table>
+</cfoutput>
+<cfheader name="content-disposition" value="attachment; filename=test.xls" />
+<cfspreadsheet action="write" filename="#ExpandPath('temp.xls')#" name="mySpreadSheet" overwrite="true" />
+<cfcontent file="#ExpandPath('temp.xls')#" reset="true" type="application/msexcel" deletefile="true" />
+

0 comments on commit 853a03e

Please sign in to comment.