使用命名范围
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"); |