[Perl][Win32::OLE]判断Excel表单元格是否为合并单元格

原帖:[Perl][Win32::OLE]判断单元格是否为合并单元格

$o = $sheet->Cells(1, 2)->{'MergeCells'};

是合并单元格时返回1,不是返回0

Code: [全选] [展开/收缩] [Download] (Untitled.pl)

use Win32::OLE qw (in with);
use Win32::OLE::Const ('Microsoft Excel');
use Win32::OLE::Variant;
use Cwd;
use Encode;
use IO::Handle;
STDOUT->autoflush(1);
 
my $fold = getcwd();  #完整路径
$fold=~s/\//\\/;
 
our $ex;
our $book;
our $sheet;
 
load_sheet( $fold ."\\info.xlsx", 2 );  #文件名,sheet表的编号
 
my $o = $sheet->Cells(1, 2);            #row, column
print $o->{'MergeCells'};
 
 
print $sheet->Cells(1, 2)->{'MergeCells'};
 
over();
exit;
 
sub load_sheet
{
    my ($file, $sh_num) = (shift, shift);
 
    # 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 {${file}->Quit;})
                or die "Oops, cannot start Excel";
    }
 
    # get a new workbook
    $book = $ex->Workbooks->open( $file );
 
    # write to a particular cell
    $sheet = $book->Worksheets( $sh_num );
}
 
sub over
{
    # save and exit
    $ex->{DisplayAlerts} = 'False';
    $book->Save();
    undef $sheet;
    undef $book;
    undef $ex;
}