0

I am trying to read an excel file using Apache Poi (v3.11 / XSSFWorkbook ). I am reading a particular sheet {e.x. sheet(0)} which is formed by different macros from around 15 sheets from the same file. The issue is I am able to read the non formatted String data but unable to read formatted data like currency or %.

I do not control the excel worksheet anyway. I will be reading the file from a network share path.

XSSFWorkbook myWorkBook = null
    XSSFSheet mySheet = null;
    Row rowHeader = null;
    Row rowData = null;
    FileInputStream fis = new FileInputStream(
            new File(
                    "inputfile.xlsx"));
    myWorkBook = new XSSFWorkbook(fis);
    mySheet = myWorkBook.getSheetAt(0);
    rowHeader = mySheet.getRow(0);
    rowData = mySheet.getRow(1);



        for (Cell s : rowHeader) {  
                System.out.print("|  "+s.getStringCellValue());
                System.out.println(" -->"+getCellValue(myWorkBook,rowData.getCell(s.getColumnIndex())));
    }

    myWorkBook.close();
}
//   $#,##0.00

private static String getCellValue(XSSFWorkbook wb, Cell cell)
{
   XSSFCell x = (XSSFCell)cell;
   return x.getRawValue();
}
}

I have a excel sheet like below

Name    Amount    Rate
Name1   $1,500  75%

If I read the above sheet using this code

Name --> Name1

Amount --> 1500 ( note the missing $ and , )

Rate --> 74.99999998 (number inacurate and missing % symbol)

10
  • 1
    Consider using BigDecimal to store your currency values. Commented Oct 26, 2015 at 15:17
  • do you need $ symbol, % with values. Commented Oct 26, 2015 at 15:19
  • the macro generates $ & %. I can see these values if I open the sheet with Ms-excel. And yes I do need $ & %. Objective is to get the cell values exactly seen in the cell. Commented Oct 26, 2015 at 15:21
  • no, what i mean. we read those values. you need those values only or with symbol Commented Oct 26, 2015 at 15:23
  • 2
    What happens if you use DataFormatter to render the cell to a String? Commented Oct 26, 2015 at 17:47

2 Answers 2

1

Thanks @Gagravarr

this below code solved the issue

private static String getCellValue(XSSFWorkbook wb, Cell cell)
{
  DataFormatter formatter = new DataFormatter();
  String formattedCellValue = formatter.formatCellValue(cell);
  return formattedCellValue; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

From the documentation,

getRawValue() would return the raw, underlying ooxml value.

Use getStringCellValue() instead as it return the value as a String and nothing is lost.

1 Comment

As I stated getStringCellValue() works if the cell is of Type 'Text'. For numbers(Type : Currency / Number /Percentage) you will get "java.lang.IllegalStateException: Cannot get a text value from a numeric cell". Even if you write a logic using getCellType() to avoid this you loose the formatting information

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.