Hi,
We have looked into your issue and the issue was occurring because you were missing Workbook.CalculateFormula() in your code.
Please call Workbook.CalculateFormula() before autofilter. It will resolve your problem.
Please also download and use the latest version:
Aspose.Cells
for .NET v7.2.2.7
For your help, please see the following
code:
C#
wb.CalculateFormula();
sh.AutoFilter.Range = CellsHelper.ColumnIndexToName(4) + “1:” + CellsHelper.ColumnIndexToName(7) + (sh.Cells.MaxDataRow);
sh.AutoFilter.Custom(1, FilterOperatorType.Equal, “X”);
sh.AutoFilter.Refresh();
Your complete code will look like the following, I have highlighted the changes in red.
Complete Codestring filePath = @“F:\Shak-Data-RW\Downloads\TestAutoFilter.xlsx”;
Workbook wb = new Workbook();
Worksheet sh = wb.Worksheets[0];
int iloop = 30;
sh.Cells[0, 4].PutValue(“Liv1”);
sh.Cells[0, 5].PutValue(“Liv2”);
sh.Cells[0, 6].PutValue(“Liv3”);
sh.Cells[0, 7].PutValue(“Liv4”);
for (int r = 1; r < iloop; r++)
{
if (r > 10 && r < 20)
{
if (r < 15)
{
sh.Cells[r, 0].PutValue(“ROW_FORMULATED_TRUE”);
sh.Cells[r, 1].PutValue(“s”);
sh.Cells[r, 2].PutValue(“s”);
sh.Cells[r, 3].PutValue(“s”);
sh.Cells[r, 4].Formula = “=IF(COUNTIF(B” + r + “:” + “D” + r + “,"s")=3,"","X")”;
sh.Cells[r, 5].Formula = “=IF(COUNTIF(B” + r + “:” + “D” + r + “,"s")=3,"","X")”;
sh.Cells[r, 6].Formula = “=IF(COUNTIF(B” + r + “:” + “D” + r + “,"s")=3,"","X")”;
sh.Cells[r, 7].Formula = “=IF(COUNTIF(B” + r + “:” + “D” + r + “,"s")=3,"","X")”;
}
else
{
sh.Cells[r, 0].PutValue(“ROW_FORMULATED_FALSE”);
sh.Cells[r, 1].PutValue(“”);
sh.Cells[r, 2].PutValue(“s”);
sh.Cells[r, 3].PutValue(“”);
sh.Cells[r, 4].Formula = “=IF(COUNTIF(B” + r + “:” + “D” + r + “,"s")=3,"","X")”;
sh.Cells[r, 5].Formula = “=IF(COUNTIF(B” + r + “:” + “D” + r + “,"s")=3,"","X")”;
sh.Cells[r, 6].Formula = “=IF(COUNTIF(B” + r + “:” + “D” + r + “,"s")=3,"","X")”;
sh.Cells[r, 7].Formula = “=IF(COUNTIF(B” + r + “:” + “D” + r + “,"s")=3,"","X")”;
}
}
else
{
if (r < 10)
{
sh.Cells[r, 0].PutValue(“ROW_X”);
sh.Cells[r, 1].PutValue(“”);
sh.Cells[r, 2].PutValue(“”);
sh.Cells[r, 3].PutValue(“”);
sh.Cells[r, 4].PutValue(“X”);
sh.Cells[r, 5].PutValue(“X”);
sh.Cells[r, 6].PutValue(“X”);
sh.Cells[r, 7].PutValue(“X”);
}
else
{
sh.Cells[r, 0].PutValue(“ROW_NULL”);
sh.Cells[r, 1].PutValue(“”);
sh.Cells[r, 2].PutValue(“”);
sh.Cells[r, 3].PutValue(“”);
sh.Cells[r, 4].PutValue(“”);
sh.Cells[r, 5].PutValue(“”);
sh.Cells[r, 6].PutValue(“”);
sh.Cells[r, 7].PutValue(“”);
}
}
}
wb.CalculateFormula();sh.AutoFilter.Range = CellsHelper.ColumnIndexToName(4) + “1:” + CellsHelper.ColumnIndexToName(7) + (sh.Cells.MaxDataRow);
sh.AutoFilter.Custom(1, FilterOperatorType.Equal, “X”);
sh.AutoFilter.Refresh();
wb.Save(“TestAutoFilter.xlsx”, SaveFormat.Xlsx);