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
This commit is contained in:
Avik Sengupta 2002-06-13 17:17:24 +00:00
parent 65400bbe1b
commit ffe4a6d83e
2 changed files with 53 additions and 49 deletions

View File

@ -37,7 +37,7 @@
Does HSSF support protected spreadsheets? Does HSSF support protected spreadsheets?
</question> </question>
<answer> <answer>
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 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. at this feel free to add it as a plugin module. We wont be hosting it here however.
</answer> </answer>
@ -48,20 +48,15 @@
</question> </question>
<answer> <answer>
Excel stores dates as numbers therefore the only way to determine if a cell is 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 actually stored as a date is to look at the formatting. There is a helper method
Jason Hoffman: in HSSFDateUtil (since after 1.6.0-dev) that checks for this.
<p> Thanks to Jason Hoffman for providing the solution.
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.
</p>
<source> <source>
/////// code snippet ////////////
case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_NUMERIC:
double d = cell.getNumericCellValue(); double d = cell.getNumericCellValue();
// test if a date! // test if a date!
if (isCellDateFormatted(cell)) { if (HSSFDateUtil.isCellDateFormatted(cell)) {
// format in form of M/D/YY // format in form of M/D/YY
cal.setTime(HSSFDateUtil.getJavaDate(d)); cal.setTime(HSSFDateUtil.getJavaDate(d));
cellText = cellText =
@ -70,45 +65,13 @@ case HSSFCell.CELL_TYPE_NUMERIC:
cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" +
cellText; cellText;
} }
/////// end code snippet ////////////
// HELPER METHOD BELOW TO DETERMINE IF DATE
// method to determine if the cell is a date, versus a number...
public static boolean isCellDateFormatted(HSSFCell cell) {
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;
}
</source> </source>
</answer> </answer>
</faq>
<faq>
<question> <question>
I'm trying to stream an XLS file from a servlet and I'm having some trouble. What's the problem? I'm trying to stream an XLS file from a servlet and I'm having some trouble. What's the problem?
</question> </question>
@ -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 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 the HTTP response to determine the file type; rather it uses the file extension
on the request. Thus you might want to add a <strong>.xls</strong> to your request on the request. Thus you might want to add a <strong>.xls</strong> to your request
string. For example http://yourserver.com/myServelet.xls?param1=xx. Sometimes string. For example <em>http://yourserver.com/myServelet.xls?param1=xx</em>. This is
a request like http://yourserver.com/myServelet?param1=xx&amp;dummy=file.xls is also easily accomplished through URL mapping in any servlet container. Sometimes
a request like <em>http://yourserver.com/myServelet?param1=xx&amp;dummy=file.xls</em> is also
known to work. known to work.
</p> </p>
<p> <p>
@ -137,7 +101,7 @@ public static boolean isCellDateFormatted(HSSFCell cell) {
request as mentioned above.) request as mentioned above.)
</p> </p>
<p> <p>
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 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 generating process is heavy, it makes sense to write out to a temporary file, so that multiple
requests happen for a static file. requests happen for a static file.

View File

@ -142,6 +142,46 @@ 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.
*/
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. * Given a double, checks if it is a valid Excel date.
* *