Dear Monks,
I'm trying to insert a 4x4 array into an excel document. I've got as far as opening it, creating two sheets (for later) and inserting single cells. However... when I try to insert an array to a range it doesn't work. Would some kind soul be able to tell me where I'm going wrong?
My other question is, if I wanted to loop through this array from @array[0][0] to @array
44 is there a way to reference the Excel cells by X and Y numerically rather than X alphabetically and Y numerically?
Would be much appreciated.
#!/usr/bin/perl -w
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
my $excel_file = 'C:/work/james_test.xls';
# -e checks to see if the file exists
if (-e "$excel_file")
{
# open Excel file
my $Book = $Excel->Workbooks->Open("$excel_file");
}
else{
$Excel->{Visible} = 0;
$Excel->{DisplayAlerts}=0; #0 is hide alerts
$Excel->{SheetsInNewWorkBook} = 1;
my $Workbook = $Excel->Workbooks->Add();
my $sheet1 = $Workbook->Worksheets(1);
$sheet1->{Name} = "WorkSheet1";
my $sheet2 = $Workbook->Worksheets->Add();
$sheet2->{Name} = "WorkSheet 2";
my @average_values_table = (
[ "1", "2", "3", "4" ],
[ "5", "8", "7", "8"],
[ "1", "2", "3", "4" ],
[ "5", "8", "7", "8"],
);
$sheet1 -> Range("A1:D4") -> {Value} = @average_values_table;
$Workbook->SaveAs($excel_file);
$Workbook->Close();
$Excel->Quit();
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.