Is there any way to set both the Value
& Formula
properties of a cell so that they persist into the written XLSX file?
Since EPPlus does not calculate the formulas (it relies on excel to do so), I am pre-calculating the result for the user. This is so that if they do not open the file, but then re-upload it, the 'value' is not null. The formulas are really simple.
Note: sheet.SetValue()
and .Value
both reset the Formula property of the ExcelCell
class, so .Value
must be called first. See the source code for ExcelCell.SetRichText()
to see it happening.
Here is almost the exact code from the test case:
var package = new ExcelPackage(); // and setup code
var sheet = package.Workbook.Worksheets.First();
sheet.Cells["A2"].Value = 10;
sheet.Cells["A2"].Formula = "=A1 * 3";
// Cell is OK now. shows both ".Value" and ".Formula"
using (var s = new MemoryStream()) {
package.Save(s);
var newPackage = new ExcelPackage(s);
var newSheet = newPackage.Workbook.Worksheets.First();
var cell = newSheet.Cells["A2"];
// cell.Value == null
// cell.Formula = "=A1 * 3"
}