如何在Word表格中选择矩形单元格区域

发布于 2024-12-29 11:35:36 字数 620 浏览 0 评论 0原文

给定类似

Table table;
Cell cell_1 = table.Cell(2,2);
Cell cell_2 = table.Cell(4,4);

我想从 cell_1 到 cell_2 选择(或突出显示)的内容(就像手动操作时的操作方式一样)。

我最初认为执行以下操作会起作用:

Selection.MoveRight(wdUnits.wdCell, numCells, WdMovementType.wdExtend)

但是根据 http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.moveright%28v=office.11​​%29.aspx 在备注下,使用 wdCells 作为单位会将 WdMovementType 默认为 wdMove,我想不出解决方法。

Given something like

Table table;
Cell cell_1 = table.Cell(2,2);
Cell cell_2 = table.Cell(4,4);

I want to select (or highlight) from cell_1 to cell_2 (like how you would if you were doing it by hand).

I originally thought that doing the following would work:

Selection.MoveRight(wdUnits.wdCell, numCells, WdMovementType.wdExtend)

But according to http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.moveright%28v=office.11%29.aspx under remarks, using wdCells as the Unit will default the WdMovementType to wdMove, and I can't think of a workaround.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

七颜 2025-01-05 11:35:36

这是我发现的解决该问题的方法。这不是最有效的方法,并且如果表格中包含合并单元格则不起作用。我发现您可以选择起始单元格的范围,然后通过以单元格为单位移动来扩展范围的终点。通过发现要选择的区域的起点和终点之间的单元格数量,您可以迭代这些单元格步骤数。下面是一般代码:

word.Table table;
word.Cell cellTopLeft; //some cell on table.
word.Cell cellBottomRight; //another cell on table. MUST BE BELOW AND/OR TO THE RIGHT OF cellTopLeft

int cellTopLeftPosition = (cellTopLeft.RowIndex - 1) * table.Columns.Count + cellTopLeft.ColumnIndex;
int cellBottomRightPosition = (cellBottomRight.RowIndex - 1) * table.Columns.Count + cellBottomRight.ColumnIndex;
int stepsToTake = cellBottomRightPosition - cellTopLeftPosition;

if (stepsToTake > 0 && 
    cellTopLeft.RowIndex <= cellBottomRight.RowIndex && //enforces bottom right cell is actually below of top left cell
    cellTopLeft.ColumnIndex <= cellBottomRight.ColumnIndex) //enforces bottom right cell is actually to the right of top left cell
{
   word.Range range = cellTopLeft.Range;
   range.MoveEnd(word.WdUnits.wdCell, stepsToTake);
   range.Select();      
}

Here is a workaround I've found to the problem. It isn't the most efficient way, and it doesn't work if the table has merged cells in it. I've discovered that you can select the range of your start cell, and then expand the end point of the range by moving by in units of cells. By discovering the number of cells between the start and end point of the region you want selected, you can iterate those number of cell steps. Here's the general code for that below:

word.Table table;
word.Cell cellTopLeft; //some cell on table.
word.Cell cellBottomRight; //another cell on table. MUST BE BELOW AND/OR TO THE RIGHT OF cellTopLeft

int cellTopLeftPosition = (cellTopLeft.RowIndex - 1) * table.Columns.Count + cellTopLeft.ColumnIndex;
int cellBottomRightPosition = (cellBottomRight.RowIndex - 1) * table.Columns.Count + cellBottomRight.ColumnIndex;
int stepsToTake = cellBottomRightPosition - cellTopLeftPosition;

if (stepsToTake > 0 && 
    cellTopLeft.RowIndex <= cellBottomRight.RowIndex && //enforces bottom right cell is actually below of top left cell
    cellTopLeft.ColumnIndex <= cellBottomRight.ColumnIndex) //enforces bottom right cell is actually to the right of top left cell
{
   word.Range range = cellTopLeft.Range;
   range.MoveEnd(word.WdUnits.wdCell, stepsToTake);
   range.Select();      
}
_畞蕅 2025-01-05 11:35:36

一种更简单的方法是使用 Document.Range 方法在矩形的两个角之间创建一个范围。这对于合并单元格同样有效。

word.Document document;    
word.Cell cellTopLeft;
word.Cell cellBottomRight;

document.Range(cellTopLeft.Range.Start, cellBottomRight.Range.End).Select

注意:可以使用此表达式返回的范围来操作表格的内容而不选择它,但它不适用于合并单元格(在后一种情况下,请使用 cell.Merge(MergeTo)代码>)。

A much simpler way to do this is to use the Document.Range method to create a range between the two corners of the rectangle. This works equally well with merged cells.

word.Document document;    
word.Cell cellTopLeft;
word.Cell cellBottomRight;

document.Range(cellTopLeft.Range.Start, cellBottomRight.Range.End).Select

Note: One can use the range returned by this expression to manipulate the contents of the table without selecting it, but it doesn't work for merging the cells (in the latter case, use cell.Merge(MergeTo) ).

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