C# 为什么我会得到;0“;最后一列中的值;“每月总金额”是多少;?

C# 为什么我会得到;0“;最后一列中的值;“每月总金额”是多少;?,c#,.net,excel,export-to-excel,epplus,C#,.net,Excel,Export To Excel,Epplus,得到零作为计算值和公式也存在。 我的目标是根据员工ID按金额分组。 我想对最后一列使用自动计算方法,但当我尝试创建文件时,它显示为零 公式也适用于相应的“0”值字段 我正在使用EPPLUS 4.0并设置公式: 类似于代码 如果(“+currentCell+”=“+previousCell+”,\“\”,SUMIF(B:B,”+ currentCell+“,I:I))” 值不是第一个“0”,而是“18000” 而不是第二个“0”值将是“32987” 等等 using (ExcelPackage e

得到零作为计算值和公式也存在。 我的目标是根据员工ID按金额分组。 我想对最后一列使用自动计算方法,但当我尝试创建文件时,它显示为零

公式也适用于相应的“0”值字段

我正在使用EPPLUS 4.0并设置公式:

类似于代码

如果(“+currentCell+”=“+previousCell+”,\“\”,SUMIF(B:B,”+ currentCell+“,I:I))”

值不是第一个“0”,而是“18000” 而不是第二个“0”值将是“32987” 等等

using (ExcelPackage excel = new ExcelPackage())
            {


                ExcelWorksheet workSheet = excel.Workbook.Worksheets.Add("Sheet1");
                excel.Workbook.CalcMode = ExcelCalcMode.Automatic;
                workSheet.TabColor = System.Drawing.Color.Black;
                workSheet.DefaultRowHeight = 12;
                //Header of table  
                //  
                workSheet.Row(1).Height = 20;
                workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                workSheet.Row(1).Style.Font.Bold = true;

                List<string> employeeIds = new List<string>();
                int cntrSameEmployeeId = 1;
                int cntrFormulaManipulate = 0;

                ds.Tables[0].Columns.RemoveAt(0);


                for (int i = 1; i < ds.Tables[0].Columns.Count + 1; i++)
                {
                    workSheet.Cells[1, i].Value = ds.Tables[0].Columns[i - 1].ColumnName;
                }

                int setLastColumnName = ds.Tables[0].Columns.Count + 1;
                workSheet.Cells[1, setLastColumnName].Value = "Total_Monthly_Amount";

                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
                    {
                        if (employeeIds.Contains(Convert.ToString(ds.Tables[0].Rows[j].ItemArray[k])))
                        {
                            cntrSameEmployeeId++;
                        }
                        if (k + 1 == 2)
                        {
                            if (!employeeIds.Contains(Convert.ToString(ds.Tables[0].Rows[j].ItemArray[k])) && employeeIds.Count != 0)
                            {
                                cntrSameEmployeeId++;
                            }
                            employeeIds.Add(Convert.ToString(ds.Tables[0].Rows[j].ItemArray[k]));
                        }
                        if (k == (ds.Tables[0].Columns.Count - 1))
                        {
                            if (cntrFormulaManipulate == 0)
                            {
                                workSheet.Cells[j + 2, ds.Tables[0].Columns.Count + 1].Formula = "IF(B2=B1,\"\",SUMIF(B:B,B2,I:I))";
                                cntrFormulaManipulate++;
                            }
                            else
                            {
                                string currentCell = "B" + (cntrSameEmployeeId + 1).ToString();
                                string previousCell = "B" + cntrSameEmployeeId.ToString();
                                string maniPulateFormula = "IF(" + currentCell + "=" + previousCell + ",\"\",SUMIF(B:B," + currentCell + ",I:I))";
                                workSheet.Cells[j + 2, ds.Tables[0].Columns.Count + 1].Formula = maniPulateFormula;
                                //workSheet.Cells[j + 2, ds.Tables[0].Columns.Count + 1].Style.Border.Bottom.Style = ExcelBorderStyle.Double;
                                //workSheet.Cells[j + 2, ds.Tables[0].Columns.Count + 1].Style.Border.Bottom.Color.SetColor(Color.Red);
                            }
                        }
                        workSheet.Cells[j + 2, k + 1].Value = Convert.ToString(ds.Tables[0].Rows[j].ItemArray[k]);

                    }

                }

                //workSheet.Cells["A27"].Formula = "=SUM(B2:B10)";

                workSheet.Cells.AutoFitColumns();

                excel.Workbook.Calculate();

                FileInfo fi = new FileInfo(path1 + @"\FormulaExample.xlsx");
                excel.SaveAs(fi);

            }
使用(ExcelPackage excel=new ExcelPackage())
{
excel工作表=excel.Workbook.Worksheets.Add(“Sheet1”);
excel.Workbook.CalcMode=ExcelCalcMode.Automatic;
workSheet.TabColor=System.Drawing.Color.Black;
workSheet.DefaultRowHeight=12;
//表头
//  
工作表。第(1)行。高度=20;
工作表.Row(1).Style.HorizontalAlignment=ExcelHorizontalAlignment.Center;
工作表.Row(1).Style.Font.Bold=true;
List employeeIds=new List();
int cntrsamememployeeid=1;
int CNTRFORMULA=0;
表[0].Columns.RemoveAt(0);
对于(int i=1;i
是的。我刚刚将金额字段值的ToString()转换为Int32()

workSheet.Cells[j + 2, k + 1].Value = Convert.ToInt32(ds.Tables[0].Rows[j].ItemArray[k]);

您的数字存储为文本,SUMIF无法添加文本。@ScottCraner您能否举例说明,以便我能更快地理解您想说什么。因为“Microsoft.Office.Interop.Excel”中也有同样的逻辑,对不起,我不知道如何修复您的代码,不知道C。我知道公式,因为你的数字是以文本形式输入的(悬停在绿色三角形上,你会得到信息),所以SUMIF会忽略它们。你需要将存储为文本的数字转换为实数,公式才能运行。非常感谢。我明白你的意思