Frage

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

War es hilfreich?

Lösung 2

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());

}

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top