From: Avik Sengupta
Date: Thu, 13 Jun 2002 17:17:24 +0000 (+0000)
Subject: Add util method to check date formatting in Excel, submitted by Jason Hoffman
X-Git-Tag: REL_1_10~311
X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=40c021bec30ea093e582e235314d8161dc4a6822;p=poi.git
Add util method to check date formatting in Excel, submitted by Jason Hoffman
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352695 13f79535-47bb-0310-9956-ffa450edef68
---
diff --git a/src/documentation/xdocs/faq.xml b/src/documentation/xdocs/faq.xml
index 71957db754..05a172291d 100644
--- a/src/documentation/xdocs/faq.xml
+++ b/src/documentation/xdocs/faq.xml
@@ -37,7 +37,7 @@
Does HSSF support protected spreadsheets?
- Protecting a spreadsheet encripts it. We wont touch encription because we're not legally educated
+ Protecting a spreadsheet encrypts it. We wont touch encryption because we're not legally educated
and don't understand the full implications of trying to implement this. If you wish to have a go
at this feel free to add it as a plugin module. We wont be hosting it here however.
@@ -48,20 +48,15 @@
Excel stores dates as numbers therefore the only way to determine if a cell is
- actually stored as a date is to look at the formatting. This solution from
- Jason Hoffman:
-
- Okay, here is a little code I used to determine if the cell was a number or
- date, and then format appropriately. I hope it helps. I keep meaning to
- submit a patch with the helper method below.... but just haven't had a
- chance.
-
+ actually stored as a date is to look at the formatting. There is a helper method
+ in HSSFDateUtil (since after 1.6.0-dev) that checks for this.
+ Thanks to Jason Hoffman for providing the solution.
+
+
I'm trying to stream an XLS file from a servlet and I'm having some trouble. What's the problem?
@@ -125,8 +88,9 @@ public static boolean isCellDateFormatted(HSSFCell cell) {
The problem in most versions of IE is that it does not use the mime type on
the HTTP response to determine the file type; rather it uses the file extension
on the request. Thus you might want to add a .xls to your request
- string. For example http://yourserver.com/myServelet.xls?param1=xx. Sometimes
- a request like http://yourserver.com/myServelet?param1=xx&dummy=file.xls is also
+ string. For example http://yourserver.com/myServelet.xls?param1=xx. This is
+ easily accomplished through URL mapping in any servlet container. Sometimes
+ a request like http://yourserver.com/myServelet?param1=xx&dummy=file.xls is also
known to work.
@@ -137,7 +101,7 @@ public static boolean isCellDateFormatted(HSSFCell cell) {
request as mentioned above.)
- Note also that sometimes when you request a document that is opened with an
+ Note also that when you request a document that is opened with an
external handler, IE sometimes makes two requests to the webserver. So if your
generating process is heavy, it makes sense to write out to a temporary file, so that multiple
requests happen for a static file.
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java b/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java
index 4a6caf10af..9ab294f3fa 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java
@@ -141,6 +141,46 @@ public class HSSFDateUtil
return null;
}
}
+
+ /**
+ * 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.
+ */
+ 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();
+ switch(i) {
+ // Internal Date Formats as described on page 427 in
+ // Microsoft Excel Dev's Kit...
+ case 0x0e:
+ case 0x0f:
+ case 0x10:
+ case 0x11:
+ case 0x12:
+ case 0x13:
+ case 0x14:
+ case 0x15:
+ case 0x16:
+ case 0x2d:
+ case 0x2e:
+ case 0x2f:
+ bDate = true;
+ break;
+
+ default:
+ bDate = false;
+ break;
+ }
+ }
+ return bDate;
+ }
+
/**
* Given a double, checks if it is a valid Excel date.