Home >Backend Development >C++ >How to Correctly Write an Array of Objects to an Excel Range?
Write array to Excel range
Writing an array of objects to an Excel range can be a challenging task. However, it can be achieved with the right approach.
Question:
Attempting to write an array of strings to a range causes each cell to contain the value of the first item in the array.
Solution:
To successfully write an array to a region, use the following method:
<code class="language-c#">object[,] arr = new object[objData.GetLength(0), objData.GetLength(1)]; for (int r = 0; r < objData.GetLength(0); r++) { for (int c = 0; c < objData.GetLength(1); c++) { arr[r, c] = objData[r, c]; } } range.Resize[arr.GetLength(0), arr.GetLength(1)].Value2 = arr;</code>
This method creates a two-dimensional array objData
from the input string array arr
. It then sets the region to the appropriate size and writes the value from arr
to the region.
Note: This method requires the objData
array to be a rectangular array to work properly.
The above is the detailed content of How to Correctly Write an Array of Objects to an Excel Range?. For more information, please follow other related articles on the PHP Chinese website!