mirror of https://github.com/apache/poi.git
New FAQ.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352586 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3f48f62365
commit
3b06a0f6a8
|
@ -42,4 +42,72 @@
|
|||
at this feel free to add it as a plugin module. We wont be hosting it here however.
|
||||
</answer>
|
||||
</faq>
|
||||
<faq>
|
||||
<question>
|
||||
How do you tell if a field contains a date with HSSF?
|
||||
</question>
|
||||
<answer>
|
||||
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:
|
||||
<p>
|
||||
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>
|
||||
/////// code snippet ////////////
|
||||
case HSSFCell.CELL_TYPE_NUMERIC:
|
||||
double d = cell.getNumericCellValue();
|
||||
// test if a date!
|
||||
if (isCellDateFormatted(cell)) {
|
||||
// format in form of M/D/YY
|
||||
cal.setTime(HSSFDateUtil.getJavaDate(d));
|
||||
cellText =
|
||||
(String.valueOf(cal.get(Calendar.YEAR))).substring(2);
|
||||
cellText = cal.get(Calendar.MONTH)+1 + "/" +
|
||||
cal.get(Calendar.DAY_OF_MONTH) + "/" +
|
||||
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>
|
||||
</answer>
|
||||
</faq>
|
||||
</faqs>
|
||||
|
|
Loading…
Reference in New Issue