我已经读过Microsoft.Office.Interop.Excel是用Excel中的值替换公式的最简单方法,但它需要安装Office。由于我需要在Windows Server(2008或2012)上进行部署,因此我正在寻找最好的和/或最简单的方法来使用EPPlus来完成此任务。
公式的添加已得到充分证明,例如
currentWorksheet.Cells["C4"].Formula = "SUM(C2:C3)";
Run Code Online (Sandbox Code Playgroud)
但是我找不到任何用公式的等效值替换整个公式工作表的示例。基本上,在Excel中是“复制”后面是“选择性粘贴”选项。
我认为Epplus内置没有任何功能可以为您量身定制。但是您可以利用以下事实:的Cells集合Worksheet仅包含具有内容的单元格的条目。所以这样的事情在性能方面应该不会太痛苦:
currentWorksheet.Cells["C2"].Value = 5;
currentWorksheet.Cells["C3"].Value = 15;
currentWorksheet.Cells["C4"].Formula = "SUM(C2:C3)";
currentWorksheet.Cells["D2"].Value = 15;
currentWorksheet.Cells["D3"].Value = 25;
currentWorksheet.Cells["D4"].Formula = "SUM(D2:D3)";
//Calculate the formulas and the overwrite them with their values
currentWorksheet.Cells.Calculate();
foreach (var cell in currentWorksheet.Cells.Where(cell => cell.Formula != null))
cell.Value = cell.Value;
Run Code Online (Sandbox Code Playgroud)