[Perl][Win32::OLE]Excel2007单元格填充颜色 - RGB色彩过渡

There's more than one way to do it!
https://metacpan.org http://perlmonks.org
回复
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

[Perl][Win32::OLE]Excel2007单元格填充颜色 - RGB色彩过渡

帖子 523066680 »

use Win32::OLE qw (in with);
use Win32::OLE::Const ('Microsoft Excel');
use Win32::OLE::Variant;
use IO::Handle;
STDOUT->autoflush(1);
use Cwd;
my $fold = getcwd();
$fold=~s/\//\\/g;

our $ex;
our $book;
our $sheet;
our $mxrow=100;
&load_sheet($fold ."\\Color.xlsx");

my ($i, $j, $v, $w);
our (@A, @B);

for ($i=0; $i<20; $i+=1)
{
for ($j=0; $j<20; $j+=1)
{
$v = &color3f(
$i/20.0,
$j/20.0,
0.0
);
$sheet->Cells($i+1 , $j+1)->Interior->{Color} = $v;
}
}

for ($i=0; $i<20; $i+=1)
{
for ($j=20; $j<40; $j+=1)
{
$v = &color3f(
$i/20.0,
1-($j-20)/20.0,
($j-20)/20.0 * (1-$i/20.0)
);
$sheet->Cells($i+1 , $j+1)->Interior->{Color} = $v;
}
}

&over();

sub color3f
{
my ($r, $g, $b) = (shift, shift, shift);
#area [0 ~ 1.0];
my $RGB;
$RGB = int($r * 255)
+ int($g * 255) * 256
+ int($b * 255) * 256 * 256;
#这里面有些蛋疼的浮点数问题,
# $g * 255 * 256 要写成 int($g * 255) * 256
return $RGB;
}

sub color_by_int
{
my ($r, $g, $b) = (shift, shift, shift);
#area [0 ~ 1.0];
my $RGB;
$RGB = $r + ($g * 256) + ($b * 256 * 256);
return $RGB;
}


sub load_sheet
{
my $xlsfile = shift;
our $ex;
our $book;
our $sheet;
# 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->open( $xlsfile );

# write to a particular cell
$sheet = $book->Worksheets(1);
}

sub over
{
our $ex;
our $book;
our $sheet;
# save and exit
$ex->{DisplayAlerts} = 'False';
$book->Save();
undef $sheet;
undef $book;
undef $ex;
}
执行前需要手动新建 Color.xlsx 文件
samplexlsx.png
(12.65 KiB) 已下载 382 次
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 6 访客