简体   繁体   English

如何将求和公式添加到Excel中的单元格

[英]How To Add Sum formula to cell in Excel

I am devloping a exe application using C# language.I would like to write a sum formula to a cell called Total, so that user may key in extra information in the excel generated by system and it is automatically reflected in the cellcalled total. 我正在开发使用C#语言的exe应用程序。我想在一个名为Total的单元格中编写一个求和公式,以便用户可以在系统生成的Excel中键入额外的信息,并将其自动反映在称为total的单元格中。

Below is the format of the excel file generated by system 以下是系统生成的excel文件的格式

Group1 ItemA Qty1(generated in system) 组1项A数量1(在系统中生成)

                ItemB      Qty2(generated in system)

                ItemC        Qty3(key in by user)

               Total           Sum of (Qty1+Qty2+Qty3) .Formula=Sum(C1:C3)

Group2 until group n 第2组,直到第n组

I have try with the code 我尝试使用代码

       objExcel.Cells[iRow, 3].Formula = "=SUM(C1:C3)";

but it is not working.It only sum the value from cell C1 to C3, but the formula is not written to the cells. 但是它不起作用。它仅将单元格C1到C3中的值相加,但公式未写入单元格中。

When user updating qty for itemC, it is not reflected in the total cell. 用户更新itemC的数量时,它不会反映在总单元格中。

Does anyone has idea on this? 有人对此有想法吗?

have you tried locking the cells eg. 您是否尝试过锁定单元格,例如。 SUM($C$1:$C$3), either than that I've also had little luck with excel programming. SUM($ C $ 1:$ C $ 3),或者说我对excel编程也不太满意。

Try the below. 请尝试以下。

string formula = string.Format("=HYPERLINK(\"#{0}!{1}\", \"{2}\")", worksheet, cellRef, string.Format("Hyperlink number {0}", count));

More information can be found in the following link which I think could be a possible duplicate. 在以下链接中可以找到更多信息,我认为这可能是重复的。

Possible Duplicate 可能重复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM