,壞消息是我認爲它幾乎不能完成的電子表格:: WriteExcel。
好消息是,它可以很容易地使用Excel ::作家:: XLSX完成。這恰好是Spreadsheet :: WriteExcel的一種後裔。請閱讀文章:Spreadsheet::WriteExcel is dead. Long live Excel::Writer::XLSX
下面的代碼不正是你想要的格式(僅基於單元格A1,而不是RC10,這是可以改變的,當然):
#!/usr/bin/perl -w
use strict;
use Excel::Writer::XLSX;
my @matrix = (
['xxxx', '<-- Change the value in cell A1 to change the colour of row 4'],
[qw(Redyard Kipling)],
[qw(If--)],
[qw(If you can keep your head when all about you)],
[qw(Are losing theirs and blaming it on you;)],
);
writeSpreadsheet('conditional.formatting.xlsx', \@matrix);
sub writeSpreadsheet {
my ($outFile, $matrix) = @_;
my $MIN_COL_WIDTH = 5;
my $MAX_COL_WIDTH = 35;
my $workbook = Excel::Writer::XLSX->new($outFile);
my $worksheet = $workbook->add_worksheet();
my $redFormat = $workbook->add_format(font => 'Arial', color => 'red');
my $greenFormat = $workbook->add_format(font => 'Arial', color => 'green', bold => 1);
$worksheet->set_row(0, undef,
$workbook->add_format(font => 'Arial', align => 'center', bold => 1));
$worksheet->conditional_formatting('A4:Z4',
{
type => 'formula',
criteria => '=$A$1 = "xxxx"',
format => $greenFormat
}
);
$worksheet->conditional_formatting('A4:Z4',
{
type => 'formula',
criteria => '=$A$1 = "yyyyyy"',
format => $redFormat
}
);
foreach my $row (0 .. $#$matrix) {
foreach my $col (0 .. $#{$matrix->[$row]}) {
$worksheet->write($row, $col, $matrix->[$row][$col] || '');
}
}
}
WriteExcel的API與XLSX完全兼容? – patseb
是的。參見[Excel :: Writer :: XLSX和Spreadsheet :: WriteExcel](http://search.cpan.org/~jmcnamara/Excel-Writer-XLSX/lib/Excel/Writer/XLSX.pm#Excel::Writer :: XLSX_and_Spreadsheet :: WriteExcel)部分的文檔。 – jmcnamara