1

我在这里找到了如何将“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”直接放在填充的单元格上方,如下所示:

在此处输入图像描述

下面是下拉 ListObject 后的样子:

在此处输入图像描述

对于那种需求来说,这是很棒的功能(只需一点点代码就可以了很多)。但是,我有一张包含月份数据列的工作表,需要过滤掉选定的月份列(排序是不需要且不需要的)。以下是在生成报告时选择了几个月后的外观:

在此处输入图像描述

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

在此处输入图像描述

所以我想要一个“ListObject”,它的过滤操作范围跨越正在显示的月份的数据(多行,多列),用户可以取消选择某些月份,隐藏取消选择月份的整行[s] .

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

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);

问题是多个 ListObject 直接挤到月份标题列上,将它们下方的行向下推,使行错位。不仅如此,他们提供的排序和过滤功能不符合我的严格标准。它不允许我过滤掉整个月份的列,而是这样做:

在此处输入图像描述

所以我想要的是:

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.

这可能吗?如果是这样,我该如何完成它?

4

0 回答 0