创建工作簿(全局)和工作表范围命名范围
Contents
[
Hide
]
Microsoft Excel 允许用户定义具有两个不同范围的命名范围:工作簿(也称为全局范围)和工作表。
- 只需使用其名称,即可从该工作簿中的任何工作表访问具有工作簿范围的命名范围。
- 使用创建它的特定工作表的引用访问工作表范围内的命名范围。
Aspose.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.getDataDir(AddNamedRangeWithWorkbookScope.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Get Worksheets collection | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = worksheets.get(0); | |
// Get worksheet Cells collection | |
Cells cells = sheet.getCells(); | |
// Creating a workbook scope named range | |
Range namedRange = cells.createRange("A1", "C10"); | |
namedRange.setName("workbookScope"); | |
// Saving the modified Excel file in default format | |
workbook.save(dataDir + "output.xls"); |
添加具有工作表范围的命名范围
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.getDataDir(AddNamedRangeWithWorkbookScope.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Get Worksheets collection | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = worksheets.get(0); | |
// Get worksheet Cells collection | |
Cells cells = sheet.getCells(); | |
// Creating a workbook scope named range | |
Range namedRange = cells.createRange("A1", "C10"); | |
namedRange.setName("Sheet1!local"); | |
// Saving the modified Excel file in default format | |
workbook.save(dataDir + "output.xls"); |