Extract Excel row to an array in C#, VB.NET
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can extract a row from Excel worksheet into an array.
Steps to extract a row from Excel worksheet to an array, programmatically:
Step 1: Create a C# console application project.
Create a new C# console application project
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package to the project
Step 3: Add an Excel file to you project and make it an embedded resource using the following steps.
- In Visual Studio, click the Project menu and select Add Existing Item. Find and select the Excel file you want to add to your project.
- In the Solution Explorer window, right-click on the Excel file you just added to your project and select Properties from the popup menu. The Properties tool window appears.
- In the Properties window, change the Build Action property to Embedded Resource.
- Build the project. The Excel file will be compiled into your project’s assembly.
Step 4: Include the following namespaces in Program.cs file.
C#
using Syncfusion.XlsIO; using System; using System.IO; using System.Reflection;
VB.NET
Imports Syncfusion.XlsIO Imports System.IO Imports System.Reflection
Step 5: Include the following code snippet in main method of Program.cs file to extract a row from Excel worksheet into an array.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the Excel application object IApplication application = excelEngine.Excel; //Load an existing Excel file into IWorkbook Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("ExtractRow.Sample.xlsx"); IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic); //Get the first worksheet in workbook into IWorksheet IWorksheet worksheet = workbook.Worksheets[0]; //Get the row and column length int rowCount = worksheet.UsedRange.Rows.Length; int columnCount = worksheet.UsedRange.Columns.Length; //Initialize a string array string[] rowValues = new string[columnCount]; //Get the row to extract data from Console.WriteLine("Enter the row from which you want to extract data : "); int n = Convert.ToInt16(Console.ReadLine()); //Get the values in row into array, if the row is in used range if (n <= rowCount) { Console.WriteLine("Values in the worksheet row are : "); for (int i = 0; i < columnCount; i++) { rowValues[i] = worksheet.Range[n, i + 1].Value; Console.WriteLine("rowValues[" + i.ToString() + "] = " + rowValues[i].ToString()); } } else { Console.WriteLine("Row - " + n.ToString() + " is not in the used range of worksheet and is empty"); } Console.ReadLine(); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Load an existing Excel file into IWorkbook Dim assembly As Assembly = GetType(Module1).GetTypeInfo.Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("ExtractRow.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic) 'Get the first worksheet in workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Get the row and column length Dim rowCount As Integer = worksheet.UsedRange.Rows.Length Dim columnCount As Integer = worksheet.UsedRange.Columns.Length 'Initialize a string array Dim rowValues() As String = New String(columnCount - 1) {} 'Get the row to extract data from Console.WriteLine("Enter the row from which you want to extract data : ") Dim n As Integer = Convert.ToInt16(Console.ReadLine) 'Get the values in row into array, if the row is in used range If (n <= rowCount) Then Console.WriteLine("Values in the worksheet row are : ") For i As Integer = 0 To columnCount - 1 Step 1 rowValues(i) = worksheet.Range(n, i + 1).Value Console.WriteLine("rowValues[" + i.ToString() + "] = " + rowValues(i).ToString()) Next Else Console.WriteLine("Row - " + n.ToString + " is not in the used range of worksheet and is empty") End If Console.ReadLine() End Using
A complete working sample to extract a row from Excel worksheet into an array can be downloaded from ExtractRow.zip.
By executing the program, you will get the console window as follows.
Console window asking for row index
On entering the row index, you will get the output in the command prompt as follows.
Row in Excel worksheet extracted into an array
Take a moment to peruse the documentation, where you will find other options like move or copy a worksheet, freeze, unfreeze and split panes, show or hide worksheet and worksheet tabs, page setup settings and more with code examples.
Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.