コード例 #1
0
  /**
   * Set the cells type (numeric, formula or string)
   *
   * @throws IllegalArgumentException if the specified cell type is invalid
   * @see #CELL_TYPE_NUMERIC
   * @see #CELL_TYPE_STRING
   * @see #CELL_TYPE_FORMULA
   * @see #CELL_TYPE_BLANK
   * @see #CELL_TYPE_BOOLEAN
   * @see #CELL_TYPE_ERROR
   */
  public void setCellType(int cellType) {
    int prevType = getCellType();

    if (isPartOfArrayFormulaGroup()) {
      notifyArrayFormulaChanging();
    }
    if (prevType == CELL_TYPE_FORMULA && cellType != CELL_TYPE_FORMULA) {
      getSheet().getWorkbook().onDeleteFormula(this);
    }

    switch (cellType) {
      case CELL_TYPE_BLANK:
        setBlank();
        break;
      case CELL_TYPE_BOOLEAN:
        String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING;
        _cell.setT(STCellType.B);
        _cell.setV(newVal);
        break;
      case CELL_TYPE_NUMERIC:
        _cell.setT(STCellType.N);
        break;
      case CELL_TYPE_ERROR:
        _cell.setT(STCellType.E);
        break;
      case CELL_TYPE_STRING:
        if (prevType != CELL_TYPE_STRING) {
          String str = convertCellValueToString();
          XSSFRichTextString rt = new XSSFRichTextString(str);
          rt.setStylesTableReference(_stylesSource);
          int sRef = _sharedStringSource.addEntry(rt.getCTRst());
          _cell.setV(Integer.toString(sRef));
        }
        _cell.setT(STCellType.S);
        break;
      case CELL_TYPE_FORMULA:
        if (!_cell.isSetF()) {
          CTCellFormula f = CTCellFormula.Factory.newInstance();
          f.setStringValue("0");
          _cell.setF(f);
          if (_cell.isSetT()) _cell.unsetT();
        }
        break;
      default:
        throw new IllegalArgumentException("Illegal cell type: " + cellType);
    }
    if (cellType != CELL_TYPE_FORMULA && _cell.isSetF()) {
      _cell.unsetF();
    }
  }
コード例 #2
0
  private void setFormula(String formula, int formulaType) {
    XSSFWorkbook wb = _row.getSheet().getWorkbook();
    if (formula == null) {
      wb.onDeleteFormula(this);
      if (_cell.isSetF()) _cell.unsetF();
      return;
    }

    XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
    // validate through the FormulaParser
    FormulaParser.parse(formula, fpb, formulaType, wb.getSheetIndex(getSheet()));

    CTCellFormula f = CTCellFormula.Factory.newInstance();
    f.setStringValue(formula);
    _cell.setF(f);
    if (_cell.isSetV()) _cell.unsetV();
  }