POI从excel读取数字数据时附加.0

use*_*327 6 java oracle junit dbunit apache-poi

我正在使用POI HSSF来读取excel数据,我正在使用JUnit来检查数据库proc RefCursor的数据.

Junit测试失败,因为来自例如100的Refcursor的数值数据与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();
        }
Run Code Online (Sandbox Code Playgroud)

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

Gag*_*arr 7

这实际上与这里的许多其他问题相同,例如返回十进制而不是字符串(POI jar)

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

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) );
    }
 }
Run Code Online (Sandbox Code Playgroud)