4

我正在使用 EPPlus 将一些信息导出到 excel 中,并且在使用公式的总计行中遇到了一个绊脚石。打开文件时只计算第一个公式,其他公式只在我双击进入和离开公式单元格时计算。下面是构建表的代码和结果。希望有人可以为我提供一些有关如何解决此问题的见解。

    private void BuildSalesTable(string label, ISalesTable table)
    {
        var sheet = ExcelPackage.Workbook.Worksheets.Add(string.Format("Sales{0}", label));

        //table header
        //BuildHeader(startRow, 2, startRow, 9, string.Format("Sales Tons - {0}", label));

        //table lables
        sheet.Cells[1, 2].Value = "Joist Qtd";
        sheet.Cells[1, 3].Value = "PP/Ton";
        sheet.Cells[1, 4].Value = "Deck Qtd";
        sheet.Cells[1, 5].Value = "PP/Ton";
        sheet.Cells[1, 6].Value = "Joist Sold";
        sheet.Cells[1, 7].Value = "PP/Ton";
        sheet.Cells[1, 8].Value = "Deck Sold";
        sheet.Cells[1, 9].Value = "PP/Ton";

        for (int i = 0; i < table.Rows.Count; i++)
        {
            var row = 2 + i;

            //every other row coloring
            if (i == 0 || i%2 == 0)
            {
                sheet.Cells[row, 1, row, 9].Style.Fill.PatternType = ExcelFillStyle.Solid;
                sheet.Cells[row, 1, row, 9].Style.Fill.BackgroundColor.SetColor(Color.Khaki);
            }

            //data
            sheet.Cells[row, 1].Value = table.Rows[i].Location;
            sheet.Cells[row, 2].Value = table.Rows[i].JoistQuoted;
            sheet.Cells[row, 3].Value = table.Rows[i].JoistQuotedPP;
            sheet.Cells[row, 4].Value = table.Rows[i].DeckQuoted;
            sheet.Cells[row, 5].Value = table.Rows[i].DeckQuotedPP;
            sheet.Cells[row, 6].Value = table.Rows[i].JoistSold;
            sheet.Cells[row, 7].Value = table.Rows[i].JoistSoldPP;
            sheet.Cells[row, 8].Value = table.Rows[i].DeckSold;
            sheet.Cells[row, 2].Value = table.Rows[i].JoistQuoted;
            sheet.Cells[row, 9].Value = table.Rows[i].DeckSoldPP;
        }

        //totals row
        sheet.Cells[table.Rows.Count + 2, 1].Value = "Total";
        sheet.Cells[table.Rows.Count + 2, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        sheet.Cells[table.Rows.Count + 2, 1].Style.Font.Color.SetColor(Color.Gray);

        sheet.Cells[table.Rows.Count + 2, 2].Formula = string.Format("=SUM(B2:B{0})", table.Rows.Count + 1);
        sheet.Cells[table.Rows.Count + 2, 3].Value = string.Format("=SUMPRODUCT(B2:B{0},C2:C{0})/SUM(B2:B{0})", table.Rows.Count + 1);
        sheet.Cells[table.Rows.Count + 2, 4].Value = string.Format("=SUM(D2:D{0})", table.Rows.Count + 1);
        sheet.Cells[table.Rows.Count + 2, 5].Value = string.Format("=SUMPRODUCT(D2:D{0},E2:E{0})/SUM(D2:D{0})", table.Rows.Count + 1);
        sheet.Cells[table.Rows.Count + 2, 6].Value = string.Format("=SUM(F2:F{0})", table.Rows.Count + 1);
        sheet.Cells[table.Rows.Count + 2, 7].Value = string.Format("=SUMPRODUCT(F2:F{0},G2:G{0})/SUM(F2:F{0})", table.Rows.Count + 1);
        sheet.Cells[table.Rows.Count + 2, 8].Value = string.Format("=SUM(H2:H{0})", table.Rows.Count + 1);
        sheet.Cells[table.Rows.Count + 2, 9].Value = string.Format("=SUMPRODUCT(H2:H{0},I2:I{0})/SUM(H2:H{0})", table.Rows.Count + 1);
        sheet.Cells[table.Rows.Count + 2, 2, table.Rows.Count + 2, 9].Style.Font.Color.SetColor(Color.DarkBlue);


        //format data
        sheet.Cells[2, 1, 2 + table.Rows.Count, 10].Style.Numberformat.Format = "#,##0";
        sheet.Workbook.CalcMode = ExcelCalcMode.Automatic;
    }

Excel 看起来像这样:

excel文档的临时图片

4

1 回答 1

8

我是一个白痴。我正在为未运行公式的单元格设置值,而不是设置公式。我不敢相信我花了多长时间才看到这个。

于 2013-08-01T13:44:06.473 回答