Perl Win32::OLE csv2excel
戻る
# csv2excel.pl 07.11.28
# 複数CSVファイルを1つのexcelファイルに縮約
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
my(%t,$array);
# use existing instance if Excel is already running
eval {$t{ex} = Win32::OLE->GetActiveObject('Excel.Application')};
die "Excel not installed" if $@;
unless (defined $t{ex}) {
$t{ex} = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Excel";
}
# get a new workbook
$t{book} = $t{ex}->Workbooks->Add;
# write to a particular cell
$t{sheet} = $t{book}->Worksheets(1);
$t{sheet}->Cells(2,2)->{Value} = "foo";
# write a 2 rows by 3 columns range
$t{sheet}->Range("A8:C9")->{Value} = [[ undef, 'Xyzzy', 'Plugh' ],
[ 82, 'Perl', 3.1415 ]];
# 線を引く
$t{sheet}->Range("B5:D5")->Borders(xlEdgeBottom)->{LineStyle} = xlDouble;
# print "XyzzyPerl"
$array = $t{sheet}->Range("A7:C9")->{Value};
for (@$array) {
for (@$_) {
print defined($_) ? "$_|" : "|";
}
print "\n";
}
$t{book}->Sheets->Add("C:\\test\\g.csv"); # ==>成功していない!
# save and exit
$t{book}->SaveAs("C:\\test\\test.xls");
undef $t{book};
undef $t{ex};
戻る