Crear rango con nombre en un libro de trabajo
Posibles escenarios de uso
Aspose.Cells admite la creación de un rango con nombre. Hay diferentes formas de crear un rango con nombre. Una de las formas más sencillas es crear primeroIRango objeto y luego establezca su nombre usandoIRange.SetName() método. Puede ver todos los rangos con nombre dentro de su archivo de Excel a través de Microsoft ExcelAdministrador de nombresinterfaz.
Crear rango con nombre en un libro de trabajo
El siguiente código de ejemplo explica cómo crear unRango con nombre vía Aspose.Cells. Una vez, elRango con nombre se crea, es visible dentro de laIWorkbook.GetIWorksheets().GetINames() recopilación. Por favor vea elarchivo de salida de Excel generado por el código para una referencia.
Código de muestra
//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); |
Salida de consola
La siguiente salida de la consola imprime los valores deObtenerTextoCompleto y GetRefersTo
métodos del creadoRango con nombreen el código anterior.
Full Text: MyNamedRange
Refers To: =Sheet1!$A$5:$C$10