Search code examples
c#.netexcelexport-to-excelepplus

Why I am getting "0" value in a last column "Total_Monthly_Amount"?


enter image description here

Getting zero as a calculated value and formula also is present. My moto is to group by Amount based on EmployeeId. I want to use Automatic calculation method for last column but when I try to create file it shows me zero.

FORMULA is also present for respective "0" value field.

I am using EPPLUS 4.0 and also setting formulas:

Like in code

"IF(" + currentCell + "=" + previousCell + ",\"\",SUMIF(B:B," + currentCell + ",I:I))";

Instead of first "0" value will be "18000" Instead of Second "0" value will be "32987" and so on.

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);

            }

Solution

  • Yes.I have just converted ToString() to Int32() for AMOUNT field value.

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