perl实现excel画图和写文件

来源:互联网 发布:买车零首付是什么软件 编辑:程序博客网 时间:2024/06/05 06:24

需要用到的包:Win32::OLE;Win32::OLE::Const 'Microsoft Excel';这些包可在CPAN上找到

 

 

例子:


my $Excel = Win32::OLE->new("Excel.Application");
$Excel->{Visible} = 0;

 


#打开一个现成的文件(你可以用对应的文件替换掉F://perl_example//Throughput vs SNR.xls):

my $Book = $Excel->Workbooks->Open("F://perl_example//Throughput vs SNR.xls")|| die("Unable to open document ", Win32::OLE->LastError());
my $Chart = $Book->Charts->Add;
my $Sheet = $Book->Worksheets(1);

 


#图形的类型

$Chart->{ChartType} = xlLineMarkers;
$Chart->SeriesCollection->NewSeries();

#获取当前excel文件中的最大行数
my $Row = $Sheet->{UsedRange}->{Rows}->{Count};


#设置图形的数据源

$Chart->SeriesCollection(1)->{XValues} = "='Throughput vs SNR'!R2C1:R${Row}C1";
$Chart->SeriesCollection(1)->{Values} = "='Throughput vs SNR'!R2C7:R${Row}C7";

$Chart->SeriesCollection(1)->{Name} = "Throughput vs SNR";
$Chart->{'HasTitle'} = 1;
$Chart->ChartTitle->{Text} = 'Throughput vs SNR';

#$Chart->Location({Where=>xlLocationAsObject, Name=>"Throughput vs SNR"});


#保存和关闭

$Book->Save;
$Book->Close;


 

向excel中写数据,则可以使用如下方法:

$Sheet->Cells(1, 1)->{Value} =  "L3-Average";

其中Cells(1, 1)的第一个参数为单元格所在的行号,第二个参数为单元格所在的列号

 

原创粉丝点击