3

I am using Apache POI for reading excel file. And while reading it I have noticed that it takes strings as float values.

If my cell contains 1 then it will fetch it as 1.0

I took some hints from previous questions here and modified the code but still the float representation remains as it is.

How would I read correctly the data for strings and dates?

DataFormatter df = new DataFormatter();

        for (Row row : sheet) {

            for(int cn=0; cn<row.getLastCellNum(); cn++) {
                   // If the cell is missing from the file, generate a blank one
                   // (Works by specifying a MissingCellPolicy)
                   Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK);
                   // Print the cell for debugging
                   cell.setCellType(Cell.CELL_TYPE_STRING);

                   System.out.println("CELL: " + cn + " --> " + df.formatCellValue(cell));

                   if (row.getRowNum() == 0) {

                        sheetColumnNames.add(cell.getRichStringCellValue().getString());
                    }

            }

        }
3
  • Code seems correct... It seems that df.formatCellValue(cell)); is the culprit... please comment the print statement and then check again. Commented Dec 7, 2013 at 14:27
  • 1
    I think the problem is the cell.setCellType(Cell.CELL_TYPE_STRING); which shouldn't be there. Does the DataFormatter get it right when you zap the setCellType line? Commented Dec 7, 2013 at 15:02
  • Removed setter. It works good. Thanks Commented Dec 9, 2013 at 16:06

2 Answers 2

2

Promoting a comment to an answer

The problem is the call

cell.setCellType(Cell.CELL_TYPE_STRING);

What that is doing is asking POI to try to convert the cell from whatever it is currently (eg a number) into a string. The conversion applied to try to do this is a fairly simple one, which is why you're loosing the formatting

If you just want to get back a String that contains the Cell Value as shown in Excel, just call DataFormatter directly, and it'll do its best. Playing around with the Cell Type will only confuse things, and will risk loosing formatting

Sign up to request clarification or add additional context in comments.

Comments

0

Adding to above answer, poi will give you 1.0 as output even when you are using dataformatter class if you are trying to execute program against LIBRE OFFICE spreadsheet. As poi does not work in similar fashion with LIBRE SPREADSHEETS as it with excel.

3 Comments

Apache POI doesn't work with LibreOffice spreadsheets, use need to use the Apache ODF Toolkit to work with ODF spreadsheets as produced by OpenOffice and LibreOffice. Apache POI works with .xls and .xlsx Excel file formats only
Then how would we know if incoming xls or xlsx file is created in excel or libre office in poi?
If the file is in .xls or .xlsx format, then it isn't in LibreOffice format! LibreOffice and OpenOffice use the .ods (Open Document Spreadsheet) format as their native spreadsheet format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.