为什么我在最后一列 "Total_Monthly_Amount" 中得到“0”值?
Why I am getting "0" value in a last column "Total_Monthly_Amount"?
作为计算值和公式得到零也存在。
我的本意是根据 EmployeeId 按金额分组。
我想对最后一列使用自动计算方法,但是当我尝试创建文件时它显示为零。
FORMULA 也存在于相应的“0”值字段中。
我用的是EPPLUS 4.0,还设置了公式:
如代码所示
"IF(" + 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);
}
Yes.I 刚刚将 ToString() 转换为 Int32() 以获得 AMOUNT 字段值。
workSheet.Cells[j + 2, k + 1].Value = Convert.ToInt32(ds.Tables[0].Rows[j].ItemArray[k]);
作为计算值和公式得到零也存在。 我的本意是根据 EmployeeId 按金额分组。 我想对最后一列使用自动计算方法,但是当我尝试创建文件时它显示为零。
FORMULA 也存在于相应的“0”值字段中。
我用的是EPPLUS 4.0,还设置了公式:
如代码所示
"IF(" + 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);
}
Yes.I 刚刚将 ToString() 转换为 Int32() 以获得 AMOUNT 字段值。
workSheet.Cells[j + 2, k + 1].Value = Convert.ToInt32(ds.Tables[0].Rows[j].ItemArray[k]);