Interface INames
A collection of all the Name objects in the application or workbook. Each Name object represents a defined name for a range of cells.
Namespace: Syncfusion.XlsIO
Assembly: Syncfusion.XlsIO.Portable.dll
Syntax
public interface INames : IEnumerable
Remarks
Below these rules are should be followed, when you're creating a named range in Excel.
- The first character of a name must be one of the following characters: letter, underscore (_), backslash ().
- Remaining characters in the name can be letters, numbers, periods, underscore characters.
- The following are not allowed: Space characters are not allowed as part of a name. Names can't look like cell addresses, such as A$35 or R2D2. C, c, R, r - can't be used as names. Excel uses them as selection shortcuts.
- Names are not case sensitive. For example, North and NORTH are treated as the same name. To know more about Named Ranges refer this link.
Properties
Application
Used without an object qualifier, this property returns an IApplication object that represents the Excel application.
Declaration
IApplication Application { get; }
Property Value
Type |
---|
IApplication |
Remarks
To know more about named ranges refer this link.
Examples
The following code illustrates use of Application property in named range collection.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet sheet = workbook.Worksheets[0];
//Gets object that represents the Excel application
IApplication namedApplication = sheet.Names.Application;
if (namedApplication != null)
{
//Your Code here
}
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Count
Returns the number of objects in the collection.
Declaration
int Count { get; }
Property Value
Type |
---|
System.Int32 |
Remarks
To know more about named ranges refer this link.
Examples
The following code illustrates use of Count property in named range collection.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet sheet = workbook.Worksheets[0];
//Gets the number of objects in the collection
int namedRangesCount = sheet.Names.Count;
if (namedRangesCount != 0)
{
//Your Code here
}
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Item[Int32]
Returns a single IName object from a Names collection.
Declaration
IName this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index |
Property Value
Type |
---|
IName |
Remarks
To know more about Named Ranges refer this link.
Examples
The following code illustrates how to get IName using index.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Student ID";
sheet.Range["B1"].Text = "Student Name";
sheet.Range["C1"].Text = "Mark(Out of 100)";
sheet.Range["A1:C1"].AutofitColumns();
sheet.Range["A2"].Number = 1;
sheet.Range["A3"].Number = 2;
sheet.Range["B2"].Text = "Andrew";
sheet.Range["B3"].Text = "Marson";
sheet.Range["C2"].Number = 77;
sheet.Range["C3"].Number = 88;
//Create a Named Range using WorkBook
IName namedTitle = workbook.Names.Add("NamedTitle");
//Returns the index number of the object within the collection of similar objects
workbook.Names[0].Name = "Titles";
workbook.Names[0].Value = "Sheet1!$A$1:$C$1";
//Create a Named Range using WorkSheet
IName namedData = sheet.Names.Add("NamedData");
//Returns the index number of the object within the collection of similar objects
sheet.Names[0].Name = "Records";
sheet.Names[0].Value = "Sheet1!$A$2:$C$3";
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Item[String]
Returns a single IName object from a names collection.
Declaration
IName this[string name] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.String | name |
Property Value
Type |
---|
IName |
Remarks
To know more about Named Ranges refer this link.
Examples
The following code illustrates how to get IName using string.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Student ID";
sheet.Range["B1"].Text = "Student Name";
sheet.Range["C1"].Text = "Mark(Out of 100)";
sheet.Range["A1:C1"].AutofitColumns();
sheet.Range["A2"].Number = 1;
sheet.Range["A3"].Number = 2;
sheet.Range["B2"].Text = "Andrew";
sheet.Range["B3"].Text = "Marson";
sheet.Range["C2"].Number = 77;
sheet.Range["C3"].Number = 88;
//Create a Named Range using WorkBook
IName namedTitle = workbook.Names.Add("NamedTitle");
//Returns a single Name object from a Names collection
workbook.Names["NamedTitle"].Name = "Titles"; //Here Name is changed from NamedTitle to Titles
workbook.Names["Titles"].Value = "Sheet1!$A$1:$C$1";
//Create a Named Range using WorkSheet
IName namedData = sheet.Names.Add("NamedData");
//Returns a single Name object from a Names collection
sheet.Names["NamedData"].Name = "Records"; //Here Name is changed from NamedData to Records
sheet.Names["Records"].Value = "Sheet1!$A$2:$C$3";
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Parent
Returns the parent object for the specified object.
Declaration
object Parent { get; }
Property Value
Type |
---|
System.Object |
Remarks
To know more about Named Ranges refer this link.
Examples
The following code illustrates use of Parent property in named range collection.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet sheet = workbook.Worksheets[0];
//Gets the parent object for the specified object
object namedParent = sheet.Names.Parent;
if(namedParent != null)
{
//Your Code Here
}
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
ParentWorksheet
Returns parent IWorksheet of the collection.
Declaration
IWorksheet ParentWorksheet { get; }
Property Value
Type |
---|
IWorksheet |
Remarks
To know more about named ranges refer this link.
Examples
The following code illustrates use of ParentWorksheet property in named range collection.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet sheet = workbook.Worksheets[0];
//Gets the number of objects in the collection
IWorksheet namedParentWorksheet = sheet.Names.ParentWorksheet;
if(namedParentWorksheet != null)
{
//Your Code Here
}
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Methods
Add(IName)
This method is used to defines a new name.
Declaration
IName Add(IName name)
Parameters
Type | Name | Description |
---|---|---|
IName | name | IName object to add. |
Returns
Type | Description |
---|---|
IName | Returns a IName object. |
Remarks
To know more about named ranges refer this link.
Examples
The following code illustrates how to use Add method in named range.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Student ID";
sheet.Range["B1"].Text = "Student Name";
sheet.Range["C1"].Text = "Mark(Out of 100)";
sheet.Range["A1:C1"].AutofitColumns();
sheet.Range["A2"].Number = 1;
sheet.Range["A3"].Number = 2;
sheet.Range["B2"].Text = "Andrew";
sheet.Range["B3"].Text = "Marson";
sheet.Range["C2"].Number = 77;
sheet.Range["C3"].Number = 88;
IName namedStuName = workbook.Names.Add("StudentName", sheet.Range["B1"]);
//method is defines a new name
IName namedData = sheet.Names.Add(namedStuName);
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Add(String)
This method is used to defines a new name.
Declaration
IName Add(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name for the new Name object. |
Returns
Type | Description |
---|---|
IName | Returns a IName. |
Remarks
To know more about named ranges refer this link.
Examples
The following code illustrates how to use Add method in named range.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Student ID";
sheet.Range["B1"].Text = "Student Name";
sheet.Range["C1"].Text = "Mark(Out of 100)";
sheet.Range["A1:C1"].AutofitColumns();
sheet.Range["A2"].Number = 1;
sheet.Range["A3"].Number = 2;
sheet.Range["B2"].Text = "Andrew";
sheet.Range["B3"].Text = "Marson";
sheet.Range["C2"].Number = 77;
sheet.Range["C3"].Number = 88;
//Create a Named Range using WorkBook
IName namedTitle = workbook.Names.Add("Titles");
namedTitle.RefersToRange = sheet.Range["A1:C1"];
//Create a Named Range using WorkSheet
IName namedData = sheet.Names.Add("Records");
namedData.RefersToRange = sheet.Range["A2:C3"];
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Add(String, IRange)
This method is used to defines a new name.
Declaration
IName Add(string name, IRange namedObject)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name for the new Name object. |
IRange | namedObject | Range that will be associated with the name. |
Returns
Type | Description |
---|---|
IName | Returns a Name object. |
Remarks
To know more about named ranges refer this link.
Examples
The following code illustrates how to use Add method in named range.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Student ID";
sheet.Range["B1"].Text = "Student Name";
sheet.Range["C1"].Text = "Mark(Out of 100)";
sheet.Range["A1:C1"].AutofitColumns();
sheet.Range["A2"].Number = 1;
sheet.Range["A3"].Number = 2;
sheet.Range["B2"].Text = "Andrew";
sheet.Range["B3"].Text = "Marson";
sheet.Range["C2"].Number = 77;
sheet.Range["C3"].Number = 88;
//Create a Named Range with add method for workbook
IName namedTitle = workbook.Names.Add("NamedTitle", sheet.Range["A1:C1"]);
//Create a Named Range with add method for worksheet
IName namedData = sheet.Names.Add("NamedData", sheet.Range["A2:C3"]);
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Contains(String)
Checks whether the Name object is present in the collection or not.
Declaration
bool Contains(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name object to check whether it is present or not. |
Returns
Type | Description |
---|---|
System.Boolean | True if given name is found in Names collection. |
Remarks
To know more about Named Ranges refer this link.
Examples
The following code illustrates how to use Contains method in named range.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Student ID";
sheet.Range["B1"].Text = "Student Name";
sheet.Range["C1"].Text = "Mark(Out of 100)";
sheet.Range["A1:C1"].AutofitColumns();
sheet.Range["A2"].Number = 1;
sheet.Range["A3"].Number = 2;
sheet.Range["B2"].Text = "Andrew";
sheet.Range["B3"].Text = "Marson";
sheet.Range["C2"].Number = 77;
sheet.Range["C3"].Number = 88;
IName namedStuID = workbook.Names.Add("StudentID", sheet.Range["A1"]);
IName namedStuName = workbook.Names.Add("StudentName", sheet.Range["B1"]);
IName namedStuMark = workbook.Names.Add("StudentMark", sheet.Range["C1"]);
//Checks whether the Name object is present in the collection or not
bool isContainsNamedRange = workbook.Names.Contains("StudentMark");
if(isContainsNamedRange)
{
//Your Code Here
}
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
Remove(String)
Removes IName object from the collection.
Declaration
void Remove(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of the object to remove from the collection. |
Remarks
To know more about named ranges refer this link.
Examples
The following code illustrates how to use Remove method in named range.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Student ID";
sheet.Range["B1"].Text = "Student Name";
sheet.Range["C1"].Text = "Mark(Out of 100)";
sheet.Range["A1:C1"].AutofitColumns();
sheet.Range["A2"].Number = 1;
sheet.Range["A3"].Number = 2;
sheet.Range["B2"].Text = "Andrew";
sheet.Range["B3"].Text = "Marson";
sheet.Range["C2"].Number = 77;
sheet.Range["C3"].Number = 88;
IName namedStuID = workbook.Names.Add("StudentID", sheet.Range["A1"]);
IName namedStuName = workbook.Names.Add("StudentName", sheet.Range["B1"]);
IName namedStuMark = workbook.Names.Add("StudentMark", sheet.Range["C1"]);
//Removes Name object from the collection
workbook.Names.Remove("StudentID");
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}
RemoveAt(Int32)
Removes the element at the specified index of the collection.
Declaration
void RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | The zero-based index of the element to remove. |
Examples
The following code illustrates how to use Remove method in named range.
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Create a worksheet.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Student ID";
sheet.Range["B1"].Text = "Student Name";
sheet.Range["C1"].Text = "Mark(Out of 100)";
sheet.Range["A1:C1"].AutofitColumns();
sheet.Range["A2"].Number = 1;
sheet.Range["A3"].Number = 2;
sheet.Range["B2"].Text = "Andrew";
sheet.Range["B3"].Text = "Marson";
sheet.Range["C2"].Number = 77;
sheet.Range["C3"].Number = 88;
IName namedStuID = workbook.Names.Add("StudentID", sheet.Range["A1"]);
IName namedStuName = workbook.Names.Add("StudentName", sheet.Range["B1"]);
IName namedStuMark = workbook.Names.Add("StudentMark", sheet.Range["C1"]);
//Removes Name object from the collection
workbook.Names.RemoveAt(0);
workbook.SaveAs("NamedRange.xlsx");
workbook.Close();
}