[Athens-pm] Perl under WIndows

Angelos Karageorgiou angelos at unix.gr
Wed Apr 21 14:22:08 CDT 2004


As I promised in the meeting , a windows example


#!perl


#
# Example of Driving Excel via Perl . You need activeperl
#

use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
use Win32::OLE::Variant;
use Win32::OLE::NLS qw(:LOCALE :DATE);

$Win32::OLE::Warn = 3; # Die on Errors.


my $excelfile = '\irene\fx-x3.xls';

my $Excel = Win32::OLE->GetActiveObject('Excel.Application') ||
Win32::OLE->new('Excel.Application', 'Quit');
$Excel->{Visible} = 1;
$Excel->{DisplayAlerts}=0;


my $Book = $Excel->Workbooks->Add();
$Book->SaveAs($excelfile); #Good habit when working with OLE, save often.
$Book = $Excel->Workbooks->Open($excelfile);


my $Sheet = $Book->Worksheets(1);
$Sheet->Activate();
$Sheet->{Name} = "Plotter";

# my ($mday,$mon,$year) = (localtime(time))[3,4,5];
#      $year += 1900;
#
#  my $str = $mon.'/'.$mday.'/'.$year;





$Sheet->Range("A1")->{Value} = "x";
$Sheet->Range("B1")->{Value} = "f(x)";

#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
$i=1;
for ($x=-100;$x<100;$x++){
	$i++;
#	$fx=5*$x*$x*$x-25*$x*$x-3000;
	$fx=5*ln($x)-30;


       my $range = 'a'.$i;
       $Sheet->Range($range)->{Value} = $x;
	$range = 'b'.$i;
       $Sheet->Range($range)->{Value} = $fx;

}
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


$Book->Save();

my $range="A1:B".$i;
my $Range= $Sheet->Range($range);

my $Chart = $Excel->Charts->Add;
$Chart->{ChartType} = xlXYScatter;
$Chart->SetSourceData({Source => $Range, PlotBy => xlColumns});
$Chart->{HasTitle} = 1;
$Chart->ChartTitle->{Text} = "Function Plot";



$Book->Save();
#$Book->Close();
0;
--
Angelos Karageorgiou
Masters of Computer Science
City University of New York
http://www.unix.gr




More information about the Athens-pm mailing list