vba 在 aspose 中选择范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14727170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:29:48  来源:igfitidea点击:

Select range in aspose

c#excelexcel-vbaaspose-cellsvba

提问by Mariusz.W

Do you know an equivalent to VBA code:

你知道一个相当于 VBA 的代码:

Range(Selection, Selection.End(xlToRight)).Select 

In Aspose.Cells. It seems that its only possible to select the last cell in the entire row:

在 Aspose.Cells 中。似乎只能选择整行中的最后一个单元格:

public Aspose.Cells.Cell EndCellInRow ( Int32 rowIndex )

Or the last cell on the right within a range:

或范围内右侧的最后一个单元格:

public Aspose.Cells.Cell EndCellInRow ( Int32 startRow, Int32 endRow, Int32 startColumn, Int32 endColumn )

but then you must know more or less how big your table is going to be.

但是你必须或多或少知道你的桌子有多大。

I found this from 2009: http://www.aspose.com/community/forums/permalink/196519/196405/showthread.aspxbut that will not resolve my problem as I may have many tables in a sheet both horizontally and vertiacally. And I can't predict where they are going to be.

我从 2009 年发现了这个:http: //www.aspose.com/community/forums/permalink/196519/196405/showthread.aspx但这并不能解决我的问题,因为我可能在一张纸上有很多水平和垂直的表格。而且我无法预测他们会去哪里。

Edit1: Sorry if this is dumb question, but ctrl+shift+arrow is such a common operation that I can't believe it would be not implemented so I'm making sure I really have to re-invent the wheel.

Edit1:对不起,如果这是一个愚蠢的问题,但 ctrl+shift+arrow 是一个如此常见的操作,我不敢相信它不会被实现,所以我确保我真的必须重新发明轮子。

回答by Iqbal

Aspose.Cells provides the list of tables in a worksheet using property named 'Worksheet.ListObjects'. 'ListObjects' is a colloection of 'ListObject' type which represents a Table in an excel sheet. That means if one has more than one Tables in a worksheet, the ListObjects collection will give access to every table in the worksheet very conveniently. Each 'ListObject' in turn contains a property named 'DataRange' which specifies all the cells inside a Table. For the sake of convenience DataRange can be used for following operations on a Table:

Aspose.Cells 使用名为“Worksheet.ListObjects”的属性提供工作表中的表格列表。'ListObjects' 是 'ListObject' 类型的集合,它代表 Excel 工作表中的一个表格。这意味着如果一个工作表中有多个表,ListObjects 集合将非常方便地访问工作表中的每个表。每个“ListObject”又包含一个名为“DataRange”的属性,该属性指定表中的所有单元格。为方便起见,DataRange 可用于对表进行以下操作:

  1. To apply styles/formatting on the cells in Table
  2. To get the data values
  3. Merge or move the cells in Range
  4. Export contents
  5. To get enumerator to traverse through Table cells
  1. 在表格中的单元格上应用样式/格式
  2. 获取数据值
  3. 合并或移动范围内的单元格
  4. 导出内容
  5. 让枚举器遍历表格单元格

To make selection of cells from DataRange, you can traverse using DataRange to get all the cells in a Row (This could also be done for a column)

要从 DataRange 中选择单元格,您可以使用 DataRange 遍历以获取一行中的所有单元格(这也可以对列进行)

Applying any operation on Table cells like after selecting cells using Ctrl+Shift+Arrow, could be performed using a workbook object as follows:

在使用 Ctrl+Shift+Arrow 选择单元格之后对表格单元格应用任何操作,可以使用工作簿对象执行,如下所示:

Workbook workbook = new Workbook(new FileStream("book1.xls", FileMode.Open));

if (workbook.Worksheets[0].ListObjects.Count > 0)
{
     foreach (ListObject table in workbook.Worksheets[0].ListObjects)
     {
          Style st = new Style();
          st.BackgroundColor = System.Drawing.Color.Aqua;
          st.ForegroundColor = System.Drawing.Color.Black;

          st.Font.Name = "Agency FB";
          st.Font.Size = 16;
          st.Font.Color = System.Drawing.Color.DarkRed;

          StyleFlag stFlag = new StyleFlag();
          stFlag.All = true;

          table.DataRange.ApplyStyle(st, stFlag);
     }
}

workbook.Save("output.xls");

There is also some worthy information available in Aspose docs about Table styles and applying formatting on a ListObject. For getting last Table cell in a certain row or column, I am sure this will help:

Aspose 文档中还有一些关于 Table 样式和在 ListObject 上应用格式的有价值的信息。为了获取特定行或列中的最后一个表格单元格,我相信这会有所帮助:

int iFirstRowIndex = table.DataRange.FirstRow;
int iFirstColumnIndex = table.DataRange.FirstColumn;

int iLastRowIndex = table.DataRange.RowCount + iFirstRowIndex;
int iLastColumnIndex = table.DataRange.ColumnCount + iFirstColumnIndex;

for (int rowIndex = 0; rowIndex < table.DataRange.RowCount; rowIndex++)
{
     //Get last cell in every row of table
     Cell cell = worksheet.Cells.EndCellInColumn(rowIndex + iFirstRowIndex, rowIndex + iFirstRowIndex, (short)iFirstColumnIndex, (short)(iLastColumnIndex - 1));

     //display cell value
     System.Console.WriteLine(cell.Value);
}