Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/girls.xls
Binary file not shown.
Binary file added examples/girls.xlsx
Binary file not shown.
96 changes: 96 additions & 0 deletions examples/read-2007.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<cfoutput>

<!--- Create an instance of the POIUtility.cfc. --->
<cfset objPOI = new lib.POIUtility() />


<!---
Read in the Exercises excel sheet. This has Push, Pull,
and Leg exercises split up on to three different sheets.
By default, the POI Utilty will read in all three sheets
from the workbook. Since our excel sheet has a header
row, we want to strip it out of our returned queries.
--->
<cfset arrSheets = objPOI.ReadExcel(
FilePath = ExpandPath( "./girls.xlsx" ),
HasHeaderRow = true
) />


<!---
The ReadExcel() has returned an array of sheet object.
Let's loop over sheets and output the data. NOTE: This
could be also done to insert into a DATABASE!
--->
<cfloop
index="intSheet"
from="1"
to="#ArrayLen( arrSheets )#"
step="1">

<!--- Get a short hand to the current sheet. --->
<cfset objSheet = arrSheets[ intSheet ] />


<!---
Output the name of the sheet. This is taken from
the Tabs at the bottom of the workbook.
--->
<h3>
#objSheet.Name#
</h3>

<!---
Output the data from the Excel sheet in a table.
We know the structrure of the Excel, so we can
use the auto-named columns. Also, since we flagged
the workbook as using column headers, the first
row of the excel was stripped out and put into an
array of column names.
--->
<table border="1">
<tr>
<td>
#objSheet.ColumnNames[ 1 ]#
</td>
<td>
#objSheet.ColumnNames[ 2 ]#
</td>
<td>
#objSheet.ColumnNames[ 3 ]#
</td>
</tr>

<!--- Loop over the data query. --->
<cfloop query="objSheet.Query">

<!---
It is possible that the query read in read in
blank rows of data. For our scenario, we know
that we HAVE to have an exercise name.
Therefore, if there is no exercise name returned
(in Column1), then this row is not valid - skip
over it.
--->
<cfif Len( objSheet.Query.column1 )>

<tr>
<td>
#objSheet.Query.column1#
</td>
<td>
#objSheet.Query.column2#
</td>
<td>
#objSheet.Query.column3#
</td>
</tr>

</cfif>

</cfloop>
</table>

</cfloop>

</cfoutput>
60 changes: 60 additions & 0 deletions examples/write-2007.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<cfoutput>

<!--- Create an instance of the POIUtility.cfc. --->
<cfset objPOI = new lib.POIUtility() />


<!--- 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 = objPOI.GetNewSheetStruct() />

<!--- Set the query into the sheet. --->
<cfset objSheet.Query = qGirl />

<!---
Define the order of the columns (and which
columns to include).
--->
<cfset objSheet.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.ColumnNames = "Name,Hair,Beast Feature" />

<!--- Set the sheet name. --->
<cfset objSheet.SheetName = "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( "./girls.xlsx" ),
Sheets = objSheet,
HeaderCSS = "border-bottom: 2px solid dark_green ;",
RowCSS = "border-bottom: 1px dotted gray ;"
) />

</cfoutput>
144 changes: 75 additions & 69 deletions lib/POIUtility.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<cffunction name="Init" access="public" returntype="POIUtility" output="false"
hint="Returns an initialized POI Utility instance.">

<!--- Return This reference. --->
<cfreturn THIS />
</cffunction>
Expand All @@ -73,17 +73,17 @@

// Create a default cell style object.
LOCAL.Style = ARGUMENTS.WorkBook.CreateCellStyle();

// Get indexed colors
LOCAL.colorEnum = CreateObject("java", "org.apache.poi.ss.usermodel.IndexedColors");

// Check for background color.
if (ListFindNoCase( "AQUA,BLACK,BLUE,BLUE_GREY,BRIGHT_GREEN,BROWN,CORAL,CORNFLOWER_BLUE,DARK_BLUE,DARK_GREEN,DARK_RED,DARK_TEAL,DARK_YELLOW,GOLD,GREEN,GREY_25_PERCENT,GREY_40_PERCENT,GREY_50_PERCENT,GREY_80_PERCENT,INDIGO,LAVENDER,LEMON_CHIFFON,LIGHT_BLUE,LIGHT_CORNFLOWER_BLUE,LIGHT_GREEN,LIGHT_ORANGE,LIGHT_TURQUOISE,LIGHT_YELLOW,LIME,MAROON,OLIVE_GREEN,ORANGE,ORCHID,PALE_BLUE,PINK,PLUM,RED,ROSE,ROYAL_BLUE,SEA_GREEN,SKY_BLUE,TAN,TEAL,TURQUOISE,VIOLET,WHITE,YELLOW", ARGUMENTS.CSS[ "background-color" ] )){

// Set the background color.
LOCAL.Style.SetFillForegroundColor(
CreateObject(
"java",
"org.apache.poi.hssf.util.HSSFColor$#UCase( ARGUMENTS.CSS[ 'background-color' ] )#"
).GetIndex()
);
LOCAL.colorEnum.valueOf(JavaCast("String", UCase(ARGUMENTS.CSS['background-color']))).getIndex()
);


