Excel中Get_Range多个领域 [英] Excel Get_Range with multiple areas

查看:246
本文介绍了Excel中Get_Range多个领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让从Excel,其中有规定,基本上是我有...

I'm trying to get a range from Excel, which has multiple areas specified, essentially I've got...

INT STARTCOLUMN结果$ B多领域的广泛$ b INT ENDCOLUMN结果
INT [] ColumnsToSkip

int StartColumn
int EndColumn
int[] ColumnsToSkip

当你把这些有可能产生与非连续区域的范围。不幸的是,我不能完全弄清楚电话得到这个... MSDN是不是很有用...

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...

工作表;

sheet.get_Range( what goes in here??? );



任何人提供任何帮助吗?干杯

Anyone provide any help? Cheers.

推荐答案

一个非常简单的解决方法是指定用逗号分隔的形式不同的领域:

A very simple solution is to specify different areas in comma-separated form:

sheet.get_Range( "A1:B1,E1:G1");

有关程序范围的组合,也有在联盟路口的ExcelApplication对象的方法。这些都是笨拙的一点点在C#中使用,因为许多可选参数。看到这里

For programmatic range combinations, there are also the Union and Intersection methods of the ExcelApplication object. Those are a little bit clumsy to use in C# because of many optional parameters. See here

http://codeidol.com/csharp/c-sharp-in-office/Working-with-Excel-Objects/Working-with-the-Range-Object 。/

有关实例

编辑:一些额外的提示:

some additional hints:

在你的情况,你首先应该转变ColumnsToSkip,在ColumnsToKeep,因为这是你所需要的任何一种细胞工会。这里是一个LINQ的解决方案:

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:

int[] ColumnsToKeep = Enumerable.Range(StartColumn, EndColumn -StartColumn + 1)
                      .Except(ColumnsToSkip)
                      .ToArray();



然后,您可以创建沿着这个例子线的东西:

Then, you can create something along the lines of this example:

   Excel.Range totalRange = null;
   foreach(int col in ColumnsToKeep)
   {
        totalRange = Union(excelApp,totalRange,(Excel.Range)sh.Cells[row, col]);
   }



其中联盟的定义,例如,像这样的>

where "Union" is defined, for example, like this:

    static Excel.Range Union(Excel.Application app, Excel.Range r1, Excel.Range r2)
    {
        if (r1 == null && r2 == null)
            return null;
        if (r1 == null)
            return r2;
        if (r2 == null)
            return r1;
        return  app.Union(r1, r2,
            null, null, null, null, null, null,
            null, null, null, null, null, null,
            null, null, null, null, null, null,
            null, null, null, null, null, null,
            null, null, null, null);
    }

这篇关于Excel中Get_Range多个领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