Excel Interop:对列进行分组

我如何分组使用Excel互操作的列?

如果我logging一个macros(通常是一个好的开始),我得到这个代码:

Columns("I:M").Select Selection.Columns.Group 

不幸的是,由于几个问题,至less在C ++中这是行不通的。 首先, Application.Selection返回一个正常的Range ,然后Range.Columns是另一个RangeRange.Group是这个方法:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.group%28v=office.11​​%29.aspx

此方法仅适用于数据透视表。

那么我如何创build一个使用Excel互操作的列组呢?

即使我设法创build一个组,我如何缩小/扩大它? 通过我的意思是点击+显示组的内容,或者相反,“点击”减去隐藏组。 如果我在录制macros的时候这样做的话,它并不是反映在macros中。

尽pipeRange.Group()文档似乎与数据透视表有关,但如果使用Range.Columns提取列,然后将Range.Columns .Group()方法应用于该范围,则会产生所需的效果。 在C#中:

 Range range = sheet.get_Range("c1","e1"); range.Columns.Group(); 

编辑:完整的例子,再次在C#(道歉,这是我有方便的例子):

 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = true; app.Workbooks.Add(); Worksheet sheet = app.Workbooks[1].Sheets[1]; Range range = sheet.get_Range("c1","e1"); range.Columns.Group(); 

macroslogging器隐藏的一件事是需要使用Range.EntireColumn属性。 以下是一些编码,展开和折叠的代码。 我是通过Google和Google混帐的,但是我觉得这个概念是正确的。 希望很容易翻译成C ++:

 Sub test() Dim ws As Excel.Worksheet Set ws = ActiveSheet With ws If .Columns.OutlineLevel > 1 Then 'clear any existing hidden grouped columns and grouping .Outline.ShowLevels columnlevels:=.Columns.OutlineLevel .Range("1:1").EntireColumn.Ungroup End If 'group .Range("A:C").EntireColumn.Group 'collapse ws.Outline.ShowLevels columnlevels:=1 'expand ws.Outline.ShowLevels columnlevels:=.Columns.OutlineLevel End With End Sub