EPPlus not caluculating formula output after binding

感情迁移 提交于 2019-12-22 09:39:18

问题


I am using EPPlus. I am stuck at cell formulas.

My code is below:

ExcelPackage pck = new ExcelPackage(@"D:\MYSheets\EmptyFile.xlsx");
var ws = pck.Workbook.Worksheets["MySheet"];

ws.Cells["A3"].Value = "2.3";
ws.Cells["A4"].Value = "10.2";

ws.Cells["A5"].Formula = "=SUM(A3:A4)";
ws.Cells["A5"].Style.Numberformat.Format = "#,##0.00";
pck.Save();

When I open Excel, by default, A5 cell is not calculating the sum of A3 and A4. Unless I modify the A3 and/or A4 cells, the A5 cell remains not calculated.

I tried using the following code but it didn't work for me:

ws.Workbook.CalcMode = ExcelCalcMode.Automatic;

回答1:


Try:

ws.Cells["A3"].Value = 2.3;
ws.Cells["A4"].Value = 10.2;

You were telling EPPlus to store the values as a string, that's why the formula failed.




回答2:


also replace

ws.Cells["A5"].Formula = "=SUM(A3:A4)";

with

ws.Cells["A5"].Formula = "SUM(A3:A4)";

to result value of A5




回答3:


perform Calculate() on the sheet, workbook or cell to ensure the formula executes. https://epplus.codeplex.com/wikipage?title=About%20Formula%20calculation



来源:https://stackoverflow.com/questions/17068387/epplus-not-caluculating-formula-output-after-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!