Get the cell value as how it was presented in excel

Excel stores some cells as strings, but most as numbers with special formatting rules applied to them. What you’ll need to do is have those formatting rules run against the numeric cells, to produce strings that look like they do in Excel.

Luckily, Apache POI has a class to do just that – DataFormatter

All you need to do is something like:

 Workbook wb = WorkbookFactory.create(new File("myfile.xls"));
 DataFormatter df = new DataFormatter();

 Sheet s = wb.getSheetAt(0);
 Row r1 = s.getRow(0);
 Cell cA1 = r1.getCell(0);

 String asItLooksInExcel = df.formatCellValue(cA1);

Doesn’t matter what the cell type is, DataFormatter will format it as best it can for you, using the rules applied in Excel

Leave a Comment