How to convert an Excel worksheet cell ranges into a two dimensional array using 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.
While working with ranges in Excel, you can convert range values into two-dimensional array. This article explains how to convert range values into a two-dimensional array using C#/VB.NET.
Steps for converting range values into a two-dimensional array programmatically:
Step 1: Create a new C# console application project.
Step 2: Install the Syncfusion.XlsIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
Step 3: Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 4: Use the following code snippet for converting range values into a two-dimensional array.
C#
// Create an instance of ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
// Set the default application version as Xlsx
application.DefaultVersion = ExcelVersion.Xlsx;
// Load an existing workbook
IWorkbook workbook = application.Workbooks.Open(fileStream);
IWorksheet worksheet = workbook.Worksheets[0];
// Set the range to convert into a two-dimensional array
IRange range = worksheet["B1:E5"];
string[,] arrayOfArrays = ConvertIRangeToArray(worksheet, range);
}
// Convert range values into two-dimensional array
public static string[,] ConvertIRangeToArray(IWorksheet worksheet, IRange range)
{
int startRow = range.Row;
int startCol = range.Column;
int endRow = range.LastRow;
int endCol = range.LastColumn;
string[,] numbers = new string[endRow - startRow + 1, endCol - startCol + 1];
for (int i = 0; i <= endRow - startRow; i++)
{
for (int j = 0; j <= endCol - startCol; j++)
{
numbers[i, j] = worksheet[startRow + i, startCol + j].Value;
Console.Write(numbers[i, j]);
Console.Write("\t");
}
Console.Write("\n______________________________________________\n");
}
return numbers;
}
VB.NET
' Create an instance of ExcelEngine
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
' Set the default application version as Xlsx
application.DefaultVersion = ExcelVersion.Xlsx
' Load an existing workbook
Dim workbook As IWorkbook = application.Workbooks.Open(fileStream)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
' Set the range to convert into a two-dimensional array
Dim range As IRange = worksheet("B1:E5")
Dim arrayOfArrays As String(,) = ConvertIRangeToArray(worksheet, range)
End Using
' Convert range values into a two-dimensional array
Public Shared Function ConvertIRangeToArray(worksheet As IWorksheet, range As IRange) As String(,)
Dim startRow As Integer = range.Row
Dim startCol As Integer = range.Column
Dim endRow As Integer = range.LastRow
Dim endCol As Integer = range.LastColumn
Dim numbers(endRow - startRow, endCol - startCol) As String
For i As Integer = 0 To endRow - startRow
For j As Integer = 0 To endCol - startCol
numbers(i, j) = worksheet(startRow + i, startCol + j).Value
Console.Write(numbers(i, j))
Console.Write(vbTab)
Next
Console.Write(vbCrLf & "______________________________________________" & vbCrLf)
Next
Return numbers
End Function
A complete working example of converting range values into a two-dimensional array can be downloaded from 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 with code examples.
Refer here to explore the rich set of Syncfusion® Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or 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 convert an Excel worksheet cell ranges into a two dimensional array using 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!