簡體   English   中英

刪除公式時出現 XmlValueDisconnectedException

[英]XmlValueDisconnectedException when remove Formula

我試圖用 java poi 讀取 Excel 文件。 我遍歷行,然后遍歷單元格。 要讀取單元格,我使用此方法:

  private String readCell(Cell cell) {
        try {
            switch (cell.getCellType()) {
                case NUMERIC:
                    if (format.isParseNumbersToInt()) {
                        return ((int) cell.getNumericCellValue()) + "";
                    } else {
                        return cell.getNumericCellValue() + "";
                    }
                case STRING:
                case _NONE:
                    return cell.getStringCellValue();
                case FORMULA:
                    if (format.isUseCashedFormulaValue()) {
                        cell.removeFormula();
                        return readCell(cell);
                    } else {
                        return cell.getCellFormula() + "";
                    }
                case BLANK:
                    return format.getBlankValue();
                case BOOLEAN:
                    return cell.getBooleanCellValue() + "";
                case ERROR:
                    if (format.isReadErrorCells()) {
                        return "ERROR_" + cell.getErrorCellValue();
                    } else {
                        return format.getErrorCellValue();
                    }
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("Failed to read cell: " + cell.getAddress(), e);
        }
        throw new IllegalStateException("Unknown CellType: " + cell.getCellType().name());
    }

在某一時刻 XmlValueDisconnectedException 拋出:

Caused by: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
    at org.apache.xmlbeans.impl.values.XmlObjectBase.check_dated(XmlObjectBase.java:1274)
    at org.apache.xmlbeans.impl.values.XmlObjectBase.getStringValue(XmlObjectBase.java:1529)
    at org.apache.poi.xssf.usermodel.XSSFCell.convertSharedFormula(XSSFCell.java:491)
    at org.apache.poi.xssf.usermodel.XSSFCell.getCellFormula(XSSFCell.java:469)
    at org.apache.poi.xssf.usermodel.XSSFSheet.onDeleteFormula(XSSFSheet.java:4654)
    at org.apache.poi.xssf.usermodel.XSSFCell.removeFormulaImpl(XSSFCell.java:571)
    at org.apache.poi.ss.usermodel.CellBase.removeFormula(CellBase.java:182)
    at de.heuboe.base.excel.controller.reader.ExcelReader.readCell(ExcelReader.java:356)
    ... 89 more

此點看起來與所有其他點相同。 文件要點:在此處輸入圖像描述

在 D219 中,以下單元格中的字符串是對上一排單元格的引用,例如 D220:“=D219”和 D221:“=D220”。 對於 E、F 和 G 列也是如此。

孔文件看起來像這樣並且可以工作,但是此時程序崩潰了。 我不知道為什么。

根據 StackTrace,共享公式存在問題。

如果您在D列中有公式=D6=D7=D8 、 ... =D219=D220 、 ... 等等,則並非所有單元格都會存儲完整的公式。 相反,只有一個單元格存儲完整的公式,后面的單元格只存儲對公式的共享引用。

OOXML中,這看起來像這樣:

在單元格D8XML中: <f ref="D8:D300" t="shared" si="1">D7</f>

在單元格D9:D300XML中: <ft="shared" si="1"/>

如果Excel操作包含此類共享公式的行,則此Excel行為往往很脆弱。

Cell.removeFormulaapache poi中的一個相當新的功能。 它可能是越野車。 但是,按照設計它應該了解此類共享公式並尊重這些公式。 因此,要獲得真正導致XmlValueDisconnectedException的原因,需要Excel文件。 可以查看工作表的XML並檢查共享公式的XML中的某些內容是否與XSSFCell.convertSharedFormula預期的默認值不同。

但是你真的需要Cell.removeFormula嗎? 因為如果目標只是獲取已兌現的公式值而不是公式字符串本身,但要避免評估,那么可以以與其他單元格值相同的方式獲得該兌現的公式值,但取決於緩存的公式結果類型。

例子:

...
case FORMULA:
 if (isUseCashedFormulaValue) {
  //cell.removeFormula();
  //return readCell(cell);
  switch (cell.getCachedFormulaResultType()) {
   case NUMERIC:
    return String.valueOf(cell.getNumericCellValue());
   case STRING:
    return cell.getStringCellValue();
   case BOOLEAN:
    return String.valueOf(cell.getBooleanCellValue());
   case ERROR:
    return "ERROR_" + cell.getErrorCellValue();
  }
 } else {
  return cell.getCellFormula();
 }
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM