Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 226
vbasic321 Avatar asked Jun 10 '11 16:06

vbasic321


1 Answers

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

like image 90
JonoW Avatar answered Sep 28 '22 23:09

JonoW