Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use warnings;
- use strict;
- use FindBin qw($Bin);
- use File::Spec::Functions qw( catfile );
- use Win32::OLE qw(in);
- use Win32::OLE::Const 'Microsoft Excel';
- $Win32::OLE::Warn = 3;
- my $excel = get_excel();
- $excel->{Visible} = 1; # for illustration only
- my @src = map $excel->Workbooks->Open($_),
- map catfile($Bin, $_),
- qw(one.xls two.xls)
- ;
- my $target = $excel->Workbooks->Add(xlWBATWorksheet);
- my $before = $target->Worksheets->Item(1);
- for my $book ( @src ) {
- my $sheets = $book->Worksheets;
- my $it = Win32::OLE::Enum->new($sheets);
- while (defined(my $sheet = $it->Next)) {
- $sheet->Copy($before);
- }
- }
- $before->Delete;
- $_->Close for @src;
- $target->SaveAs(catfile($Bin, 'test.xls'));
- $target->Close;
- sub get_excel {
- my $excel = Win32::OLE->GetActiveObject('Excel.Application');
- unless(defined $excel) {
- $excel = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit })
- or die "Oops, cannot start Excel: ",
- Win32::OLE->LastError, "n";
- }
- return $excel;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement