0

Could anyone help? By using that code, I was able to get a value from an Excel field as well. There is a value for a specific column = 5.

public Integer  Check4(int b) throws IOException  {
    InputStream myxls = new FileInputStream("book.xls");
    HSSFWorkbook wb     = new HSSFWorkbook(myxls);
    HSSFSheet sheet = wb.getSheetAt(0);       // first sheet
    HSSFRow row     = sheet.getRow(0);        // first row
    //HSSFCell cell0   = row.getCell((short)a);  // first arg
    HSSFCell cell1   = row.getCell((short)b);  // second arg
    cell1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    System.out.println("smth "+  cell1);
    return ;

}

However, The output of the such code is:

"smth + 5.0"

I'd get it, how to convert the var cell1 5.0 to 5 ? Math.round, Integer.parseInt() don't, actually, help

Leo
  • 1,787
  • 5
  • 21
  • 43
  • What does Integer.parseInt() return? Exception? – Priyesh May 12 '14 at 13:02
  • java.lang.Error: Unresolved compilation problem: The method parseInt(String) in the type Integer is not applicable for the arguments (HSSFCell) – Leo May 12 '14 at 13:19

2 Answers2

1

You have to get the string value from the HSSFCell before using Integer.parseInt(). Use Integer.parseInt(cell1.getStringCellValue()). You can probably use getNumericCellValue() but that will return a double.

Priyesh
  • 2,041
  • 1
  • 17
  • 25
0

finally, solve by (int) Math.round(cell1.getNumericCellValue()))

    public Integer  Check4(int b) throws IOException  {
    InputStream myxls = new FileInputStream("book.xls");
    HSSFWorkbook wb     = new HSSFWorkbook(myxls);
    HSSFSheet sheet = wb.getSheetAt(0);       // first sheet
    HSSFRow row     = sheet.getRow(1);        // first row
    //HSSFCell cell0   = row.getCell((short)a);  // first arg
    HSSFCell cell1   = row.getCell((short)b);  // second arg
    String sell = cell1.toString();
    cell1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    System.out.println("smth "+ (int) Math.round(cell1.getNumericCellValue()));
    return  (int) Math.round(cell1.getNumericCellValue());

}
Leo
  • 1,787
  • 5
  • 21
  • 43