【问题标题】:Copy cell value of formula POI复制公式 POI 的单元格值
【发布时间】: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);
 }

感谢你们的帮助!

【问题讨论】:

标签: java arrays excel apache-poi


【解决方案1】:

不要使用cell.toString(),使用单元格值。

根据 Cell API:Apache POI Javadoc

选项是getBooleanCellValue(), getDateCellValue(), getErrorCellValue(), getNumericCellValue(), getRichStringCellValue(), getStringCellValue()

【讨论】:

  • 编辑澄清
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多