范围返回错误的单元格集

问题描述 投票:0回答:1

我正在使用Excel Interop,并试图返回相对于从工作表中获取的第一个单元格范围的单元格范围。问题是第二次获取单元格范围时,没有得到预期的单元格。下面是代码。

Range cells = xlWorkSheet.Range[
    xlWorkSheet.Cells[5, 5],
    xlWorkSheet.Cells[9, 9]
];

Range cells2 = cells.Range[cells[2, 2], cells[3, 3]];

在第二个Range调用中,我希望得到[6,6]到[7,7]的单元格(相对于工作表)。相反,我得到的是[10,10]到[11,11]。

如果我单独得到一个单元格,我得到的是预期的那个单元格。

Range c1 = cells.Cells[2, 2];

这将返回[6, 6]单元格。有什么想法可以告诉我为什么会出现这种行为?

c# excel-interop
1个回答
1
投票

这里解释一下为什么你没有得到你需要的结果。

enum ColorConstants 
{
    vbYellow = 65535,
    vbRed = 255
}

Excel.Application excel = new Excel.Application
{ 
    Visible = true, 
    WindowState = Excel.XlWindowState.xlMaximized 
};
Excel.Workbook book = excel.Workbooks.Add();
Excel.Worksheet sheet = book.Sheets[1] as Excel.Worksheet;

// We have initial range "E5:I9"
Excel.Range cells = sheet.Range[sheet.Cells[5, 5], sheet.Cells[9, 9]];
cells.Interior.Color = ColorConstants.vbYellow; //Color the range

// Now your goal is to get range "F6:G7" (relative to the sheet),
// which is range "B2:C2" (relative to "cells" range)

// Let's dissect why this isn't the result you need

// You try to achieve your goal with this code:
Excel.Range cells2 = cells.Range[cells.Cells[2, 2], cells.Cells[3, 3]];
cells2.Interior.Color = ColorConstants.vbRed;

// However, the result is incorrect:
// it gives you "J10:K11" range:
string addr = cells2.Address[0, 0]; //=> J10:K11

// Let's see why this happens.
// The thing is that the cell, returned by Cells,
// is relative to SHEET - and not to YOUR RANGE.

// Start cell (top-left): its address is "F6"
Excel.Range cell_start = cells.Cells[2, 2];

// End cell (bottom-rigth): its address is "G7"
Excel.Range cell_end = cells.Cells[3, 3];

// So, we have "F6:G7" address.
// Now we must imagine that our initial range "E5:I9" - and namely "E5" - is the start cell of the sheet.
// When you do it, our "F6:G7" relatively to "E5:I9" will be "J10:K11",
// because "F6:G7" goes OUTSIDE the boundaries of "E5:I9".
// If you calculate correctly, it will be our incorrect "J10:K11" range.
// This is why you get incorrect result.

// To calculate the offset correctly,
// you must imagine your range as a small worksheet.
// For instance, for our range "E5:I9":
// • "E5" (statrt cell) can be referred to as:
//    1) cells.Cells[1]
//    2) cells.Cells[1, 1]
//    2) cells.Cells[1, "A"]
//    3) cells.Cells["A1"]
// • "F6" (end cell) can be referred to as:
//    1) cells.Cells[7]
//    2) cells.Cells[2, 2]
//    3) cells.Cells[2, "B"]
//    4) cells.Range["B2"]

// Thus, we need another approach:
cells2 = cells.Range["B2"].Resize[2, 2];
cells2 = cells.Cells[2, 2].Resize[2, 2];
cells2 = cells.Range["B2:C3"];
cells2.Interior.Color = ColorConstants.vbRed; //Color the range

1
投票

是的,在你的情况下,cells.Parent是xlWorkSheet对象。

为了完整起见,建议你可以使用以下任何一种方法,你将得到你想看到的相同的成功结果。

Range cells2 = cells.Parent.Range[cells.Cells[2, 2], cells.Cells[3, 3]];

Range cells2 = xlWorkSheet.Range[cells.Cells[2, 2], cells.Cells[3, 3]];

Range cells2 = cells.Range[xlWorkSheet.Cells[2, 2], xlWorkSheet.Cells[3, 3]];
© www.soinside.com 2019 - 2024. All rights reserved.