Skip to main content
Inspiring
March 23, 2010
Question

Ben Nadel's POI Utility ColdFusion Component

  • March 23, 2010
  • 1 reply
  • 3650 views

I am hoping someone out there has used Ben's Component.  It seems like it will do exactly what I need it to in CF8.  Thanks in advance

I am trying to use Ben's POI utility to create a multi-tab spreadsheet.  I have it set up fine and I was able to create a multi-tab spreadsheet as long as the queries were created using the <cf_makequery> custom tag. When I use my own query via cfinvoke I get the following message:

-----------------------------------------------------------------------------------------------------------

Element  TYPENAME is undefined in a CFML structure referenced as part of an  expression.

The error occurred in C:\ColdFusion8\wwwroot\ExcelTest\POIUtility.cfc: line 1540
Called from C:\ColdFusion8\wwwroot\ExcelTest\POIUtility.cfc: line 1387
Called from C:\ColdFusion8\wwwroot\ExcelTest\write_new.cfm: line 77

1538 :                
1539 :                 // Map the column name to the data type.
1540 :                 LOCAL.DataMap[ LOCAL.MetaData[ LOCAL.MetaIndex ].Name ] = LOCAL.MetaData[ LOCAL.MetaIndex ].TypeName;
1541 :             }
1542 :

------------------------------------------------------------------------------------------------------------

If I change the line <cfset objSheet[1].Query = qAll /> to refer to a query I have made that is when I get the error. Below Is the code from Ben's write.cfm page. Nothing is altered except the value of objSheet[1].Query.

Ben's code

----------------------------------------------------------------------------------------------------------

<cfset subQuery = #queryNew("fydate,sHours")#>      
    <cfloop from="1" to="3" index="idx">
        <cfset temp = #queryAddRow(subQuery)#>
        <cfset temp = #querySetCell(subQuery,"fydate",200903)#>
        <cfset temp = #querySetCell(subQuery,"sHours",5)#>                        
    </cfloop>
<cfdump var="#subquery#">

<!---Ben's code begins here--->
<cfoutput>

    <!--- Create an instance of the POIUtility.cfc. --->
    <cfset objPOI = CreateObject(
        "component",
        "POIUtility"
        ).Init()
        />
       
       
    <!--- Simulate a query object. --->       
    <cf_makequery name="qGirl">
        name|hair|best_feature
        Julie|Blonde|Forearms
        Lydia|Brunette|Eyes
        Cynthia|Blonde|Eyes
    </cf_makequery>   
    <!---
        Create a sheet object for this query. This will
        return a structure with the following keys:
        - Query
        - ColumnList
        - ColumnNames
        - SheetName
    --->
    <cfset objSheet = ArrayNew(1)>
    <cfset objSheet[1] = objPOI.GetNewSheetStruct() />
    <cfset objSheet[2] = objPOI.GetNewSheetStruct() />
   
    <!--- Set the query into the sheet. --->
    <cfset objSheet[1].Query = subQuery />
    <cfset objSheet[2].Query = qGirl />
    <!---
        Define the order of the columns (and which
        columns to include).
    --->
   <cfset objSheet[1].ColumnList = "fydate,sHours" />
   <cfset objSheet[2].ColumnList = "name,hair,best_feature" />
    <!---
        We want to include a header Row in our outputted excel
        workbook. Therefore, provide header values in the SAME
        order as the column list above.
    --->
    <!--- <cfset objSheet[1].ColumnNames = "fydate,sHours" />
    <cfset objSheet[2].ColumnNames = "Name,Hair,Best Feature" /> --->   
    <!--- Set the sheet name. --->
    <cfset objSheet[1].SheetName = "Data" />
    <cfset objSheet[2].SheetName = "More Girls" />
       
       
    <!---
        Now, let's write the sheet to a workbook on the file
        sysetm (this will create a NEW file). When doing so, we
        have the option to pass either a single sheet object (as
        we are donig in this example, or an array of sheet
        objects). We can also define header and row CSS.
    --->
    <cfset objPOI.WriteExcel(
        FilePath = ExpandPath( "./allData.xls" ),
        Sheets = objSheet,
        HeaderCSS = "border-bottom: 2px solid dark_green ;",
        RowCSS = "border-bottom: 1px dotted gray ;"
        ) />
   
</cfoutput>

    This topic has been closed for replies.

    1 reply

    Inspiring
    March 24, 2010


    This can give you some clue I guess..


    //////creating the workbook
    <cfset workBook = createObject("java","org.apache.poi.hssf.usermodel.HSSFWorkbook").init()/>

    /////setting up the styles
    <cfset cellstyle = workbook.createCellStyle()>
    <cfset fontface = workbook.createFont()>
    <cfset fontface.setBoldweight(fontface.BOLDWEIGHT_BOLD)>
    <cfset cellstyle.setFont(fontface)>


    /////creating new sheet
    <cfset newSheetLegend = workBook.createSheet()/>

    ////creating new row           
    <cfset rowLegend = newSheetLegend.createRow(0)/>


    ///assigning the sheet name
    <cfset workBook.setSheetName(0, "text as the sheet name")/>

    ////creating a cell
    <cfset cellLegend = rowLegend.createCell(0)/>

    ////Applying style for the cell
    <cfset cellLegend.setCellStyle(cellstyle)/>

    ////writing the content in the cell
    <cfset cellLegend.setCellValue("text in the cell")/>


    ////getting sheet at an index -- example is 1
    cfset currentsheet = workBook.getSheetAt(1)>


    You can put this in appropriate order and in a loop to get different sheets, name the sheet, assign values to cells and also apply styles to cells.