当用java中的apache poi从excel读取浮点数时,浮点数被视为' CELL_TYPE_STRING'

时间:2017-01-04 07:42:07

标签: java floating-point apache-poi readfile

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

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视为字符串,因此不在结果中打印它们。有谁可以帮助我?

0 个答案:

没有答案