Skip to main content

IXlCell.SetSharedFormula(XlCellPosition) Method

Creates the shared formula based on the formula contained in the host cell.

Namespace: DevExpress.Export.Xl

Assembly: DevExpress.Printing.v24.1.Core.dll

NuGet Package: DevExpress.Printing.Core

Declaration

void SetSharedFormula(
    XlCellPosition hostCell
)

Parameters

Name Type Description
hostCell XlCellPosition

An XlCellPosition structure that determines a cell which contains the host formula.

Example

It takes two steps to build a shared formula. First, add a formula to the first cell in a range - it is the host formula. Next, add a reference to the host formula to any other cell of the range. The IXlCell.SetSharedFormula method with different parameters serves both steps, as illustrated in the code below.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/excel-export-api-examples

// Create data rows.
for (int i = 0; i < 4; i++) {
    using (IXlRow row = sheet.CreateRow()) {
        using (IXlCell cell = row.CreateCell()) {
            cell.Value = product[i];
        }
        using (IXlCell cell = row.CreateCell()) {
            cell.Value = qty[i];
        }
        using (IXlCell cell = row.CreateCell()) {
            cell.Value = price[i];
        }
        using (IXlCell cell = row.CreateCell()) {
            // Use the shared formula to calculate the amount per product. 
            if (i == 0)
                cell.SetSharedFormula("B2*C2", XlCellRange.FromLTRB(3, 1, 3, 4));
            else
                cell.SetSharedFormula(new XlCellPosition(3, 1));
        }
    }
}
See Also