in reply to OLE write to Excel

There are differences between $mydata and @mydata.

The $mydata scalar is a reference to an array-of-arrays data structure. There are 3 arrays, each with 3 elements.

@mydata is a flat array consisting of 9 elements.

It seems like whatever OLE module you're using requires you to use the reference to an array-of-arrays, not a flat array. Refer to the module documentation. Can you tell us the exact module you are using?

A good reference is perldsc.

Replies are listed 'Best First'.
Re^2: OLE write to Excel
by NewMonk2Perl (Sexton) on Jan 20, 2011 at 15:37 UTC
    I an using the Win32::OLE. Is this module not as good as the other modules to use with Excel? Thanks again for the quick response.

      Hi NewMonk2Perl, Welcome to the Monastery.

      In the Perl community from time to time you'll hear/read "TMTOWTDI", meaning There's more than one way to do it. Have you seen the module Spreadsheet::WriteExcel? You can use it to create Excel documents on various platforms, you don't need excel installed. It has great documentation and Examples.

      #!/usr/bin/perl use strict; use warnings; use Spreadsheet::WriteExcel; my @mydata = ( ['Item', 'Category', 'Price'], ['Nails', 'Hardware', '5.25'], ['Shirt', 'Clothing', '23.00'] ); my $workbook = Spreadsheet::WriteExcel->new('test.xls'); my $worksheet = $workbook->add_worksheet(); $worksheet->write_col('A1', \@mydata);

      If you're new to Perl I'd suggest working through the following:

      Update: I'll leave the formatting of the cells as an exercise for you, it's covered in the Documentation and the examples.

      Update 2:Thanks toolc s/next/need/

        Thank you for all the help and links to help me learn Perl. This is awesome information. There is just so much info on the net, its hard to seperate the good information from not so good information. Thanks again, Paul

      I am using Win32::OLE for automatization of my Excel-tasks, and it works fine for me :-) . The main drawback of the module is that it requires Windows (and in my case also Excel) to be installed - so your scripts are not very portable.

      HTH, Rata
      Is this module not as good as the other modules to use with Excel?
      I have no idea how Win32::OLE compares to other CPAN offerings since I have never used it. For what it's worth, it has received positive reviews.
Re^2: OLE write to Excel
by NewMonk2Perl (Sexton) on Jan 20, 2011 at 16:06 UTC
    # Hey changed the code from $rng->{Value} = @mydata; # To $rng->{Value} = \@mydata;
    It at least ran without errors but instead of using all the data in the array, it only uses the 1st 3 elements and uses it for all the rows.
      I am not sure how to get it to use all the rows with just using "\@mydata". I modified the code to work, but it is very inefficient. Can anyone think of a more efficient way to resolve this issue without using a loop? The code is below:
      # Our data that we will add to the workbook... @mydata = ("Item", "Category", "Price"); @mydata1 = ("Nails", "Hardware", "5.25"); @mydata2 = ("Shirt", "Clothing", "23.00"); @mydata3 = ("Hammer", "Hardware", "16.25"); @mydata4 = ("Sandwich", "Food", "5.00"); @mydata5 = ("Pants", "Clothing", "31.00"); @mydata6 = ("Drinks", "Food", "2.25"); # Write all the data at once... $rngt = $xlBook->ActiveSheet->Range("A1:C7"); $rng = $xlBook->ActiveSheet->Range("A1:C1"); $rng1 = $xlBook->ActiveSheet->Range("A2:C2"); $rng2 = $xlBook->ActiveSheet->Range("A3:C3"); $rng3 = $xlBook->ActiveSheet->Range("A4:C4"); $rng4 = $xlBook->ActiveSheet->Range("A5:C5"); $rng5 = $xlBook->ActiveSheet->Range("A6:C6"); $rng6 = $xlBook->ActiveSheet->Range("A7:C7"); $rng->{Value} = \@mydata; $rng1->{Value} = \@mydata1; $rng2->{Value} = \@mydata2; $rng3->{Value} = \@mydata3; $rng4->{Value} = \@mydata4; $rng5->{Value} = \@mydata5; $rng6->{Value} = \@mydata6;

        Read the code I gave you here, copy it, populate @mydata with all the data you want, run the script.