|
| 1 | +--- |
| 2 | +title: Retrieve the list of named ranges in an Excel workbook | Syncfusion |
| 3 | +description: Code example to retrieve the list of named ranges in an Excel workbook using Syncfusion .NET Excel library (XlsIO). |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to retrieve the list of named ranges in an Excel workbook? |
| 10 | + |
| 11 | +The following code examples demonstrate retrieving the list of named ranges in an Excel workbook using C# (Cross-platform and Windows-specific) and VB.NET. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Named%20Range/.NET/RetrieveNamedRanges/RetrieveNamedRanges/Program.cs,180" %} |
| 15 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 16 | +{ |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 19 | + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); |
| 20 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 21 | + |
| 22 | + //Retrieving names defined in the workbook |
| 23 | + IName[] names = new IName[workbook.Names.Count]; |
| 24 | + for (int i = 0; i < workbook.Names.Count; i++) |
| 25 | + { |
| 26 | + names[i] = workbook.Names[i]; |
| 27 | + Console.WriteLine(names[i].Name); |
| 28 | + } |
| 29 | + |
| 30 | + //Saving the workbook |
| 31 | + workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx")); |
| 32 | +} |
| 33 | +{% endhighlight %} |
| 34 | + |
| 35 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 36 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 37 | +{ |
| 38 | + IApplication application = excelEngine.Excel; |
| 39 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 40 | + IWorkbook workbook = application.Workbooks.Open("Input.xlsx"); |
| 41 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 42 | + |
| 43 | + //Retrieving names defined in the workbook |
| 44 | + IName[] names = new IName[workbook.Names.Count]; |
| 45 | + for (int i = 0; i < workbook.Names.Count; i++) |
| 46 | + { |
| 47 | + names[i] = workbook.Names[i]; |
| 48 | + Console.WriteLine(names[i].Name); |
| 49 | + } |
| 50 | + |
| 51 | + //Saving the workbook |
| 52 | + workbook.SaveAs("Output.xlsx"); |
| 53 | +} |
| 54 | +{% endhighlight %} |
| 55 | + |
| 56 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 57 | +Using excelEngine As New ExcelEngine() |
| 58 | + ' Instantiate the Excel application object |
| 59 | + Dim application As IApplication = excelEngine.Excel |
| 60 | + |
| 61 | + ' Set the default application version |
| 62 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 63 | + |
| 64 | + ' Load the existing Excel workbook into IWorkbook |
| 65 | + Dim workbook As IWorkbook = application.Workbooks.Open("Input.xlsx") |
| 66 | + |
| 67 | + ' Get the first worksheet in the workbook into IWorksheet |
| 68 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 69 | + |
| 70 | + ' Retrieving names defined in the workbook |
| 71 | + Dim names(workbook.Names.Count - 1) As IName |
| 72 | + For i As Integer = 0 To workbook.Names.Count - 1 |
| 73 | + names(i) = workbook.Names(i) |
| 74 | + Console.WriteLine(names(i).Name) |
| 75 | + Next |
| 76 | + |
| 77 | + ' Saving the workbook |
| 78 | + workbook.SaveAs("Output.xlsx") |
| 79 | +End Using |
| 80 | +{% endhighlight %} |
| 81 | +{% endtabs %} |
| 82 | + |
| 83 | +A complete working example in C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Named%20Range/.NET/RetrieveNamedRanges">this GitHub page</a>. |
0 commit comments