Excel – Using Open XML how do you insert a formula into an Excel 2010 worksheet

excelopenxmlopenxml-sdkvb.net

I'm using Visual Studio 2010 (VB.Net) and Open XML SDK 2.0. How do you insert a formula into an Excel 2010 worksheet? When I do this I also wish to set the CellValue property of the cell to a DBNull or EmptyString to force Excel to recalculate the cell when the user opens the workbok.

Best Answer

Just leave the CellValue as null, and instantiate a new CellFormula like so:

Cell cell = new Cell()
{
  CellReference = "A3",
  DataType = new EnumValue<CellValues>(CellValues.Number),
  CellFormula = "SUM(A1:A2)"
};

The cells value will be computed when the document is opened in Excel