Thanks to John the set_column() method is now much easier to use. Nonetheless I discarded all approaches I developed in this thread and found the solution below which is much easier to comprehend, I believe.

#!perl -l015 use strict; use Spreadsheet::WriteExcel; my $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 => "0.00", align => "right", ); my $string = $workbook->addformat( font => "Helvetica", size => 18, align => "left", bold => 1, ); my $format = [ [ $string, $number, $string ], [ 16, 12, 16 ] ]; my $line; for my $row (@$arr) { $worksheet->write_formatted_row( 3 + $line++, 0, $row, $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; my $width_is_set = 0; sub write_formatted_row { my ( $self, $rowstart, $colstart, $ref, $format ) = @_; my @arr = @$ref; unless ($width_is_set) { for my $col ( 0 .. $#{ @$format->[1] } ) { my $colwidth = $format->[1][$col]; $self->set_column( $colstart + $col, $colstart + $col, $colwidth, undef ); } $width_is_set = 1; } for my $col ( 0 .. $#arr ) { my $colformat = $format->[0][$col]; die "parameter error: no format objects specified\n" unless ref $colformat eq 'Spreadsheet::WriteExcel::Format'; $self->write( $rowstart, $colstart + $col, $arr[$col], $colformat ); } }

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.