Is there a way to determine if cell is a date? I know about style.getDataFormatString() but that doesn't help me, because I can't determine if is formating is for date or not.
Asked
Active
Viewed 1.6k times
2 Answers
13
If you're using the XSSF UserModel, then you want DateUtil.isCellDateFormatted(Cell) - this will return a boolean telling you if the format string for the cell represents a data or not.
If you're down at the low level XML stuff, you need DateUtil.isADateFormat(int,String) instead. The style ID comes from the cell xml. The style string you'll have to get out of the styles table, which is a different Package Part. There are helpers for loading that though
You probably want to look at XSSFExcelExtractorDecorator from Tika for an example of doing the latter - it does formatting of cells from an event parsing.

Gagravarr
- 47,320
- 10
- 111
- 156
-
I'm actually copy/paste http://mail-archives.apache.org/mod_mbox/poi-user/200905.mbox/%3C4A0B1DC5.3070803@invest-faq.com%3E and try to modify it. Yes I did see that possibility but don't know how to retrieve Cell. – Zemzela Jul 13 '11 at 11:01
-
Are you using XSSF usermodel, or doing event model raw XML stuff? – Gagravarr Jul 13 '11 at 11:56
4
cell.getCellTypeEnum() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)

Evan Knox Thomas
- 592
- 6
- 12
-
1Why are you checking if the CellType is Numeric if the question is related to Date cell? – araknoid Sep 05 '17 at 09:16
-
1when you use DateUtil.isCellDateFormatted(cell), but the CellType is String, it will appear "java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell" @araknoid – Evan Knox Thomas Sep 05 '17 at 10:00
-
You should add this details to the answer and explain it a bit because as it is written, It's not a self-explaining answer – araknoid Sep 05 '17 at 11:46