为什么Excel公式没有制定?

时间:2016-08-22 22:32:07

标签: c# excel-formula sum epplus epplus-4

我正在使用EPPlus创建电子表格。我试图用这个代码得到一个公式来计算一个值的总和:

using (var totalOccurrencesCell = priceComplianceWorksheet.Cells[rowToPopulate, 2])
{
    totalOccurrencesCell.Style.Font.Size = DATA_FONT_SIZE;
    totalOccurrencesCell.Style.Numberformat.Format = NUMBER_FORMAT_THOUSANDS;
    if (rowToPopulate <= SUMMARY_HEADING_ROW + 1)
    {
        totalOccurrencesCell.Value = "0";
    }
    else
    {
        //totalOccurrencesCell.Formula = string.Format("SUM(B5:B{0})", rowToPopulate - 1);
        // TODO: Comment out or remove below after finding out why the above is not working
        totalOccurrencesCell.Formula = "SUM(B5:B19)";
        totalOccurrencesCell.Calculate();
    }
}

我认为正确的是应用于该单元格,如此处所示,即&#34; = SUM(B5:B19)&#34;:

enter image description here

那么为什么&#34; 0&#34;结果?对于C列也是如此,由于某些原因,D也会因为某些原因而出现问题。

这个类似的代码 在工作表的其他地方工作:

using (var totalVarianceCell = priceComplianceWorksheet.Cells[rowToPopulate, DETAIL_TOTALVARIANCE_COL])
{
    totalVarianceCell.Style.Font.Size = DATA_FONT_SIZE;
    totalVarianceCell.Style.Numberformat.Format = NUMBER_FORMAT_CURRENCY;
    totalVarianceCell.Formula = string.Format("SUM(J{0}:J{1})", _firstDetailDataRow, rowToPopulate - 1);
    totalVarianceCell.Calculate();
}

它将J(10)列的适当范围内的值相加,并且当点击&#34; sum&#34;单元格,它表示&#34; = SUM(J23:J39)&#34;作为那里的价值。

为什么它会在一个案例中起作用,但在其他案例中失败?

注意:我正在填充单元格(&#34; total&#34;是int):

totalOccurrencesCell.Value = total.ToString("N0", CultureInfo.CurrentCulture);

更新

这是不优雅的,有点令人失望,但至少现在,至少,我必须蛮力地强迫它&#34;这样:

totalOccurrencesCell.Value = SumCellVals(2, 5, rowToPopulate - 1);
. . .
private string SumCellVals(int colNum, int firstRow, int lastRow)
{
    double runningTotal = 0.0;
    double currentVal;
    for (int i = firstRow; i <= lastRow; i++)
    {
        using (var taterTotCell = priceComplianceWorksheet.Cells[i, colNum])
        {
            currentVal = Convert.ToDouble(taterTotCell.Value);
            runningTotal = runningTotal + currentVal;
        }
    }
    return runningTotal.ToString();
}

1 个答案:

答案 0 :(得分:1)

行为的线索在你的问题的句子中:

  

注意:我正在填充单元格(&#34; total&#34;是int):

totalOccurrencesCell.Value = total.ToString("N0", CultureInfo.CurrentCulture);

在将数值赋值给单元格的Value属性之前,无需将数值转换为字符串。

我可以通过运行此MCVE来重现您所看到的内容:

static void test4()
{
    var fs = new System.IO.FileInfo(@"c:\temp\test4.xlsx");

    if (fs.Exists) fs.Delete();

    using (ExcelPackage package = new ExcelPackage(fs))
    {

        ExcelWorksheet worksheet = package.Workbook.Worksheets["Test"];
        if (worksheet == null) worksheet = package.Workbook.Worksheets.Add("Test");
        var WS = worksheet;
        WS.Workbook.CalcMode = ExcelCalcMode.Manual;

        // strings with .
        WS.Cells[1, 1].Value = "100.0";
        WS.Cells[2, 1].Value = "42.1";
        using (var sum = WS.Cells[3, 1])
        {
            sum.Formula = "SUM(A1:A2)";
            sum.Calculate();
        }

        // strings with ,
        WS.Cells[1, 2].Value = "100,0";
        WS.Cells[2, 2].Value = "42,1";
        using (var sum = WS.Cells[3, 2])
        {
            sum.Formula = "SUM(B1:B2)";
            sum.Calculate();
        }

        // strings with ,
        WS.Cells[1, 3].Value = "1,100";
        WS.Cells[2, 3].Value = "42";
        using (var sum = WS.Cells[3, 3])
        {
            sum.Formula = "SUM(C1:C2)";
            sum.Calculate();
        }

        // let EPPLUS handle it 
        WS.Cells[1, 4].Value = 100; // int
        WS.Cells[2, 4].Value = 42.1d; // double
        using (var sum = WS.Cells[3, 4])
        {
            sum.Formula = "SUM(D1:D2)";
            sum.Calculate();
        }

        package.Save();
    }
}

这是我在Excel表格中得到的结果:

result in excel

您注意到某些单元格有错误指示符,但有些单元格没有。很容易被这个误导。此效果受分隔符设置的影响:

separators in Excel Options

在我的测试中,总是计算D列,无论我如何更改千位和小数分隔符,总是计算SUM。特别是如果要转换为字符串,使用当前文化并不能保证这将起作用,因为Excel中的设置可能已被过度使用。

tl; dr; 不要将数字(int,double,float)转换为字符串,而是将它们按原样分配给单元格的值。 EPPLUS和Excel都将处理正确的表示。