Java 从excel读取数值数据时,POI追加.0
我使用POI HSSF读取excel数据,并使用JUnit根据数据库proc RefCursor检查数据 Junit测试失败,因为来自Refcursor(例如100)的数字数据与excel工作表100中的数据进行了比较,但由于POI将其读取为100.0,Junit测试失败Java 从excel读取数值数据时,POI追加.0,java,oracle,junit,apache-poi,dbunit,Java,Oracle,Junit,Apache Poi,Dbunit,我使用POI HSSF读取excel数据,并使用JUnit根据数据库proc RefCursor检查数据 Junit测试失败,因为来自Refcursor(例如100)的数字数据与excel工作表100中的数据进行了比较,但由于POI将其读取为100.0,Junit测试失败 InputStream fileInputStream = Testdb.class.getClassLoader().getResourceAsStream(fileName); //retr
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();
}
InputStream文件InputStream=Testdb.class.getClassLoader().getResourceAsStream(文件名);
//检索列数和行数
int numRows=0,numCols=0,i,j,minColIndex=0,maxColIndex=0;
poifsffilesystem fsFileSystem=新的poifsffilesystem(fileInputStream);
HSSF工作簿=新的HSSF工作簿(FSF文件系统);
HSSFSheet HSSFSheet=工作簿。getSheetAt(0);
迭代器rowIterator=hssfSheet.rowIterator();
while(roweiterator.hasNext())
{
numRows++;
HSSFRow HSSFRow=(HSSFRow)行迭代器.next();
迭代器迭代器=hssfRow.cellIterator();
List celltemplast=newarraylist();
如果(numRows==1)
{
minColIndex=hssfRow.getFirstCellNum();
maxColIndex=hssfRow.getLastCellNum();
numCols=maxColIndex;
}
对于(int-colIndex=minColIndex;colIndex 对于(i=0;i而言,这实际上与此处的许多其他问题相同,例如
答案如下:
POI将为您提供Excel存储在文件中的确切值。通常,如果您在Excel单元格中写入数字,Excel会将其存储为带格式的数字。如果您需要,POI将为您提供格式设置支持(大多数人不需要-他们希望数字作为数字,以便使用)
你要找的类是。你的代码应该是
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) );
}
}
嗨,我的解决方案就是放上这个符号:
'
在每个数字前面。然后数字被处理为文本
完成此操作后,您将看到小绿色三角形和警告:
对我来说,这不是问题,因为它可以工作。不要将所有内容都视为字符串。请使用最合适的类型,例如BigDecimal、Integer。excel数据不会特定于表,它可能包含任何引用游标的数据,因此无法预先确定列的内容和数据类型。当我浏览网络时,我发现Formatter可以用来解决这个问题,但不确定这里需要做什么。一个足够聪明的系统知道应该忽略带有尾随“.0”的字符串听起来并不聪明,它听起来很糟糕:)我对Junit和POI很陌生,我可以使程序足够智能,但这将涉及合并逻辑,这将使程序变得复杂,我不想重新发明轮子,以防POI中已经存在为我们提供的功能