简体   繁体   English

列不执行公式Excel / C#/ EPPlus

[英]Column doesn't execute formula Excel/C#/EPPlus

I have this excel sheet I produce right out of a C#.NET application with EPPlus and I have a formula that won't execute unless I click manually on the cell and press enter. 我有这张Excel工作表,是我使用EPPlus从C#.NET应用程序中直接生成的,并且只有在单元格上手动单击并按Enter时,该公式才会执行。

This is my current output (First value being F column and second being G) : 这是我当前的输出(第一个值为F列,第二个为G列):

02:10:58    04:30:00    =F1/G1
01:50:52    04:06:00    =F2/G2

This is what it should look like: 它应该是这样的:

02:10:58    04:30:00    0,485061728
01:50:52    04:06:00    0,450677507

The values on C# side are initialized as strings and that's probably what's causing the error : C#端的值初始化为字符串,这可能是导致错误的原因:

string performance = "=F" + (list.Count + 2) + "/G" + (list.Count + 2)

Is there a workaround or another way to initialized my column in order to show directly the value to user? 是否有解决方法或其他方式来初始化我的列,以便直接向用户显示值?

Thanks! 谢谢!

EDIT 编辑

Code that creates the excel file 创建Excel文件的代码

ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Informations_OF");
worksheet.Cells["A1"].LoadFromCollection(list.ToListExport(), true);
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();

It looks like you're probably setting the Value property of the cell in your C# code. 看来您可能正在C#代码中设置单元格的Value属性。 Try setting the Formula property instead. 尝试改为设置Formula属性。 Oh, and drop the leading "=" in your formula definition - the documentation for EPPlus states that you shouldn't put it in there. 哦,在公式定义中添加前导“ =”-EPPlus的文档指出您不应将其放在此处。

Updated with code sample 更新了代码示例

var path = @"C:\Temp\EPPlus Demo.xlsx";

var fi = new FileInfo(path);
if (fi.Exists)
    fi.Delete();

using (var pck = new ExcelPackage())
{
    var wb = pck.Workbook;
    var ws = wb.Worksheets.Add("Demo");
    ws.Cells["A1"].LoadFromCollection(list.ToListExport(), true);
    for (var r = 2; r <= ws.Dimension.Rows; r++)
    {
        ws.Cells[r, 8].Formula = $"F{r}/G{r}";
        // Or alternatively, using R1C1 format
        ws.Cells[r, 8].FormulaR1C1 = "RC[-2]/RC[-1]";
    }
    pck.SaveAs(fi);
}

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

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