1

I use jxl library to import data from excel. I have a cell with values : 8.628282828. When i debug as below image :

Debug sheet.getCell(5,i).getContents()

I see it is type : NumberRecord and values is right : 8.628282828. But when i get content of cell :

InputStream is = new ByteArrayInputStream(data);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setEncoding("CP1252");
Workbook w;
w = Workbook.getWorkbook(is, wbSettings);
Sheet sheet = w.getSheet(0);
for (int i = 1; i < sheet.getRows(); i++) {
sheet.getCell(5,i).getContents();
}

It's 8.628 How can i get exactly value of that cell ?

TungHarry
  • 1,097
  • 11
  • 26

2 Answers2

1

Cast it to a NumberCell and get the value as a double:

double value = ((NumberCell) sheet.getCell(5,i)).getValue();

As the documentation for Cell.getContents() states:

Quick and dirty function to return the contents of this cell as a string. For more complex manipulation of the contents, it is necessary to cast this interface to correct subinterface

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Thanks. Sorry for my question does not clear. I just want it's text, not double. Maybe i can cast double to string like code of you. But i think problem is format cell at file. When i insert character **'** at first : **'8.268282828** is text type in excel. And last i edit format cell in excel file is ok. – TungHarry Aug 10 '15 at 08:55
  • Convert double to string: `Double.toString(value);` Obviously, if it's not a numeric cell, the cast to `NumberCell` will fail. You can get the cell type bye calling [`Cell.getType()`](http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/CellType.html). – Robby Cornelissen Aug 10 '15 at 08:57
0

Cast to NumberCell interface and call getValue() to get the double value.