java - 复制公式 POI 的单元格值

标签 java arrays excel apache-poi

我正在尝试将 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/

相关文章:

java - 如何使用正则表达式从该行中提取子字符串

java - className.main(new String[] {"filename.txt"})?

javascript - 如何通过属性使 Javascript 数组唯一

java - 如何使用 xpath 检索 xml 值?

java - 同时按空格、点和逗号分割字符串

java - IntelliJ 上的 Lombok @SuperBuilder 解决方法

python - 从另一个数组明显创建数组?

java - 将文件中的数据加载到二维数组中

excel - 从 FTP 服务器获取 excel 文件并保存在 Google Drive

excel - 在 Node.js 中解析 xls/xlsx/xlsm