Excel Interop中的“AutoFilter”是否有“姐妹”function,适用于列而不是行?

Excel Interop中的“AutoFilter”function允许您过滤列显示下方行中的数据。

我需要一个类似的function,允许用户选择要隐藏的列。 例如,具体而言,如果有一组列3..15(C..0)从“9月15日”到“9月16日”,它们之间有明显的月份(“10月15日”等)我希望用户能够选择要查看的那些列的任何子集。 被取消选择的人会“消失”

如何实现这一目标?

我尝试了以下,但第二个filter没有效果:

private void AddFilters() { Range column1Row6 = _xlSheet.Range[_xlSheet.Cells[6, 1], _xlSheet.Cells[6, 1]]; column1Row6.AutoFilter(1, Type.Missing, XlAutoFilterOperator.xlAnd, Type.Missing, true); // Try to filter columns, too (not the contents, but which columns display) Range columns3And4 = _xlSheet.Range[_xlSheet.Cells[6, 3], _xlSheet.Cells[6, 15]]; columns3And4.AutoFilter(1, Type.Missing, XlAutoFilterOperator.xlAnd, Type.Missing, true); } 

这可能吗?

UPDATE

看一些遗留代码,看起来这段代码就是这样做的:

 var fld = ((PivotField) pvt.PivotFields("Month")); fld.Orientation = XlPivotFieldOrientation.xlColumnField; fld.NumberFormat = "MMM yy"; 

具体来说,将PivotField的方向设置为xlColumnField会使列运行排序/过滤按钮,在操作时,有条件地显示/隐藏各个列。 它是否根据数字格式确定哪些列可显示/可隐藏? 也就是说,如果值是“9月15日”或“10月16日”,它是隐藏/重新显示的候选者? 我不知道,但我在代码中看不到任何其他更具体设置列过滤限制的内容。

有关更多背景信息:

 var pch = _xlBook.PivotCaches(); pch.Add(XlPivotTableSourceType.xlDatabase, "PivotData!A1:H" + rowIdx).CreatePivotTable(_xlSheet.Cells[6, 1], "PivTab1", Type.Missing, Type.Missing); var pvt = _xlSheet.PivotTables("PivTab1") as PivotTable; if (pvt != null) { pvt.MergeLabels = true; pvt.ErrorString = ""; pvt.DisplayErrorString = true; var fld = ((PivotField) pvt.PivotFields("Description")); fld.Orientation = XlPivotFieldOrientation.xlRowField; fld = ((PivotField) pvt.PivotFields("Month")); fld.Orientation = XlPivotFieldOrientation.xlColumnField; fld.NumberFormat = "MMM yy"; . . . 

我不认为有这样的function,但使用数据透视表可以实现类似的function。

我仍然有工作要做到这一切看起来不错,但我确实找到了如何基于这里的答案创建列filter。

使用以下代码:

 var pch = _xlBook.PivotCaches(); // TODO: Make range dynamic Range sourceData = _xlBook.Worksheets["PivotData"].Range["A1:G318"]; PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, sourceData); PivotTable pvt = pc.CreatePivotTable(_xlPivotTableSheet.Range["A8"], "PivotTable"); pvt.PivotFields("Description").Orientation = XlPivotFieldOrientation.xlRowField; pvt.PivotFields("MonthYr").Orientation = XlPivotFieldOrientation.xlColumnField; 

…我可以从一张纸(“PivotData”)获取源数据并将其拍在另一张纸上(_xlPivotTableSheet)。 给我“过滤一组列”function的部分是上面的最后一行。 这让我有以下几点:

在此处输入图像描述

虽然你看不到它,但是有一列“201509”以及“201510”,它们可以过滤显示与否,无论是单独还是集体。

现在要将其余数据显示为应该…