fionbarr has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: various Excel::Writer::SLSX questions
by roboticus (Chancellor) on Jul 16, 2014 at 20:00 UTC | |
Check the various example programs in the examples directory for Excel::Writer::XLSX. You'll probably want to look at examples/formats.pl and examples/merge1.pl. Generally, if you want examples for a module, check the directories provided in the module. Often there are directories for examples or contributed code. If not, generally the directory containing tests (t) will have example uses. Update: Added links to the example programs. ...roboticus When your only tool is a hammer, all problems look like your thumb. | [reply] |
|
Re: various Excel::Writer::SLSX questions
by bulrush (Scribe) on Jul 17, 2014 at 12:02 UTC | |
Notice how I set up a format, like $fmtdef, then use that in a write statement.
use Spreadsheet::WriteExcel;
my($xlsfnout,$row,$workbook,$sheet);
$xlsfnout = $basefn.".xls";
if (-e $xlsfnout) # First deleted old SS.
{
unlink $xlsfnout or die "ERROR: Could not delete $xlsfn";
}
$workbook = Spreadsheet::WriteExcel->new($xlsfnout);
if (!$workbook)
{
$s="ERROR: Could not create workbook for $xlsfnout";
writeerr($s);
exit 1;
}
# Add a worksheet and set print options.
$sheet = $workbook->add_worksheet('Sheet1');
$sheet->print_row_col_headers(); # Print cell letters and numbers
$sheet->print_across(); # For wide pages that don't fit on one page.
$sheet->set_footer('&CPage &P',0.25);
$sheet->set_header('&C&Z&F&R&D &T',0.25);
$sheet->set_landscape();
$sheet->set_print_scale(80); # Print zoom.
#$sheet->set_paper(3); # 0=printer default, 3 or 4=11x17
$sheet->freeze_panes(1,0); # Freeze top row only
$sheet->set_margins(0.5); # Inches
$sheet->set_margins_LR(0.5); # Inches
#$sheet->set_zoom(75); # '75' = 75%. View zoom.
# Change width for only first column
# Do set_column() or set_row() before any writes.
$sheet->set_column(0,0,20); # (col start, col end, width). Also set_column('A:B',20);
# Define the format and add it to the worksheet
# Define a custom color.
my $tan=$workbook->set_custom_color(40,0xDB,0xB8,0x4d); # (index, r, g, b)
my $fmttan = $workbook->add_format(
font => 'Arial',
size => 10,
color => 'black',
bg_color => $tan,
align => 'left'
);
# Default format.
my $fmtdef = $workbook->add_format(
font => 'Arial',
size => 10,
color => 'black',
align => 'left'
);
$format = $workbook->add_format(
center_across => 1,
bold => 1,
size => 10,
border => 4,
color => 'black',
bg_color => 'cyan',
border_color => 'black',
align => 'vcenter',
);
# Hdr
$t=`pwd`;
chomp($t);
@a=(@a,"Extracted from $filename using ".$t.'/'.$0);
$row=0;
for ($i=0; $i<=$#a; $i++) # Write hdr
{
$sheet->write($row,$i, $a$i, $fmtdef); # (row,col,data,fmt)
}
$row++;
# Write details
for ($i=0; $i<=$#a; $i++) # Write one detail line
{
$sheet->write($row,$i, $a$i, $fmtdef); # (row,col,data,fmt)
}
$row++;
$workbook->close();
Perl 5.8.8 on Redhat Linux RHEL 5.5.56 (64-bit)
| [reply] |
by roboticus (Chancellor) on Jul 17, 2014 at 13:17 UTC | |
Nice example of usage, but it could be improved a bit. Here are just a few suggestions: First, using code tags (<c> your code goes here </c>) is preferred over <pre> and other formatting, because it gives the reader a download link, consistent line wrapping, add the ability for further customization (Syntax coloring via custom javascript, for example. My settings add line numbers so I can easily find the lines given with error messages.) Code tags also prevent the code from being accidentally altered, such as changing $a[$i] to $a$i. I suspect that it also messed up the indentation on your add_format calls as well. If you provide an example, it's best if it compiles and runs--otherwise you risk confusing the person you're trying to help. (In this case, the only problem is that the use of <pre> tags altered the code as mentioned earlier.) Also, you should trim irrelevent parts to focus the example. (In this case, I would remove the $fmttan definition, as it's not used, and I'd remove the code you commented out.) Using the strict and warnings pragmas will help you detect potential problems, as well as help you find variables that aren't set. (For example, when I compiled it, I saw the error: Global symbol "$basefn" requires explicit package at 1094021.pl line 8.. When I looked at that line, it was an open statement, where $basefn was part of the file name.) You didn't provide a definition for warnerr, so I'd suggest just using warn. So tweaking your example a bit, I came up with the following. (Note: I converted it to Excel::Writer::XLSX, as I don't have Spreadsheet::WriteExcel installed at the moment.) Read more... (3 kB)
...roboticus When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |