使用perl excel win32 ole检索excel单元格背景颜色

时间:2022-05-31 12:17:12

I need to retrieve the background color of an excel cell (set to red/green based on a conditional formatting in the sheet). Tried searching the forums but couldnt find anything to retrieve although setting the colors are there. Any help would be appreciated...

我需要检索excel单元格的背景颜色(根据工作表中的条件格式设置为红色/绿色)。尝试搜索论坛,但无法找到任何东西,虽然设置颜色在那里。任何帮助,将不胜感激...

1 个解决方案

#1


2  

You need $Range->Interior()->ColorIndex();

你需要$ Range-> Interior() - > ColorIndex();

Here is sample program:

这是示例程序:

#!/usr/bin/perl

use Modern::Perl;
use Win32::OLE;
use FindBin qw($Bin);

my $ex;
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";
}

my $book = $ex->Workbooks->Open("$Bin/test_background.xls");

my $sheet = $book->Worksheets(1);
my $Range = $sheet->Range("A1:A1");
say $Range->Interior()->ColorIndex();

$Range = $sheet->Range("B1:B1");
say $Range->Interior()->ColorIndex();

$Range = $sheet->Range("C1:C1");
say $Range->Interior()->ColorIndex();

The output for this file使用perl excel win32 ole检索excel单元格背景颜色 is this:

这个文件的输出是这样的:

3
6
3

#1


2  

You need $Range->Interior()->ColorIndex();

你需要$ Range-> Interior() - > ColorIndex();

Here is sample program:

这是示例程序:

#!/usr/bin/perl

use Modern::Perl;
use Win32::OLE;
use FindBin qw($Bin);

my $ex;
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";
}

my $book = $ex->Workbooks->Open("$Bin/test_background.xls");

my $sheet = $book->Worksheets(1);
my $Range = $sheet->Range("A1:A1");
say $Range->Interior()->ColorIndex();

$Range = $sheet->Range("B1:B1");
say $Range->Interior()->ColorIndex();

$Range = $sheet->Range("C1:C1");
say $Range->Interior()->ColorIndex();

The output for this file使用perl excel win32 ole检索excel单元格背景颜色 is this:

这个文件的输出是这样的:

3
6
3