Excel Get_Range 具有多个区域

发布于 2024-11-09 23:55:06 字数 297 浏览 1 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失眠症患者 2024-11-16 23:55:06

一个非常简单的解决方案是以逗号分隔的形式指定不同的区域:

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

对于编程范围组合,还有 ExcelApplication 对象的 UnionIntersection 方法。由于有许多可选参数,这些在 C# 中使用起来有点笨拙。请参阅此处

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

例如。

编辑:一些额外的提示:

在您的情况下,您首先应该转换“ColumnsToKeep”中的“ColumnsToSkip”,因为这是任何类型的单元联合所需要的。这是一个 Linq 解决方案:

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

然后,您可以按照本示例的思路创建一些内容:

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

其中定义了“Union”,例如,如下所示:

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

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

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

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/

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:

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);
    }
淡淡的优雅 2024-11-16 23:55:06

试试这个:

using Excel = Microsoft.Office.Interop.Excel;
  1. 将范围收集到数组中:
Excel.Range[] ranges = new Excel.Range[] {yourRange1, yourRange2, ... };
  1. 使用范围地址创建字符串范围并从中获取多范围:
string multiRangeStr = "";
foreach (Excel.Range range in ranges)
{
    string address = range.Address[true, true, Excel.XlReferenceStyle.xlA1];
    multiRangeStr += (multiRangeStr == "" ? "" : ";") + address;
}
//output: multiRangeStr: "A1:A3;B1:B3"

Excel.Range multiRange = wsheet.Range(multiRangeStr);

Try this:

using Excel = Microsoft.Office.Interop.Excel;
  1. Collect your ranges into an array:
Excel.Range[] ranges = new Excel.Range[] {yourRange1, yourRange2, ... };
  1. Create string-range with ranges addresses and get multi-range from it:
string multiRangeStr = "";
foreach (Excel.Range range in ranges)
{
    string address = range.Address[true, true, Excel.XlReferenceStyle.xlA1];
    multiRangeStr += (multiRangeStr == "" ? "" : ";") + address;
}
//output: multiRangeStr: "A1:A3;B1:B3"

Excel.Range multiRange = wsheet.Range(multiRangeStr);
暖阳 2024-11-16 23:55:06

处理非连续范围的另一种方法是使用命名范围在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文