// Check for background style.
Expand Down Expand Up @@ -193,11 +193,8 @@
if (ListFindNoCase( "AQUA,BLACK,BLUE,BLUE_GREY,BRIGHT_GREEN,BROWN,CORAL,CORNFLOWER_BLUE,DARK_BLUE,DARK_GREEN,DARK_RED,DARK_TEAL,DARK_YELLOW,GOLD,GREEN,GREY_25_PERCENT,GREY_40_PERCENT,GREY_50_PERCENT,GREY_80_PERCENT,INDIGO,LAVENDER,LEMON_CHIFFON,LIGHT_BLUE,LIGHT_CORNFLOWER_BLUE,LIGHT_GREEN,LIGHT_ORANGE,LIGHT_TURQUOISE,LIGHT_YELLOW,LIME,MAROON,OLIVE_GREEN,ORANGE,ORCHID,PALE_BLUE,PINK,PLUM,RED,ROSE,ROYAL_BLUE,SEA_GREEN,SKY_BLUE,TAN,TEAL,TURQUOISE,VIOLET,WHITE,YELLOW", ARGUMENTS.CSS[ "border-bottom-color" ] )){

LOCAL.Style.SetBottomBorderColor(
CreateObject(
"java",
"org.apache.poi.hssf.util.HSSFColor$#UCase( ARGUMENTS.CSS[ 'border-bottom-color' ] )#"
).GetIndex()
);
LOCAL.colorEnum.valueOf(JavaCast("String", UCase(ARGUMENTS.CSS['border-bottom-color']))).getIndex()
);

}

Expand Down Expand Up @@ -287,11 +284,8 @@
if (ListFindNoCase( "AQUA,BLACK,BLUE,BLUE_GREY,BRIGHT_GREEN,BROWN,CORAL,CORNFLOWER_BLUE,DARK_BLUE,DARK_GREEN,DARK_RED,DARK_TEAL,DARK_YELLOW,GOLD,GREEN,GREY_25_PERCENT,GREY_40_PERCENT,GREY_50_PERCENT,GREY_80_PERCENT,INDIGO,LAVENDER,LEMON_CHIFFON,LIGHT_BLUE,LIGHT_CORNFLOWER_BLUE,LIGHT_GREEN,LIGHT_ORANGE,LIGHT_TURQUOISE,LIGHT_YELLOW,LIME,MAROON,OLIVE_GREEN,ORANGE,ORCHID,PALE_BLUE,PINK,PLUM,RED,ROSE,ROYAL_BLUE,SEA_GREEN,SKY_BLUE,TAN,TEAL,TURQUOISE,VIOLET,WHITE,YELLOW", ARGUMENTS.CSS[ "border-left-color" ] )){

LOCAL.Style.SetLeftBorderColor(
CreateObject(
"java",
"org.apache.poi.hssf.util.HSSFColor$#UCase( ARGUMENTS.CSS[ 'border-left-color' ] )#"
).GetIndex()
);
LOCAL.colorEnum.valueOf(JavaCast("String", UCase(ARGUMENTS.CSS['border-left-color']))).getIndex()
);

}

Expand Down Expand Up @@ -381,11 +375,8 @@
if (ListFindNoCase( "AQUA,BLACK,BLUE,BLUE_GREY,BRIGHT_GREEN,BROWN,CORAL,CORNFLOWER_BLUE,DARK_BLUE,DARK_GREEN,DARK_RED,DARK_TEAL,DARK_YELLOW,GOLD,GREEN,GREY_25_PERCENT,GREY_40_PERCENT,GREY_50_PERCENT,GREY_80_PERCENT,INDIGO,LAVENDER,LEMON_CHIFFON,LIGHT_BLUE,LIGHT_CORNFLOWER_BLUE,LIGHT_GREEN,LIGHT_ORANGE,LIGHT_TURQUOISE,LIGHT_YELLOW,LIME,MAROON,OLIVE_GREEN,ORANGE,ORCHID,PALE_BLUE,PINK,PLUM,RED,ROSE,ROYAL_BLUE,SEA_GREEN,SKY_BLUE,TAN,TEAL,TURQUOISE,VIOLET,WHITE,YELLOW", ARGUMENTS.CSS[ "border-right-color" ] )){

LOCAL.Style.SetRightBorderColor(
CreateObject(
"java",
"org.apache.poi.hssf.util.HSSFColor$#UCase( ARGUMENTS.CSS[ 'border-right-color' ] )#"
).GetIndex()
);
LOCAL.colorEnum.valueOf(JavaCast("String", UCase(ARGUMENTS.CSS['border-right-color']))).getIndex()
);

}

