我正在尝试将 Excel 文件的单元格值复制到另一个 Excel 文件。 但是,源文件包含公式,当我获取单元格时,我得到公式,在最终文件中我得到公式而不是值。
for (int j = 0; j < cellSource[0].length; j++)
{ //For each columns of source
for (int i = 0; i < cellFinal[0].length ; i++)
{ //For each columns of final
if (cellSource[0][j].toString().equals(cellFinal[0][i].toString()))
{ //Checking is Equals
for (int p = 0; p < cell.length; p++)
{ //If their are equals, replace the data
cellFinal[p][i].setCellValue(cellSource[p][j].toString());
//Replace the value with the Formula of the main
}
}
}
}
我尝试了一些 getCachedFormulaResultType()
但它不起作用..
Cell cell = rowFileDa.getCell();
switch (cell .getCellType()) {
case FORMULA:
fileDa[i][j] = cell.getCachedFormulaResultType().toString();
break;
//Return NUMERIC
}
谢谢!
解决了:
cellValue = evaluator.evaluate(cellSource[p][j]);
String tmp = cellValue.formatAsString();
tmp = tmp.replace("\"", "");
if (isNumeric(tmp) == false) {
cell[p][i].setCellValue(tmp);
} else {
double tmpBis = Double.valueOf(tmp);
cell[p][i].setCellValue(tmpBis);
}
感谢各位的帮助!
最佳答案
不要使用cell.toString()
,使用单元格值。
根据 Cell API:Apache POI Javadoc
选项为getBooleanCellValue()、getDateCellValue()、getErrorCellValue()、getNumericCellValue()、getRichStringCellValue()、getStringCellValue()
。
关于java - 复制公式 POI 的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53508662/