访问工作表的 Cells
Contents
[
Hide
]
我们知道所有工作表都可能包含基本上存储在单元格中的数据(工作表由单元格组成)。单元格是工作表的基本部分,用于将整个工作表构建为一系列行和列。在我们尝试从工作表访问数据之前,我们需要访问其单元格。因此,在本主题中,我们将讨论使用 Aspose.Cells 在运行时访问工作表单元格的一些基本方法。
访问 Cells
Aspose.Cells提供类工作簿表示一个 Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了Cells代表工作表中所有单元格的集合。
我们可以用Cells集合以访问工作表中的单元格。 Aspose.Cells 提供了访问工作表中单元格的三种基本方法:
- 使用单元名称。
- 使用单元格的行和列索引。
- 在中使用单元格索引Cells收藏
我们已经提到第三种方法是最快的,而第一种方法是最慢的。两种方法之间的性能差异非常小,因此无论您使用哪种方法,都不必担心性能下降。
使用 Cell 名称
开发人员可以通过将其单元名称传递给Cells的集合工作表类作为索引。
如果您在开始时创建一个空白工作表,则计数Cells收藏为零。当您使用这种方法访问一个单元格时,它会检查该单元格是否存在于集合中。如果是,它返回集合中的单元格对象,否则,它创建一个新的电池对象,将对象添加到Cells集合,然后返回该对象。如果您熟悉 Microsoft Excel,此方法是访问单元格的最简单方法,但与其他方法相比,它是最慢的方法。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleData = dirPath->StringAppend(new String("sampleData.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleData); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Get cells from sheet | |
intrusive_ptr<ICells> cells = worksheet->GetICells(); | |
//Accessing a cell using its name | |
intrusive_ptr<ICell> cell = cells->GetObjectByIndex(new String("B3")); | |
//Write string value of the cell on console | |
Console::Write(new String("Value of cell B3: ")); | |
Console::WriteLine(cell->GetStringValue()); |
使用 Cell 的行和列索引
开发人员可以通过将其行和列的索引传递给Cells的集合工作表班级。这种方法的工作方式与第一种方法相同。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleData = dirPath->StringAppend(new String("sampleData.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleData); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Get cells from sheet | |
intrusive_ptr<ICells> cells = worksheet->GetICells(); | |
//Accessing cell B3 using its row and column index | |
intrusive_ptr<ICell> cell = cells->GetObjectByIndex(2, 1); | |
//Write string value of the cell on console | |
Console::Write(new String("Value of cell B3: ")); | |
Console::WriteLine(cell->GetStringValue()); |
访问工作表的最大显示范围
Aspose.Cells 允许开发人员访问工作表的最大显示范围。最大显示范围 - 第一个和最后一个包含内容的单元格之间的单元格范围 - 当您需要复制、选择或在图像中显示工作表的全部内容时非常有用。
您可以使用访问工作表的最大显示范围最大显示范围的方法Cells收藏。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input excel file | |
StringPtr sampleData = dirPath->StringAppend(new String("sampleData.xlsx")); | |
//Read input excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(sampleData); | |
//Accessing the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
//Get cells from sheet | |
intrusive_ptr<ICells> cells = worksheet->GetICells(); | |
//Access the Maximum Display Range | |
intrusive_ptr<IRange> range = cells->GetMaxDisplayIRange(); | |
//Print string value of the cell on console | |
Console::Write(new String("Maximum Display Range of Worksheet: ")); | |
Console::WriteLine(range->GetRefersTo()); |