如何在每行和每列中写出输出?
use Win32::OLE;
@a = (1..5);
@b = (20..30);
@c = (30..48);
@array = ("@a","@b","@c");
my $xl = Win32::OLE->new('Excel.Application');
$xl->{EnableEvents} = 0;
$xl->{Visible} = 1;
my $wb = $xl->Workbooks->Add;
my $sht = $wb->Sheets(1);
$sht->{Name} = "Occurance";
foreach $s (@array){
@each = split(' ',$s);
$col++;
foreach (@each){
$row++;
$sht->cells($row,$col)->{Value} = "$_";
}
}
在这个脚本中给出输出,但我希望数组值“@b”写入列B,第1行写入数组的末尾。然后“@c”写入列c,第1行写入数组末尾。怎么办?
答案 0 :(得分:0)
你没有在列循环中初始化$ row。
你拥有的是:
foreach $s (@array){
$col++;
foreach (@$s){
$row++;
$sht->cells($row,$col)->{Value} = "$_";
}
}
添加'my $ row = 0;'
foreach $s (@array){
$col++;
my $row = 0;
foreach (@$s){
$row++;
$sht->cells($row,$col)->{Value} = "$_";
}
}
对于更高级的perl,你可以使@array成为一个数组数组而不是一个字符串数组:
use strict;
use warnings;
use Win32::OLE;
my @a = (1..5);
my @b = (20..30);
my @c = (30..48);
my @array = (\@a,\@b,\@c);
my $xl = Win32::OLE->new('Excel.Application');
$xl->{EnableEvents} = 0;
$xl->{Visible} = 1;
my $wb = $xl->Workbooks->Add;
my $sht = $wb->Sheets(1);
$sht->{Name} = "Occurance";
my $col = 0;
foreach my $s (@array){
$col++;
my $row = 0;
foreach my $item (@$s){
$row++;
$sht->cells($row,$col)->{Value} = $item;
}
}