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. It 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 an 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 a reference to your .NET Framework application from NuGet.org.
Install the NuGet package to the project.
Step 3: Add an Excel file to your 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 the main method of the Program.cs file to extract a row from an 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 the 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 the row into an array if the row is in the 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 the 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 the 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.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to extract Excel row to an array in C#, VB.NET.
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!
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!