范围返回一组错误的单元格

Range returning a wrong set of Cells

我正在使用 Excel Interop 并尝试 return 相对于我从工作表中获得的第一个单元格范围的单元格范围。问题是第二次获取范围时它没有给我预期的单元格。这是代码:

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];

这个 return 是 [6, 6] 单元格。关于我为什么会出现这种行为有什么想法吗?

是的,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]];

这里解释了为什么您没有得到您需要的结果。

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