Readdy Write

C #: Converting a 2D Object Array to 2D String Array

30.10.2018 (๐Ÿ‘116)


 

Task:

This code example in C # shows how to transform a two-dimensional object [,] array into a 2-dimensional string [,] array.

The array consists of rows and columns, which result from the Excel-Used-Range in Interop.Excel

 

This shows in C #:

On the left, the objects [,] array of rows and columns from Excel

On the right the string [,] array, which contains only strings and no null values.

 

Subject:

2 dimensional array in C #, Windows Forms

 

Transformation Code C #

For the objects [,] array to string [,] array

Excel.Range usedRange = _worksheet.UsedRange;

log_with_Date("get UsedRange", dtStart);

 

//*fast Excel-Read: 

//< create 2D Array >

//*from excel with cell-content-object

object[,] arrObjectValues = usedRange.Value2;

 

int nRows = arrObjectValues.GetLength(0);

int nColumns = arrObjectValues.GetLength(1);

string[,] arrStringValues = new string[nRows, nColumns];

//</ create 2D Array >

 

//--< convert array object to string >--

for (int iRow = 0; iRow < nRows ; iRow++)

{

    for(int iCol=0;iCol<nColumns ; iCol++)

    {

        //< Convert object to string >

        arrStringValues[iRow, iCol] = Convert.ToString(arrObjectValues[iRow+1, iCol+1]);

        //</ Convert object to string >

    }

}

//--</ convert array object to string >--

 

 


0,00 €