使用命名范围
Contents
[
Hide
]
通常,您可以使用工作表上列和行的标签来引用这些列和行内的单元格。但是您可以创建描述性名称来代表单元格、单元格范围、公式或固定值。名称可能指代表示单元格、单元格范围、公式或固定值的一串字符。例如,使用易于理解的名称,比如“Products”,来引用难以理解的范围,比如“Sales!C20:C30”。标签可以用于引用同一工作表上的数据的公式;如果要表示另一工作表上的范围,则可以使用名称。命名范围是Microsoft中功能最强大的特性之一。用户可以为命名范围分配一个名称,这样就可以在公式中使用该名称来引用这一范围的单元格。Aspose.Cells.GridDesktop确实支持此功能。
在公式中添加/引用命名范围
GridDesktop控件支持在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-.NET | |
// Clear the Worsheets first | |
_grid.Clear(); | |
// The path to the documents directory. | |
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Specifying the path of Excel file using ImportExcelFile method of the control | |
_grid.ImportExcelFile(dataDir + "book1.xlsx"); | |
// Apply a formula to a cell that refers to a named range "Rang1" | |
_grid.Worksheets[0].Cells["G6"].SetCellValue("=SUM(Range1)"); | |
// Add a new named range "MyRange" with based area A2:B5 | |
int index = _grid.Names.Add("MyRange", "Sheet1!A2:B5"); | |
// Apply a formula to G7 cell | |
_grid.Worksheets[0].Cells["G7"].SetCellValue("=SUM(MyRange)"); | |
// Calculate the results of the formulas | |
_grid.RunAllFormulas(); | |
// Save the Excel file | |
_grid.ExportExcelFile(dataDir + @"ouputBook1_out.xlsx"); |