Expand Down Expand Up @@ -475,11 +466,8 @@
if (ListFindNoCase( "AQUA,BLACK,BLUE,BLUE_GREY,BRIGHT_GREEN,BROWN,CORAL,CORNFLOWER_BLUE,DARK_BLUE,DARK_GREEN,DARK_RED,DARK_TEAL,DARK_YELLOW,GOLD,GREEN,GREY_25_PERCENT,GREY_40_PERCENT,GREY_50_PERCENT,GREY_80_PERCENT,INDIGO,LAVENDER,LEMON_CHIFFON,LIGHT_BLUE,LIGHT_CORNFLOWER_BLUE,LIGHT_GREEN,LIGHT_ORANGE,LIGHT_TURQUOISE,LIGHT_YELLOW,LIME,MAROON,OLIVE_GREEN,ORANGE,ORCHID,PALE_BLUE,PINK,PLUM,RED,ROSE,ROYAL_BLUE,SEA_GREEN,SKY_BLUE,TAN,TEAL,TURQUOISE,VIOLET,WHITE,YELLOW", ARGUMENTS.CSS[ "border-top-color" ] )){

LOCAL.Style.SetTopBorderColor(
CreateObject(
"java",
"org.apache.poi.hssf.util.HSSFColor$#UCase( ARGUMENTS.CSS[ 'border-top-color' ] )#"
).GetIndex()
);
LOCAL.colorEnum.valueOf(JavaCast("String", UCase(ARGUMENTS.CSS['border-top-color']))).getIndex()
);

}

Expand All @@ -493,11 +481,8 @@
if (ListFindNoCase( "AQUA,BLACK,BLUE,BLUE_GREY,BRIGHT_GREEN,BROWN,CORAL,CORNFLOWER_BLUE,DARK_BLUE,DARK_GREEN,DARK_RED,DARK_TEAL,DARK_YELLOW,GOLD,GREEN,GREY_25_PERCENT,GREY_40_PERCENT,GREY_50_PERCENT,GREY_80_PERCENT,INDIGO,LAVENDER,LEMON_CHIFFON,LIGHT_BLUE,LIGHT_CORNFLOWER_BLUE,LIGHT_GREEN,LIGHT_ORANGE,LIGHT_TURQUOISE,LIGHT_YELLOW,LIME,MAROON,OLIVE_GREEN,ORANGE,ORCHID,PALE_BLUE,PINK,PLUM,RED,ROSE,ROYAL_BLUE,SEA_GREEN,SKY_BLUE,TAN,TEAL,TURQUOISE,VIOLET,WHITE,YELLOW", ARGUMENTS.CSS[ "color" ] )){

LOCAL.Font.SetColor(
CreateObject(
"java",
"org.apache.poi.hssf.util.HSSFColor$#UCase( ARGUMENTS.CSS[ 'color' ] )#"
).GetIndex()
);
LOCAL.colorEnum.valueOf(JavaCast("String", UCase(ARGUMENTS.CSS['color']))).getIndex()
);

}

Expand Down Expand Up @@ -865,23 +850,20 @@

// Define the local scope.
var LOCAL = StructNew();


// Create a file input stream to the given Excel file.
LOCAL.FileInputStream = CreateObject( "java", "java.io.FileInputStream" ).Init( ARGUMENTS.FilePath );

// Create the Excel file system object. This object is responsible
// for reading in the given Excel file.
LOCAL.ExcelFileSystem = CreateObject( "java", "org.apache.poi.poifs.filesystem.POIFSFileSystem" ).Init( LOCAL.FileInputStream );


// Get the workbook from the Excel file system.
LOCAL.WorkBook = CreateObject(
"java",
"org.apache.poi.hssf.usermodel.HSSFWorkbook"
).Init(
LOCAL.ExcelFileSystem
);
// Get Xcel Workbook
if (!fileExists(arguments.filepath)) {
throw(message="Workbook does not exist at #arguments.filepath#");
}

