当前位置: 首页 > 面试题库 >

从Excel读取数值数据时POI附加.0

督烨赫
2023-03-14
问题内容

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

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

        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”。关于如何解决这个问题的任何投入?


问题答案:

答案与我在这里给出的答案相同:

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

您要查找的类是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) );
    }
 }


 类似资料:
  • 我正在尝试使用POI Apache从Excel表中读取数据。我遇到的问题是,我想同时读取一行中所有单元格的数据,并将其存储在类类型的ArrayList中,但输出只是一个单元格接一个单元格。 下面是打开excel表并逐单元读取数据的类。 数据类

  • 我正在将Selenium与Apache POI一起使用,但据我所知,我已经检查了它从Excel读取空白数据。任何人都可以帮忙吗? [http://www . tools QA . com/selenium-web driver/data-driven-testing-excel-poi/][1] 这是我使用代码的链接。 http://www . tools QA . com/selenium-we

  • 问题内容: 我正在使用Apache POI读取Excel文件。在阅读时,我注意到它将字符串作为浮点值。 如果我的单元格包含1,则它将获取为1.0 我从以前的问题中得到了一些提示,并修改了代码,但float表示仍然保持原样。 如何正确读取字符串和日期的数据? 问题答案: 对评论发表评论 问题是电话 这样做是在要求POI尝试将单元格从当前单元格(例如数字)转换为字符串。尝试执行此转换非常简单,这就是为

  • 这是我的代码 错误如下: 线程“main”中出现异常Java . lang . nosuchfielderror:RAW _ XML _ FILE _ HEADER at org . Apache . poi . poi fs . FILE system . FILE magic .(FILE magic . Java:42)at org . Apache . poi . open XML 4j

  • 我已经花了几个小时阅读产品分支中的0和1。请在Firebase数据库有经验的人帮助我:(

  • 问题内容: 我将POI HSSF API用于Java中的excel操作。我在一个excel单元格中有一个日期值“ 8/1/2009”,当我尝试使用HSSF API读取此值时,它将检测到单元格类型为数字并返回我的日期的“双精度”值。请参见下面的示例代码: Cell.getCellType()返回NUMERIC_TYPE,因此此代码将日期转换为两倍!:( 有什么方法可以读取HSSF POI中的日期!