Excel Get_Range 具有多个区域
我正在尝试从 Excel 获取一个范围,该范围指定了多个区域,本质上我已经...
int StartColumn
int EndColumn
int[] ColumnsToSkip
当您将它们组合起来时,可能会产生一个包含非连续区域的范围。不幸的是,我不太清楚获得这个的调用... MSDN 不是很有用...
WorkSheet 表;
sheet.get_Range( what goes in here??? );
有人提供任何帮助吗?干杯。
I'm trying to get a range from Excel, which has multiple areas specified, essentially I've got...
int StartColumn
int EndColumn
int[] ColumnsToSkip
When you combine these it's possible to produce a range with non-contiguous areas. Unfortunately I can't quite figure out the call to get this... MSDN isn't very useful...
WorkSheet sheet;
sheet.get_Range( what goes in here??? );
Anyone provide any help? Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个非常简单的解决方案是以逗号分隔的形式指定不同的区域:
对于编程范围组合,还有 ExcelApplication 对象的
Union
和Intersection
方法。由于有许多可选参数,这些在 C# 中使用起来有点笨拙。请参阅此处http://codeidol.com/csharp/c-sharp-in-office/Working-with-Excel-Objects/Working-with-the-Range-Object/
例如。
编辑:一些额外的提示:
在您的情况下,您首先应该转换“ColumnsToKeep”中的“ColumnsToSkip”,因为这是任何类型的单元联合所需要的。这是一个 Linq 解决方案:
然后,您可以按照本示例的思路创建一些内容:
其中定义了“Union”,例如,如下所示:
A very simple solution is to specify different areas in comma-separated form:
For programmatic range combinations, there are also the
Union
andIntersection
methods of the ExcelApplication object. Those are a little bit clumsy to use in C# because of many optional parameters. See herehttp://codeidol.com/csharp/c-sharp-in-office/Working-with-Excel-Objects/Working-with-the-Range-Object/
for examples.
EDIT: some additional hints:
In your case, you first should transform the "ColumnsToSkip" in "ColumnsToKeep", since that is what you will need for any kind of cell union. Here is a Linq solution:
Then, you can create something along the lines of this example:
where "Union" is defined, for example, like this:
试试这个:
Try this:
处理非连续范围的另一种方法是使用命名范围在 Excel 中将它们组合在一起,例如:
=CHOOSE({1;2;3},RANGE1,RANGE2,RANGE3)
这将产生由彼此“堆叠”的范围组成的数组。将此公式分配给命名范围,您可以像任何其他
Range
对象一样以编程方式使用它。Another way to work with non contiguous ranges is to just combine them together within Excel using a named range, e.g.:
=CHOOSE({1;2;3},RANGE1,RANGE2,RANGE3)
This will produce an array consisting of the ranges "stacked" on top of each other. Assign this formula to a named range and you can use it programmatically just like any other
Range
object.