获得零作为计算值和公式。 我的moto是根据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);
}
答案 0 :(得分:1)
是的。我刚刚将ToString()转换为Int32()以获取AMOUNT字段值。
workSheet.Cells[j + 2, k + 1].Value = Convert.ToInt32(ds.Tables[0].Rows[j].ItemArray[k]);