Create Named Range in a Workbook

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 Range object and then set its name using Range.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 Workbook.GetWorksheets().GetNames() collection. Please see the output excel file generated by the code for a reference.

Sample Code

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of output excel file
U16String outputCreateNamedRange = outPath + "outputCreateNamedRange.xlsx";
//Create a workbook
Workbook wb;
//Access first worksheet
Worksheet ws = wb.GetWorksheets().Get(0);
//Create a range
Range rng = ws.GetCells().CreateRange(u"A5:C10");
//Set its name to make it named range
rng.SetName(u"MyNamedRange");
//Read the named range created above from names collection
Name nm = wb.GetWorksheets().GetNames().Get(0);
//Print its FullText and RefersTo memebers
std::cout << "Full Text : " << nm.GetFullText().ToUtf8() << std::endl;
std::cout << "Refers To: " << nm.GetRefersTo().ToUtf8() << std::endl;
//Save the workbook in xlsx format
wb.Save(outputCreateNamedRange, SaveFormat::Xlsx);
Aspose::Cells::Cleanup();

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