在列上添加特定的自动筛选器

我正在尝试在列上设置filter。 这是我在Interop中完成的方式:

private void CheckMasterFile(string path) { var xlApp = new Excel.Application(); var xlWorkbook = xlApp.Workbooks.Open(path); Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; foreach (var project in projects) { if (string.IsNullOrEmpty(project.ProjectID.Value)) { continue; } var xlRange = xlWorksheet.UsedRange; if (xlWorksheet.AutoFilter != null) { xlWorksheet.AutoFilterMode = false; } xlRange.AutoFilter(Field: 2, Criteria1: project.ProjectID.Value); var result = xlRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);//only shows filtered values double sum = 0; foreach (Excel.Range row in result.Rows) { if (row.Cells[2, 2].Value2() != null) { if (!NOT_ALLOWED_RUBRIQUES.Contains((string)row.Cells[2, 8].Value2())) {//check if rubrique is allowed or not //finish method } } } } xlWorkbook.Close(SaveChanges: false); xlApp.Quit(); } 

但是,我可以在EPPlus中使用自动筛选function的唯一方法是启用或禁用它,而不是筛选特定值的列。 像这样的东西:

 sheet.Cells["A RANGE HERE"].AutoFilter = true; 

所以我想在这里实现的是过滤一个特定的值的庞大的Excel文件,然后只显示这些值的行。

如果我明白你的问题,这不是EPPlus的意图。 它意味着生成excel文件作为端点。 你想要做的似乎是实际使用Excel作为你的分析工具。

如果您真的想要将filter应用于输出的列,那么您不必手动执行,因为EPPlus本身不应用filter。 所以像这样的东西(不得不自己处理这个)。 但是,责任仍然在你身上(生成器)实际执行分析 – 在这种情况下最后一个linq查询:

 [TestMethod] public void AutoFilter_Test() { //http://stackoverflow.com/questions/32723483/adding-a-specific-autofilter-on-a-column //Throw in some data var datatable = new DataTable("tblData"); datatable.Columns.AddRange(new[] { new DataColumn("Col1", typeof(int)), new DataColumn("Col2", typeof(int)), new DataColumn("Col3", typeof(object)) }); for (var i = 0; i < 10; i++) { var row = datatable.NewRow(); row[0] = i; row[1] = i * 10;row[2] = Path.GetRandomFileName(); datatable.Rows.Add(row); } //Create a test file var fi = new FileInfo(@"c:\temp\autofilter.xlsx"); if (fi.Exists) fi.Delete(); using (var pck = new ExcelPackage(fi)) { var worksheet = pck.Workbook.Worksheets.Add("Sheet1"); worksheet.Cells.LoadFromDataTable(datatable, true); var range = worksheet.Cells["A1:C10"]; range.AutoFilter = true; pck.Save(); } //Needed prior save in order for the XML to be generated using (var pck = new ExcelPackage(fi)) { var worksheet = pck.Workbook.Worksheets.First(); //Get reference to the worksheet xml for proper namespace var xdoc = worksheet.WorksheetXml; var nsm = new XmlNamespaceManager(xdoc.NameTable); nsm.AddNamespace("default", xdoc.DocumentElement.NamespaceURI); //Create the filters themselves var filter1 = xdoc.CreateNode(XmlNodeType.Element, "filter", xdoc.DocumentElement.NamespaceURI); var att = xdoc.CreateAttribute("val"); att.Value = "40"; filter1.Attributes.Append(att); var filter2 = xdoc.CreateNode(XmlNodeType.Element, "filter", xdoc.DocumentElement.NamespaceURI); att = xdoc.CreateAttribute("val"); att.Value = "50"; filter2.Attributes.Append(att); //Add filters to the collection var filters = xdoc.CreateNode(XmlNodeType.Element, "filters", xdoc.DocumentElement.NamespaceURI); filters.AppendChild(filter1); filters.AppendChild(filter2); //Create the parent filter container var filterColumn = xdoc.CreateNode(XmlNodeType.Element, "filterColumn", xdoc.DocumentElement.NamespaceURI); att = xdoc.CreateAttribute("colId"); att.Value = "1"; filterColumn.Attributes.Append(att); filterColumn.AppendChild(filters); //Now add it to the autoFilters node var autoFilter = xdoc.SelectSingleNode("/default:worksheet/default:autoFilter", nsm); autoFilter.AppendChild(filterColumn); //Have to manually hide rows based on criteria worksheet.Cells .Where(cell => cell.Address.StartsWith("B") && cell.Value is double && (double) cell.Value != 40d && (double) cell.Value != 50d) .Select(cell => cell.Start.Row) .ToList() .ForEach(r => worksheet.Row(r).Hidden = true); pck.Save(); } } 

OUTPUT

在这里输入图像说明