I checked the "writemany.pl" example and found it easier to achieve my goal by writing just a "formatmany" method. Eventually what I really need is formatting in one go with predefined formats.

Below is the code I have so far (work in progress!)

The problem I have with this - the format doesn't seem to have any effect in the resulting file no matter what style settings I select. Perhaps some monk sees the problem I can't find ...

Thanks for your time, Axel.

#!perl -l015 use strict; use Spreadsheet::WriteExcel; my ( @arr, @format, $rows, $cols ); @arr = ( [ "a11", "12", "a13" ], [ "a21", "22", "a23" ], [ "a31", "32", "a33" ], ); my $outputfile = "output.xls"; my $workbook = Spreadsheet::WriteExcel->new($outputfile); my $worksheet = $workbook->addworksheet("a test"); my $number = $workbook->addformat( font => "Helvetica", size => 18, num_format => "000000.00", align => "right", ); my $string = $workbook->addformat( font => "Helvetica", size => 18, align => "left", bold => 1, ); @format = ( [ $string, $number, $string ] ); $worksheet->writemany( 0, 0, \@arr ); $worksheet->formatmany(@format); $workbook->close(); chomp( my $pwd = `pwd` ); my $file = $pwd . $outputfile; $^O =~ /Mac/ and MacPerl::DoAppleScript( <<eos ); tell application "Microsoft Excel" open "$file" activate end tell eos package Spreadsheet::WriteExcel::Worksheet; sub formatmany { my ( $self, @format ) = @_; my ( $rows, $cols ) = $self->checkdim(@format); print "DEBUG: got ( $rows, $cols ) format elements"; return if $cols == 0 or $rows == 0; # colums only if ( $rows == 1 ) { print "DEBUG: setting columns only"; for ( 0 .. $cols - 1 ) { my $colformat = $format[0]->[$_]; die "no format object found" unless ref $colformat eq "Spreadsheet::WriteExcel::Format"; $self->set_column( $_, $_, 20, $colformat ); } } elsif ( $cols == 0 ) { # yadda-yadda ... $self->set_row(); } else { # yadda-yadda ... } } sub writemany { my ( $self, $row, $col, $ref, $options ) = @_; # If this is an arrayref, go through it if ( ref($ref) eq "ARRAY" ) { # Work out the direction we're going my $direction = $options->{direction} || "row"; # Work out the converse direction my $otherdirection = { row => "col", col => "row" }->{$direction}; # Cycle through for (@$ref) { $self->writemany( $row, $col, $_, { direction => $otherdirection, format => $options->{format} || undef } ); $direction eq "row" ? $row++ : $col++; } } else { # It's a simple scalar value (or something that we don't # handle), so pass it through to write $self->write( $row, $col, $ref, $options->{format} ); } } sub checkdim { my ( $self, @arr ) = @_; my $rows = 0; for (@arr) { $rows++ } my $maxcols = 0; for ( 0 .. $rows - 1 ) { my $row = $_; my $cols = 0; for ( 0 .. $#{ $arr[$row] } ) { $cols++ } $maxcols = $cols > $maxcols ? $cols : $maxcols; } return ( $rows, $maxcols ); }
(please change #!perl -l015 for your needs, I'm on MacPerl in the moment, writemany() taken from John's example)

In reply to Re: writing with WriteExcel in OO style by axelrose
in thread writing with WriteExcel in OO style by axelrose

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.