Hi everyone! I'm running into a problem trying to read an existing sheet, formatting some of the cells differently (font, cell fill color, etc.) and then writing to a new spreadsheet. I'm reading in each cell's properties, adjusting some of the properties of some of the cells, and reproducing the cell in the new spreadsheet. To begin this effort, I simply wanted to read in an existing sheet, save the properties of each cell, then write them out to the new spreadsheet. However, I'm noticing that some of the cells are not being reproduced with the same properties as the original cells. Here's my code:
#!c:\perl64\bin\perl.exe use strict; use warnings; use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; my $result_file = "new.xls"; my $property_file = "properties.txt"; open (PROP, ">$property_file"); my $parser = Spreadsheet::ParseExcel->new(); my $workbook_parse = $parser->parse( 'old.xls' ); if ( !defined $workbook_parse ) { die $parser->error(), "Error processing workbook parse.\n"; } my $worksheet_parse = $workbook_parse->worksheet( 'Sheet1' ); my ( $col_min, $col_max ) = $worksheet_parse->col_range(); my ( $row_min, $row_max ) = $worksheet_parse->row_range(); my $workbook = Spreadsheet::WriteExcel->new( $result_file ); my $worksheet = $workbook->addworksheet( 'Sheet1' ); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_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]; my $wrap = $format->{Wrap}; my $font = $format->{Font}; my $fontcolor = $font->{Color}; my $bold = $font->{Bold}; my $alignH = $format->{AlignH}; my $alignV = $format->{AlignV}; ## Apply format to cell per previous findings in above logic my $updformat = $workbook->add_format( pattern => $pattern, fg_color => $color1, bg_color => $color2, align => 'left', valign => 'top', text_wrap => $wrap, border => 1, color => $fontcolor, bold => $bold ); $worksheet->write( $row, $col, $value, $updformat); ## Write out cell properties to our properties.txt document print PROP "\nRow, Col = ($row, $col)\n"; print PROP "Format is: $format\n"; print PROP "Pattern = $pattern\n"; print PROP "Value = $value\n"; print PROP "Fill = $pattern $color1 $color2\n"; print PROP "Wrap = $wrap\n"; print PROP "Font = $fontcolor\n"; print PROP "Bold = $bold\n"; print PROP "AlignH = $alignH\n"; print PROP "AlignV = $alignV\n"; } } ## Housekeeping $workbook->close() or die "Error closing file: $!"; close(PROP); exit;
You can use this with any Excel 97-2003 spreadsheet, just name it: old.xls and the resulting spreadsheet will be named: new.xls When you view the new.xls you will see what I mean. I'm sure it's something quite simple. Your help is much appreciated! Thank you, in advance.

In reply to Spreadsheet::ParseExcel / WriteExcel question .... by jhalbrook

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.