Create Named Range in a Workbook
Contents
[
Hide
]
Possible Usage Scenarios
Aspose.Cells supports the creation of a named range. There are different ways to create a named range. One of the simplest ways is to first create IRange object and then set its name using IRange.SetName() method. You can see all the named ranges inside your excel file via Microsoft Excel Name Manager interface.
Create Named Range in a Workbook
The following sample code explains how to create a Named Range via Aspose.Cells. Once, the Named Range is created, it is visible inside the IWorkbook.GetIWorksheets().GetINames() collection. Please see the output excel file generated by the code for a reference.
Sample Code
This file contains hidden or 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-C | |
//Path of output excel file | |
StringPtr outputCreateNamedRange = dirPath->StringAppend(new String("outputCreateNamedRange.xlsx")); | |
//Create a workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Access first worksheet | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Create a range | |
intrusive_ptr<IRange> rng = ws->GetICells()->CreateIRange((intrusive_ptr<String>)new String("A5:C10")); | |
//Set its name to make it named range | |
rng->SetName((intrusive_ptr<String>)new String("MyNamedRange")); | |
//Read the named range created above from names collection | |
intrusive_ptr<IName> nm = wb->GetIWorksheets()->GetINames()->GetObjectByIndex(0); | |
//Print its FullText and RefersTo memebers | |
StringPtr fullTect = new String("Full Text : "); | |
Console::WriteLine(fullTect->StringAppend(nm->GetFullText())); | |
StringPtr referTo = new String("Refers To: "); | |
Console::WriteLine(referTo->StringAppend(nm->GetRefersTo())); | |
//Save the workbook in xlsx format | |
wb->Save(outputCreateNamedRange, SaveFormat_Xlsx); |
Console Output
The following console output prints the values of GetFullText and GetRefersTo
methods of the created Named Range in the above code.
Full Text: MyNamedRange
Refers To: =Sheet1!$A$5:$C$10