in reply to Getting the background color of a cell in an existing Excel spreadsheet
fill is a combination of pattern and 2 colors so for a solid pattern the color you want is probably in the first color
$format->{Fill}->[1]. You also need to create a new format object for each color rather that re-using the same one with set_bg_color
Try this
poj#!perl use strict; use Data::Dumper; use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook_parse = $parser->parse( 'Report.xls' ); my $worksheet_parse = $workbook_parse->Worksheet( "Health_Report" ); my ( $col_min, $col_max ) = $worksheet_parse->col_range(); my ( $row_min, $row_max ) = $worksheet_parse->row_range(); my $workbook = Spreadsheet::WriteExcel->new( "Report_new.xls" ); my $worksheet = $workbook->addworksheet( "Health_Report" ); my %bkgd_color = (); for my $col ( $col_min .. $col_max ) { for my $row ( $row_min .. $row_max ) { # Return the cell object at $row and $col my $cell = $worksheet_parse->get_cell( $row, $col ); next unless $cell; my $value = $cell->value(); my $format = $cell->get_format(); my $pattern = $format->{Fill}->[0]; my $color1 = $format->{Fill}->[1]; my $color2 = $format->{Fill}->[2]; print "Row, Col = ($row, $col) "; print "Value = $value\n"; print "Fill = $pattern $color1 $color2\n"; ### Here trying to rewrite into Excel and apply the ### same background color which the cell had previously if ($pattern == 1 ){ if ( ! exists $bkgd_color{$color1} ){ $bkgd_color{$color1} = $workbook->addformat( pattern => $pattern, bg_color => $color1 ); } $worksheet->write( $row, $col, $value, $bkgd_color{$color1}) } else { $worksheet->write( $row, $col, $value); } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Getting the background color of a cell in an existing Excel spreadsheet
by thanos1983 (Parson) on Apr 19, 2017 at 11:32 UTC | |
by poj (Abbot) on Apr 19, 2017 at 12:14 UTC |