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

#!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); } } }
poj

In reply to Re: Getting the background color of a cell in an existing Excel spreadsheet by poj
in thread Getting the background color of a cell in an existing Excel spreadsheet by venky1937

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.