How to access entire named range count in a workbook?
The named ranges in a workbook are separated into two types based on their values. If the value is a valid range, then it is known as a known named range. If the value is an invalid range (i.e., #REF), then it is known as an unknown named range. We have maintained two properties to return the count of named ranges with and without unknown named ranges.
Property | Description |
INames.Count | Returns the count of the known named range. |
WorkbookNamesCollection.Count | Returns the count of the named range, including the unknown named range. |
To access the entire named range count, the INames object must be type-casted to WorkbookNamesCollection. Please refer to the code example below to access the entire named range count in a workbook.
C#
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
listView1.Items.Clear();
if (0 == textBox1.Text.Length || !File.Exists(textBox1.Text))
return;
IWorkbook Workbook = application.Workbooks.OpenReadOnly(textBox1.Text);
if (null != Workbook)
{
WorkbookNamesCollection Names = Workbook.Names as WorkbookNamesCollection;
for (int i = 0; i < Names.Count; i++)
{
NameRangeData Data = new NameRangeData(Workbook.Names[i]);
listView1.Items.Add(Data.ListItem);
}
}
MessageBox.Show("Named ranges count is " + listView1.Items.Count);
Workbook.Version = ExcelVersion.Excel2013;
Workbook.SaveAs(@"../../Output/Output.xlsx");
Workbook.Close();
excelEngine.Dispose();
VB
Dim excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
listView1.Items.Clear()
If 0 = textBox1.Text.Length Or Not File.Exists(textBox1.Text) Then
Return
End If
Dim Workbook As IWorkbook = application.Workbooks.OpenReadOnly(textBox1.Text)
If Nothing <> Workbook Then
Dim Names As WorkbookNamesCollection = Workbook.Names as WorkbookNamesCollection
Dim i As Integer
For i = 0 To Names.Count - 1 Step i + 1
Dim Data As NameRangeData = New NameRangeData(Workbook.Names(i))
listView1.Items.Add(Data.ListItem)
Next
End If
MessageBox.Show("Named ranges count is " + listView1.Items.Count)
Workbook.Version = ExcelVersion.Excel2013
Workbook.SaveAs("../../Output/Output.xlsx")
Workbook.Close()
excelEngine.Dispose()
The sample illustrating this behavior can be downloaded here.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to access the entire named range count in a workbook.
You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!