- 论坛徽章:
- 0
|
以下是CPAN中关于Win32::OLE的例子,其中“ # write a 2 rows by 3 columns range”部分为写入了一个2行3列的内容,而在“# print "XyzzyPerl"”部分为读取了Worksheets(1)中的内容,并放到多维数组$array中。
问题1:现在我想从硬盘上读取一xls格式的文件,放入数组$array中,该如何修改呢?
问题2:以上代码(仅仅2行3列)运行时间怎么需要近3s时间,效率怎么能提高呢?
#!/usr/bin/perl -W
use Win32::OLE;
# use existing instance if Excel is already running
eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
die "Excel not installed" if $@;
unless (defined $ex) {
$ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Excel";
}
# get a new workbook
$book = $ex->Workbooks->Add;
# write to a particular cell
$sheet = $book->Worksheets(1); #sheet为工作表的号码
$sheet->Cells(1,2)->{Value} = "joson"; #元素写入的位置
# write a 2 rows by 3 columns range
$sheet->Range("C2:E3")->{Value} = [[ 22, 'Xyzzy', 'Plugh' ], #Range为表中的位置
[ 42, 'Perl', 3.1415 ]];
# print "XyzzyPerl"
$array = $sheet->Range("A8:C9")->{Value};
for (@$array) {
for (@$_) {
print defined($_) ? "$_|" : "<undef>|";
}
print "\n";
}
# save and exit
$book->SaveAs( 'test.xls' );
undef $book;
undef $ex; |
|