Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.