如何在C#Excel Interop应用程序中指定ListObject(排序/过滤器)控件的单元格位置?

时间:2015-12-04 23:05:19

标签: c# filter filtering excel-interop listobject

我发现了here如何添加" ListObject" (我认为它是"排序/过滤控制")到Excel电子表格。这是一个如何实现它的简单例子:

// add the data
_xlSheetListObjectTest.Cells[1, 1] = "Marionberry";
_xlSheetListObjectTest.Cells[2, 1] = "Apple";
_xlSheetListObjectTest.Cells[3, 1] = "Strawberry";
_xlSheetListObjectTest.Cells[4, 1] = "Cashew";
_xlSheetListObjectTest.Cells[5, 1] = "Kumquat";
_xlSheetListObjectTest.Cells[6, 1] = "Pomegranate";
_xlSheetListObjectTest.Cells[7, 1] = "Banana";
_xlSheetListObjectTest.Cells[8, 1] = "Pineapple";
_xlSheetListObjectTest.Cells[9, 1] = "Kiwi";
_xlSheetListObjectTest.Cells[10, 1] = "Huckleberry";
_xlSheetListObjectTest.Cells[11, 1] = "Gooseberry";

// create a Sort/Filter Control for the data above
Excel.ListObject fruitList =
    _xlSheetListObjectTest.
        ListObjects.Add(Excel.XlListObjectSourceType.xlSrcRange,
            _xlSheetListObjectTest.Range[
                _xlSheetListObjectTest.Cells[1, 1],
                _xlSheetListObjectTest.Cells[11, 1]],
            Type.Missing, Excel.XlYesNoGuess.xlNo);

这就是" ListObject"在人口密集的细胞正上方,如下所示:

enter image description here

以下是ListObject下拉的原因:

enter image description here

这种令人敬畏的功能(对于一小部分代码而言,很多爆炸)需要这种要求。但是,我有一个包含月份数据列的工作表,需要过滤掉选定的月份列(排序是不必要的和不需要的)。以下是生成报告时选择几个月后的外观:

enter image description here

但是,用户可以生成少数月份的报告(少至1个,多达13个),例如:

enter image description here

所以我想要一个" ListObject"其范围的过滤操作跨越显示的月份的数据(许多行,多列),然后用户可以取消选择某些月份,隐藏取消选择的月份的整个行。

但是,使用此代码定义所有月份数据的范围:

Excel.ListObject monthFilterControl = // Resharper complains that this is not used (grays out "monthFilterControl"), but this is what creates the control
    _xlSheet.
        ListObjects.Add(Excel.XlListObjectSourceType.xlSrcRange,
            _xlSheet.Range[
                _xlSheet.Cells[COLUMN_HEADING_ROW, MONTH1_COL],
                _xlSheet.Cells[_xlSheet.UsedRange.Rows.Count, _xlSheet.UsedRange.Columns.Count - 1]],
            Type.Missing, Excel.XlYesNoGuess.xlNo);

问题是多个ListObjects正好向下弯曲到月份标题列上,向下推动它们下面的行,使行不对齐。不仅如此,他们提供的排序和过滤功能并不符合我的严格标准。它不是允许我过滤掉整个月的列,而是这样做:

enter image description here

所以我想要的是:

There should only be one ListObject control in a single cell a row above the month columns, not one in every month header cell.
A Filter list (array of checkboxes) of all Months; deselecting "September" would cause that month column to hide, etc.

这可能吗?如果是这样,我该怎么做呢?

0 个答案:

没有答案