package
com.java.connect.poi;
import
java.io.FileInputStream;
import
java.io.InputStream;
import
java.text.SimpleDateFormat;
import
java.util.Iterator;
import
org.apache.poi.ss.usermodel.Cell;
import
org.apache.poi.ss.usermodel.DateUtil;
import
org.apache.poi.ss.usermodel.Row;
import
org.apache.poi.ss.usermodel.Sheet;
import
org.apache.poi.ss.usermodel.Workbook;
import
org.apache.poi.ss.usermodel.WorkbookFactory;
public
class
POIReadingCellValueAsStringExample {
public
static
void
main(String[] args)
throws
Exception {
InputStream inputStream =
new
FileInputStream(
"POIAddDifferentTypeCellsExample.xls"
);
Workbook wb = WorkbookFactory.create(inputStream);
Sheet sheet = wb.getSheetAt(
0
);
for
(Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
Row row = rit.next();
for
(Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
Cell cell = cit.next();
System.out.println(
"Output : "
+ getCellValueAsString(cell));
}
}
}
/**
* This method for the type of data in the cell, extracts the data and
* returns it as a string.
*/
public
static
String getCellValueAsString(Cell cell) {
String strCellValue =
null
;
if
(cell !=
null
) {
switch
(cell.getCellType()) {
case
Cell.CELL_TYPE_STRING:
strCellValue = cell.toString();
break
;
case
Cell.CELL_TYPE_NUMERIC:
if
(DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateFormat =
new
SimpleDateFormat(
"dd/MM/yyyy"
);
strCellValue = dateFormat.format(cell.getDateCellValue());
}
else
{
Double value = cell.getNumericCellValue();
Long longValue = value.longValue();
strCellValue =
new
String(longValue.toString());
}
break
;
case
Cell.CELL_TYPE_BOOLEAN:
strCellValue =
new
String(
new
Boolean(
cell.getBooleanCellValue()).toString());
break
;
case
Cell.CELL_TYPE_BLANK:
strCellValue =
""
;
break
;
}
}
return
strCellValue;
}
}
No comments:
Post a Comment