Articles in this section
Category / Section

How to convert an Excel worksheet cell ranges into a two dimensional array using C#, VB.NET?

4 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. 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.

Create a new C# console application.png

Step 2: Install the Syncfusion.XlsIO.Net.Core NuGet package as reference to your .NET Core applications from NuGet.org.

Install nuget package.png

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 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;

   //Loads an existing workbook
   IWorkbook workbook = application.Workbooks.Open(fileStream);
   IWorksheet worksheet = workbook.Worksheets[0];

   //Set the range to convert into two dimensional array
   IRange range = worksheet["B1:E5"];
   string[,] arrayOfArrays = ConvertIRangeToArray(worksheet, range);
}

//Converting range values into 2 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

   ' Loads an existing workbook
   Dim workbook As IWorkbook = application.Workbooks.Open(fileStream)
   Dim worksheet As IWorksheet = workbook.Worksheets(0)

   ' Set the range to convert into two-dimensional array
   Dim range As IRange = worksheet("B1:E5")
   Dim arrayOfArrays As String(,) = ConvertIRangeToArray(worksheet, range)
End Using

' Converting range values into a 2-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.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied