访问工作表的单元格
访问单元格
Aspose.Cells提供一个代表Excel文件的 Workbook 类。Workbook 类包含一个 Worksheets 集合,允许访问Excel文件中的每个工作表。工作表由 Worksheet 类表示。Worksheet 类提供一个 Cells 集合,表示工作表中的所有单元格。
我们可以使用 Cells 集合来访问工作表中的单元格。Aspose.Cells提供了三种基本方法来访问工作表中的单元格:
- 使用单元格名称。
- 使用单元格的行和列索引。
- 在 Cells 集合中使用单元格索引
使用单元格名称
开发人员可以通过将单元格名称传递给 Worksheet 类的 Cells 集合作为索引来访问任何特定单元格。
如果在开始时创建一个空白工作表,那么 Cells 集合的计数为零。当您使用这种方法访问单元格时,它会检查该单元格是否存在于集合中。如果存在,则返回集合中的单元格对象,否则将创建一个新的 Cell 对象,将对象添加到 Cells 集合中,然后返回该对象。这种方法是在熟悉 Microsoft Excel 的情况下访问单元格的最简单方法,但与其他方法相比,它是最慢的。
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleData = dirPath + u"sampleData.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleData); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Get cells from sheet | |
Cells cells = worksheet.GetCells(); | |
//Accessing a cell using its name | |
Cell cell = cells.Get(u"B3"); | |
//Write string value of the cell on console | |
std::cout << "Value of cell B3: " | |
<< cell.GetStringValue().ToUtf8() << std::endl; | |
Aspose::Cells::Cleanup(); |
使用单元格的行和列索引
开发人员可以通过将其行和列的索引传递给 Worksheet 类的 Cells 集合来访问任何特定单元格。该方法与第一种方法的工作方式相同。
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleData = dirPath + u"sampleData.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleData); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Get cells from sheet | |
Cells cells = worksheet.GetCells(); | |
//Accessing cell B3 using its row and column index | |
Cell cell = cells.Get(2, 1); | |
//Write string value of the cell on console | |
std::cout << "Value of cell B3: " | |
<< cell.GetStringValue().ToUtf8() << std::endl; | |
Aspose::Cells::Cleanup(); |
访问工作表的最大显示范围
Aspose.Cells 允许开发人员访问工作表的最大显示范围。最大显示范围 - 即内容的第一个单元格与最后一个单元格之间的单元格范围 - 在您需要复制、选择或显示工作表的全部内容时非常有用。
您可以使用 Cells 集合的 MaxDisplayRange 方法访问工作表的最大显示范围。
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleData = dirPath + u"sampleData.xlsx"; | |
//Read input excel file | |
Workbook workbook(sampleData); | |
//Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Get cells from sheet | |
Cells cells = worksheet.GetCells(); | |
//Access the Maximum Display Range | |
Range range = cells.GetMaxDisplayRange(); | |
//Print string value of the cell on console | |
std::cout << "Maximum Display Range of Worksheet: " | |
<< range.GetRefersTo().ToUtf8() << std::endl; | |
Aspose::Cells::Cleanup(); |