简体   繁体   English

Apache POI 字符串公式错误

[英]Apache POI string formula error

I use Apache POI to create Excel file with string formula and get #VALUE!我使用 Apache POI 创建带有字符串公式的 Excel 文件并获得 #VALUE! error in result file.结果文件中的错误。

Source code is源代码是

public static void main(String[] args) throws IOException {
    Workbook wb = new HSSFWorkbook();
    FormulaEvaluator ev = wb.getCreationHelper().createFormulaEvaluator();
    Sheet ws = wb.createSheet();
    Row row = ws.createRow(0);
    Cell cell;
    cell = row.createCell(0);
    cell.setCellValue("abc");

    cell = row.createCell(1);
    printCellInfo(row.getCell(1),"before formula");
    cell.setCellFormula("IF(A1<>\"\",MID(A1,1,2),\" \")");

    printCellInfo(row.getCell(1), "after formula");
    ev.evaluateAll();
    printCellInfo(row.getCell(1), "after recalc");

    OutputStream os = new FileOutputStream("xx.xls");
    wb.write(os);

}

( printCellInfo is info method I tell below) and the error cell is B1. printCellInfo是我在下面讲述的信息方法),错误单元格是 B1。 Excel says " value used in cell has incorrect data type ". Excel 显示“单元格中使用的值具有不正确的数据类型”。 BUT if open this cell to edit (using F2) and press Enter - formula calculates correctly!!!但是,如果打开此单元格进行编辑(使用 F2)并按 Enter - 公式计算正确!!!

Now about cell info.现在关于细胞信息。 Helping method is帮助方法是

static void printCellInfo(Cell cell, String infoText) {
    System.out.println("Cell "+CellReference.convertNumToColString(cell.getColumnIndex())+cell.getRowIndex()+" "+infoText);
    int ct = cell.getCellType();
    System.out.println("  Cell type "+ct);
    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        ct = cell.getCachedFormulaResultType();
        System.out.println("  Cached type "+ct);
    }
    switch (ct) {
        case Cell.CELL_TYPE_NUMERIC: 
            System.out.println("  Number <"+cell.getNumericCellValue()+">");
            break;
        case Cell.CELL_TYPE_STRING:
            System.out.println(" String <"+cell.getStringCellValue()+">");
    }
    System.out.println("============================");
}

and output of main execution is主要执行的输出是

Cell B0 before formula
  Cell type 3
============================
Cell B0 after formula
  Cell type 2
  Cached type 0
  Number <0.0>
============================
Cell B0 after recalc
  Cell type 2
  Cached type 1
 String <ab>
============================

So B0 is BLANK at first and NUMBER after formula insertion.所以 B0 首先是空白,公式插入后是 NUMBER。 Why POI do so and maybe problem starts here?为什么 POI 这样做,也许问题从这里开始? Unfortunately, correct datatype in POI after recalculation does not become correct in excel.不幸的是,重新计算后 POI 中的正确数据类型在 excel 中并不正确。

By the way, formulas set from POI IF(A1<>"","A","B") or MID(A1,1,2) are calculated correct.顺便说一下,从 POI IF(A1<>"","A","B") 或 MID(A1,1,2) 设置的公式计算正确。 And B1=MID(A1,1,2) and C1=IF(A1<>"",B1," "), set from POI is calculated correct true.并且 B1=MID(A1,1,2) 和 C1=IF(A1<>"",B1," "),从 POI 设置的计算是正确的。

Any ideas?有任何想法吗? I found the only topic https://openclassrooms.com/forum/sujet/apache-poi-formula-valeur over the internet, there are some ideas, but no working solution...我在互联网上找到了唯一的主题https://openclassrooms.com/forum/sujet/apache-poi-formula-valeur ,有一些想法,但没有可行的解决方案......

cell = row.getCell(1);
cell.setCellValue(cell.getStringCellValue());

does not help too.也无济于事。

I tried your code on my machine and it is working fine.我在我的机器上试过你的代码,它工作正常。

I am using beta POI 3.13 on Excel for mac 2013我在 Excel for mac 2013 上使用 beta POI 3.13

Your excel sheet is also working fine (after you gave it to me in chat)你的 excel 表也工作正常(在你在聊天中给我之后)

so the problem is wrong compatibilities between your POI version and you Excel 2010 libraries.所以问题是您的 POI 版本和 Excel 2010 库之间的兼容性错误。

This is your sheet as well.这也是你的工作表。

在此处输入图片说明

I had the same problem recently.我最近遇到了同样的问题。 I solved it with this line before saving my workbook :在保存我的工作簿之前,我用这一行解决了它:

myWorkbook.getCreationHelper().createFormulaEvaluator().evaluateAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM