Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: writing with WriteExcel in OO style

by axelrose (Scribe)
on May 13, 2002 at 21:17 UTC ( #166299=note: print w/replies, xml ) Need Help??


in reply to writing with WriteExcel in OO style

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 ); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://166299]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-11-29 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?