简体   繁体   English

如何在 EPPlus 中使用 C# 删除自动过滤器

[英]How to remove AutoFilter Using C# in EPPlus

I have tried the below C# CODING:我尝试了以下C# 编码:

wsDt.Cells["A10:G10"].AutoFilter = false;

but the filter is not removed from my excel.但过滤器并没有从我的 excel 中删除。

Any other way to remove it.任何其他方式来删除它。

Thanks...谢谢...

In Excel, when you use the Format as Table option it will not only style the data but will also create a Named Range - Table1. 在Excel中,当您使用Format as Table选项时,它不仅会设置数据样式,还会创建命名范围 - Table1。 This option also automatically enables the Filter Buttons. 此选项还会自动启用过滤器按钮。 After formatting as a table, you can uncheck Filter Buttons in the Table Tools -> Table Styles Options. 格式化为表格后,您可以在表格工具 - >表格样式选项中取消选中过滤按钮。

What works for me is doing the same programmatically. 对我有用的是以编程方式做同样的事情。

  1. LoadFromDataTable(DataTable, bool, TableStyles) basically LoadFromDataTable(DataTable,bool,TableStyles)基本上
    • pastes data to the worksheet starting at the ExcelRange 将数据粘贴到从ExcelRange开始的工作表
    • applies a Table Format 应用表格格式
    • uses the DataTable.TableName to name the range 使用DataTable.TableName命名范围
    • enables Filter Button 启用过滤按钮
  2. Disable the Filter Button 禁用过滤器按钮

    • use the DataTable.TableName to reference the named range 使用DataTable.TableName引用命名范围
    • set ShowFilter to false enter code here 将ShowFilter设置为false在此输入代码

       //imagine a table with 5 columns DataTable dt = new DataTable(); dt.TableName = "UniqueTableName"; //define the cells where the headers will appear int topRow = 1; int leftMostColumn = 1; int rightMostColumn = 5; //bind the DataTable using LoadFromDataTable() OfficeOpenXml.ExcelRange excelRange = worksheet.Cells[topRow, leftMostColumn, topRow, rightMostColumn]; excelRange.LoadFromDataTable(dt, true, OfficeOpenXml.Table.TableStyles.Light8); //turn of the filtering OfficeOpenXml.Table.ExcelTable table = worksheet.Tables[dt.TableName]; table.ShowFilter = false; 

This seems to be an EPPlus bug and I don't think it has been resolved as of the latest release (4.04), at least I could figure out a solution. 这似乎是一个EPPlus错误,我不认为它已经解决了最新版本(4.04),至少我可以找到一个解决方案。 My workaround is to simply load the spreadsheet values a row at a time with a loop: 我的解决方法是简单地使用循环一次加载电子表格值:

int sheetRow = 3;
for (int outer = 0; outer < outerSourceTable.Rows.Count; outer++)
{
    var outerThingId = Convert.ToInt32(outerSourceTable.Rows[outer]["OuterThingId"]);
    var outerThingName = Convert.ToString(outerSourceTable.Rows[outer]["OuterThing"]);
    var innerThingsTable = _repository.GetInnerThings(outerThingId);
    if (innerThingsTable.Rows.Count > 0)
    {
        myWorksheet.Cells[sheetRow, 1].Value = outerThingName;

        // Load the data into the worksheet. We need to load a row at a time
        // to avoid the auto-filter bug
        for (int inner = 0; inner < innerThingsTable.Rows.Count; inner++)
        {
            var innerName = Convert.ToString(innerThingsTable.Rows[inner]["Name"]);
            var innerDescr = Convert.ToString(innerThingsTable.Rows[inner]["Description"]);
            myWorksheet.Cells[sheetRow, 2].Value = innerName;
            myWorksheet.Cells[sheetRow, 3].Value = innerDescr;
            sheetRow++;
        }
        sheetRow++;
    }
}

If you populate your excel data using the LoadFromCollection() call. 如果使用LoadFromCollection()调用填充Excel数据。 You can then reference it using the default Excel table name of "Table 1". 然后,您可以使用默认的Excel表名“表1”来引用它。

This is the same idea as Patricks answer but demonstrates the use without DataTable. 这与Patricks的回答相同,但演示了没有DataTable的用法。

excelWorksheet.Cells.LoadFromCollection(myCollection);
ExcelTable table = excelWorksheet.Tables["Table1"];
table.ShowFilter = false;

Sometimes excel creates the Table as a named range.有时 excel 将表创建为命名范围。 In my instance the Table was the only thing in the first worksheet, so the following helped me:在我的例子中,表格是第一个工作表中唯一的东西,所以以下帮助了我:

var ws = wb.Worksheets.First();
ws.NamedRanges.FirstOrDefault()?.Ranges.FirstOrDefault()?.SetAutoFilter(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM