gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:10:26 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com