访问工作表的 Cells
Contents
[
Hide
]
我们知道所有工作表都可能包含基本上存储在单元格中的数据(工作表由单元格组成)。单元格是工作表的基本部分,用于将整个工作表构建为一系列行和列。在我们尝试从工作表访问数据之前,我们需要访问其单元格。因此,在本主题中,我们将讨论使用 Aspose.Cells 在运行时访问工作表单元格的一些基本方法。
访问 Cells
Aspose.Cells提供了一个类,工作簿表示 Microsoft Excel 文件。这工作簿类包含一个工作表集合允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了Cells代表工作表中所有单元格的集合。
我们可以使用Cells集合以访问工作表中的单元格。 Aspose.Cells 提供了访问单元格的不同基本方法:
使用 Cell 名称
开发人员可以通过将其单元名称传递给Cells的集合工作表班级。
如果您在开始时创建一个空白工作表,则计数Cells收藏为零。当您使用这种方法访问一个单元格时,它会检查该单元格是否存在于集合中。如果是,它返回集合中的单元格对象,否则,它创建一个新的Cell对象,将对象添加到Cells集合,然后返回对象。如果您熟悉 Microsoft Excel,此方法是访问单元格的最简单方法,但它比其他方法慢。
This file contains 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(UsingCellName.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the worksheet in the Excel file | |
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(0); | |
com.aspose.cells.Cells cells = worksheet.getCells(); | |
// Accessing a cell using its name | |
com.aspose.cells.Cell cell = cells.get("A1"); | |
// Print message | |
System.out.println("Cell Value: " + cell.getValue()); |
使用 Cell 的行和列索引
开发人员可以通过将其行和列的索引传递给Cells的集合工作表班级。
这种方法的工作方式与第一种方法相同。
This file contains 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(UsingRowAndColumnIndexOfCell.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the worksheet in the Excel file | |
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(0); | |
com.aspose.cells.Cells cells = worksheet.getCells(); | |
// Accessing a cell using the indices of its row and column | |
com.aspose.cells.Cell cell = cells.get(0, 0); | |
// Print message | |
System.out.println("Cell Value: " + cell.getValue()); |
相关文章
访问工作表的最大显示范围
Aspose.Cells 允许开发人员访问工作表的最大显示范围。最大显示范围 - 第一个和最后一个有内容的单元格之间的单元格范围 - 当您需要复制、选择或在图像中显示工作表的全部内容时非常有用。
您可以使用访问工作表的最大显示范围工作表.getCells().getMaxDisplayRange().
下图中,选中的工作表最大显示范围为A1:G15。
显示此工作表的最大显示范围
下面的示例代码说明了如何访问最大显示范围财产。该代码生成以下输出。
Maximum Display Range: =Sheet1!$A$1:$G$15
This file contains 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-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(AccessingMaximumDisplayRangeofWorksheet.class) + "data/"; | |
// Instantiate a workbook from source file | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Access the first workbook | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access the Maximum Display Range | |
Range range = worksheet.getCells().getMaxDisplayRange(); | |
// Print the Maximum Display Range RefersTo property | |
System.out.println("Maximum Display Range: " + range.getRefersTo()); |