Interop Excel范围公式赋值与string连接

当试图给Range.Formula分配一个串联的string,不起作用,但简单的string完美。

工作代码:

string formula = string.Format("=Round1!D{0} + Round2!D{0}", cellId); myRange.Formula = formula; 

失败代码:

 string formula = string.Format("=Round{0}!D{1}!", 1, cellId); for (int i = 2; i <= numRounds; i++) formula += string.Format(" + Round{0}!D{1}!", i, cellId); myRange.Formula = formula; 

¿WTF? 的xD

这使得无法生成配方dinamicly

先谢谢你。

 string formula = string.Format("=Round{0}!D{2} + Round{1}!D{2}", 1, 2 cellId); myRange.Formula = formula; 

如果工作簿中的工作表Round1Round3numRounds )按顺序排列,则可以使用三维引用 。 例如:

 = Sum( Round1:Round3!D3 ) 

这将是相同的

 = Round1!D3 + Round2!D3 + Round3!D3