2

POI 3.8 を使用して Excel を読み取っています。HSSF と XSSF の両方を読み取ることができる POI のユーザー モデル API を使用していますが、式の評価に問題があります POI 3.8 は Excle の IFERROR 関数をサポートしていません。古いバージョンの Excel ではサポートされていないため、数式を ISERROR に変換したくありません。

POI 3.8がIFERRORをサポートしていないことは知っていますが、それを行うにはどうすればよいですか-事前に感謝します

os this 以下は、スレッド「メイン」の例外です。 :356) org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:297) で org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:229) で org.apache. poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateFormulaCellValue(XSSFFormulaEvaluator.java:264) で org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluate(XSSFFormulaEvaluator.java:117) で TestFormula.cellValue(TestFormula.java:48) で TestFormula .loadCell(TestFormula.java:37) で TestFormula.loadRows(TestFormula.java:29) で TestFormula.testRun(TestFormula.java:22) FISValidator.main(FISValidator.java:27) で 原因: org.apache.poi.ss.formula.eval.NotImplementedException: org.apache.poi.ss.formula.atp.AnalysisToolPak$NotImplemented.evaluate で IFERROR( AnalysisToolPak.java:40) org.apache.poi.ss.formula.UserDefinedFunction.evaluate(UserDefinedFunction.java:64) org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:129) org .apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:491) org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:287)org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:129) の UserDefinedFunction.evaluate(UserDefinedFunction.java:64) org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java: 491) org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny (WorkbookEvaluator.java:287) でorg.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:129) の UserDefinedFunction.evaluate(UserDefinedFunction.java:64) org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java: 491) org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny (WorkbookEvaluator.java:287) で

4

2 に答える 2

6

編集:新しいApache POI APIがIFERROR()をサポートしているため、私の回答は古くなっています


最近、IFERRORでもこの問題が発生しました。そこで、ちょっとしたハックを書きました。お役に立てれば:

IFERROR(value, value_if_error)はIF(ISERROR(value), value_if_error, value) と同じように機能する ことに注意してください 。

そのため、評価する前にこれらの数式を置き換えました。シート全体を自動的に処理するreplaceIfErrorFormulas()を呼び出すだけです。

public static final int SIZE = "IFERROR(".length();   

private void replaceIfErrorFormulas(Sheet pSheet)
{
   for (Row row : pSheet)
   {
      for (Cell cell : row)
      {
         if ((cell != null) && 
             (cell.getCellType() == Cell.CELL_TYPE_FORMULA) &&
             (cell.getCellFormula().indexOf("IFERROR") != -1))
         {
            cell.setCellFormula(buildFormulaString(cell.getCellFormula()));
         }
      }
   }
}

private String buildFormulaString(String pFormula)
{
   if (pFormula.indexOf("IFERROR") == -1)
   {
      return pFormula;
   }

   String[] values = new String[2]; // will hold value, value_if_error
   char[] tokens = pFormula.toCharArray();
   int length = computeLength(pFormula); // length of IFERROR substring
   int iBegin = pFormula.indexOf("IFERROR");
   int iEnd = iBegin + length - 1;
   assert (iEnd < pFormula.length());      
   int iParam = 0;  // 0: value; 1: value_if_error
   int numPar = 0;  // number of parentheses

   // Split the parameters into VALUE and VALUE_IF_ERROR in values[]
   for (int i = iBegin; i < (length + iBegin) ; i++)
   {
      if (tokens[i] == '(')
      {
         values[iParam] += tokens[i];
         numPar++;
      }
      else if (tokens[i] == ')')
      {
         if (iParam < 1)
         {
            values[iParam] += tokens[i];
         }
         numPar--;
      }
      else if (Character.getType(tokens[i]) == Character.MATH_SYMBOL)
      {
         values[iParam] += tokens[i];
      }
      else if (tokens[i] == ',')
      {
         if (numPar > 1)
         {
            values[iParam] += tokens[i];
         }
         else
         {
            values[iParam++] += ')';
            numPar--;
         }
      }
      else
      {
         values[iParam] += tokens[i];
      }
      if (numPar < 0 && iParam == 1)
      {
         break;
      }
   }

   // Re-assign those parameters back to strings, removing the null character 
   String value        = values[0];
   String valueIfError = values[1];
   value        = value.substring(4 + SIZE - 1);
   valueIfError = valueIfError.substring(4);


   // Build new Formula that is equivalent to the old one.
   String newFormula = "IF(ISERROR(" + value + ")," 
                                     + valueIfError + ","
                                     + value +")";

   // Concatenate the untouched parts of the old formula to the new one      
   String left = pFormula.substring(0, iBegin);
   String right = pFormula.substring(iEnd + 1, pFormula.length());
   newFormula = left + newFormula + right;

   return buildFormulaString(newFormula);
}

// by checking the parentheses proceededing IFERROR, this method
// determines what is the size of the IFERROR substring
private int computeLength(String pFormula) 
{
   int length = SIZE;
   int numPar = 1; // Number of parentheses
   int iStart = pFormula.indexOf("IFERROR");
   char [] tokens = pFormula.toCharArray();

   for (int i = length + iStart; i < pFormula.length(); i++)
   {
      if (numPar == 0)
          break;
      if (tokens[i] == '(')
          numPar++;
      else if (tokens[i] == ')')
          numPar--;
      length++;
   }
   return length;
}

更新:式を大幅に改善しました! =D 前: 式が文字列の先頭にあり、それ以外に他の式やパラメーターがない場合にのみ、式が置き換えられます。後: 文字列全体で IFERROR のインスタンスを検索し、それらを ALL =D に置き換えます。

于 2012-12-04T08:56:02.997 に答える