浮点—在java中,apache poi从excel读取浮点数时,浮点数被视为“cell\u type\u string”类型

ttp71kqs  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(211)

我需要从excel文件中读取java中的数字数据。我成功地通过以下代码读取了所有数据:

try
{
    FileInputStream file = new FileInputStream(new File("book1.xlsx"));

    //Create Workbook instance holding reference to .xlsx file
    XSSFWorkbook workbook = new XSSFWorkbook(file);

    //Get first/desired sheet from the workbook
    XSSFSheet sheet = workbook.getSheetAt(0);

    //Iterate through each rows one by one
    Iterator<Row> rowIterator = sheet.iterator();

    while (rowIterator.hasNext()) 
    {
        Row row = rowIterator.next();

        //For each row, iterate through all the columns
        Iterator<Cell> cellIterator = row.cellIterator();

        while (cellIterator.hasNext()) 
        {
            Cell cell = cellIterator.next();

            //Check the cell type and format accordingly
            switch (cell.getCellType()) 
            {
                case Cell.CELL_TYPE_NUMERIC:
                    int value = new Double(cell.getNumericCellValue()).intValue();
                    System.out.print(value + " ");
                    break;
                case Cell.CELL_TYPE_STRING:
                    //do nothing
                    break;
            }
        }
        System.out.println("");
    }
    file.close();
} 
catch (Exception e) 
{
    e.printStackTrace();
}

book1.xslx:

Cr1 Co1 Co2 Co3
Co1 1   1.3 5
Co2 3   1   7
Co3 1.5 1.7 1

但代码结果是:

1 5 
3 1 7 
1

这意味着,它将1.3、1.5和1.7视为字符串,因此不在结果中打印它们。有人能帮我吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题