apache poi - DateUtil.isCellDateFormatted(Cell cell) not working in java p o i -
this input in cell a4 there date. how can in java program . dateutil.iscelldateformated(cell) not working . have used dataformatter giving me no of days. 
public string getdatavalueasstring(cell cell){ string value = null; celltype type = cell.getcelltypeenum(); dataformatter dataformat = new dataformatter(); creationhelper ch = null; switch(type){ case blank: value = ""; break; case boolean: value = string.valueof(cell.getbooleancellvalue()); break; case error: value = dataformat.formatcellvalue(cell); break; case formula: formulaevaluator evaluator = cell.getsheet().getworkbook().getcreationhelper().createformulaevaluator(); value = dataformat.formatcellvalue(cell, evaluator); break; case numeric: if(dateutil.iscelldateformatted(cell)){ system.out.println("cell date formatted : "); ch = cell.getsheet().getworkbook().getcreationhelper(); short formatindex = ch.createdataformat().getformat(cell.getcellstyle().getdataformatstring()); system.out.println("format index : "+formatindex); string format = cell.getcellstyle().getdataformatstring(); system.out.println("format : "+format); }else{ //value = string.valueof(cell.getnumericcellvalue()); value = dataformat.formatcellvalue(cell); } break; case string: value = cell.getstringcellvalue(); break; default: value = dataformat.formatcellvalue(cell); } return value; }
it seems can try use cell.html#getdatecellvalue() method.
some quick example:
import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.dataformatter; import org.apache.poi.ss.usermodel.row; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.text.simpledateformat; import java.util.date; import java.util.iterator; public class externalcaller { public static final string mm_dd_yyyy = "mm/dd/yyyy"; public static void main(string... args) throws ioexception { fileinputstream file = new fileinputstream(new file("d:\\test.xls")); hssfworkbook workbook = new hssfworkbook(file); hssfsheet sheet = workbook.getsheetat(0); iterator<row> rowiterator = sheet.iterator(); while (rowiterator.hasnext()) { row row = rowiterator.next(); iterator<cell> celliterator = row.celliterator(); while (celliterator.hasnext()) { cell cell = celliterator.next(); int celltype = cell.getcelltype(); try { determinevalue(celltype, cell); } catch (unsupportedoperationexception ex) { system.out.println(ex.getmessage()); } } } } public static void determinevalue(int celltype, cell cell) { switch (celltype) { case cell.cell_type_numeric: determinedate(cell); break; case cell.cell_type_string: system.out.println(cell.getstringcellvalue()); break; default: throw new unsupportedoperationexception("this type of cell should additionally implemented"); } } private static void determinedate(cell cell) { short dataformat = cell.getcellstyle().getdataformat(); if (14 == dataformat) { date datecellvalue = cell.getdatecellvalue(); system.out.println(new simpledateformat(mm_dd_yyyy).format(datecellvalue)); } else { system.out.println(new dataformatter().formatcellvalue(cell)); } } } the output cell
will be:
123.0546 killme 78% 11/22/1995 type of cell should additionally implemented 1190 please let me know if works you, otherwise remove answer.


Comments
Post a Comment