<?xml version="1.0" encoding="windows-1252"?>
<node id="44448" title="Handy wrapper for Spreadsheet::WriteExcel" created="2000-12-01 19:02:55" updated="2005-08-15 08:17:30">
<type id="1748">
sourcecode</type>
<author id="29594">
nop</author>
<data>
<field name="doctext">
&lt;code&gt;package Excel;
use strict;
use warnings;
use diagnostics;
use Spreadsheet::WriteExcel;

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = {};
    bless ($self, $class);
    $self-&gt;init(@_);
    return $self;
}

sub init {
    my ($self, $fname) = @_;
    $self-&gt;{workbook} = Spreadsheet::WriteExcel-&gt;new($fname);
}

sub getformat {
    my ($self, @fmts) = @_;
    my $format = $self-&gt;{workbook}-&gt;addformat();
    # metafmts allows you define combinations of formats, like CSS
    # may be nested, may not be circular
    my %metafmts = (
		    header =&gt; [qw(bold blue)]
		    );
    while (@fmts) {
	$_ = shift @fmts;
	if ($metafmts{$_}) {push(@fmts, @{$metafmts{$_}}); delete $metafmts{$_}; next;} 
	if (/default/) {
	    $format-&gt;set_format(3); # commas, no decimals
	} elsif (/bigmoney/) {
	    $format-&gt;set_format(5); # dollar sign, commas, no decimals
	} elsif (/money/) {
	    $format-&gt;set_format(7); # dollar sign, commas, pennies
	} elsif (/pct/) {
	    $format-&gt;set_format(10); # percent, two digit
	} elsif (/bold/) {
	    $format-&gt;set_bold(1); # bold on
	} elsif (/sumline/) {
	    $format-&gt;set_bottom(6); # double underline
	} elsif (/border/) {
	    $format-&gt;set_border(1); # border all around
	} elsif (/size(\d+)/) {
	    $format-&gt;set_size(0+$1); # size
	} elsif (/italic/) {
	    $format-&gt;set_italic(1); # italic
	} elsif (/underline/) {
	    $format-&gt;set_underline(1); # text underline
	} elsif (/left|right|center/) {
	    $format-&gt;set_align($_); # alignment
	} elsif (/black|blue|red|green|purple|silver|yellow|gray|orange/) {
	    $format-&gt;set_color($_); # colors
	} else {
	    die "unknown or circular format string: $_";
	}
    }
    return $format;
}

sub writecell {
    my ($self, $worksheet, $row, $col, $value) = @_;
    my ($val, @formats);
    if (!ref($value)) {
	$val = $value; @formats = qw(default);
    } elsif (ref($value) eq 'ARRAY') {
	$val = shift(@{$value});  @formats = @{$value};
    } else {die ref($value);}
    my $fmtobj = $self-&gt;getformat(@formats);
    $self-&gt;{worksheet}{$worksheet}{ptr}-&gt;write($row,$col, $val, $fmtobj);
    
}

sub write {
    my ($self, $worksheet, @vals) = @_;
    # if this is a new tab, create it
    if (!defined($self-&gt;{worksheet}{$worksheet}{ptr})) {
	$self-&gt;{worksheet}{$worksheet}{ptr} =
	    $self-&gt;{workbook}-&gt;addworksheet($worksheet);
	$self-&gt;{worksheet}{$worksheet}{row} = 0;
	$self-&gt;{worksheet}{$worksheet}{col} = 0;
    }
    my $row = $self-&gt;{worksheet}{$worksheet}{row};
    my $col = $self-&gt;{worksheet}{$worksheet}{col};
    foreach (@vals) {
	$self-&gt;writecell($worksheet,$row,$col,$_);
	$col++;
    }
    $self-&gt;{worksheet}{$worksheet}{col} = $col;
}

sub writeln {
    my ($self, $worksheet, @vals) = @_;
    $self-&gt;write($worksheet, @vals);
    $self-&gt;{worksheet}{$worksheet}{row}++;
    $self-&gt;{worksheet}{$worksheet}{col} = 0;
}

1;
    
&lt;/code&gt;


</field>
<field name="codedescription">
This is a small wrapper around Spreadsheet::WriteExcel.  It lets you quickly open a workbook, and spit data into it by row by column (analogous to writing a tab delim text file, or using &lt;code&gt;write&lt;/code&gt; and &lt;code&gt;writeln&lt;/code&gt; in Pascal). Sometimes it is easier to think of generating a report by row by column, rather than popping all around the sheet.&lt;br&gt;&lt;br&gt;
The package also supports multiple tabs (sheets) nicely, setting them up automatically the first time a new tab is referenced.  The package also supports common formats, and allows you to create "metaformats" (ala cascading style sheets). &lt;br&gt;&lt;br&gt;
Here's a example that shows the module in action:
&lt;code&gt;
use strict;
use warnings;
use diagnostics;

use Excel;

my $excel = Excel-&gt;new("test_excel1.xls");

$excel-&gt;writeln('tab1', "Header");
$excel-&gt;writeln('tab1');
$excel-&gt;writeln('tab1', qw(1 3 4 5 6));
$excel-&gt;writeln('tab1', qw(A B C D E F));

$excel-&gt;writeln('tab1');
$excel-&gt;writeln('tab1', qw(1.1 1000 101011.33 -14 -14.555 .000001));
$excel-&gt;writeln('tab1', map {[$_, 'money']} qw(1.1 1000 101011.33 -14 -14.555 .000001));
$excel-&gt;writeln('tab1', map {[$_, 'bigmoney']} qw(1.1 1000 101011.33 -14 -14.555 .000001));
$excel-&gt;writeln('tab1', map {[$_, 'pct']} qw(1.1 1000 101011.33 -14 -14.555 .000001));


$excel-&gt;writeln('tab1', "VErrry long striiiiiiiiiiiiiiiiiiiiiiiiiiing");

$excel-&gt;writeln('tab1', ['hello', 'bold'], [.003, 'pct','bold']);

foreach (qw(1 2 3 4 5)) {
    $excel-&gt;write('tab2', [$_, 'money', 'bold']);
    
}
$excel-&gt;writeln('tab2', 'hello', [.003, 'pct','bold'], 'goodbye', 99999999999999);

foreach (qw(red blue yellow green orange)) {
    $excel-&gt;writeln('tab3', 'hello', [$_, $_, 'bold']);
}

$excel-&gt;writeln('tab4', ['i am a header', 'header']);
$excel-&gt;writeln('tab4', ['i am a header', 'header', 'sumline']);
$excel-&gt;writeln('tab4', ['i am a header', 'header', 'size20']);








&lt;/code&gt;

</field>
<field name="codecategory">
Win32 Stuff</field>
<field name="codeauthor">
[nop]</field>
</data>
</node>
