EPPLUS 的公式问题

Tom*_*Tom 3 c# formulas epplus

尝试使用 EPPLUS 编写一个简单的公式,但似乎找不到解决方法。

公式如下:=C12-(SUM(D12:E12))

我尝试了各种组合,但不太确定哪个选项可以让我定义这样一个简单的公式。

它总是抱怨“-”不能应用于字符串字符串等。

任何想法如何编写这样的公式

worksheet.Cells[this.CurrRow, CurrColumn + 1].Formula = "SUBTOTAL" + (worksheet.Cells[this.CurrRow, iStart_column - 1]) - ("SUM(" + worksheet.Cells[this.CurrRow, iStart_column, this.CurrRow, CurrColumn]);
Run Code Online (Sandbox Code Playgroud)

Ern*_*e S 5

如果我理解您想要做什么,那么您正在动态生成公式。在这种情况下,您会收到错误,因为您正在对-字符串使用数学,当然,这实际上无法完成。我认为你想要的是这样的:

worksheet.Cells[this.CurrRow, CurrColumn + 1].Formula = "=SUBTOTAL("+ worksheet.Cells[this.CurrRow, iStart_column - 1].Address + "-(SUM(" + worksheet.Cells[this.CurrRow, iStart_column, this.CurrRow, CurrColumn].Address + ")))";
Run Code Online (Sandbox Code Playgroud)

请注意属性的使用Address,该属性将给出对象的字符串表示形式Cell。

注意:您的实现SUBTOTAL并不真正正确,Excel 在打开文件时会抱怨它。这更像是一个 Excel 问题,而不是 EPPlus,因此您必须解决该问题或发布更多详细信息。