java - POI 在从 excel 读取数字数据时追加 .0

标签 java oracle junit apache-poi dbunit

我正在使用 POI HSSF 读取 excel 数据,我正在使用 JUnit 根据数据库 proc RefCursor 检查数据。

Junit 测试失败,因为将来自 Refcursor 的数字数据(例如 100)与 Excel 工作表 100 中的数据进行比较,但由于 POI 将其读取为 100.0 而失败。

        InputStream fileInputStream = Testdb.class.getClassLoader().getResourceAsStream(fileName);
        //retrieve number of columns and rows
        int numRows=0, numCols=0, i, j, minColIndex=0, maxColIndex=0;
        POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
        HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
        HSSFSheet hssfSheet = workBook.getSheetAt(0);
        Iterator rowIterator = hssfSheet.rowIterator();
        while (rowIterator.hasNext())
        {
            numRows++;
            HSSFRow hssfRow = (HSSFRow) rowIterator.next();
            Iterator iterator = hssfRow.cellIterator();
            List cellTempList = new ArrayList();
            if (numRows == 1)
            {
                minColIndex = hssfRow.getFirstCellNum();
                maxColIndex = hssfRow.getLastCellNum();
                numCols = maxColIndex;
            }
            for(int colIndex = minColIndex; colIndex < maxColIndex; colIndex++)
            {
                HSSFCell hssfCell = hssfRow.getCell(colIndex);
                cellTempList.add(hssfCell);

            }
            cellDataList.add(cellTempList);
        }


        String expected[][] = new String[numRows][numCols];
        String[] tableColumns = new String[numCols];
        System.out.println("Rows : " + numRows + "Columns : " + numCols);
        System.out.println("Min Col Index : " +minColIndex + "Max Col Index : " + maxColIndex);
        for (i=0; i<numRows; i++)
        {
            List cellTempList = (List) cellDataList.get(i);
            for (j=0; j < numCols; j++)
            {
                HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);
                if (i == 0)
                {
                    tableColumns[j] = hssfCell.toString();
                    System.out.print(tableColumns[j] + "\t");
                }
                else
                {
                    if(hssfCell != null)
                    {
                        expected[i-1][j] = hssfCell.toString();
                    }
                    else
                    {
                        expected[i-1][j] = null;
                    }
                    System.out.print(expected[i-1][j] + "\t");
                }
            }
            System.out.println();
        }

这是我正在构建的通用框架程序,因此该框架应该足够智能以忽略“.0”。 关于如何解决这个问题的任何意见?

最佳答案

这实际上与此处的许多其他问题相同,例如 returning decimal instead of string (POI jar)

答案与one I gave here相同:

POI 为您提供 Excel 存储在文件中的确切值。通常,如果您在 Excel 单元格中写入数字,Excel 会将其存储为带格式的数字。如果您需要,POI 会为您提供格式化支持(大多数人不需要 - 他们希望数字作为数字以便他们可以使用它们)

您要找的类(class)是DataFormatter .您的代码类似于

 DataFormatter fmt = new DataFormatter();
 for (Row r : sheet) {
    for (Cell c : r) {
       CellReference cr = new CellRefence(c);
       System.out.println("Cell " + cr.formatAsString() + " is " + 
                          fmt.formatCellValue(c) );
    }
 }

关于java - POI 在从 excel 读取数字数据时追加 .0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12800635/

相关文章:

java - jFileChooser 与任务计划程序一起使用时不工作

sql - 包装存储过程

sql - 获取两个表中几列的总和

java - 找不到 PowerMock 类

unit-testing - 对依赖 EJB 和 UserTransaction 的类进行单元测试

ant - 为 ant junit 任务指定 junit.jar 的备用名称

Java ConcurrentHashmap 问题

java - 如何在 SWT/Java 应用程序中禁用 Mac 操作系统中的全屏按钮?

java - 在java中打印菱形+字母的图形

sql - 根据列的最大值选择行