使用EPPlus库在Excel中用它们的值replace公式

我读过Microsoft.Office.Interop.Excel将是最简单的方法来replaceExcel中的值,但它需要安装Office。 由于我将需要在Windows Server(2008或2012)上部署,因此我正在寻找使用EPPlus来完成该操作的最佳和/或最简单的方法。

添加公式是有据可查的,例如

currentWorksheet.Cells["C4"].Formula = "SUM(C2:C3)"; 

但是我找不到任何用等价值replace公式的整个工作表的例子。 基本上复制,然后在Excel中的select性粘贴选项。

我不认为Epplus里面有任何一种function可以为你做。 但是,您可以利用“ Worksheet的“ Cells集合仅包含具有内容的单元格的条目。 所以像这样的事情不应该太痛苦的performance明智:

 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;