Dateutil. isCellDateFormatted(Cell Cell) Not Working in Java P O I
This My Input in Cell A4 There Is a Date. How Can I Get This in My Java Program as It Is. My Dateutil. isCellDateFormated(Cell) Not Working. I Have Used...
This my input in cell A4 there is a date. How can i get this in my java program as it is . My DateUtil.isCellDateFormated(Cell) not working . i have used DataFormatter it is 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 is 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;
}
1 Answer
It seems that you can try to 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 be 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 for the cell
will be:
123.0546
Killme
78%
11/22/1995
This type of cell should be additionally implemented
1190
Please let me know if it works for you, otherwise I will remove the answer.