From d03f88c96b57d6354623f0d400ac896c4b316a8d Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 6 Aug 2007 13:38:48 +0000 Subject: [PATCH] Have HSSFDateUtil.isCellDateFormatted make use of HSSFDateUtils.isADateFormat. A few improvements to isADateFormat, and to HSSFCellStyle to allow calling of it. Plus tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@563129 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 4 + src/documentation/content/xdocs/status.xml | 4 + .../apache/poi/hssf/usermodel/HSSFCell.java | 8 ++ .../poi/hssf/usermodel/HSSFCellStyle.java | 13 +++ .../poi/hssf/usermodel/HSSFDateUtil.java | 44 +++++++++- .../org/apache/poi/hssf/data/DateFormats.xls | Bin 0 -> 13824 bytes .../poi/hssf/usermodel/TestHSSFDateUtil.java | 77 +++++++++++++++++- 7 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/DateFormats.xls diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 64fb1d0be..be1def70b 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,7 +36,11 @@ + Improvements to HSSFDateUtils.isADateFormat, and have HSSFDateUtil.isCellDateFormatted use this + 42999 - [PATCH] - Fix for HSSFPatriarch positioning problems + Support for write-protecting a HSSF workbook Support for querying, setting and un-setting protection on sheets in a HSSF workbook + Initial HSMF (outlook) support Tidy up the javadocs diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 34694bc30..5fe08dbd4 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,7 +33,11 @@ + Improvements to HSSFDateUtils.isADateFormat, and have HSSFDateUtil.isCellDateFormatted use this + 42999 - [PATCH] - Fix for HSSFPatriarch positioning problems + Support for write-protecting a HSSF workbook Support for querying, setting and un-setting protection on sheets in a HSSF workbook + Initial HSMF (outlook) support Tidy up the javadocs diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 752f598af..ac3943da4 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -256,6 +256,14 @@ public class HSSFCell } return retval; } + + /** + * Returns the Workbook that this Cell is bound to + * @return + */ + protected Workbook getBoundWorkbook() { + return book; + } /** * set the cell's number within the row (0 based) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index e978642c5..e2725937f 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -18,7 +18,9 @@ package org.apache.poi.hssf.usermodel; +import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.ExtendedFormatRecord; +import org.apache.poi.hssf.record.FormatRecord; import org.apache.poi.hssf.util.*; /** @@ -266,6 +268,17 @@ public class HSSFCellStyle { return format.getFormatIndex(); } + + /** + * Get the contents of the format string, by looking up + * the DataFormat against the supplied workbook + * @see org.apache.poi.hssf.usermodel.HSSFDataFormat + */ + public String getDataFormatString(Workbook workbook) { + HSSFDataFormat format = new HSSFDataFormat(workbook); + + return format.getFormat(getDataFormat()); + } /** * set the font for this style diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java b/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java index 2da04e7f4..af16204fd 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java @@ -159,7 +159,7 @@ public class HSSFDateUtil * non US date formats. * * @param formatIndex The index of the format, eg from ExtendedFormatRecord.getFormatIndex - * @param formatString The format string + * @param formatString The format string, eg from FormatRecord.getFormatString * @see #isInternalDateFormat(int) */ public static boolean isADateFormat(int formatIndex, String formatString) { @@ -173,12 +173,26 @@ public class HSSFDateUtil return false; } + String fs = formatString; + // Translate \- into just -, before matching - String fs = formatString.replaceAll("\\\\-","-"); + fs = fs.replaceAll("\\\\-","-"); + // And \, into , + fs = fs.replaceAll("\\\\,",","); + // And '\ ' into ' ' + fs = fs.replaceAll("\\\\ "," "); + + // If it end in ;@, that's some crazy dd/mm vs mm/dd + // switching stuff, which we can ignore + fs = fs.replaceAll(";@", ""); + + // If it starts with [$-...], then it is a date, but + // who knows what that starting bit is all about + fs = fs.replaceAll("\\[\\$\\-.*?\\]", ""); // Otherwise, check it's only made up of: - // y m d - / - if(fs.matches("^[ymd\\-/]+$")) { + // y m d - / , + if(fs.matches("^[ymd\\-/, ]+$")) { return true; } @@ -222,12 +236,34 @@ public class HSSFDateUtil * Check if a cell contains a date * Since dates are stored internally in Excel as double values * we infer it is a date if it is formatted as such. + * @see #isADateFormat(int,string) * @see #isInternalDateFormat(int) */ public static boolean isCellDateFormatted(HSSFCell cell) { if (cell == null) return false; boolean bDate = false; + double d = cell.getNumericCellValue(); + if ( HSSFDateUtil.isValidExcelDate(d) ) { + HSSFCellStyle style = cell.getCellStyle(); + int i = style.getDataFormat(); + String f = style.getDataFormatString(cell.getBoundWorkbook()); + bDate = isADateFormat(i, f); + } + return bDate; + } + /** + * Check if a cell contains a date, checking only for internal + * excel date formats. + * As Excel stores a great many of its dates in "non-internal" + * date formats, you will not normally want to use this method. + * @see #isADateFormat(int,string) + * @see #isInternalDateFormat(int) + */ + public static boolean isCellInternalDateFormatted(HSSFCell cell) { + if (cell == null) return false; + boolean bDate = false; + double d = cell.getNumericCellValue(); if ( HSSFDateUtil.isValidExcelDate(d) ) { HSSFCellStyle style = cell.getCellStyle(); diff --git a/src/testcases/org/apache/poi/hssf/data/DateFormats.xls b/src/testcases/org/apache/poi/hssf/data/DateFormats.xls new file mode 100644 index 0000000000000000000000000000000000000000..6b2c3480b257f0847f8eebffb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