访问工作表的 Cells

访问 Cells

Aspose.Cells提供了一个类,工作簿表示一个 Excel 文件。这工作簿类包含一个工作表集合允许访问 Excel 文件中的每个工作表。工作表由工作表班级。这工作表类提供了Cells代表工作表中所有单元格的集合。

我们可以用Cells集合以访问工作表中的单元格。 Aspose.Cells 提供了访问工作表中单元格的三种基本方法:

  1. 使用单元名称。
  2. 使用单元格的行和列索引。
  3. 在中使用单元格索引Cells收藏

**重要的:**我们已经提到第三种方法是最快的,而第一种方法是最慢的。两种方法之间的性能差异非常小,因此无论您使用哪种方法,都不必担心性能下降。

使用 Cell 名称

开发人员可以通过将其单元名称传递给Cells的集合工作表类作为索引。

如果您在开始时创建一个空白工作表,则计数Cells收藏为零。当您使用这种方法访问一个单元格时,它会检查该单元格是否存在于集合中。如果是,它返回集合中的单元格对象,否则,它创建一个新的Cell对象,将对象添加到Cells集合,然后返回对象。如果您熟悉 Microsoft Excel,此方法是访问单元格的最简单方法,但与其他方法相比,它是最慢的方法。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Using the Sheet 1 in Workbook
Worksheet worksheet = workbook.Worksheets[0];
// Accessing a cell using its name
Cell cell = worksheet.Cells["A1"];
string value = cell.Value.ToString();
Console.WriteLine(value);

使用 Cell 的行和列索引

开发人员可以通过将其行和列的索引传递给Cells的集合工作表班级。

这种方法的工作方式与第一种方法相同。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Using the Sheet 1 in Workbook
Worksheet worksheet = workbook.Worksheets[0];
// Accessing a cell using its row and column
Cell cell = worksheet.Cells[0,0];
string value = cell.Value.ToString();
Console.WriteLine(value);

在 Cells 集合中使用 Cell 索引

也可以通过将单元格的数字索引传递给Cells收藏。

如果您使用此方法访问单元格,则如果单元格的数字索引超出范围,则会引发异常。这种方法是访问单元格最快的方法,但需要知道的重要一点是,如果您使用这种方法访问单元格对象,则数字索引可能会在新单元格添加到Cells收藏。中的单元格对象Cells集合在内部按行和列索引排序。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open an existing worksheet
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Using the Sheet 1 in Workbook
Worksheet worksheet = workbook.Worksheets[0];
// Accessing a cell using its row and column.
Cell cell = worksheet.Cells.CheckCell(0, 0);
if (cell != null)
{
string value = cell.Value.ToString();
Console.WriteLine(value);
}

访问工作表的最大显示范围

Aspose.Cells 允许开发人员访问工作表的最大显示范围。最大显示范围 - 第一个和最后一个有内容的单元格之间的单元格范围 - 当您需要复制、选择或在图像中显示工作表的全部内容时非常有用。

您可以使用访问工作表的最大显示范围工作表.Cells.MaxDisplayRange .下面的示例代码说明了如何访问最大显示范围财产。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Path to source file
string filePath = dataDir + "Book1.xlsx";
// Instantiating a Workbook object
Workbook workbook = new Workbook(filePath);
// Instantiate a workbook from source file
Workbook wb = new Workbook(filePath);
// Access the first workbook
Worksheet worksheet = wb.Worksheets[0];
// Access the Maximum Display Range
Range range = worksheet.Cells.MaxDisplayRange;
// Print the Maximum Display Range RefersTo property
Console.WriteLine("Maximum Display Range: " + range.RefersTo);