Search code examples
apache-poixlsxlsx

XSSFSheet get all cell type values as string


Is there any possiblity to get all types(numeric,date,string etc) as String only.I couldn't find such methods.

sheet.getCell(rowIndex,colIndex) like this ?

InputStream ExcelFileToRead = new FileInputStream(file1);
XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);
XSSFWorkbook test = new XSSFWorkbook(); 
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;

Iterator rows = sheet.rowIterator();
String[] Excelarray=new String[26];
int count=0;
Map<String, String> data = new HashMap<String, String>();

while (rows.hasNext())
{
row=(XSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext())
{
cell=(XSSFCell) cells.next();
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
{
System.out.print(cell.getStringCellValue()+",");
}
else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
{
System.out.print(cell.getNumericCellValue()+",");
}
else
{
}
}
System.out.println("----Closed");
}

Solution

  • Someone else already supplied a generic implementation that does what you are looking to do. POI doesn't have anything directly but it's easy enough to make a helper method/class.