// Opening in Office 2003 format, if that fails try opening in OOXML Office 2007+ format
try {
LOCAL.FileInputStream = CreateObject( "java", "java.io.FileInputStream" ).Init( ARGUMENTS.FilePath );
LOCAL.WorkBook = CreateObject("java","org.apache.poi.hssf.usermodel.HSSFWorkbook").Init(LOCAL.FileInputStream);
} catch(org.apache.poi.poifs.filesystem.OfficeXmlFileException cfcatch) {
LOCAL.FileInputStream = CreateObject( "java", "java.io.FileInputStream" ).Init( ARGUMENTS.FilePath );
LOCAL.WorkBook = CreateObject("java","org.apache.poi.xssf.usermodel.XSSFWorkbook").Init(LOCAL.FileInputStream);
}


// Check to see if we are returning an array of sheets OR just
Expand Down Expand Up @@ -1206,20 +1188,27 @@

// Get the type of data in this cell.
LOCAL.CellType = LOCAL.Cell.GetCellType();


// Get teh value of the cell based on the data type. The thing
// to worry about here is cell forumlas and cell dates. Formulas
// can be strange and dates are stored as numeric types. For
// this demo, I am not going to worry about that at all. I will
// just grab dates as floats and formulas I will try to grab as
// numeric values.


// Get the value of the cell based on the data type. The thing
// to worry about here is cell forumlas and cell dates. Formulas
// can be strange and dates are stored as numeric types.
if (LOCAL.CellType EQ LOCAL.Cell.CELL_TYPE_NUMERIC) {

// Get numeric cell data. This could be a standard number,
// could also be a date value. I am going to leave it up to
// the calling program to decide.
LOCAL.CellValue = LOCAL.Cell.GetNumericCellValue();

// Check whether the cell is formatted as a date. If so
// get the format object from the cell style using that
// to format the java date returned from the cell value
// else get the numeric cell value
LOCAL.dateUtil = CreateObject("java","org.apache.poi.hssf.usermodel.HSSFDateUtil").init();
if (LOCAL.dateUtil.isCellDateFormatted(LOCAL.Cell)) {
LOCAL.CellFmt = LOCAL.Cell.getCellStyle().getDataFormatString();
LOCAL.CellValue = CreateObject("java","org.apache.poi.ss.format.CellDateFormatter").init(LOCAL.CellFmt).
format(LOCAL.dateUtil.getJavaDate(LOCAL.CellValue));
} else {

LOCAL.CellValue = LOCAL.Cell.GetNumericCellValue();

}

} else if (LOCAL.CellType EQ LOCAL.Cell.CELL_TYPE_STRING){

Expand Down Expand Up @@ -1348,17 +1337,36 @@
default=""
hint="Defines the limited CSS available for the alternate non-header rows. This style overwrites parts of the RowCSS."
/>

<cfargument
name="OfficeFormat"
type="string"
required="false"
default=""
hint="Defines the file format to save the workbook as. Options include 2007,2003 for Office 2007+ and for Office 2003 documents."
/>

<cfscript>

// Set up local scope.
var LOCAL = StructNew();

// Create Excel workbook.
LOCAL.WorkBook = CreateObject(
"java",
"org.apache.poi.hssf.usermodel.HSSFWorkbook"
).Init();

// Set the Office Format
if (arguments.OfficeFormat IS "" and right(arguments.FilePath, 4) eq 'xlsx') {
arguments.OfficeFormat = "2007";
} else
if (arguments.OfficeFormat IS "" and right(arguments.FilePath, 3) eq 'xls') {
arguments.OfficeFormat = "2003";
}

if (arguments.OfficeFormat IS "2007") {
// Try opening file as Office 2003 format
LOCAL.WorkBook = CreateObject("java","org.apache.poi.xssf.usermodel.XSSFWorkbook").Init();
} else if (arguments.OfficeFormat IS "2003") {
LOCAL.WorkBook = CreateObject("java","org.apache.poi.hssf.usermodel.HSSFWorkbook").Init();
} else {
throw("Invalid argument OfficeFormat: #arguments.OfficeFormat#. Valid values are [2007,2003]");
}

// Check to see if we are dealing with an array of sheets or if we were
// passed in a single sheet.
Expand Down Expand Up @@ -1435,7 +1443,7 @@
// Close the file output stream. This will release any locks on
// the file and finalize the process.
LOCAL.FileOutputStream.Close();

// Return out.
return;

Expand Down Expand Up @@ -1907,8 +1915,6 @@
</cfscript>
</cffunction>



<cffunction name="Debug">
<cfdump var="#ARGUMENTS#" />
<cfabort />
Expand Down
Loading