【发布时间】:2018-11-27 21:45:24
【问题描述】:
我正在尝试将 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);
}
感谢你们的帮助!
【问题讨论】:
-
最佳实践是使用
DataFormatter和FormulaEvaluator: DataFormatter.formatCellValue(Cell cell, FormulaEvaluator evaluator) -
刚刚解决了,代码贴在main中。谢谢;)
标签: java arrays excel apache-poi