Method SetFormula
SetFormula(int, int, string)
Sets a cell's formula. Its format is kept; it has no value until an application calculates it.
public void SetFormula(int row, int column, string formula)
Parameters
rowintThe one-based row.
columnintThe one-based column.
formulastringThe formula, with or without its leading
=, as Excel stores it: English function names, commas between arguments.
Remarks
The formula is stored as given and is not checked: one Excel cannot read makes Excel offer to repair the workbook. After the edit the cell reads back with this formula and a blank value.
Exceptions
- ArgumentNullException
formulais null.- ArgumentException
formulais empty or longer than the 8,192 characters a formula may be.- ArgumentOutOfRangeException
The row or the column is outside the worksheet grid.
- InvalidOperationException
The sheet is not a worksheet.
- UnsupportedFeatureException
The cell is part of an array formula entered over several cells, or is a header or totals cell of a table.
- InvalidDocumentException
The sheet's part is malformed.
SetFormula(string, string)
Sets a cell's formula by its reference. See SetFormula(int, int, string).
public void SetFormula(string reference, string formula)
Parameters
referencestringThe reference, such as
B4.formulastringThe formula, with or without its leading
=.
Exceptions
- ArgumentNullException
referenceorformulais null.- ArgumentException
referenceis not a cell reference inside the worksheet grid, or the formula is empty or too long.- InvalidOperationException
The sheet is not a worksheet.
- UnsupportedFeatureException
The cell is part of an array formula entered over several cells, or is a header or totals cell of a table.
- InvalidDocumentException
The sheet's part is malformed.