#!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("test.xls"); my $worksheet = $workbook->add_worksheet(); my $format_heading_yellow = $workbook->add_format(bg_color => 'yellow' ); my $format_heading_pink = $workbook->add_format(bg_color => 'magenta'); my $format_heading_blue = $workbook->add_format(bg_color => 'blue' ); my $format_heading_green = $workbook->add_format(bg_color => 'green' ); my $condition = "a"; my $format_heading = getFormat($condition); $worksheet->write("A1", "sometext", $format_heading); $condition = "c"; $format_heading = getFormat($condition); $worksheet->write("A2", "sometext", $format_heading); $condition = "d"; $format_heading = getFormat($condition); $worksheet->write("A3", "sometext", $format_heading); $condition = "b"; $format_heading = getFormat($condition); $worksheet->write("A4", "sometext", $format_heading); sub getFormat { $condition = shift; if ($condition eq 'a'){ return $format_heading_yellow; } elsif ($condition eq 'b'){ return $format_heading_pink; } elsif ($condition eq 'c'){ return $format_heading_blue; } else { return $format_heading_green; # Default color } }