How to Get Column Names from Range with Headers in WinForms XlsIO?
This article explains how to retrieve the column names from a named range with column headers using our WinForms XIsIO’s feature tour page.
XlsIO does not have any direct way of retrieving column names from a named range with column headers. However, there are a few workarounds to achieve this. One approach is exporting the named range to data table and retrieving the column names from data table. The alternative is processing the named range directly.
XlsIO always considers the first row of the named range as the column header. Therefore, the column names can be retrieved by accessing the first row of the named range.
To learn more about exporting data to data table, please refer the documentation.
The following complete code snippet explains how to retrieve column names of a named range with column headers by processing it.
C#
using Syncfusion.XlsIO; using System.Collections.Generic; using System.IO; using System.Reflection; namespace ColumnNames_NamedRange { class Program { static void Main(string[] args) { using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2013; // Opening existing workbook with named range Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream inputStream = assembly.GetManifestResourceStream("ColumnNames_NamedRange.Sample.xlsx"); IWorkbook workbook = application.Workbooks.Open(inputStream); // Accessing named range IName namedRange = workbook.Names["DataValue"]; List<string> columnName = new List<string>(); int firstrow = namedRange.RefersToRange.Row; // Since the first row of named range is considered as column header, accessing the first row to get the column name for (int i = 1; i <= namedRange.RefersToRange.Columns.Length; i++) { columnName.Add(namedRange.RefersToRange[firstrow, i].Text); } // Adding the column names to another worksheet IWorksheet worksheet = workbook.Worksheets.Create("Column Names"); for (int i = 1, j = 0; i <= columnName.Count; i++, j++) { worksheet[i, 1].Text = columnName[j]; } // Saving the workbook as stream Stream outputStream = File.Create("Output.xlsx"); workbook.SaveAs(outputStream); } } } }
VB
Imports System.IO Imports System.Reflection Imports System.Collections.Generic Imports Syncfusion.XlsIO Namespace ColumnNames_NamedRange Class Program Private Shared Sub Main(ByVal args() As String) Using excelEngine As ExcelEngine = New ExcelEngine Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2013 'Opening existing workbook with named range Dim assembly As Assembly = GetType(Program).GetTypeInfo.Assembly Dim inputStream As Stream = assembly.GetManifestResourceStream("ColumnNames_NamedRange.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(inputStream) 'Accessing named range Dim namedRange As IName = workbook.Names("DataValue") Dim columnName As List(Of String) = New List(Of String) Dim firstrow As Integer = namedRange.RefersToRange.Row 'Since the first row of named range is considered as column header, accessing the first row to get the column name Dim i As Integer = 1 Do While (i <= namedRange.RefersToRange.Columns.Length) columnName.Add(namedRange.RefersToRange(firstrow, i).Text) i = (i + 1) Loop 'Adding the column names to another worksheet Dim worksheet As IWorksheet = workbook.Worksheets.Create("Column Names") i = 1 Dim j As Integer = 0 Do While (i <= columnName.Count) worksheet(i, 1).Text = columnName(j) i = (i + 1) Loop 'Saving the workbook as stream Dim outputStream As Stream = File.Create("Output.xlsx") workbook.SaveAs(outputStream) End Using End Sub End Class End Namespace
Conclusion
I hope you enjoyed learning about how to Get Column Names from Range with Headers in WinForms XlsIO.
You can refer to our XIsIO’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, Support Tickets, or feedback portal. We are always happy to assist you!