Perl and Excel
am 06.04.2007 07:06:13 von aaron80vHi,
I modified slightly the sample codes shown here,
http://www.ngbdigital.com/perl_ole_excel.html. However, when I tried
to run it, it keeps generating the error, "Can't use an undefined
value as a HASH reference at ole5.pl line 27." which is the line that
says "my $LastRow = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row};"
Can I point me to why I am facing this problem?
My environments:
1. Windows XP SP2
2. Excel 2002
3. Active Perl-5.8.8.817
The sample code I am running is
========================
use strict;
use warnings;
use Win32::OLE;
use Win32::OLE qw(in with);
use Win32::OLE::Variant;
use Win32::OLE::Const 'Microsoft Excel';
my $Excel = Win32::OLE->GetActiveObject('Excel.Application') ||
Win32::OLE->new('Excel.Application');
$Excel->{'Visible'} = 0; #0 is hidden, 1 is visible
$Excel->{DisplayAlerts}=0; #0 is hide alerts
# Open File and Worksheet
my $Book = $Excel->Workbooks->Open
('E:\tmp\test1.xls'); # open Excel file
my $Sheet = $Book->Worksheets(1);
# Create New Workbook
$Excel->{SheetsInNewWorkBook} = 1;
my $Book2 = $Excel->Workbooks->Add();
my $Sheet2 = $Book2->Worksheets(1);
$Sheet2->{Name} = 'My test worksheet';
# Find Last Column and Row
my $LastRow = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row};
my $LastCol = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious, SearchOrder=>xlByColumns})->{Column};
my $mylastcol = '';
for (my $m=1;$m<$LastCol;$m++){$mylastcol++;}
my $mylastcol2 = '';
for (my $m=1;$m<($LastCol - 1);$m++){$mylastcol2++;}
# Draw Borders
my @edges = qw (xlInsideHorizontal xlInsideVertical);
my $range = "a1:$mylastcol$LastRow";
foreach my $edge (@edges)
{
with (my $Borders = $Sheet->Range($range)->Borders(eval($edge)),
LineStyle =>xlContinuous,
Weight => xlThin ,
ColorIndex => 1);
}
# Cell Values
$Sheet->Range('a1')->{Value} = $Sheet->Range('b2')->{Value};
# Resize Columns
my @columnheaders = qw(A:B);
foreach my $range(@columnheaders){
$Sheet->Columns($range)->AutoFit();
}
# Order Rows
my $tmp = "$mylastcol2".'3';
my $Rangea = $Sheet->Range("$tmp");
my $Rangeb = $Sheet->Range("a3");
$Excel->Selection->Sort({Key1 => $Rangea,
Order1 => xlDescending,
Key2 => $Rangeb});
# Merge Cells
my $mynextcol = 'b';
for (my $n=1;$n<$LastCol;$n+=2)
{
my $range = $mynextcol++ . '1:' . $mynextcol++ . '1';
$Sheet->Range($range)->Merge();
$Sheet->Range($range)->{HorizontalAlignment} = xlHAlignCenter;
}
# Pie Chart
my $Range = $Sheet->Range('a1:d2');
my $Chart = $Book->Charts->Add;
$Chart->ChartWizard($Range,xl3DPie,7,xlRows,1,0,2,"Sales
Percentages");
# Bar Graph and Rotate
$Range = $Sheet->Range('a1:a3');
$Chart = $Excel->Charts->Add;
$Chart->{Type} = xl3DColumn;
for (my $i = 30; $i <=180; $i+=10)
{
$Chart->{Rotation} = $i;
sleep(1);
}
# Line Chart and Save
$Range = $Sheet->Range('a1:d2');
$Chart = $Excel->Charts->Add;
$Chart->{ChartType} = xlLine;
$Chart->SetSourceData({Source => $Range, PlotBy => xlColumns});
$Chart->{HasTitle} = 1;
$Chart->ChartTitle->{Text} = "Some Title";
my $ChartObj = $Sheet->ChartObjects;
$Chart->Export({
FileName => my $filegraphname,
FilterName => 'GIF',
Interactive => 0});
# Save as PDF
$Excel->ActiveWindow->SelectedSheets->PrintOut({
Copy => 1,
ActivePrinter => 'Acrobat PDFWriter'});
# Save as Excel
$Book->SaveAs({Filename =>'E:\tmp\test1.xls',
FileFormat => xlWorkbookNormal});
$Book->Close();
$Excel->Quit();
========================