# How to populate a 2D array data into Excel using WIN32OLE

**URL:** <https://www.ruby-forum.com/t/how-to-populate-a-2d-array-data-into-excel-using-win32ole/78234>\
**Category:** Ruby\
**Created:** [December 4, 2006, 11:36am UTC](https://www.ruby-forum.com/t/how-to-populate-a-2d-array-data-into-excel-using-win32ole/78234 "2006-12-04T11:36:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 11:36am UTC](https://www.ruby-forum.com/t/how-to-populate-a-2d-array-data-into-excel-using-win32ole/78234/1 "2006-12-04T11:36:23Z")

</div>

Hi all,

I process a raw data file into a 2D array. The only way I can think of  
to populate it into Excel is to use the following lines, which is  
obviously not a good way:

worksheet.Range(“A1:L1”).Value= f\_2D\_array[0]  
worksheet.Range(“A2:L2”).Value= f\_2D\_array[1]  
worksheet.Range(“A3:L3”).Value= f\_2D\_array[2]  
worksheet.Range(“A4:L4”).Value= f\_2D\_array[3]  
worksheet.Range(“A5:L5”).Value= f\_2D\_array[4]  
worksheet.Range(“A6:L6”).Value= f\_2D\_array[5]  
worksheet.Range(“A7:L7”).Value= f\_2D\_array[6]  
worksheet.Range(“A8:L8”).Value= f\_2D\_array[7]  
…

I want to find another way such that I just mention the starting cell  
and can dump my 2D data automatically. I read some documents about  
WIN32OLE and Excel scripting. But they only mention how to set the value  
for one cell or use the above Range method to set a collection of data.

Any input will be appreciated.

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/how-to-populate-a-2d-array-data-into-excel-using-win32ole/78234/2 "2006-12-04T12:02:55Z")

</div>

Li Chen wrote:

> worksheet.Range(“A5:L5”).Value= f\_2D\_array[4]  
> Any input will be appreciated.
> 
> Thanks,
> 
> Li

Hey

I haven’t tried it, but does this work? :

worksheet.Range(“A1:L#{f\_2D\_array.length}”).Value = f\_2D\_array

Just a wild guess, but it seems a plausible.

Hope it helps,  
Gustav P.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 21, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/how-to-populate-a-2d-array-data-into-excel-using-win32ole/78234/3 "2007-01-21T23:55:22Z")

</div>

Any examples for doing just the opposite? Taking the Excel range and  
putting the data into an array?

dvn

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 4, 2006, 3:01pm UTC](https://www.ruby-forum.com/t/how-to-populate-a-2d-array-data-into-excel-using-win32ole/78234/4 "2006-12-04T15:01:19Z")

</div>

— Gustav P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey
> 
> I haven’t tried it, but does this work? :
> 
> worksheet.Range(“A1:L#{f\_2D\_array.length}”).Value =  
> f\_2D\_array
> 
> Just a wild guess, but it seems a plausible.

Yes it works. Thank you very much, Paul.

Li
