From dc0f25bd8f5e08b7a999027ceefd69f666d8d389 Mon Sep 17 00:00:00 2001 From: Arthur Blake Date: Mon, 17 Feb 2014 13:48:20 -0500 Subject: [PATCH] properly handle cells containing dates tested against Railo 3.2.1.000 final with POI 3.10 final but should be fine in other flavors too... --- lib/POIUtility.cfc | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/POIUtility.cfc b/lib/POIUtility.cfc index 8e1aa91..cedaba8 100755 --- a/lib/POIUtility.cfc +++ b/lib/POIUtility.cfc @@ -1208,18 +1208,40 @@ 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 formulas and cell dates. Formulas + // can be strange and dates are stored as numeric types. For + // this demo, I am not going to worry about formulas + // (and just try to grab them as numeric values). + + // I will convert the date numerics to M/D/YY format as described here: + // http://poi.apache.org/faq.html#faq-N1008D 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. + + // Get numeric cell data. This could be a standard number, + // could also be a date value. LOCAL.CellValue = LOCAL.Cell.GetNumericCellValue(); + + // test for numeric that's formatted as a date and if so, convert it back to + // a a String that's formatted M/D/YY + + LOCAL.HSSFDateUtil = CreateObject( + "java", + "org.apache.poi.hssf.usermodel.HSSFDateUtil" + ); + + if (LOCAL.HSSFDateUtil.isCellDateFormatted(LOCAL.Cell)) { + LOCAL.Cal = CreateObject("java", "java.util.Calendar").getInstance(); + + // format in form of M/D/YY + LOCAL.Cal.setTime(LOCAL.HSSFDateUtil.getJavaDate(LOCAL.CellValue)); + + LOCAL.CellValue = LOCAL.Cal.get(LOCAL.Cal.MONTH)+1 & "/" & + LOCAL.Cal.get(LOCAL.Cal.DAY_OF_MONTH) & "/" & + (CreateObject("java", "java.lang.String"). + valueOf(LOCAL.Cal.get(LOCAL.Cal.YEAR))).substring(2); + } + } else if (LOCAL.CellType EQ LOCAL.Cell.CELL_TYPE_STRING){