Quick way to add 1D array to Multidimensional array
-
Hi, I am trying to find a quick way to add 1 dimensional array into multidimensional array. For example:
object[] oneDArray1 = new object[10]; object[] oneDArray2 = new object[10]; object[] oneDArray3 = new object[10];
Then, I need to add this array into 2D Multidimensional array.object[,] twoDArray = new object[3, 10];
The reason I need to do this is because I have a DataSet and I need to convert this DataSet to Excel Application. Currently, I do the following:oRng.Value2 = oDataTable.Rows[iRowIdx].ItemArray
This code work if the Range is within single row. Now, if I have 1000 rows, this is process is very slow. It takes about 20 seconds to loop through the range and set the rows. Alternatively, I could set the value of the range at one go. For example I want to set the value from Row #1 Column #1 to Row #1000 Column #1000, I must doobject[,] 2DMultiDimensionalArray = new object[1000, 1000]; oRng = oSheet.get_Range("A1", "ALL1000"); oRng.Value2 = 2DMultiDimensionalArray;
In order to set the excel value at one go, I MUST convert each DataRow.ItemArray into MultiDimensional array. I need to add Single Dimensional Array into MultiDimensional array (NOT jagged array because I have try it and it throw an error).An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in mscorlib.dll
Additional information: Specified array was not of the expected type.
Any help is greatly appreciated! Thanks :)
-
Hi, I am trying to find a quick way to add 1 dimensional array into multidimensional array. For example:
object[] oneDArray1 = new object[10]; object[] oneDArray2 = new object[10]; object[] oneDArray3 = new object[10];
Then, I need to add this array into 2D Multidimensional array.object[,] twoDArray = new object[3, 10];
The reason I need to do this is because I have a DataSet and I need to convert this DataSet to Excel Application. Currently, I do the following:oRng.Value2 = oDataTable.Rows[iRowIdx].ItemArray
This code work if the Range is within single row. Now, if I have 1000 rows, this is process is very slow. It takes about 20 seconds to loop through the range and set the rows. Alternatively, I could set the value of the range at one go. For example I want to set the value from Row #1 Column #1 to Row #1000 Column #1000, I must doobject[,] 2DMultiDimensionalArray = new object[1000, 1000]; oRng = oSheet.get_Range("A1", "ALL1000"); oRng.Value2 = 2DMultiDimensionalArray;
In order to set the excel value at one go, I MUST convert each DataRow.ItemArray into MultiDimensional array. I need to add Single Dimensional Array into MultiDimensional array (NOT jagged array because I have try it and it throw an error).An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in mscorlib.dll
Additional information: Specified array was not of the expected type.
Any help is greatly appreciated! Thanks :)
-
Hi, I am trying to find a quick way to add 1 dimensional array into multidimensional array. For example:
object[] oneDArray1 = new object[10]; object[] oneDArray2 = new object[10]; object[] oneDArray3 = new object[10];
Then, I need to add this array into 2D Multidimensional array.object[,] twoDArray = new object[3, 10];
The reason I need to do this is because I have a DataSet and I need to convert this DataSet to Excel Application. Currently, I do the following:oRng.Value2 = oDataTable.Rows[iRowIdx].ItemArray
This code work if the Range is within single row. Now, if I have 1000 rows, this is process is very slow. It takes about 20 seconds to loop through the range and set the rows. Alternatively, I could set the value of the range at one go. For example I want to set the value from Row #1 Column #1 to Row #1000 Column #1000, I must doobject[,] 2DMultiDimensionalArray = new object[1000, 1000]; oRng = oSheet.get_Range("A1", "ALL1000"); oRng.Value2 = 2DMultiDimensionalArray;
In order to set the excel value at one go, I MUST convert each DataRow.ItemArray into MultiDimensional array. I need to add Single Dimensional Array into MultiDimensional array (NOT jagged array because I have try it and it throw an error).An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in mscorlib.dll
Additional information: Specified array was not of the expected type.
Any help is greatly appreciated! Thanks :)
-
Do you want to export a DataTable to Excel? I am asking, because I would then suggest to use instead of arrays two loops. An outer-loop to do your rows and a inner-loop to do your columns. Much faster than arrays. Regards, Elizma
Hi Elizma, Yes, I am indeed exporting DataTable to Excel. Doing loop for each row and each column will be quick if the number of columns aren't big. Here, I am talking about about 1000+ rows by 1000+ columns. Currently, if I do
for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
{
oRng = oSht.get_Range("A" + iRow.ToString(), strLastCol + iRow.ToString());
oRng.Value2 = (System.Array)dt.Rows[iRow].ItemArray;
}This code is about 30x faster than nested loop. In my PC this loop takes about 20 seconds where as nested loop (to set each cell) takes about 10 minutes. However, if you set directly to MultiDimensional Array, it takes less than 1 second (and probably few seconds to add the 1D array into Multidimensional array). I am starting to think that it is impossible to add 1D array to Multidimensional array without nested loop :( Any other idea to speed up this process? Thanks for any input/help. Cheers :)
-
-