I am trying to read an existing Excel spreadsheet and write it into a new Excel document with all the same format options. I am able to read the cell contents and write them into the new Excel sheet, but I cannot get the background color of the cell. If I use the $cell->get_format->{Fill}2 method it gives me numbers like either 64 or 65 if the cell has a background color. How can I get the actual color of a cell and apply the same background color to the cell in new Excel sheet? I am doing all these things as there is no method available in the Spreadsheet::ParseExcel module to set the background color for a cell after appending data to an existing Excel sheet. Here is my code
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 = $workbook->addformat(); 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 $backgroundcolor = $format->{Fill}->[2]; print "Row, Col = ($row, $col) "; print "Value = $value\n"; print "Format = $backgroundcolor\n"; $bkgd_color->set_bg_color( $backgroundcolor ); ### Here trying to rewrite into Excel and apply the ### same background color which the cell had previously $worksheet->write( $row, $col, $value, $bkgd_color ); } }
Part of my output from print statements:
Format = 65 Row, Col = (25, 4) Value = -115966 Format = 65 Row, Col = (10, 5) Value = 20170417 Format = 65 Row, Col = (11, 5) Value = 0 Format = 64 Row, Col = (16, 5) Value = 0 Format = 64

In reply to 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.