.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Spreadsheet::WriteExcel::Examples 3pm" .TH Spreadsheet::WriteExcel::Examples 3pm "2020-12-28" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Examples \- Spreadsheet::WriteExcel example programs. .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is a documentation only module showing the examples that are included in the Spreadsheet::WriteExcel distribution. .PP This file was auto-generated via the gen_examples_pod.pl program that is also included in the examples directory. .SH "Example programs" .IX Header "Example programs" The following is a list of the 85 example programs that are included in the Spreadsheet::WriteExcel distribution. .IP "\(bu" 4 \&\*(L"Example: a_simple.pl\*(R" A get started example with some basic features. .IP "\(bu" 4 \&\*(L"Example: demo.pl\*(R" A demo of some of the available features. .IP "\(bu" 4 \&\*(L"Example: regions.pl\*(R" A simple example of multiple worksheets. .IP "\(bu" 4 \&\*(L"Example: stats.pl\*(R" Basic formulas and functions. .IP "\(bu" 4 \&\*(L"Example: formats.pl\*(R" All the available formatting on several worksheets. .IP "\(bu" 4 \&\*(L"Example: bug_report.pl\*(R" A template for submitting bug reports. .IP "\(bu" 4 \&\*(L"Example: autofilter.pl\*(R" Examples of worksheet autofilters. .IP "\(bu" 4 \&\*(L"Example: autofit.pl\*(R" Simulate Excel's autofit for column widths. .IP "\(bu" 4 \&\*(L"Example: bigfile.pl\*(R" Write past the 7MB limit with OLE::Storage_Lite. .IP "\(bu" 4 \&\*(L"Example: cgi.pl\*(R" A simple \s-1CGI\s0 program. .IP "\(bu" 4 \&\*(L"Example: chart_area.pl\*(R" A demo of area style charts. .IP "\(bu" 4 \&\*(L"Example: chart_bar.pl\*(R" A demo of bar (vertical histogram) style charts. .IP "\(bu" 4 \&\*(L"Example: chart_column.pl\*(R" A demo of column (histogram) style charts. .IP "\(bu" 4 \&\*(L"Example: chart_line.pl\*(R" A demo of line style charts. .IP "\(bu" 4 \&\*(L"Example: chart_pie.pl\*(R" A demo of pie style charts. .IP "\(bu" 4 \&\*(L"Example: chart_scatter.pl\*(R" A demo of scatter style charts. .IP "\(bu" 4 \&\*(L"Example: chart_stock.pl\*(R" A demo of stock style charts. .IP "\(bu" 4 \&\*(L"Example: chess.pl\*(R" An example of reusing formatting via properties. .IP "\(bu" 4 \&\*(L"Example: colors.pl\*(R" A demo of the colour palette and named colours. .IP "\(bu" 4 \&\*(L"Example: comments1.pl\*(R" Add comments to worksheet cells. .IP "\(bu" 4 \&\*(L"Example: comments2.pl\*(R" Add comments with advanced options. .IP "\(bu" 4 \&\*(L"Example: copyformat.pl\*(R" Example of copying a cell format. .IP "\(bu" 4 \&\*(L"Example: data_validate.pl\*(R" An example of data validation and dropdown lists. .IP "\(bu" 4 \&\*(L"Example: date_time.pl\*(R" Write dates and times with \fBwrite_date_time()\fR. .IP "\(bu" 4 \&\*(L"Example: defined_name.pl\*(R" Example of how to create defined names. .IP "\(bu" 4 \&\*(L"Example: diag_border.pl\*(R" A simple example of diagonal cell borders. .IP "\(bu" 4 \&\*(L"Example: easter_egg.pl\*(R" Expose the Excel97 flight simulator. .IP "\(bu" 4 \&\*(L"Example: filehandle.pl\*(R" Examples of working with filehandles. .IP "\(bu" 4 \&\*(L"Example: formula_result.pl\*(R" Formulas with user specified results. .IP "\(bu" 4 \&\*(L"Example: headers.pl\*(R" Examples of worksheet headers and footers. .IP "\(bu" 4 \&\*(L"Example: hide_sheet.pl\*(R" Simple example of hiding a worksheet. .IP "\(bu" 4 \&\*(L"Example: hyperlink1.pl\*(R" Shows how to create web hyperlinks. .IP "\(bu" 4 \&\*(L"Example: hyperlink2.pl\*(R" Examples of internal and external hyperlinks. .IP "\(bu" 4 \&\*(L"Example: images.pl\*(R" Adding images to worksheets. .IP "\(bu" 4 \&\*(L"Example: indent.pl\*(R" An example of cell indentation. .IP "\(bu" 4 \&\*(L"Example: merge1.pl\*(R" A simple example of cell merging. .IP "\(bu" 4 \&\*(L"Example: merge2.pl\*(R" A simple example of cell merging with formatting. .IP "\(bu" 4 \&\*(L"Example: merge3.pl\*(R" Add hyperlinks to merged cells. .IP "\(bu" 4 \&\*(L"Example: merge4.pl\*(R" An advanced example of merging with formatting. .IP "\(bu" 4 \&\*(L"Example: merge5.pl\*(R" An advanced example of merging with formatting. .IP "\(bu" 4 \&\*(L"Example: merge6.pl\*(R" An example of merging with Unicode strings. .IP "\(bu" 4 \&\*(L"Example: mod_perl1.pl\*(R" A simple mod_perl 1 program. .IP "\(bu" 4 \&\*(L"Example: mod_perl2.pl\*(R" A simple mod_perl 2 program. .IP "\(bu" 4 \&\*(L"Example: outline.pl\*(R" An example of outlines and grouping. .IP "\(bu" 4 \&\*(L"Example: outline_collapsed.pl\*(R" An example of collapsed outlines. .IP "\(bu" 4 \&\*(L"Example: panes.pl\*(R" An examples of how to create panes. .IP "\(bu" 4 \&\*(L"Example: properties.pl\*(R" Add document properties to a workbook. .IP "\(bu" 4 \&\*(L"Example: protection.pl\*(R" Example of cell locking and formula hiding. .IP "\(bu" 4 \&\*(L"Example: repeat.pl\*(R" Example of writing repeated formulas. .IP "\(bu" 4 \&\*(L"Example: right_to_left.pl\*(R" Change default sheet direction to right to left. .IP "\(bu" 4 \&\*(L"Example: row_wrap.pl\*(R" How to wrap data from one worksheet onto another. .IP "\(bu" 4 \&\*(L"Example: sales.pl\*(R" An example of a simple sales spreadsheet. .IP "\(bu" 4 \&\*(L"Example: sendmail.pl\*(R" Send an Excel email attachment using Mail::Sender. .IP "\(bu" 4 \&\*(L"Example: stats_ext.pl\*(R" Same as stats.pl with external references. .IP "\(bu" 4 \&\*(L"Example: stocks.pl\*(R" Demonstrates conditional formatting. .IP "\(bu" 4 \&\*(L"Example: tab_colors.pl\*(R" Example of how to set worksheet tab colours. .IP "\(bu" 4 \&\*(L"Example: textwrap.pl\*(R" Demonstrates text wrapping options. .IP "\(bu" 4 \&\*(L"Example: win32ole.pl\*(R" A sample Win32::OLE example for comparison. .IP "\(bu" 4 \&\*(L"Example: write_arrays.pl\*(R" Example of writing 1D or 2D arrays of data. .IP "\(bu" 4 \&\*(L"Example: write_handler1.pl\*(R" Example of extending the \fBwrite()\fR method. Step 1. .IP "\(bu" 4 \&\*(L"Example: write_handler2.pl\*(R" Example of extending the \fBwrite()\fR method. Step 2. .IP "\(bu" 4 \&\*(L"Example: write_handler3.pl\*(R" Example of extending the \fBwrite()\fR method. Step 3. .IP "\(bu" 4 \&\*(L"Example: write_handler4.pl\*(R" Example of extending the \fBwrite()\fR method. Step 4. .IP "\(bu" 4 \&\*(L"Example: write_to_scalar.pl\*(R" Example of writing an Excel file to a Perl scalar. .IP "\(bu" 4 \&\*(L"Example: unicode_utf16.pl\*(R" Simple example of using Unicode \s-1UTF16\s0 strings. .IP "\(bu" 4 \&\*(L"Example: unicode_utf16_japan.pl\*(R" Write Japanese Unicode strings using \s-1UTF\-16.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_cyrillic.pl\*(R" Write Russian Cyrillic strings using \s-1UTF\-8.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_list.pl\*(R" List the chars in a Unicode font. .IP "\(bu" 4 \&\*(L"Example: unicode_2022_jp.pl\*(R" Japanese: \s-1ISO\-2022\-JP\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: unicode_8859_11.pl\*(R" Thai: \s-1ISO\-8859_11\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: unicode_8859_7.pl\*(R" Greek: \s-1ISO\-8859_7\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: unicode_big5.pl\*(R" Chinese: \s-1BIG5\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: unicode_cp1251.pl\*(R" Russian: \s-1CP1251\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: unicode_cp1256.pl\*(R" Arabic: \s-1CP1256\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: unicode_koi8r.pl\*(R" Russian: \s-1KOI8\-R\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: unicode_polish_utf8.pl\*(R" Polish : \s-1UTF8\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: unicode_shift_jis.pl\*(R" Japanese: Shift \s-1JIS\s0 to utf8 in perl 5.8. .IP "\(bu" 4 \&\*(L"Example: csv2xls.pl\*(R" Program to convert a \s-1CSV\s0 file to an Excel file. .IP "\(bu" 4 \&\*(L"Example: tab2xls.pl\*(R" Program to convert a tab separated file to xls. .IP "\(bu" 4 \&\*(L"Example: datecalc1.pl\*(R" Convert Unix/Perl time to Excel time. .IP "\(bu" 4 \&\*(L"Example: datecalc2.pl\*(R" Calculate an Excel date using Date::Calc. .IP "\(bu" 4 \&\*(L"Example: lecxe.pl\*(R" Convert Excel to WriteExcel using Win32::OLE. .IP "\(bu" 4 \&\*(L"Example: convertA1.pl\*(R" Helper functions for dealing with A1 notation. .IP "\(bu" 4 \&\*(L"Example: function_locale.pl\*(R" Add non-English function names to Formula.pm. .IP "\(bu" 4 \&\*(L"Example: writeA1.pl\*(R" Example of how to extend the module. .SS "Example: a_simple.pl" .IX Subsection "Example: a_simple.pl" A simple example of how to use the Spreadsheet::WriteExcel module to write some text and numbers to an Excel binary file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple example of how to use the Spreadsheet::WriteExcel module to write \& # some text and numbers to an Excel binary file. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook called simple.xls and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqa_simple.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& # The general syntax is write($row, $column, $token). Note that row and \& # column are zero indexed \& # \& \& # Write some text \& $worksheet\->write(0, 0, "Hi Excel!"); \& \& \& # Write some numbers \& $worksheet\->write(2, 0, 3); # Writes 3 \& $worksheet\->write(3, 0, 3.00000); # Writes 3 \& $worksheet\->write(4, 0, 3.00001); # Writes 3.00001 \& $worksheet\->write(5, 0, 3.14159); # TeX revision no.? \& \& \& # Write some formulas \& $worksheet\->write(7, 0, \*(Aq=A3 + A6\*(Aq); \& $worksheet\->write(8, 0, \*(Aq=IF(A5>3,"Yes", "No")\*(Aq); \& \& \& # Write a hyperlink \& $worksheet\->write(10, 0, \*(Aqhttp://www.perl.com/\*(Aq); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: demo.pl" .IX Subsection "Example: demo.pl" A simple demo of some of the features of Spreadsheet::WriteExcel. .PP This program is used to create the project screenshot for Freshmeat: .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # A simple demo of some of the features of Spreadsheet::WriteExcel. \& # \& # This program is used to create the project screenshot for Freshmeat: \& # L \& # \& # reverse(\*(Aq(c)\*(Aq), October 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("demo.xls"); \& my $worksheet = $workbook\->add_worksheet(\*(AqDemo\*(Aq); \& my $worksheet2 = $workbook\->add_worksheet(\*(AqAnother sheet\*(Aq); \& my $worksheet3 = $workbook\->add_worksheet(\*(AqAnd another\*(Aq); \& \& my $bold = $workbook\->add_format(bold => 1); \& \& \& ####################################################################### \& # \& # Write a general heading \& # \& $worksheet\->set_column(\*(AqA:A\*(Aq, 36, $bold); \& $worksheet\->set_column(\*(AqB:B\*(Aq, 20 ); \& $worksheet\->set_row (0, 40 ); \& \& my $heading = $workbook\->add_format( \& bold => 1, \& color => \*(Aqblue\*(Aq, \& size => 16, \& merge => 1, \& align => \*(Aqvcenter\*(Aq, \& ); \& \& my @headings = (\*(AqFeatures of Spreadsheet::WriteExcel\*(Aq, \*(Aq\*(Aq); \& $worksheet\->write_row(\*(AqA1\*(Aq, \e@headings, $heading); \& \& \& ####################################################################### \& # \& # Some text examples \& # \& my $text_format = $workbook\->add_format( \& bold => 1, \& italic => 1, \& color => \*(Aqred\*(Aq, \& size => 18, \& font =>\*(AqLucida Calligraphy\*(Aq \& ); \& \& # A phrase in Cyrillic \& my $unicode = pack "H*", "042d0442043e002004440440043004370430002004". \& "3d043000200440044304410441043a043e043c0021"; \& \& \& $worksheet\->write(\*(AqA2\*(Aq, "Text"); \& $worksheet\->write(\*(AqB2\*(Aq, "Hello Excel"); \& $worksheet\->write(\*(AqA3\*(Aq, "Formatted text"); \& $worksheet\->write(\*(AqB3\*(Aq, "Hello Excel", $text_format); \& $worksheet\->write(\*(AqA4\*(Aq, "Unicode text"); \& $worksheet\->write_utf16be_string(\*(AqB4\*(Aq, $unicode); \& \& ####################################################################### \& # \& # Some numeric examples \& # \& my $num1_format = $workbook\->add_format(num_format => \*(Aq$#,##0.00\*(Aq); \& my $num2_format = $workbook\->add_format(num_format => \*(Aq d mmmm yyy\*(Aq); \& \& \& $worksheet\->write(\*(AqA5\*(Aq, "Numbers"); \& $worksheet\->write(\*(AqB5\*(Aq, 1234.56); \& $worksheet\->write(\*(AqA6\*(Aq, "Formatted numbers"); \& $worksheet\->write(\*(AqB6\*(Aq, 1234.56, $num1_format); \& $worksheet\->write(\*(AqA7\*(Aq, "Formatted numbers"); \& $worksheet\->write(\*(AqB7\*(Aq, 37257, $num2_format); \& \& \& ####################################################################### \& # \& # Formulae \& # \& $worksheet\->set_selection(\*(AqB8\*(Aq); \& $worksheet\->write(\*(AqA8\*(Aq, \*(AqFormulas and functions, "=SIN(PI()/4)"\*(Aq); \& $worksheet\->write(\*(AqB8\*(Aq, \*(Aq=SIN(PI()/4)\*(Aq); \& \& \& ####################################################################### \& # \& # Hyperlinks \& # \& $worksheet\->write(\*(AqA9\*(Aq, "Hyperlinks"); \& $worksheet\->write(\*(AqB9\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq ); \& \& \& ####################################################################### \& # \& # Images \& # \& $worksheet\->write(\*(AqA10\*(Aq, "Images"); \& $worksheet\->insert_image(\*(AqB10\*(Aq, \*(Aqrepublic.png\*(Aq, 16, 8); \& \& \& ####################################################################### \& # \& # Misc \& # \& $worksheet\->write(\*(AqA18\*(Aq, "Page/printer setup"); \& $worksheet\->write(\*(AqA19\*(Aq, "Multiple worksheets"); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: regions.pl" .IX Subsection "Example: regions.pl" An example of how to use the Spreadsheet:WriteExcel module to write a basic Excel workbook with multiple worksheets. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # An example of how to use the Spreadsheet:WriteExcel module to write a basic \& # Excel workbook with multiple worksheets. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new Excel workbook \& my $workbook = Spreadsheet::WriteExcel\->new("regions.xls"); \& \& # Add some worksheets \& my $north = $workbook\->add_worksheet("North"); \& my $south = $workbook\->add_worksheet("South"); \& my $east = $workbook\->add_worksheet("East"); \& my $west = $workbook\->add_worksheet("West"); \& \& # Add a Format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_color(\*(Aqblue\*(Aq); \& \& # Add a caption to each worksheet \& foreach my $worksheet ($workbook\->sheets()) { \& $worksheet\->write(0, 0, "Sales", $format); \& } \& \& # Write some data \& $north\->write(0, 1, 200000); \& $south\->write(0, 1, 100000); \& $east\->write (0, 1, 150000); \& $west\->write (0, 1, 100000); \& \& # Set the active worksheet \& $south\->activate(); \& \& # Set the width of the first column \& $south\->set_column(0, 0, 20); \& \& # Set the active cell \& $south\->set_selection(0, 1); .Ve .PP Download this example: .SS "Example: stats.pl" .IX Subsection "Example: stats.pl" A simple example of how to use functions with the Spreadsheet::WriteExcel module. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple example of how to use functions with the Spreadsheet::WriteExcel \& # module. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("stats.xls"); \& my $worksheet = $workbook\->add_worksheet(\*(AqTest data\*(Aq); \& \& # Set the column width for columns 1 \& $worksheet\->set_column(0, 0, 20); \& \& \& # Create a format for the headings \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& \& \& # Write the sample data \& $worksheet\->write(0, 0, \*(AqSample\*(Aq, $format); \& $worksheet\->write(0, 1, 1); \& $worksheet\->write(0, 2, 2); \& $worksheet\->write(0, 3, 3); \& $worksheet\->write(0, 4, 4); \& $worksheet\->write(0, 5, 5); \& $worksheet\->write(0, 6, 6); \& $worksheet\->write(0, 7, 7); \& $worksheet\->write(0, 8, 8); \& \& $worksheet\->write(1, 0, \*(AqLength\*(Aq, $format); \& $worksheet\->write(1, 1, 25.4); \& $worksheet\->write(1, 2, 25.4); \& $worksheet\->write(1, 3, 24.8); \& $worksheet\->write(1, 4, 25.0); \& $worksheet\->write(1, 5, 25.3); \& $worksheet\->write(1, 6, 24.9); \& $worksheet\->write(1, 7, 25.2); \& $worksheet\->write(1, 8, 24.8); \& \& # Write some statistical functions \& $worksheet\->write(4, 0, \*(AqCount\*(Aq, $format); \& $worksheet\->write(4, 1, \*(Aq=COUNT(B1:I1)\*(Aq); \& \& $worksheet\->write(5, 0, \*(AqSum\*(Aq, $format); \& $worksheet\->write(5, 1, \*(Aq=SUM(B2:I2)\*(Aq); \& \& $worksheet\->write(6, 0, \*(AqAverage\*(Aq, $format); \& $worksheet\->write(6, 1, \*(Aq=AVERAGE(B2:I2)\*(Aq); \& \& $worksheet\->write(7, 0, \*(AqMin\*(Aq, $format); \& $worksheet\->write(7, 1, \*(Aq=MIN(B2:I2)\*(Aq); \& \& $worksheet\->write(8, 0, \*(AqMax\*(Aq, $format); \& $worksheet\->write(8, 1, \*(Aq=MAX(B2:I2)\*(Aq); \& \& $worksheet\->write(9, 0, \*(AqStandard Deviation\*(Aq, $format); \& $worksheet\->write(9, 1, \*(Aq=STDEV(B2:I2)\*(Aq); \& \& $worksheet\->write(10, 0, \*(AqKurtosis\*(Aq, $format); \& $worksheet\->write(10, 1, \*(Aq=KURT(B2:I2)\*(Aq); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: formats.pl" .IX Subsection "Example: formats.pl" Examples of formatting using the Spreadsheet::WriteExcel module. .PP This program demonstrates almost all possible formatting options. It is worth running this program and viewing the output Excel file if you are interested in the various formatting possibilities. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Examples of formatting using the Spreadsheet::WriteExcel module. \& # \& # This program demonstrates almost all possible formatting options. It is worth \& # running this program and viewing the output Excel file if you are interested \& # in the various formatting possibilities. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2002, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqformats.xls\*(Aq); \& \& # Some common formats \& my $center = $workbook\->add_format(align => \*(Aqcenter\*(Aq); \& my $heading = $workbook\->add_format(align => \*(Aqcenter\*(Aq, bold => 1); \& \& # The named colors \& my %colors = ( \& 0x08, \*(Aqblack\*(Aq, \& 0x0C, \*(Aqblue\*(Aq, \& 0x10, \*(Aqbrown\*(Aq, \& 0x0F, \*(Aqcyan\*(Aq, \& 0x17, \*(Aqgray\*(Aq, \& 0x11, \*(Aqgreen\*(Aq, \& 0x0B, \*(Aqlime\*(Aq, \& 0x0E, \*(Aqmagenta\*(Aq, \& 0x12, \*(Aqnavy\*(Aq, \& 0x35, \*(Aqorange\*(Aq, \& 0x21, \*(Aqpink\*(Aq, \& 0x14, \*(Aqpurple\*(Aq, \& 0x0A, \*(Aqred\*(Aq, \& 0x16, \*(Aqsilver\*(Aq, \& 0x09, \*(Aqwhite\*(Aq, \& 0x0D, \*(Aqyellow\*(Aq, \& ); \& \& # Call these subroutines to demonstrate different formatting options \& intro(); \& fonts(); \& named_colors(); \& standard_colors(); \& numeric_formats(); \& borders(); \& patterns(); \& alignment(); \& misc(); \& \& # Note: this is required \& $workbook\->close(); \& \& \& ###################################################################### \& # \& # Intro. \& # \& sub intro { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqIntroduction\*(Aq); \& \& $worksheet\->set_column(0, 0, 60); \& \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_size(14); \& $format\->set_color(\*(Aqblue\*(Aq); \& $format\->set_align(\*(Aqcenter\*(Aq); \& \& my $format2 = $workbook\->add_format(); \& $format2\->set_bold(); \& $format2\->set_color(\*(Aqblue\*(Aq); \& \& $worksheet\->write(2, 0, \*(AqThis workbook demonstrates some of\*(Aq, $format); \& $worksheet\->write(3, 0, \*(Aqthe formatting options provided by\*(Aq, $format); \& $worksheet\->write(4, 0, \*(Aqthe Spreadsheet::WriteExcel module.\*(Aq, $format); \& \& $worksheet\->write(\*(AqA7\*(Aq, \*(AqSections:\*(Aq, $format2); \& $worksheet\->write(\*(AqA8\*(Aq, "internal:Fonts!A1", \*(AqFonts\*(Aq ); \& $worksheet\->write(\*(AqA9\*(Aq, "internal:\*(AqNamed colors\*(Aq!A1", \*(AqNamed colors\*(Aq ); \& $worksheet\->write(\*(AqA10\*(Aq, "internal:\*(AqStandard colors\*(Aq!A1", \*(AqStandard colors\*(Aq); \& $worksheet\->write(\*(AqA11\*(Aq, "internal:\*(AqNumeric formats\*(Aq!A1", \*(AqNumeric formats\*(Aq); \& $worksheet\->write(\*(AqA12\*(Aq, "internal:Borders!A1", \*(AqBorders\*(Aq ); \& $worksheet\->write(\*(AqA13\*(Aq, "internal:Patterns!A1", \*(AqPatterns\*(Aq ); \& $worksheet\->write(\*(AqA14\*(Aq, "internal:Alignment!A1", \*(AqAlignment\*(Aq ); \& $worksheet\->write(\*(AqA15\*(Aq, "internal:Miscellaneous!A1", \*(AqMiscellaneous\*(Aq ); \& \& } \& \& \& ###################################################################### \& # \& # Demonstrate the named colors. \& # \& sub named_colors { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqNamed colors\*(Aq); \& \& $worksheet\->set_column(0, 3, 15); \& \& $worksheet\->write(0, 0, "Index", $heading); \& $worksheet\->write(0, 1, "Index", $heading); \& $worksheet\->write(0, 2, "Name", $heading); \& $worksheet\->write(0, 3, "Color", $heading); \& \& my $i = 1; \& \& while (my($index, $color) = each %colors) { \& my $format = $workbook\->add_format( \& bg_color => $color, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet\->write($i+1, 0, $index, $center); \& $worksheet\->write($i+1, 1, sprintf("0x%02X", $index), $center); \& $worksheet\->write($i+1, 2, $color, $center); \& $worksheet\->write($i+1, 3, \*(Aq\*(Aq, $format); \& $i++; \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel colors in the range 8..63. \& # \& sub standard_colors { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqStandard colors\*(Aq); \& \& $worksheet\->set_column(0, 3, 15); \& \& $worksheet\->write(0, 0, "Index", $heading); \& $worksheet\->write(0, 1, "Index", $heading); \& $worksheet\->write(0, 2, "Color", $heading); \& $worksheet\->write(0, 3, "Name", $heading); \& \& for my $i (8..63) { \& my $format = $workbook\->add_format( \& bg_color => $i, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet\->write(($i \-7), 0, $i, $center); \& $worksheet\->write(($i \-7), 1, sprintf("0x%02X", $i), $center); \& $worksheet\->write(($i \-7), 2, \*(Aq\*(Aq, $format); \& \& # Add the color names \& if (exists $colors{$i}) { \& $worksheet\->write(($i \-7), 3, $colors{$i}, $center); \& \& } \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard numeric formats. \& # \& sub numeric_formats { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqNumeric formats\*(Aq); \& \& $worksheet\->set_column(0, 4, 15); \& $worksheet\->set_column(5, 5, 45); \& \& $worksheet\->write(0, 0, "Index", $heading); \& $worksheet\->write(0, 1, "Index", $heading); \& $worksheet\->write(0, 2, "Unformatted", $heading); \& $worksheet\->write(0, 3, "Formatted", $heading); \& $worksheet\->write(0, 4, "Negative", $heading); \& $worksheet\->write(0, 5, "Format", $heading); \& \& my @formats; \& push @formats, [ 0x00, 1234.567, 0, \*(AqGeneral\*(Aq ]; \& push @formats, [ 0x01, 1234.567, 0, \*(Aq0\*(Aq ]; \& push @formats, [ 0x02, 1234.567, 0, \*(Aq0.00\*(Aq ]; \& push @formats, [ 0x03, 1234.567, 0, \*(Aq#,##0\*(Aq ]; \& push @formats, [ 0x04, 1234.567, 0, \*(Aq#,##0.00\*(Aq ]; \& push @formats, [ 0x05, 1234.567, \-1234.567, \*(Aq($#,##0_);($#,##0)\*(Aq ]; \& push @formats, [ 0x06, 1234.567, \-1234.567, \*(Aq($#,##0_);[Red]($#,##0)\*(Aq ]; \& push @formats, [ 0x07, 1234.567, \-1234.567, \*(Aq($#,##0.00_);($#,##0.00)\*(Aq ]; \& push @formats, [ 0x08, 1234.567, \-1234.567, \*(Aq($#,##0.00_);[Red]($#,##0.00)\*(Aq ]; \& push @formats, [ 0x09, 0.567, 0, \*(Aq0%\*(Aq ]; \& push @formats, [ 0x0a, 0.567, 0, \*(Aq0.00%\*(Aq ]; \& push @formats, [ 0x0b, 1234.567, 0, \*(Aq0.00E+00\*(Aq ]; \& push @formats, [ 0x0c, 0.75, 0, \*(Aq# ?/?\*(Aq ]; \& push @formats, [ 0x0d, 0.3125, 0, \*(Aq# ??/??\*(Aq ]; \& push @formats, [ 0x0e, 36892.521, 0, \*(Aqm/d/yy\*(Aq ]; \& push @formats, [ 0x0f, 36892.521, 0, \*(Aqd\-mmm\-yy\*(Aq ]; \& push @formats, [ 0x10, 36892.521, 0, \*(Aqd\-mmm\*(Aq ]; \& push @formats, [ 0x11, 36892.521, 0, \*(Aqmmm\-yy\*(Aq ]; \& push @formats, [ 0x12, 36892.521, 0, \*(Aqh:mm AM/PM\*(Aq ]; \& push @formats, [ 0x13, 36892.521, 0, \*(Aqh:mm:ss AM/PM\*(Aq ]; \& push @formats, [ 0x14, 36892.521, 0, \*(Aqh:mm\*(Aq ]; \& push @formats, [ 0x15, 36892.521, 0, \*(Aqh:mm:ss\*(Aq ]; \& push @formats, [ 0x16, 36892.521, 0, \*(Aqm/d/yy h:mm\*(Aq ]; \& push @formats, [ 0x25, 1234.567, \-1234.567, \*(Aq(#,##0_);(#,##0)\*(Aq ]; \& push @formats, [ 0x26, 1234.567, \-1234.567, \*(Aq(#,##0_);[Red](#,##0)\*(Aq ]; \& push @formats, [ 0x27, 1234.567, \-1234.567, \*(Aq(#,##0.00_);(#,##0.00)\*(Aq ]; \& push @formats, [ 0x28, 1234.567, \-1234.567, \*(Aq(#,##0.00_);[Red](#,##0.00)\*(Aq ]; \& push @formats, [ 0x29, 1234.567, \-1234.567, \*(Aq_(* #,##0_);_(* (#,##0);_(* "\-"_);_(@_)\*(Aq ]; \& push @formats, [ 0x2a, 1234.567, \-1234.567, \*(Aq_($* #,##0_);_($* (#,##0);_($* "\-"_);_(@_)\*(Aq ]; \& push @formats, [ 0x2b, 1234.567, \-1234.567, \*(Aq_(* #,##0.00_);_(* (#,##0.00);_(* "\-"??_);_(@_)\*(Aq ]; \& push @formats, [ 0x2c, 1234.567, \-1234.567, \*(Aq_($* #,##0.00_);_($* (#,##0.00);_($* "\-"??_);_(@_)\*(Aq ]; \& push @formats, [ 0x2d, 36892.521, 0, \*(Aqmm:ss\*(Aq ]; \& push @formats, [ 0x2e, 3.0153, 0, \*(Aq[h]:mm:ss\*(Aq ]; \& push @formats, [ 0x2f, 36892.521, 0, \*(Aqmm:ss.0\*(Aq ]; \& push @formats, [ 0x30, 1234.567, 0, \*(Aq##0.0E+0\*(Aq ]; \& push @formats, [ 0x31, 1234.567, 0, \*(Aq@\*(Aq ]; \& \& my $i; \& foreach my $format (@formats){ \& my $style = $workbook\->add_format(); \& $style\->set_num_format($format\->[0]); \& \& $i++; \& $worksheet\->write($i, 0, $format\->[0], $center); \& $worksheet\->write($i, 1, sprintf("0x%02X", $format\->[0]), $center); \& $worksheet\->write($i, 2, $format\->[1], $center); \& $worksheet\->write($i, 3, $format\->[1], $style); \& \& if ($format\->[2]) { \& $worksheet\->write($i, 4, $format\->[2], $style); \& } \& \& $worksheet\->write_string($i, 5, $format\->[3]); \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the font options. \& # \& sub fonts { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqFonts\*(Aq); \& \& $worksheet\->set_column(0, 0, 30); \& $worksheet\->set_column(1, 1, 10); \& \& $worksheet\->write(0, 0, "Font name", $heading); \& $worksheet\->write(0, 1, "Font size", $heading); \& \& my @fonts; \& push @fonts, [ 10, \*(AqArial\*(Aq ]; \& push @fonts, [ 12, \*(AqArial\*(Aq ]; \& push @fonts, [ 14, \*(AqArial\*(Aq ]; \& push @fonts, [ 12, \*(AqArial Black\*(Aq ]; \& push @fonts, [ 12, \*(AqArial Narrow\*(Aq ]; \& push @fonts, [ 12, \*(AqCentury Schoolbook\*(Aq ]; \& push @fonts, [ 12, \*(AqCourier\*(Aq ]; \& push @fonts, [ 12, \*(AqCourier New\*(Aq ]; \& push @fonts, [ 12, \*(AqGaramond\*(Aq ]; \& push @fonts, [ 12, \*(AqImpact\*(Aq ]; \& push @fonts, [ 12, \*(AqLucida Handwriting\*(Aq] ; \& push @fonts, [ 12, \*(AqTimes New Roman\*(Aq ]; \& push @fonts, [ 12, \*(AqSymbol\*(Aq ]; \& push @fonts, [ 12, \*(AqWingdings\*(Aq ]; \& push @fonts, [ 12, \*(AqA font that doesn\e\*(Aqt exist\*(Aq ]; \& \& my $i; \& foreach my $font (@fonts){ \& my $format = $workbook\->add_format(); \& \& $format\->set_size($font\->[0]); \& $format\->set_font($font\->[1]); \& \& $i++; \& $worksheet\->write($i, 0, $font\->[1], $format); \& $worksheet\->write($i, 1, $font\->[0], $format); \& } \& \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel border styles. \& # \& sub borders { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqBorders\*(Aq); \& \& $worksheet\->set_column(0, 4, 10); \& $worksheet\->set_column(5, 5, 40); \& \& $worksheet\->write(0, 0, "Index", $heading); \& $worksheet\->write(0, 1, "Index", $heading); \& $worksheet\->write(0, 3, "Style", $heading); \& $worksheet\->write(0, 5, "The style is highlighted in red for ", $heading); \& $worksheet\->write(1, 5, "emphasis, the default color is black.", $heading); \& \& for my $i (0..13){ \& my $format = $workbook\->add_format(); \& $format\->set_border($i); \& $format\->set_border_color(\*(Aqred\*(Aq); \& $format\->set_align(\*(Aqcenter\*(Aq); \& \& $worksheet\->write((2*($i+1)), 0, $i, $center); \& $worksheet\->write((2*($i+1)), 1, sprintf("0x%02X", $i), $center); \& \& $worksheet\->write((2*($i+1)), 3, "Border", $format); \& } \& \& $worksheet\->write(30, 0, "Diag type", $heading); \& $worksheet\->write(30, 1, "Index", $heading); \& $worksheet\->write(30, 3, "Style", $heading); \& $worksheet\->write(30, 5, "Diagonal Boder styles", $heading); \& \& for my $i (1..3){ \& my $format = $workbook\->add_format(); \& $format\->set_diag_type($i); \& $format\->set_diag_border(1); \& $format\->set_diag_color(\*(Aqred\*(Aq); \& $format\->set_align(\*(Aqcenter\*(Aq); \& \& $worksheet\->write((2*($i+15)), 0, $i, $center); \& $worksheet\->write((2*($i+15)), 1, sprintf("0x%02X", $i), $center); \& \& $worksheet\->write((2*($i+15)), 3, "Border", $format); \& } \& } \& \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel cell patterns. \& # \& sub patterns { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqPatterns\*(Aq); \& \& $worksheet\->set_column(0, 4, 10); \& $worksheet\->set_column(5, 5, 50); \& \& $worksheet\->write(0, 0, "Index", $heading); \& $worksheet\->write(0, 1, "Index", $heading); \& $worksheet\->write(0, 3, "Pattern", $heading); \& \& $worksheet\->write(0, 5, "The background colour has been set to silver.", $heading); \& $worksheet\->write(1, 5, "The foreground colour has been set to green.", $heading); \& \& for my $i (0..18){ \& my $format = $workbook\->add_format(); \& \& $format\->set_pattern($i); \& $format\->set_bg_color(\*(Aqsilver\*(Aq); \& $format\->set_fg_color(\*(Aqgreen\*(Aq); \& $format\->set_align(\*(Aqcenter\*(Aq); \& \& $worksheet\->write((2*($i+1)), 0, $i, $center); \& $worksheet\->write((2*($i+1)), 1, sprintf("0x%02X", $i), $center); \& \& $worksheet\->write((2*($i+1)), 3, "Pattern", $format); \& \& if ($i == 1) { \& $worksheet\->write((2*($i+1)), 5, "This is solid colour, the most useful pattern.", $heading); \& } \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel cell alignments. \& # \& sub alignment { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqAlignment\*(Aq); \& \& $worksheet\->set_column(0, 7, 12); \& $worksheet\->set_row(0, 40); \& $worksheet\->set_selection(7, 0); \& \& my $format01 = $workbook\->add_format(); \& my $format02 = $workbook\->add_format(); \& my $format03 = $workbook\->add_format(); \& my $format04 = $workbook\->add_format(); \& my $format05 = $workbook\->add_format(); \& my $format06 = $workbook\->add_format(); \& my $format07 = $workbook\->add_format(); \& my $format08 = $workbook\->add_format(); \& my $format09 = $workbook\->add_format(); \& my $format10 = $workbook\->add_format(); \& my $format11 = $workbook\->add_format(); \& my $format12 = $workbook\->add_format(); \& my $format13 = $workbook\->add_format(); \& my $format14 = $workbook\->add_format(); \& my $format15 = $workbook\->add_format(); \& my $format16 = $workbook\->add_format(); \& my $format17 = $workbook\->add_format(); \& \& $format02\->set_align(\*(Aqtop\*(Aq); \& $format03\->set_align(\*(Aqbottom\*(Aq); \& $format04\->set_align(\*(Aqvcenter\*(Aq); \& $format05\->set_align(\*(Aqvjustify\*(Aq); \& $format06\->set_text_wrap(); \& \& $format07\->set_align(\*(Aqleft\*(Aq); \& $format08\->set_align(\*(Aqright\*(Aq); \& $format09\->set_align(\*(Aqcenter\*(Aq); \& $format10\->set_align(\*(Aqfill\*(Aq); \& $format11\->set_align(\*(Aqjustify\*(Aq); \& $format12\->set_merge(); \& \& $format13\->set_rotation(45); \& $format14\->set_rotation(\-45); \& $format15\->set_rotation(270); \& \& $format16\->set_shrink(); \& $format17\->set_indent(1); \& \& $worksheet\->write(0, 0, \*(AqVertical\*(Aq, $heading); \& $worksheet\->write(0, 1, \*(Aqtop\*(Aq, $format02); \& $worksheet\->write(0, 2, \*(Aqbottom\*(Aq, $format03); \& $worksheet\->write(0, 3, \*(Aqvcenter\*(Aq, $format04); \& $worksheet\->write(0, 4, \*(Aqvjustify\*(Aq, $format05); \& $worksheet\->write(0, 5, "text\enwrap", $format06); \& \& $worksheet\->write(2, 0, \*(AqHorizontal\*(Aq, $heading); \& $worksheet\->write(2, 1, \*(Aqleft\*(Aq, $format07); \& $worksheet\->write(2, 2, \*(Aqright\*(Aq, $format08); \& $worksheet\->write(2, 3, \*(Aqcenter\*(Aq, $format09); \& $worksheet\->write(2, 4, \*(Aqfill\*(Aq, $format10); \& $worksheet\->write(2, 5, \*(Aqjustify\*(Aq, $format11); \& \& $worksheet\->write(3, 1, \*(Aqmerge\*(Aq, $format12); \& $worksheet\->write(3, 2, \*(Aq\*(Aq, $format12); \& \& $worksheet\->write(3, 3, \*(AqShrink \*(Aq x 3, $format16); \& $worksheet\->write(3, 4, \*(AqIndent\*(Aq, $format17); \& \& \& $worksheet\->write(5, 0, \*(AqRotation\*(Aq, $heading); \& $worksheet\->write(5, 1, \*(AqRotate 45\*(Aq, $format13); \& $worksheet\->write(6, 1, \*(AqRotate \-45\*(Aq, $format14); \& $worksheet\->write(7, 1, \*(AqRotate 270\*(Aq, $format15); \& } \& \& \& ###################################################################### \& # \& # Demonstrate other miscellaneous features. \& # \& sub misc { \& \& my $worksheet = $workbook\->add_worksheet(\*(AqMiscellaneous\*(Aq); \& \& $worksheet\->set_column(2, 2, 25); \& \& my $format01 = $workbook\->add_format(); \& my $format02 = $workbook\->add_format(); \& my $format03 = $workbook\->add_format(); \& my $format04 = $workbook\->add_format(); \& my $format05 = $workbook\->add_format(); \& my $format06 = $workbook\->add_format(); \& my $format07 = $workbook\->add_format(); \& \& $format01\->set_underline(0x01); \& $format02\->set_underline(0x02); \& $format03\->set_underline(0x21); \& $format04\->set_underline(0x22); \& $format05\->set_font_strikeout(); \& $format06\->set_font_outline(); \& $format07\->set_font_shadow(); \& \& $worksheet\->write(1, 2, \*(AqUnderline 0x01\*(Aq, $format01); \& $worksheet\->write(3, 2, \*(AqUnderline 0x02\*(Aq, $format02); \& $worksheet\->write(5, 2, \*(AqUnderline 0x21\*(Aq, $format03); \& $worksheet\->write(7, 2, \*(AqUnderline 0x22\*(Aq, $format04); \& $worksheet\->write(9, 2, \*(AqStrikeout\*(Aq, $format05); \& $worksheet\->write(11, 2, \*(AqOutline (Macintosh only)\*(Aq, $format06); \& $worksheet\->write(13, 2, \*(AqShadow (Macintosh only)\*(Aq, $format07); \& } \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: bug_report.pl" .IX Subsection "Example: bug_report.pl" A template for submitting a bug report. .PP Run this program and read the output from the command line. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& \& ############################################################################### \& # \& # A template for submitting a bug report. \& # \& # Run this program and read the output from the command line. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& \& print << \*(AqHINTS_1\*(Aq; \& \& REPORTING A BUG OR ASKING A QUESTION \& \& Feel free to report bugs or ask questions. However, to save time \& consider the following steps first: \& \& Read the documentation: \& \& The Spreadsheet::WriteExcel documentation has been refined in \& response to user questions. Therefore, if you have a question it is \& possible that someone else has asked it before you and that it is \& already addressed in the documentation. Since there is a lot of \& documentation to get through you should at least read the table of \& contents and search for keywords that you are interested in. \& \& Look at the example programs: \& \& There are over 70 example programs shipped with the standard \& Spreadsheet::WriteExcel distribution. Many of these were created in \& response to user questions. Try to identify an example program that \& corresponds to your query and adapt it to your needs. \& \& HINTS_1 \& print "Press enter ..."; ; \& \& print << \*(AqHINTS_2\*(Aq; \& \& If you submit a bug report here are some pointers. \& \& 1. Put "WriteExcel:" at the beginning of the subject line. This helps \& to filter genuine messages from spam. \& \& 2. Describe the problems as clearly and as concisely as possible. \& \& 3. Send a sample program. It is often easier to describe a problem in \& code than in written prose. \& \& 4. The sample program should be as small as possible to demonstrate the \& problem. Don\*(Aqt copy and past large sections of your program. The \& program should also be self contained and working. \& \& A sample bug report is generated below. If you use this format then it \& will help to analyse your question and respond to it more quickly. \& \& Please don\*(Aqt send patches without contacting the author first. \& \& \& HINTS_2 \& print "Press enter ..."; ; \& \& \& print << \*(AqEMAIL\*(Aq; \& \& ======================================================================= \& \& To: John McNamara \& Subject: WriteExcel: Problem with something. \& \& Hi John, \& \& I am using Spreadsheet::WriteExcel and I have encountered a problem. I \& want it to do SOMETHING but the module appears to do SOMETHING_ELSE. \& \& Here is some code that demonstrates the problem. \& \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("reload.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->write(0, 0, "Hi Excel!"); \& \& _\|_END_\|_ \& \& \& I tested using Excel XX (or Gnumeric or OpenOffice.org). \& \& My automatically generated system details are as follows: \& EMAIL \& \& \& print "\en Perl version : $]"; \& print "\en OS name : $^O"; \& print "\en Module versions: (not all are required)\en"; \& \& \& my @modules = qw( \& Spreadsheet::WriteExcel \& Spreadsheet::ParseExcel \& OLE::Storage_Lite \& Parse::RecDescent \& File::Temp \& Digest::MD4 \& Digest::Perl::MD4 \& Digest::MD5 \& ); \& \& \& for my $module (@modules) { \& my $version; \& eval "require $module"; \& \& if (not $@) { \& $version = $module\->VERSION; \& $version = \*(Aq(unknown)\*(Aq if not defined $version; \& } \& else { \& $version = \*(Aq(not installed)\*(Aq; \& } \& \& printf "%21s%\-24s\et%s\en", "", $module, $version; \& } \& \& \& print << "BYE"; \& Yours etc., \& \& A. Person \& \-\- \& \& BYE \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: autofilter.pl" .IX Subsection "Example: autofilter.pl" An example of how to create autofilters with Spreadsheet::WriteExcel. .PP An autofilter is a way of adding drop down lists to the headers of a 2D range of worksheet data. This is turn allow users to filter the data based on simple criteria so that some data is shown and some is hidden. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # An example of how to create autofilters with Spreadsheet::WriteExcel. \& # \& # An autofilter is a way of adding drop down lists to the headers of a 2D range \& # of worksheet data. This is turn allow users to filter the data based on \& # simple criteria so that some data is shown and some is hidden. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2007, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqautofilter.xls\*(Aq); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& my $worksheet4 = $workbook\->add_worksheet(); \& my $worksheet5 = $workbook\->add_worksheet(); \& my $worksheet6 = $workbook\->add_worksheet(); \& \& my $bold = $workbook\->add_format(bold => 1); \& \& \& # Extract the data embedded at the end of this file. \& my @headings = split \*(Aq \*(Aq, ; \& my @data; \& push @data, [split] while ; \& \& \& # Set up several sheets with the same data. \& for my $worksheet ($workbook\->sheets()) { \& $worksheet\->set_column(\*(AqA:D\*(Aq, 12); \& $worksheet\->set_row(0, 20, $bold); \& $worksheet\->write(\*(AqA1\*(Aq, \e@headings); \& } \& \& \& ############################################################################### \& # \& # Example 1. Autofilter without conditions. \& # \& \& $worksheet1\->autofilter(\*(AqA1:D51\*(Aq); \& $worksheet1\->write(\*(AqA2\*(Aq, [[@data]]); \& \& \& ############################################################################### \& # \& # \& # Example 2. Autofilter with a filter condition in the first column. \& # \& \& # The range in this example is the same as above but in row\-column notation. \& $worksheet2\->autofilter(0, 0, 50, 3); \& \& # The placeholder "Region" in the filter is ignored and can be any string \& # that adds clarity to the expression. \& # \& $worksheet2\->filter_column(0, \*(AqRegion eq East\*(Aq); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& my $row = 1; \& \& for my $row_data (@data) { \& my $region = $row_data\->[0]; \& \& if ($region eq \*(AqEast\*(Aq) { \& # Row is visible. \& } \& else { \& # Hide row. \& $worksheet2\->set_row($row, undef, undef, 1); \& } \& \& $worksheet2\->write($row++, 0, $row_data); \& } \& \& \& ############################################################################### \& # \& # \& # Example 3. Autofilter with a dual filter condition in one of the columns. \& # \& \& $worksheet3\->autofilter(\*(AqA1:D51\*(Aq); \& \& $worksheet3\->filter_column(\*(AqA\*(Aq, \*(Aqx eq East or x eq South\*(Aq); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data (@data) { \& my $region = $row_data\->[0]; \& \& if ($region eq \*(AqEast\*(Aq or $region eq \*(AqSouth\*(Aq) { \& # Row is visible. \& } \& else { \& # Hide row. \& $worksheet3\->set_row($row, undef, undef, 1); \& } \& \& $worksheet3\->write($row++, 0, $row_data); \& } \& \& \& ############################################################################### \& # \& # \& # Example 4. Autofilter with filter conditions in two columns. \& # \& \& $worksheet4\->autofilter(\*(AqA1:D51\*(Aq); \& \& $worksheet4\->filter_column(\*(AqA\*(Aq, \*(Aqx eq East\*(Aq); \& $worksheet4\->filter_column(\*(AqC\*(Aq, \*(Aqx > 3000 and x < 8000\*(Aq ); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data (@data) { \& my $region = $row_data\->[0]; \& my $volume = $row_data\->[2]; \& \& if ($region eq \*(AqEast\*(Aq and \& $volume > 3000 and $volume < 8000 \& ) \& { \& # Row is visible. \& } \& else { \& # Hide row. \& $worksheet4\->set_row($row, undef, undef, 1); \& } \& \& $worksheet4\->write($row++, 0, $row_data); \& } \& \& \& ############################################################################### \& # \& # \& # Example 5. Autofilter with filter for blanks. \& # \& \& # Create a blank cell in our test data. \& $data[5]\->[0] = \*(Aq\*(Aq; \& \& \& $worksheet5\->autofilter(\*(AqA1:D51\*(Aq); \& $worksheet5\->filter_column(\*(AqA\*(Aq, \*(Aqx == Blanks\*(Aq); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data (@data) { \& my $region = $row_data\->[0]; \& \& if ($region eq \*(Aq\*(Aq) \& { \& # Row is visible. \& } \& else { \& # Hide row. \& $worksheet5\->set_row($row, undef, undef, 1); \& } \& \& $worksheet5\->write($row++, 0, $row_data); \& } \& \& \& ############################################################################### \& # \& # \& # Example 6. Autofilter with filter for non\-blanks. \& # \& \& \& $worksheet6\->autofilter(\*(AqA1:D51\*(Aq); \& $worksheet6\->filter_column(\*(AqA\*(Aq, \*(Aqx == NonBlanks\*(Aq); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data (@data) { \& my $region = $row_data\->[0]; \& \& if ($region ne \*(Aq\*(Aq) \& { \& # Row is visible. \& } \& else { \& # Hide row. \& $worksheet6\->set_row($row, undef, undef, 1); \& } \& \& $worksheet6\->write($row++, 0, $row_data); \& } \& \& \& \& _\|_DATA_\|_ \& Region Item Volume Month \& East Apple 9000 July \& East Apple 5000 July \& South Orange 9000 September \& North Apple 2000 November \& West Apple 9000 November \& South Pear 7000 October \& North Pear 9000 August \& West Orange 1000 December \& West Grape 1000 November \& South Pear 10000 April \& West Grape 6000 January \& South Orange 3000 May \& North Apple 3000 December \& South Apple 7000 February \& West Grape 1000 December \& East Grape 8000 February \& South Grape 10000 June \& West Pear 7000 December \& South Apple 2000 October \& East Grape 7000 December \& North Grape 6000 April \& East Pear 8000 February \& North Apple 7000 August \& North Orange 7000 July \& North Apple 6000 June \& South Grape 8000 September \& West Apple 3000 October \& South Orange 10000 November \& West Grape 4000 July \& North Orange 5000 August \& East Orange 1000 November \& East Orange 4000 October \& North Grape 5000 August \& East Apple 1000 December \& South Apple 10000 March \& East Grape 7000 October \& West Grape 1000 September \& East Grape 10000 October \& South Orange 8000 March \& North Apple 4000 July \& South Orange 5000 July \& West Apple 4000 June \& East Apple 5000 April \& North Pear 3000 August \& East Grape 9000 November \& North Orange 8000 October \& East Apple 10000 June \& South Pear 1000 December \& North Grape 10000 July \& East Grape 6000 February .Ve .PP Download this example: .SS "Example: autofit.pl" .IX Subsection "Example: autofit.pl" Simulate Excel's autofit for column widths. .PP Excel provides a function called Autofit (Format\->Columns\->Autofit) that adjusts column widths to match the length of the longest string in a column. Excel calculates these widths at run time when it has access to information about string lengths and font information. This function is *not* a feature of the file format and thus cannot be implemented by Spreadsheet::WriteExcel. .PP However, we can make an attempt to simulate it by keeping track of the longest string written to each column and then adjusting the column widths prior to closing the file. .PP We keep track of the longest strings by adding a handler to the \fBwrite()\fR function. See \fBadd_handler()\fR in the S::WE docs for more information. .PP The main problem with trying to simulate Autofit lies in defining a relationship between a string length and its width in a arbitrary font and size. We use two approaches below. The first is a simple direct relationship obtained by trial and error. The second is a slightly more sophisticated method using an external module. For more complicated applications you will probably have to work out your own methods. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # Simulate Excel\*(Aqs autofit for column widths. \& # \& # Excel provides a function called Autofit (Format\->Columns\->Autofit) that \& # adjusts column widths to match the length of the longest string in a column. \& # Excel calculates these widths at run time when it has access to information \& # about string lengths and font information. This function is *not* a feature \& # of the file format and thus cannot be implemented by Spreadsheet::WriteExcel. \& # \& # However, we can make an attempt to simulate it by keeping track of the \& # longest string written to each column and then adjusting the column widths \& # prior to closing the file. \& # \& # We keep track of the longest strings by adding a handler to the write() \& # function. See add_handler() in the S::WE docs for more information. \& # \& # The main problem with trying to simulate Autofit lies in defining a \& # relationship between a string length and its width in a arbitrary font and \& # size. We use two approaches below. The first is a simple direct relationship \& # obtained by trial and error. The second is a slightly more sophisticated \& # method using an external module. For more complicated applications you will \& # probably have to work out your own methods. \& # \& # reverse(\*(Aq(c)\*(Aq), May 2006, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqautofit.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& ############################################################################### \& # \& # Add a handler to store the width of the longest string written to a column. \& # We use the stored width to simulate an autofit of the column widths. \& # \& # You should do this for every worksheet you want to autofit. \& # \& $worksheet\->add_write_handler(qr[\ew], \e&store_string_widths); \& \& \& \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqHello\*(Aq); \& $worksheet\->write(\*(AqB1\*(Aq, \*(AqHello World\*(Aq); \& $worksheet\->write(\*(AqD1\*(Aq, \*(AqHello\*(Aq); \& $worksheet\->write(\*(AqF1\*(Aq, \*(AqThis is a long string as an example.\*(Aq); \& \& # Run the autofit after you have finished writing strings to the workbook. \& autofit_columns($worksheet); \& \& \& \& ############################################################################### \& # \& # Functions used for Autofit. \& # \& ############################################################################### \& \& ############################################################################### \& # \& # Adjust the column widths to fit the longest string in the column. \& # \& sub autofit_columns { \& \& my $worksheet = shift; \& my $col = 0; \& \& for my $width (@{$worksheet\->{_\|_col_widths}}) { \& \& $worksheet\->set_column($col, $col, $width) if $width; \& $col++; \& } \& } \& \& \& ############################################################################### \& # \& # The following function is a callback that was added via add_write_handler() \& # above. It modifies the write() function so that it stores the maximum \& # unwrapped width of a string in a column. \& # \& sub store_string_widths { \& \& my $worksheet = shift; \& my $col = $_[1]; \& my $token = $_[2]; \& \& # Ignore some tokens that we aren\*(Aqt interested in. \& return if not defined $token; # Ignore undefs. \& return if $token eq \*(Aq\*(Aq; # Ignore blank cells. \& return if ref $token eq \*(AqARRAY\*(Aq; # Ignore array refs. \& return if $token =~ /^=/; # Ignore formula \& \& # Ignore numbers \& return if $token =~ /^([+\-]?)(?=\ed|\e.\ed)\ed*(\e.\ed*)?([Ee]([+\-]?\ed+))?$/; \& \& # Ignore various internal and external hyperlinks. In a real scenario \& # you may wish to track the length of the optional strings used with \& # urls. \& return if $token =~ m{^[fh]tt?ps?://}; \& return if $token =~ m{^mailto:}; \& return if $token =~ m{^(?:in|ex)ternal:}; \& \& \& # We store the string width as data in the Worksheet object. We use \& # a double underscore key name to avoid conflicts with future names. \& # \& my $old_width = $worksheet\->{_\|_col_widths}\->[$col]; \& my $string_width = string_width($token); \& \& if (not defined $old_width or $string_width > $old_width) { \& # You may wish to set a minimum column width as follows. \& #return undef if $string_width < 10; \& \& $worksheet\->{_\|_col_widths}\->[$col] = $string_width; \& } \& \& \& # Return control to write(); \& return undef; \& } \& \& \& ############################################################################### \& # \& # Very simple conversion between string length and string width for Arial 10. \& # See below for a more sophisticated method. \& # \& sub string_width { \& \& return 0.9 * length $_[0]; \& } \& \& _\|_END_\|_ \& \& \& \& ############################################################################### \& # \& # This function uses an external module to get a more accurate width for a \& # string. Note that in a real program you could "use" the module instead of \& # "require"\-ing it and you could make the Font object global to avoid repeated \& # initialisation. \& # \& # Note also that the $pixel_width to $cell_width is specific to Arial. For \& # other fonts you should calculate appropriate relationships. A future version \& # of S::WE will provide a way of specifying column widths in pixels instead of \& # cell units in order to simplify this conversion. \& # \& sub string_width { \& \& require Font::TTFMetrics; \& \& my $arial = Font::TTFMetrics\->new(\*(Aqc:\ewindows\efonts\earial.ttf\*(Aq); \& \& my $font_size = 10; \& my $dpi = 96; \& my $units_per_em = $arial\->get_units_per_em(); \& my $font_width = $arial\->string_width($_[0]); \& \& # Convert to pixels as per TTFMetrics docs. \& my $pixel_width = 6 + $font_width *$font_size *$dpi /(72 *$units_per_em); \& \& # Add extra pixels for border around text. \& $pixel_width += 6; \& \& # Convert to cell width (for Arial) and for cell widths > 1. \& my $cell_width = ($pixel_width \-5) /7; \& \& return $cell_width; \& \& } \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: bigfile.pl" .IX Subsection "Example: bigfile.pl" Example of creating a Spreadsheet::WriteExcel that is larger than the default 7MB limit. .PP This is exactly that same as any other Spreadsheet::WriteExcel program except that is requires that the OLE::Storage module is installed. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of creating a Spreadsheet::WriteExcel that is larger than the \& # default 7MB limit. \& # \& # This is exactly that same as any other Spreadsheet::WriteExcel program except \& # that is requires that the OLE::Storage module is installed. \& # \& # reverse(\*(Aq(c)\*(Aq), Jan 2007, John McNamara, jmcnamara@cpan.org \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqbigfile.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->set_column(0, 50, 18); \& \& for my $col (0 .. 50) { \& for my $row (0 .. 6000) { \& $worksheet\->write($row, $col, "Row: $row Col: $col"); \& } \& } \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: cgi.pl" .IX Subsection "Example: cgi.pl" Example of how to use the Spreadsheet::WriteExcel module to send an Excel file to a browser in a \s-1CGI\s0 program. .PP On Windows the hash-bang line should be something like: .PP .Vb 1 \& #!C:\ePerl\ebin\eperl.exe .Ve .PP The \*(L"Content-Disposition\*(R" line will cause a prompt to be generated to save the file. If you want to stream the file to the browser instead, comment out that line as shown below. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the Spreadsheet::WriteExcel module to send an Excel \& # file to a browser in a CGI program. \& # \& # On Windows the hash\-bang line should be something like: \& # \& # #!C:\ePerl\ebin\eperl.exe \& # \& # The "Content\-Disposition" line will cause a prompt to be generated to save \& # the file. If you want to stream the file to the browser instead, comment out \& # that line as shown below. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Set the filename and send the content type \& my $filename ="cgitest.xls"; \& \& print "Content\-type: application/vnd.ms\-excel\en"; \& # The Content\-Disposition will generate a prompt to save the file. If you want \& # to stream the file to the browser, comment out the following line. \& print "Content\-Disposition: attachment; filename=$filename\en"; \& print "\en"; \& \& # Create a new workbook and add a worksheet. The special Perl filehandle \- will \& # redirect the output to STDOUT \& # \& my $workbook = Spreadsheet::WriteExcel\->new(\e*STDOUT); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Set the column width for column 1 \& $worksheet\->set_column(0, 0, 20); \& \& \& # Create a format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_size(15); \& $format\->set_color(\*(Aqblue\*(Aq); \& \& \& # Write to the workbook \& $worksheet\->write(0, 0, "Hi Excel!", $format); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_area.pl" .IX Subsection "Example: chart_area.pl" A simple demo of Area charts in Spreadsheet::WriteExcel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple demo of Area charts in Spreadsheet::WriteExcel. \& # \& # reverse(\*(Aq(c)\*(Aq), December 2009, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new( \*(Aqchart_area.xls\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqCategory\*(Aq, \*(AqValues 1\*(Aq, \*(AqValues 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 1, 4, 5, 2, 1, 5 ], \& [ 3, 6, 7, 5, 4, 3 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 1. A minimal chart. \& # \& my $chart1 = $workbook\->add_chart( type => \*(Aqarea\*(Aq ); \& \& # Add values only. Use the default categories. \& $chart1\->add_series( values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 2. A minimal chart with user specified categories (X axis) \& # and a series name. \& # \& my $chart2 = $workbook\->add_chart( type => \*(Aqarea\*(Aq ); \& \& # Configure the series. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& \& ############################################################################### \& # \& # Example 3. Same as previous chart but with added title and axes labels. \& # \& my $chart3 = $workbook\->add_chart( type => \*(Aqarea\*(Aq ); \& \& # Configure the series. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart3\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 4. Same as previous chart but with an added series and with a \& # user specified chart sheet name. \& # \& my $chart4 = $workbook\->add_chart( name => \*(AqResults Chart\*(Aq, type => \*(Aqarea\*(Aq ); \& \& # Configure the series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add another series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& name => \*(AqTest data series 2\*(Aq, \& ); \& \& # Add some labels. \& $chart4\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart4\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart4\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 5. Same as Example 3 but as an embedded chart. \& # \& my $chart5 = $workbook\->add_chart( type => \*(Aqarea\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart5\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart5\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart5\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& # Insert the chart into the main worksheet. \& $worksheet\->insert_chart( \*(AqE2\*(Aq, $chart5 ); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_bar.pl" .IX Subsection "Example: chart_bar.pl" A simple demo of Bar charts in Spreadsheet::WriteExcel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple demo of Bar charts in Spreadsheet::WriteExcel. \& # \& # reverse(\*(Aq(c)\*(Aq), December 2009, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new( \*(Aqchart_bar.xls\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqCategory\*(Aq, \*(AqValues 1\*(Aq, \*(AqValues 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 1, 4, 5, 2, 1, 5 ], \& [ 3, 6, 7, 5, 4, 3 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 1. A minimal chart. \& # \& my $chart1 = $workbook\->add_chart( type => \*(Aqbar\*(Aq ); \& \& # Add values only. Use the default categories. \& $chart1\->add_series( values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 2. A minimal chart with user specified categories (X axis) \& # and a series name. \& # \& my $chart2 = $workbook\->add_chart( type => \*(Aqbar\*(Aq ); \& \& # Configure the series. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& \& ############################################################################### \& # \& # Example 3. Same as previous chart but with added title and axes labels. \& # \& my $chart3 = $workbook\->add_chart( type => \*(Aqbar\*(Aq ); \& \& # Configure the series. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart3\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 4. Same as previous chart but with an added series and with a \& # user specified chart sheet name. \& # \& my $chart4 = $workbook\->add_chart( name => \*(AqResults Chart\*(Aq, type => \*(Aqbar\*(Aq ); \& \& # Configure the series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add another series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& name => \*(AqTest data series 2\*(Aq, \& ); \& \& # Add some labels. \& $chart4\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart4\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart4\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 5. Same as Example 3 but as an embedded chart. \& # \& my $chart5 = $workbook\->add_chart( type => \*(Aqbar\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart5\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart5\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart5\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& # Insert the chart into the main worksheet. \& $worksheet\->insert_chart( \*(AqE2\*(Aq, $chart5 ); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_column.pl" .IX Subsection "Example: chart_column.pl" A simple demo of Column charts in Spreadsheet::WriteExcel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple demo of Column charts in Spreadsheet::WriteExcel. \& # \& # reverse(\*(Aq(c)\*(Aq), December 2009, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new( \*(Aqchart_column.xls\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqCategory\*(Aq, \*(AqValues 1\*(Aq, \*(AqValues 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 1, 4, 5, 2, 1, 5 ], \& [ 3, 6, 7, 5, 4, 3 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 1. A minimal chart. \& # \& my $chart1 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq ); \& \& # Add values only. Use the default categories. \& $chart1\->add_series( values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 2. A minimal chart with user specified categories (X axis) \& # and a series name. \& # \& my $chart2 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq ); \& \& # Configure the series. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& \& ############################################################################### \& # \& # Example 3. Same as previous chart but with added title and axes labels. \& # \& my $chart3 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq ); \& \& # Configure the series. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart3\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 4. Same as previous chart but with an added series and with a \& # user specified chart sheet name. \& # \& my $chart4 = $workbook\->add_chart( name => \*(AqResults Chart\*(Aq, type => \*(Aqcolumn\*(Aq ); \& \& # Configure the series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add another series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& name => \*(AqTest data series 2\*(Aq, \& ); \& \& # Add some labels. \& $chart4\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart4\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart4\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 5. Same as Example 3 but as an embedded chart. \& # \& my $chart5 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart5\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart5\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart5\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& # Insert the chart into the main worksheet. \& $worksheet\->insert_chart( \*(AqE2\*(Aq, $chart5 ); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_line.pl" .IX Subsection "Example: chart_line.pl" A simple demo of Line charts in Spreadsheet::WriteExcel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple demo of Line charts in Spreadsheet::WriteExcel. \& # \& # reverse(\*(Aq(c)\*(Aq), December 2009, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new( \*(Aqchart_line.xls\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqCategory\*(Aq, \*(AqValues 1\*(Aq, \*(AqValues 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 1, 4, 5, 2, 1, 5 ], \& [ 3, 6, 7, 5, 4, 3 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 1. A minimal chart. \& # \& my $chart1 = $workbook\->add_chart( type => \*(Aqline\*(Aq ); \& \& # Add values only. Use the default categories. \& $chart1\->add_series( values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 2. A minimal chart with user specified categories (X axis) \& # and a series name. \& # \& my $chart2 = $workbook\->add_chart( type => \*(Aqline\*(Aq ); \& \& # Configure the series. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& \& ############################################################################### \& # \& # Example 3. Same as previous chart but with added title and axes labels. \& # \& my $chart3 = $workbook\->add_chart( type => \*(Aqline\*(Aq ); \& \& # Configure the series. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart3\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 4. Same as previous chart but with an added series and with a \& # user specified chart sheet name. \& # \& my $chart4 = $workbook\->add_chart( name => \*(AqResults Chart\*(Aq, type => \*(Aqline\*(Aq ); \& \& # Configure the series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add another series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& name => \*(AqTest data series 2\*(Aq, \& ); \& \& # Add some labels. \& $chart4\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart4\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart4\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 5. Same as Example 3 but as an embedded chart. \& # \& my $chart5 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart5\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart5\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart5\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& # Insert the chart into the main worksheet. \& $worksheet\->insert_chart( \*(AqE2\*(Aq, $chart5 ); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_pie.pl" .IX Subsection "Example: chart_pie.pl" A simple demo of Pie charts in Spreadsheet::WriteExcel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple demo of Pie charts in Spreadsheet::WriteExcel. \& # \& # reverse(\*(Aq(c)\*(Aq), December 2009, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new( \*(Aqchart_pie.xls\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqCategory\*(Aq, \*(AqValues\*(Aq ]; \& my $data = [ \& [ \*(AqApple\*(Aq, \*(AqCherry\*(Aq, \*(AqPecan\*(Aq ], \& [ 60, 30, 10 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 1. A minimal chart. \& # \& my $chart1 = $workbook\->add_chart( type => \*(Aqpie\*(Aq ); \& \& # Add values only. Use the default categories. \& $chart1\->add_series( values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 2. A minimal chart with user specified categories and a series name. \& # \& my $chart2 = $workbook\->add_chart( type => \*(Aqpie\*(Aq ); \& \& # Configure the series. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& name => \*(AqPie sales data\*(Aq, \& ); \& \& \& ############################################################################### \& # \& # Example 3. Same as previous chart but with an added title. \& # \& my $chart3 = $workbook\->add_chart( type => \*(Aqpie\*(Aq ); \& \& # Configure the series. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& name => \*(AqPie sales data\*(Aq, \& ); \& \& # Add a title. \& $chart3\->set_title( name => \*(AqPopular Pie Types\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 4. Same as previous chart with a user specified chart sheet name. \& # \& my $chart4 = $workbook\->add_chart( name => \*(AqResults Chart\*(Aq, type => \*(Aqpie\*(Aq ); \& \& # Configure the series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& name => \*(AqPie sales data\*(Aq, \& ); \& \& # The other chart_*.pl examples add a second series in example 4 but additional \& # series aren\*(Aqt plotted in a pie chart. \& \& # Add a title. \& $chart4\->set_title( name => \*(AqPopular Pie Types\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 5. Same as Example 3 but as an embedded chart. \& # \& my $chart5 = $workbook\->add_chart( type => \*(Aqpie\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& name => \*(AqPie sales data\*(Aq, \& ); \& \& # Add a title. \& $chart5\->set_title( name => \*(AqPopular Pie Types\*(Aq ); \& \& # Insert the chart into the main worksheet. \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart5 ); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_scatter.pl" .IX Subsection "Example: chart_scatter.pl" A simple demo of Scatter charts in Spreadsheet::WriteExcel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple demo of Scatter charts in Spreadsheet::WriteExcel. \& # \& # reverse(\*(Aq(c)\*(Aq), December 2009, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new( \*(Aqchart_scatter.xls\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqCategory\*(Aq, \*(AqValues 1\*(Aq, \*(AqValues 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 1, 4, 5, 2, 1, 5 ], \& [ 3, 6, 7, 5, 4, 3 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 1. A minimal chart. \& # \& my $chart1 = $workbook\->add_chart( type => \*(Aqscatter\*(Aq ); \& \& # Add values only. Use the default categories. \& $chart1\->add_series( values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 2. A minimal chart with user specified categories (X axis) \& # and a series name. \& # \& my $chart2 = $workbook\->add_chart( type => \*(Aqscatter\*(Aq ); \& \& # Configure the series. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& \& ############################################################################### \& # \& # Example 3. Same as previous chart but with added title and axes labels. \& # \& my $chart3 = $workbook\->add_chart( type => \*(Aqscatter\*(Aq ); \& \& # Configure the series. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart3\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 4. Same as previous chart but with an added series and with a \& # user specified chart sheet name. \& # \& my $chart4 = $workbook\->add_chart( name => \*(AqResults Chart\*(Aq, type => \*(Aqscatter\*(Aq ); \& \& # Configure the series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add another series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& name => \*(AqTest data series 2\*(Aq, \& ); \& \& # Add some labels. \& $chart4\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart4\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart4\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 5. Same as Example 3 but as an embedded chart. \& # \& my $chart5 = $workbook\->add_chart( type => \*(Aqscatter\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& name => \*(AqTest data series 1\*(Aq, \& ); \& \& # Add some labels. \& $chart5\->set_title( name => \*(AqResults of sample analysis\*(Aq ); \& $chart5\->set_x_axis( name => \*(AqSample number\*(Aq ); \& $chart5\->set_y_axis( name => \*(AqSample length (cm)\*(Aq ); \& \& # Insert the chart into the main worksheet. \& $worksheet\->insert_chart( \*(AqE2\*(Aq, $chart5 ); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_stock.pl" .IX Subsection "Example: chart_stock.pl" A simple demo of Stock charts in Spreadsheet::WriteExcel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple demo of Stock charts in Spreadsheet::WriteExcel. \& # \& # reverse(\*(Aq(c)\*(Aq), January 2010, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new( \*(Aqchart_stock.xls\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& ############################################################################### \& # \& # Set up the data worksheet that the charts will refer to. We read the example \& # data from the _\|_DATA_\|_ section at the end of the file. This simulates \& # reading the data from a database or other source. \& # \& # The default Excel Stock chart is an Open\-High\-Low\-Close chart. Therefore \& # we will need data for each of those series. \& # \& # The layout of the _\|_DATA_\|_ section is similar to the layout of the worksheet. \& # \& \& # Add some formats. \& my $bold = $workbook\->add_format( bold => 1 ); \& my $date_format = $workbook\->add_format( num_format => \*(Aqdd/mm/yyyy\*(Aq ); \& \& # Increase the width of the column used for date to make it clearer. \& $worksheet\->set_column( \*(AqA:A\*(Aq, 12 ); \& \& # Read the data from the _\|_DATA_\|_ section at the end. In a real example this \& # would probably be a database query. \& my @stock_data; \& \& while ( ) { \& next unless /\eS/; # Skip blank lines. \& next if /^#/; # Skip comments. \& \& push @stock_data, [split]; \& } \& \& # Write the data to the worksheet. \& my $row = 0; \& my $col = 0; \& \& my $headers = shift @stock_data; \& $worksheet\->write( $row++, $col, $headers, $bold ); \& \& for my $stock_data ( @stock_data ) { \& \& my @data = @$stock_data; \& my $date = shift @data; \& \& $worksheet\->write( $row, $col, $date, $date_format ); \& $worksheet\->write( $row, $col + 1, \e@data ); \& \& $row++; \& } \& \& \& ############################################################################### \& # \& # Example 1. A default Open\-High\-Low\-Close chart with series names, axes labels \& # and a title. \& # \& \& my $chart1 = $workbook\->add_chart( type => \*(Aqstock\*(Aq ); \& \& # Add a series for each of the Open\-High\-Low\-Close columns. The categories are \& # the dates in the first column. \& \& $chart1\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$10\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$10\*(Aq, \& name => \*(AqOpen\*(Aq, \& ); \& \& $chart1\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$10\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$10\*(Aq, \& name => \*(AqHigh\*(Aq, \& ); \& \& $chart1\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$10\*(Aq, \& values => \*(Aq=Sheet1!$D$2:$D$10\*(Aq, \& name => \*(AqLow\*(Aq, \& ); \& \& $chart1\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$10\*(Aq, \& values => \*(Aq=Sheet1!$E$2:$E$10\*(Aq, \& name => \*(AqClose\*(Aq, \& ); \& \& # Add a chart title and axes labels. \& $chart1\->set_title( name => \*(AqOpen\-High\-Low\-Close\*(Aq, ); \& $chart1\->set_x_axis( name => \*(AqDate\*(Aq, ); \& $chart1\->set_y_axis( name => \*(AqShare price\*(Aq, ); \& \& ############################################################################### \& # \& # Example 2. Same as the previous as an embedded chart. \& # \& \& my $chart2 = $workbook\->add_chart( type => \*(Aqstock\*(Aq, embedded => 1 ); \& \& # Add a series for each of the Open\-High\-Low\-Close columns. The categories are \& # the dates in the first column. \& \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$10\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$10\*(Aq, \& name => \*(AqOpen\*(Aq, \& ); \& \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$10\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$10\*(Aq, \& name => \*(AqHigh\*(Aq, \& ); \& \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$10\*(Aq, \& values => \*(Aq=Sheet1!$D$2:$D$10\*(Aq, \& name => \*(AqLow\*(Aq, \& ); \& \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$10\*(Aq, \& values => \*(Aq=Sheet1!$E$2:$E$10\*(Aq, \& name => \*(AqClose\*(Aq, \& ); \& \& # Add a chart title and axes labels. \& $chart2\->set_title( name => \*(AqOpen\-High\-Low\-Close\*(Aq, ); \& $chart2\->set_x_axis( name => \*(AqDate\*(Aq, ); \& $chart2\->set_y_axis( name => \*(AqShare price\*(Aq, ); \& \& # Insert the chart into the main worksheet. \& $worksheet\->insert_chart( \*(AqG2\*(Aq, $chart2 ); \& \& \& _\|_DATA_\|_ \& # Some sample stock data used for charting. \& Date Open High Low Close \& 2009\-08\-19 100.00 104.06 95.96 100.34 \& 2009\-08\-20 101.01 109.08 100.50 108.31 \& 2009\-08\-23 110.75 113.48 109.05 109.40 \& 2009\-08\-24 111.24 111.60 103.57 104.87 \& 2009\-08\-25 104.96 108.00 103.88 106.00 \& 2009\-08\-26 104.95 107.95 104.66 107.91 \& 2009\-08\-27 108.10 108.62 105.69 106.15 \& 2009\-08\-30 105.28 105.49 102.01 102.01 \& 2009\-08\-31 102.30 103.71 102.16 102.37 .Ve .PP Download this example: .SS "Example: chess.pl" .IX Subsection "Example: chess.pl" Example of formatting using the Spreadsheet::WriteExcel module via property hashes. .PP Setting format properties via hashes of values is useful when you have to deal with a large number of similar formats. Consider for example a chess board pattern with black squares, white unformatted squares and a border. .PP This relatively simple example requires 14 separate Format objects although there are only 5 different properties: black background, top border, bottom border, left border and right border. .PP Using property hashes it is possible to define these 5 sets of properties and then add them together to create the 14 Format configurations. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ######################################################################## \& # \& # Example of formatting using the Spreadsheet::WriteExcel module via \& # property hashes. \& # \& # Setting format properties via hashes of values is useful when you have \& # to deal with a large number of similar formats. Consider for example a \& # chess board pattern with black squares, white unformatted squares and \& # a border. \& # \& # This relatively simple example requires 14 separate Format \& # objects although there are only 5 different properties: black \& # background, top border, bottom border, left border and right border. \& # \& # Using property hashes it is possible to define these 5 sets of \& # properties and then add them together to create the 14 Format \& # configurations. \& # \& # reverse(\*(Aq(c)\*(Aq), July 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("chess.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Some row and column formatting \& $worksheet\->set_column(\*(AqB:I\*(Aq, 10); \& \& for my $i (1..8) { \& $worksheet\->set_row($i, 50); \& } \& \& \& # Define the property hashes \& # \& my %black = ( \& \*(Aqfg_color\*(Aq => \*(Aqblack\*(Aq, \& \*(Aqpattern\*(Aq => 1, \& ); \& \& my %top = ( \*(Aqtop\*(Aq => 6 ); \& my %bottom = ( \*(Aqbottom\*(Aq => 6 ); \& my %left = ( \*(Aqleft\*(Aq => 6 ); \& my %right = ( \*(Aqright\*(Aq => 6 ); \& \& \& # Define the formats \& # \& my $format01 = $workbook\->add_format(%top, %left ); \& my $format02 = $workbook\->add_format(%top, %black ); \& my $format03 = $workbook\->add_format(%top, ); \& my $format04 = $workbook\->add_format(%top, %right, %black ); \& \& my $format05 = $workbook\->add_format(%left ); \& my $format06 = $workbook\->add_format(%black ); \& my $format07 = $workbook\->add_format( ); \& my $format08 = $workbook\->add_format(%right, %black ); \& my $format09 = $workbook\->add_format(%right ); \& my $format10 = $workbook\->add_format(%left, %black ); \& \& my $format11 = $workbook\->add_format(%bottom, %left, %black ); \& my $format12 = $workbook\->add_format(%bottom ); \& my $format13 = $workbook\->add_format(%bottom, %black ); \& my $format14 = $workbook\->add_format(%bottom, %right ); \& \& \& # Draw the pattern \& $worksheet\->write(\*(AqB2\*(Aq, \*(Aq\*(Aq, $format01); \& $worksheet\->write(\*(AqC2\*(Aq, \*(Aq\*(Aq, $format02); \& $worksheet\->write(\*(AqD2\*(Aq, \*(Aq\*(Aq, $format03); \& $worksheet\->write(\*(AqE2\*(Aq, \*(Aq\*(Aq, $format02); \& $worksheet\->write(\*(AqF2\*(Aq, \*(Aq\*(Aq, $format03); \& $worksheet\->write(\*(AqG2\*(Aq, \*(Aq\*(Aq, $format02); \& $worksheet\->write(\*(AqH2\*(Aq, \*(Aq\*(Aq, $format03); \& $worksheet\->write(\*(AqI2\*(Aq, \*(Aq\*(Aq, $format04); \& \& $worksheet\->write(\*(AqB3\*(Aq, \*(Aq\*(Aq, $format10); \& $worksheet\->write(\*(AqC3\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqD3\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqE3\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqF3\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqG3\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqH3\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqI3\*(Aq, \*(Aq\*(Aq, $format09); \& \& $worksheet\->write(\*(AqB4\*(Aq, \*(Aq\*(Aq, $format05); \& $worksheet\->write(\*(AqC4\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqD4\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqE4\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqF4\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqG4\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqH4\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqI4\*(Aq, \*(Aq\*(Aq, $format08); \& \& $worksheet\->write(\*(AqB5\*(Aq, \*(Aq\*(Aq, $format10); \& $worksheet\->write(\*(AqC5\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqD5\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqE5\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqF5\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqG5\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqH5\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqI5\*(Aq, \*(Aq\*(Aq, $format09); \& \& $worksheet\->write(\*(AqB6\*(Aq, \*(Aq\*(Aq, $format05); \& $worksheet\->write(\*(AqC6\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqD6\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqE6\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqF6\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqG6\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqH6\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqI6\*(Aq, \*(Aq\*(Aq, $format08); \& \& $worksheet\->write(\*(AqB7\*(Aq, \*(Aq\*(Aq, $format10); \& $worksheet\->write(\*(AqC7\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqD7\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqE7\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqF7\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqG7\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqH7\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqI7\*(Aq, \*(Aq\*(Aq, $format09); \& \& $worksheet\->write(\*(AqB8\*(Aq, \*(Aq\*(Aq, $format05); \& $worksheet\->write(\*(AqC8\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqD8\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqE8\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqF8\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqG8\*(Aq, \*(Aq\*(Aq, $format06); \& $worksheet\->write(\*(AqH8\*(Aq, \*(Aq\*(Aq, $format07); \& $worksheet\->write(\*(AqI8\*(Aq, \*(Aq\*(Aq, $format08); \& \& $worksheet\->write(\*(AqB9\*(Aq, \*(Aq\*(Aq, $format11); \& $worksheet\->write(\*(AqC9\*(Aq, \*(Aq\*(Aq, $format12); \& $worksheet\->write(\*(AqD9\*(Aq, \*(Aq\*(Aq, $format13); \& $worksheet\->write(\*(AqE9\*(Aq, \*(Aq\*(Aq, $format12); \& $worksheet\->write(\*(AqF9\*(Aq, \*(Aq\*(Aq, $format13); \& $worksheet\->write(\*(AqG9\*(Aq, \*(Aq\*(Aq, $format12); \& $worksheet\->write(\*(AqH9\*(Aq, \*(Aq\*(Aq, $format13); \& $worksheet\->write(\*(AqI9\*(Aq, \*(Aq\*(Aq, $format14); .Ve .PP Download this example: .SS "Example: colors.pl" .IX Subsection "Example: colors.pl" Demonstrates Spreadsheet::WriteExcel's named colors and the Excel color palette. .PP The \fBset_custom_color()\fR Worksheet method can be used to override one of the built-in palette values with a more suitable colour. See the main docs. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ################################################################################ \& # \& # Demonstrates Spreadsheet::WriteExcel\*(Aqs named colors and the Excel color \& # palette. \& # \& # The set_custom_color() Worksheet method can be used to override one of the \& # built\-in palette values with a more suitable colour. See the main docs. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2002, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("colors.xls"); \& \& # Some common formats \& my $center = $workbook\->add_format(align => \*(Aqcenter\*(Aq); \& my $heading = $workbook\->add_format(align => \*(Aqcenter\*(Aq, bold => 1); \& \& \& ###################################################################### \& # \& # Demonstrate the named colors. \& # \& \& my %colors = ( \& 0x08, \*(Aqblack\*(Aq, \& 0x0C, \*(Aqblue\*(Aq, \& 0x10, \*(Aqbrown\*(Aq, \& 0x0F, \*(Aqcyan\*(Aq, \& 0x17, \*(Aqgray\*(Aq, \& 0x11, \*(Aqgreen\*(Aq, \& 0x0B, \*(Aqlime\*(Aq, \& 0x0E, \*(Aqmagenta\*(Aq, \& 0x12, \*(Aqnavy\*(Aq, \& 0x35, \*(Aqorange\*(Aq, \& 0x21, \*(Aqpink\*(Aq, \& 0x14, \*(Aqpurple\*(Aq, \& 0x0A, \*(Aqred\*(Aq, \& 0x16, \*(Aqsilver\*(Aq, \& 0x09, \*(Aqwhite\*(Aq, \& 0x0D, \*(Aqyellow\*(Aq, \& ); \& \& my $worksheet1 = $workbook\->add_worksheet(\*(AqNamed colors\*(Aq); \& \& $worksheet1\->set_column(0, 3, 15); \& \& $worksheet1\->write(0, 0, "Index", $heading); \& $worksheet1\->write(0, 1, "Index", $heading); \& $worksheet1\->write(0, 2, "Name", $heading); \& $worksheet1\->write(0, 3, "Color", $heading); \& \& my $i = 1; \& \& while (my($index, $color) = each %colors) { \& my $format = $workbook\->add_format( \& fg_color => $color, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet1\->write($i+1, 0, $index, $center); \& $worksheet1\->write($i+1, 1, sprintf("0x%02X", $index), $center); \& $worksheet1\->write($i+1, 2, $color, $center); \& $worksheet1\->write($i+1, 3, \*(Aq\*(Aq, $format); \& $i++; \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel colors in the range 8..63. \& # \& \& my $worksheet2 = $workbook\->add_worksheet(\*(AqStandard colors\*(Aq); \& \& $worksheet2\->set_column(0, 3, 15); \& \& $worksheet2\->write(0, 0, "Index", $heading); \& $worksheet2\->write(0, 1, "Index", $heading); \& $worksheet2\->write(0, 2, "Color", $heading); \& $worksheet2\->write(0, 3, "Name", $heading); \& \& for my $i (8..63) { \& my $format = $workbook\->add_format( \& fg_color => $i, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet2\->write(($i \-7), 0, $i, $center); \& $worksheet2\->write(($i \-7), 1, sprintf("0x%02X", $i), $center); \& $worksheet2\->write(($i \-7), 2, \*(Aq\*(Aq, $format); \& \& # Add the color names \& if (exists $colors{$i}) { \& $worksheet2\->write(($i \-7), 3, $colors{$i}, $center); \& \& } \& } \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: comments1.pl" .IX Subsection "Example: comments1.pl" This example demonstrates writing cell comments. .PP A cell comment is indicated in Excel by a small red triangle in the upper right-hand corner of the cell. .PP For more advanced comment options see comments2.pl. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # This example demonstrates writing cell comments. \& # \& # A cell comment is indicated in Excel by a small red triangle in the upper \& # right\-hand corner of the cell. \& # \& # For more advanced comment options see comments2.pl. \& # \& # reverse(\*(Aq(c)\*(Aq), November 2005, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("comments1.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& \& $worksheet\->write (\*(AqA1\*(Aq, \*(AqHello\*(Aq ); \& $worksheet\->write_comment(\*(AqA1\*(Aq, \*(AqThis is a comment\*(Aq); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: comments2.pl" .IX Subsection "Example: comments2.pl" This example demonstrates writing cell comments. .PP A cell comment is indicated in Excel by a small red triangle in the upper right-hand corner of the cell. .PP Each of the worksheets demonstrates different features of cell comments. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # This example demonstrates writing cell comments. \& # \& # A cell comment is indicated in Excel by a small red triangle in the upper \& # right\-hand corner of the cell. \& # \& # Each of the worksheets demonstrates different features of cell comments. \& # \& # reverse(\*(Aq(c)\*(Aq), November 2005, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("comments2.xls"); \& my $text_wrap = $workbook\->add_format(text_wrap => 1, valign => \*(Aqtop\*(Aq); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& my $worksheet4 = $workbook\->add_worksheet(); \& my $worksheet5 = $workbook\->add_worksheet(); \& my $worksheet6 = $workbook\->add_worksheet(); \& my $worksheet7 = $workbook\->add_worksheet(); \& my $worksheet8 = $workbook\->add_worksheet(); \& \& \& # Variables that we will use in each example. \& my $cell_text = \*(Aq\*(Aq; \& my $comment = \*(Aq\*(Aq; \& \& \& \& \& ############################################################################### \& # \& # Example 1. Demonstrates a simple cell comment without formatting and Unicode \& # comments encoded as UTF\-16 and as UTF\-8. \& # \& \& # Set up some formatting. \& $worksheet1\->set_column(\*(AqC:C\*(Aq, 25); \& $worksheet1\->set_row(2, 50); \& $worksheet1\->set_row(5, 50); \& \& \& # Simple ascii string. \& $cell_text = \*(AqHold the mouse over this cell to see the comment.\*(Aq; \& \& $comment = \*(AqThis is a comment.\*(Aq; \& \& $worksheet1\->write (\*(AqC3\*(Aq, $cell_text, $text_wrap); \& $worksheet1\->write_comment(\*(AqC3\*(Aq, $comment); \& \& \& # UTF\-16 string. \& $cell_text = \*(AqThis is a UTF\-16 comment.\*(Aq; \& \& $comment = pack "n", 0x263a; \& \& $worksheet1\->write (\*(AqC6\*(Aq, $cell_text, $text_wrap); \& $worksheet1\->write_comment(\*(AqC6\*(Aq, $comment, encoding => 1); \& \& \& # UTF\-8 string in perl 5.8. \& if ($] >= 5.008) { \& \& $worksheet1\->set_row(8, 50); \& $cell_text = \*(AqThis is a UTF\-8 string.\*(Aq; \& $comment = chr 0x263a; \& \& $worksheet1\->write (\*(AqC9\*(Aq, $cell_text, $text_wrap); \& $worksheet1\->write_comment(\*(AqC9\*(Aq, $comment); \& } \& \& \& \& ############################################################################### \& # \& # Example 2. Demonstrates visible and hidden comments. \& # \& \& # Set up some formatting. \& $worksheet2\->set_column(\*(AqC:C\*(Aq, 25); \& $worksheet2\->set_row(2, 50); \& $worksheet2\->set_row(5, 50); \& \& \& $cell_text = \*(AqThis cell comment is visible.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet2\->write (\*(AqC3\*(Aq, $cell_text, $text_wrap); \& $worksheet2\->write_comment(\*(AqC3\*(Aq, $comment, visible => 1); \& \& \& $cell_text = "This cell comment isn\*(Aqt visible (the default)."; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet2\->write (\*(AqC6\*(Aq, $cell_text, $text_wrap); \& $worksheet2\->write_comment(\*(AqC6\*(Aq, $comment); \& \& \& \& \& ############################################################################### \& # \& # Example 3. Demonstrates visible and hidden comments set at the worksheet \& # level. \& # \& \& # Set up some formatting. \& $worksheet3\->set_column(\*(AqC:C\*(Aq, 25); \& $worksheet3\->set_row(2, 50); \& $worksheet3\->set_row(5, 50); \& $worksheet3\->set_row(8, 50); \& \& # Make all comments on the worksheet visible. \& $worksheet3\->show_comments(); \& \& $cell_text = \*(AqThis cell comment is visible, explicitly.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet3\->write (\*(AqC3\*(Aq, $cell_text, $text_wrap); \& $worksheet3\->write_comment(\*(AqC3\*(Aq, $comment, visible => 1); \& \& \& $cell_text = \*(AqThis cell comment is also visible because \*(Aq. \& \*(Aqwe used show_comments().\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet3\->write (\*(AqC6\*(Aq, $cell_text, $text_wrap); \& $worksheet3\->write_comment(\*(AqC6\*(Aq, $comment); \& \& \& $cell_text = \*(AqHowever, we can still override it locally.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet3\->write (\*(AqC9\*(Aq, $cell_text, $text_wrap); \& $worksheet3\->write_comment(\*(AqC9\*(Aq, $comment, visible => 0); \& \& \& \& \& ############################################################################### \& # \& # Example 4. Demonstrates changes to the comment box dimensions. \& # \& \& # Set up some formatting. \& $worksheet4\->set_column(\*(AqC:C\*(Aq, 25); \& $worksheet4\->set_row(2, 50); \& $worksheet4\->set_row(5, 50); \& $worksheet4\->set_row(8, 50); \& $worksheet4\->set_row(15, 50); \& \& $worksheet4\->show_comments(); \& \& $cell_text = \*(AqThis cell comment is default size.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write (\*(AqC3\*(Aq, $cell_text, $text_wrap); \& $worksheet4\->write_comment(\*(AqC3\*(Aq, $comment); \& \& \& $cell_text = \*(AqThis cell comment is twice as wide.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write (\*(AqC6\*(Aq, $cell_text, $text_wrap); \& $worksheet4\->write_comment(\*(AqC6\*(Aq, $comment, x_scale => 2); \& \& \& $cell_text = \*(AqThis cell comment is twice as high.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write (\*(AqC9\*(Aq, $cell_text, $text_wrap); \& $worksheet4\->write_comment(\*(AqC9\*(Aq, $comment, y_scale => 2); \& \& \& $cell_text = \*(AqThis cell comment is scaled in both directions.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write (\*(AqC16\*(Aq, $cell_text, $text_wrap); \& $worksheet4\->write_comment(\*(AqC16\*(Aq, $comment, x_scale => 1.2, y_scale => 0.8); \& \& \& $cell_text = \*(AqThis cell comment has width and height specified in pixels.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write (\*(AqC19\*(Aq, $cell_text, $text_wrap); \& $worksheet4\->write_comment(\*(AqC19\*(Aq, $comment, width => 200, height => 20); \& \& \& \& ############################################################################### \& # \& # Example 5. Demonstrates changes to the cell comment position. \& # \& \& $worksheet5\->set_column(\*(AqC:C\*(Aq, 25); \& $worksheet5\->set_row(2, 50); \& $worksheet5\->set_row(5, 50); \& $worksheet5\->set_row(8, 50); \& $worksheet5\->set_row(11, 50); \& \& $worksheet5\->show_comments(); \& \& $cell_text = \*(AqThis cell comment is in the default position.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet5\->write (\*(AqC3\*(Aq, $cell_text, $text_wrap); \& $worksheet5\->write_comment(\*(AqC3\*(Aq, $comment); \& \& \& $cell_text = \*(AqThis cell comment has been moved to another cell.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet5\->write (\*(AqC6\*(Aq, $cell_text, $text_wrap); \& $worksheet5\->write_comment(\*(AqC6\*(Aq, $comment, start_cell => \*(AqE4\*(Aq); \& \& \& $cell_text = \*(AqThis cell comment has been moved to another cell.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet5\->write (\*(AqC9\*(Aq, $cell_text, $text_wrap); \& $worksheet5\->write_comment(\*(AqC9\*(Aq, $comment, start_row => 8, start_col => 4); \& \& \& $cell_text = \*(AqThis cell comment has been shifted within its default cell.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet5\->write (\*(AqC12\*(Aq, $cell_text, $text_wrap); \& $worksheet5\->write_comment(\*(AqC12\*(Aq, $comment, x_offset => 30, y_offset => 12); \& \& \& \& ############################################################################### \& # \& # Example 6. Demonstrates changes to the comment background colour. \& # \& \& $worksheet6\->set_column(\*(AqC:C\*(Aq, 25); \& $worksheet6\->set_row(2, 50); \& $worksheet6\->set_row(5, 50); \& $worksheet6\->set_row(8, 50); \& \& $worksheet6\->show_comments(); \& \& $cell_text = \*(AqThis cell comment has a different colour.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet6\->write (\*(AqC3\*(Aq, $cell_text, $text_wrap); \& $worksheet6\->write_comment(\*(AqC3\*(Aq, $comment, color => \*(Aqgreen\*(Aq); \& \& \& $cell_text = \*(AqThis cell comment has the default colour.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet6\->write (\*(AqC6\*(Aq, $cell_text, $text_wrap); \& $worksheet6\->write_comment(\*(AqC6\*(Aq, $comment); \& \& \& $cell_text = \*(AqThis cell comment has a different colour.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet6\->write (\*(AqC9\*(Aq, $cell_text, $text_wrap); \& $worksheet6\->write_comment(\*(AqC9\*(Aq, $comment, color => 0x35); \& \& \& \& \& ############################################################################### \& # \& # Example 7. Demonstrates how to set the cell comment author. \& # \& \& $worksheet7\->set_column(\*(AqC:C\*(Aq, 30); \& $worksheet7\->set_row(2, 50); \& $worksheet7\->set_row(5, 50); \& $worksheet7\->set_row(8, 50); \& $worksheet7\->set_row(11, 50); \& \& my $author = \*(Aq\*(Aq; \& my $cell = \*(AqC3\*(Aq; \& \& $cell_text = "Move the mouse over this cell and you will see \*(AqCell commented ". \& "by $author\*(Aq (blank) in the status bar at the bottom"; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet7\->write ($cell, $cell_text, $text_wrap); \& $worksheet7\->write_comment($cell, $comment); \& \& \& $author = \*(AqPerl\*(Aq; \& $cell = \*(AqC6\*(Aq; \& $cell_text = "Move the mouse over this cell and you will see \*(AqCell commented ". \& "by $author\*(Aq in the status bar at the bottom"; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet7\->write ($cell, $cell_text, $text_wrap); \& $worksheet7\->write_comment($cell, $comment, author => $author); \& \& \& $author = pack "n", 0x20AC; # UTF\-16 Euro \& $cell = \*(AqC9\*(Aq; \& $cell_text = "Move the mouse over this cell and you will see \*(AqCell commented ". \& "by Euro\*(Aq in the status bar at the bottom"; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet7\->write ($cell, $cell_text, $text_wrap); \& $worksheet7\->write_comment($cell, $comment, author => $author, \& author_encoding => 1 ); \& \& # UTF\-8 string in perl 5.8. \& if ($] >= 5.008) { \& $author = chr 0x20AC; \& $cell = \*(AqC12\*(Aq; \& $cell_text = "Move the mouse over this cell and you will see \*(AqCell commented ". \& "by $author\*(Aq in the status bar at the bottom"; \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet7\->write ($cell, $cell_text, $text_wrap); \& $worksheet7\->write_comment($cell, $comment, author => $author); \& \& } \& \& \& ############################################################################### \& # \& # Example 8. Demonstrates the need to explicitly set the row height. \& # \& \& # Set up some formatting. \& $worksheet8\->set_column(\*(AqC:C\*(Aq, 25); \& $worksheet8\->set_row(2, 80); \& \& $worksheet8\->show_comments(); \& \& \& $cell_text = \*(AqThe height of this row has been adjusted explicitly using \*(Aq . \& \*(Aqset_row(). The size of the comment box is adjusted \*(Aq . \& \*(Aqaccordingly by WriteExcel.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet8\->write (\*(AqC3\*(Aq, $cell_text, $text_wrap); \& $worksheet8\->write_comment(\*(AqC3\*(Aq, $comment); \& \& \& $cell_text = \*(AqThe height of this row has been adjusted by Excel due to the \*(Aq . \& \*(Aqtext wrap property being set. Unfortunately this means that \*(Aq . \& \*(Aqthe height of the row is unknown to WriteExcel at run time \*(Aq . \& "and thus the comment box is stretched as well.\en\en" . \& \*(AqUse set_row() to specify the row height explicitly to avoid \*(Aq . \& \*(Aqthis problem.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet8\->write (\*(AqC6\*(Aq, $cell_text, $text_wrap); \& $worksheet8\->write_comment(\*(AqC6\*(Aq, $comment); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: copyformat.pl" .IX Subsection "Example: copyformat.pl" Example of how to use the format copying method with Spreadsheet::WriteExcel. .PP This feature isn't required very often. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the format copying method with Spreadsheet::WriteExcel. \& # \& # This feature isn\*(Aqt required very often. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create workbook1 \& my $workbook1 = Spreadsheet::WriteExcel\->new("workbook1.xls"); \& my $worksheet1 = $workbook1\->add_worksheet(); \& my $format1a = $workbook1\->add_format(); \& my $format1b = $workbook1\->add_format(); \& \& # Create workbook2 \& my $workbook2 = Spreadsheet::WriteExcel\->new("workbook2.xls"); \& my $worksheet2 = $workbook2\->add_worksheet(); \& my $format2a = $workbook2\->add_format(); \& my $format2b = $workbook2\->add_format(); \& \& \& # Create a global format object that isn\*(Aqt tied to a workbook \& my $global_format = Spreadsheet::WriteExcel::Format\->new(); \& \& # Set the formatting \& $global_format\->set_color(\*(Aqblue\*(Aq); \& $global_format\->set_bold(); \& $global_format\->set_italic(); \& \& # Create another example format \& $format1b\->set_color(\*(Aqred\*(Aq); \& \& # Copy the global format properties to the worksheet formats \& $format1a\->copy($global_format); \& $format2a\->copy($global_format); \& \& # Copy a format from worksheet1 to worksheet2 \& $format2b\->copy($format1b); \& \& # Write some output \& $worksheet1\->write(0, 0, "Ciao", $format1a); \& $worksheet1\->write(1, 0, "Ciao", $format1b); \& \& $worksheet2\->write(0, 0, "Hello", $format2a); \& $worksheet2\->write(1, 0, "Hello", $format2b); .Ve .PP Download this example: .SS "Example: data_validate.pl" .IX Subsection "Example: data_validate.pl" Example of how to add data validation and dropdown lists to a Spreadsheet::WriteExcel file. .PP Data validation is a feature of Excel which allows you to restrict the data that a users enters in a cell and to display help and warning messages. It also allows you to restrict input to values in a drop down list. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add data validation and dropdown lists to a \& # Spreadsheet::WriteExcel file. \& # \& # Data validation is a feature of Excel which allows you to restrict the data \& # that a users enters in a cell and to display help and warning messages. It \& # also allows you to restrict input to values in a drop down list. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2008, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqdata_validate.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Add a format for the header cells. \& my $header_format = $workbook\->add_format( \& border => 1, \& bg_color => 43, \& bold => 1, \& text_wrap => 1, \& valign => \*(Aqvcenter\*(Aq, \& indent => 1, \& ); \& \& # Set up layout of the worksheet. \& $worksheet\->set_column(\*(AqA:A\*(Aq, 64); \& $worksheet\->set_column(\*(AqB:B\*(Aq, 15); \& $worksheet\->set_column(\*(AqD:D\*(Aq, 15); \& $worksheet\->set_row(0, 36); \& $worksheet\->set_selection(\*(AqB3\*(Aq); \& \& \& # Write the header cells and some data that will be used in the examples. \& my $row = 0; \& my $txt; \& my $heading1 = \*(AqSome examples of data validation in Spreadsheet::WriteExcel\*(Aq; \& my $heading2 = \*(AqEnter values in this column\*(Aq; \& my $heading3 = \*(AqSample Data\*(Aq; \& \& $worksheet\->write(\*(AqA1\*(Aq, $heading1, $header_format); \& $worksheet\->write(\*(AqB1\*(Aq, $heading2, $header_format); \& $worksheet\->write(\*(AqD1\*(Aq, $heading3, $header_format); \& \& $worksheet\->write(\*(AqD3\*(Aq, [\*(AqIntegers\*(Aq, 1, 10]); \& $worksheet\->write(\*(AqD4\*(Aq, [\*(AqList data\*(Aq, \*(Aqopen\*(Aq, \*(Aqhigh\*(Aq, \*(Aqclose\*(Aq]); \& $worksheet\->write(\*(AqD5\*(Aq, [\*(AqFormula\*(Aq, \*(Aq=AND(F5=50,G5=60)\*(Aq, 50, 60]); \& \& \& # \& # Example 1. Limiting input to an integer in a fixed range. \& # \& $txt = \*(AqEnter an integer between 1 and 10\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 10, \& }); \& \& \& # \& # Example 2. Limiting input to an integer outside a fixed range. \& # \& $txt = \*(AqEnter an integer that is not between 1 and 10 (using cell references)\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqnot between\*(Aq, \& minimum => \*(Aq=E3\*(Aq, \& maximum => \*(Aq=F3\*(Aq, \& }); \& \& \& # \& # Example 3. Limiting input to an integer greater than a fixed value. \& # \& $txt = \*(AqEnter an integer greater than 0\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 0, \& }); \& \& \& # \& # Example 4. Limiting input to an integer less than a fixed value. \& # \& $txt = \*(AqEnter an integer less than 10\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq<\*(Aq, \& value => 10, \& }); \& \& \& # \& # Example 5. Limiting input to a decimal in a fixed range. \& # \& $txt = \*(AqEnter a decimal between 0.1 and 0.5\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqdecimal\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 0.1, \& maximum => 0.5, \& }); \& \& \& # \& # Example 6. Limiting input to a value in a dropdown list. \& # \& $txt = \*(AqSelect a value from a drop down list\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqlist\*(Aq, \& source => [\*(Aqopen\*(Aq, \*(Aqhigh\*(Aq, \*(Aqclose\*(Aq], \& }); \& \& \& # \& # Example 6. Limiting input to a value in a dropdown list. \& # \& $txt = \*(AqSelect a value from a drop down list (using a cell range)\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqlist\*(Aq, \& source => \*(Aq=E4:G4\*(Aq, \& }); \& \& \& # \& # Example 7. Limiting input to a date in a fixed range. \& # \& $txt = \*(AqEnter a date between 1/1/2008 and 12/12/2008\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqdate\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => \*(Aq2008\-01\-01T\*(Aq, \& maximum => \*(Aq2008\-12\-12T\*(Aq, \& }); \& \& \& # \& # Example 8. Limiting input to a time in a fixed range. \& # \& $txt = \*(AqEnter a time between 6:00 and 12:00\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqtime\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => \*(AqT06:00\*(Aq, \& maximum => \*(AqT12:00\*(Aq, \& }); \& \& \& # \& # Example 9. Limiting input to a string greater than a fixed length. \& # \& $txt = \*(AqEnter a string longer than 3 characters\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqlength\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 3, \& }); \& \& \& # \& # Example 10. Limiting input based on a formula. \& # \& $txt = \*(AqEnter a value if the following is true "=AND(F5=50,G5=60)"\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqcustom\*(Aq, \& value => \*(Aq=AND(F5=50,G5=60)\*(Aq, \& }); \& \& \& # \& # Example 11. Displaying and modify data validation messages. \& # \& $txt = \*(AqDisplays a message when you select the cell\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqEnter an integer:\*(Aq, \& input_message => \*(Aqbetween 1 and 100\*(Aq, \& }); \& \& \& # \& # Example 12. Displaying and modify data validation messages. \& # \& $txt = \*(AqDisplay a custom error message when integer isn\e\*(Aqt between 1 and 100\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqEnter an integer:\*(Aq, \& input_message => \*(Aqbetween 1 and 100\*(Aq, \& error_title => \*(AqInput value is not valid!\*(Aq, \& error_message => \*(AqIt should be an integer between 1 and 100\*(Aq, \& }); \& \& \& # \& # Example 13. Displaying and modify data validation messages. \& # \& $txt = \*(AqDisplay a custom information message when integer isn\e\*(Aqt between 1 and 100\*(Aq; \& $row += 2; \& \& $worksheet\->write($row, 0, $txt); \& $worksheet\->data_validation($row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqEnter an integer:\*(Aq, \& input_message => \*(Aqbetween 1 and 100\*(Aq, \& error_title => \*(AqInput value is not valid!\*(Aq, \& error_message => \*(AqIt should be an integer between 1 and 100\*(Aq, \& error_type => \*(Aqinformation\*(Aq, \& }); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: date_time.pl" .IX Subsection "Example: date_time.pl" Spreadsheet::WriteExcel example of writing dates and times using the \&\fBwrite_date_time()\fR Worksheet method. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Spreadsheet::WriteExcel example of writing dates and times using the \& # write_date_time() Worksheet method. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2004, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("date_time.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format(bold => 1); \& my $row = 0; \& \& \& # Expand the first column so that the date is visible. \& $worksheet\->set_column("A:B", 30); \& \& \& # Write the column headers \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqFormatted date\*(Aq, $bold); \& $worksheet\->write(\*(AqB1\*(Aq, \*(AqFormat\*(Aq, $bold); \& \& \& # Examples date and time formats. In the output file compare how changing \& # the format codes change the appearance of the date. \& # \& my @date_formats = ( \& \*(Aqdd/mm/yy\*(Aq, \& \*(Aqmm/dd/yy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqd mm yy\*(Aq, \& \*(Aqdd mm yy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqdd m yy\*(Aq, \& \*(Aqdd mm yy\*(Aq, \& \*(Aqdd mmm yy\*(Aq, \& \*(Aqdd mmmm yy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqdd mm y\*(Aq, \& \*(Aqdd mm yyy\*(Aq, \& \*(Aqdd mm yyyy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqd mmmm yyyy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqdd/mm/yy\*(Aq, \& \*(Aqdd/mm/yy hh:mm\*(Aq, \& \*(Aqdd/mm/yy hh:mm:ss\*(Aq, \& \*(Aqdd/mm/yy hh:mm:ss.000\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqhh:mm\*(Aq, \& \*(Aqhh:mm:ss\*(Aq, \& \*(Aqhh:mm:ss.000\*(Aq, \& ); \& \& \& # Write the same date and time using each of the above formats. The empty \& # string formats create a blank line to make the example clearer. \& # \& for my $date_format (@date_formats) { \& $row++; \& next if $date_format eq \*(Aq\*(Aq; \& \& # Create a format for the date or time. \& my $format = $workbook\->add_format( \& num_format => $date_format, \& align => \*(Aqleft\*(Aq \& ); \& \& # Write the same date using different formats. \& $worksheet\->write_date_time($row, 0, \*(Aq2004\-08\-01T12:30:45.123\*(Aq, $format); \& $worksheet\->write ($row, 1, $date_format); \& } \& \& \& # The following is an example of an invalid date. It is written as a string \& # instead of a number. This is also Excel\*(Aqs default behaviour. \& # \& $row += 2; \& $worksheet\->write_date_time($row, 0, \*(Aq2004\-13\-01T12:30:45.123\*(Aq); \& $worksheet\->write ($row, 1, \*(AqInvalid date. Written as string.\*(Aq, $bold); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: defined_name.pl" .IX Subsection "Example: defined_name.pl" Example of how to create defined names in a Spreadsheet::WriteExcel file. .PP This method is used to defined a name that can be used to represent a value, a single cell or a range of cells in a workbook. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to create defined names in a Spreadsheet::WriteExcel file. \& # \& # This method is used to defined a name that can be used to represent a value, \& # a single cell or a range of cells in a workbook. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2008, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqdefined_name.xls\*(Aq); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& \& \& $workbook\->define_name(\*(AqExchange_rate\*(Aq, \*(Aq=0.96\*(Aq); \& $workbook\->define_name(\*(AqSales\*(Aq, \*(Aq=Sheet1!$G$1:$H$10\*(Aq); \& $workbook\->define_name(\*(AqSheet2!Sales\*(Aq, \*(Aq=Sheet2!$G$1:$G$10\*(Aq); \& \& \& for my $worksheet ($workbook\->sheets()) { \& $worksheet\->set_column(\*(AqA:A\*(Aq, 45); \& $worksheet\->write(\*(AqA2\*(Aq, \*(AqThis worksheet contains some defined names,\*(Aq); \& $worksheet\->write(\*(AqA3\*(Aq, \*(AqSee the Insert \-> Name \-> Define dialog.\*(Aq); \& \& } \& \& \& $worksheet1\->write(\*(AqA4\*(Aq, \*(Aq=Exchange_rate\*(Aq); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: diag_border.pl" .IX Subsection "Example: diag_border.pl" A simple formatting example that demonstrates how to add a diagonal cell border with Spreadsheet::WriteExcel .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple formatting example that demonstrates how to add a diagonal cell \& # border with Spreadsheet::WriteExcel \& # \& # reverse(\*(Aq(c)\*(Aq), May 2004, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqdiag_border.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& my $format1 = $workbook\->add_format(diag_type => \*(Aq1\*(Aq); \& \& my $format2 = $workbook\->add_format(diag_type => \*(Aq2\*(Aq); \& \& my $format3 = $workbook\->add_format(diag_type => \*(Aq3\*(Aq); \& \& my $format4 = $workbook\->add_format( \& diag_type => \*(Aq3\*(Aq, \& diag_border => \*(Aq7\*(Aq, \& diag_color => \*(Aqred\*(Aq, \& ); \& \& \& $worksheet\->write(\*(AqB3\*(Aq, \*(AqText\*(Aq, $format1); \& $worksheet\->write(\*(AqB6\*(Aq, \*(AqText\*(Aq, $format2); \& $worksheet\->write(\*(AqB9\*(Aq, \*(AqText\*(Aq, $format3); \& $worksheet\->write(\*(AqB12\*(Aq, \*(AqText\*(Aq, $format4); \& \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: easter_egg.pl" .IX Subsection "Example: easter_egg.pl" This uses the Win32::OLE module to expose the Flight Simulator easter egg in Excel 97 \s-1SR2.\s0 .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # This uses the Win32::OLE module to expose the Flight Simulator easter egg \& # in Excel 97 SR2. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Win32::OLE; \& \& my $application = Win32::OLE\->new("Excel.Application"); \& my $workbook = $application\->Workbooks\->Add; \& my $worksheet = $workbook\->Worksheets(1); \& \& $application\->{Visible} = 1; \& \& $worksheet\->Range("L97:X97")\->Select; \& $worksheet\->Range("M97")\->Activate; \& \& my $message = "Hold down Shift and Ctrl and click the ". \& "Chart Wizard icon on the toolbar.\en\en". \& "Use the mouse motion and buttons to control ". \& "movement. Try to find the monolith. ". \& "Close this dialog first."; \& \& $application\->InputBox($message); .Ve .PP Download this example: .SS "Example: filehandle.pl" .IX Subsection "Example: filehandle.pl" Example of using Spreadsheet::WriteExcel to write Excel files to different filehandles. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of using Spreadsheet::WriteExcel to write Excel files to \& # different filehandles. \& # \& # reverse(\*(Aq(c)\*(Aq), April 2003, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& use IO::Scalar; \& \& \& \& \& ############################################################################### \& # \& # Example 1. This demonstrates the standard way of creating an Excel file by \& # specifying a file name. \& # \& \& my $workbook1 = Spreadsheet::WriteExcel\->new(\*(Aqfh_01.xls\*(Aq); \& my $worksheet1 = $workbook1\->add_worksheet(); \& \& $worksheet1\->write(0, 0, "Hi Excel!"); \& \& \& \& \& ############################################################################### \& # \& # Example 2. Write an Excel file to an existing filehandle. \& # \& \& open TEST, "> fh_02.xls" or die "Couldn\*(Aqt open file: $!"; \& binmode TEST; # Always do this regardless of whether the platform requires it. \& \& my $workbook2 = Spreadsheet::WriteExcel\->new(\e*TEST); \& my $worksheet2 = $workbook2\->add_worksheet(); \& \& $worksheet2\->write(0, 0, "Hi Excel!"); \& \& \& \& \& ############################################################################### \& # \& # Example 3. Write an Excel file to an existing OO style filehandle. \& # \& \& my $fh = FileHandle\->new("> fh_03.xls") \& or die "Couldn\*(Aqt open file: $!"; \& \& binmode($fh); \& \& my $workbook3 = Spreadsheet::WriteExcel\->new($fh); \& my $worksheet3 = $workbook3\->add_worksheet(); \& \& $worksheet3\->write(0, 0, "Hi Excel!"); \& \& \& \& \& ############################################################################### \& # \& # Example 4. Write an Excel file to a string via IO::Scalar. Please refer to \& # the IO::Scalar documentation for further details. \& # \& \& my $xls_str; \& \& tie *XLS, \*(AqIO::Scalar\*(Aq, \e$xls_str; \& \& my $workbook4 = Spreadsheet::WriteExcel\->new(\e*XLS); \& my $worksheet4 = $workbook4\->add_worksheet(); \& \& $worksheet4\->write(0, 0, "Hi Excel 4"); \& $workbook4\->close(); # This is required before we use the scalar \& \& \& # The Excel file is now in $xls_str. As a demonstration, print it to a file. \& open TMP, "> fh_04.xls" or die "Couldn\*(Aqt open file: $!"; \& binmode TMP; \& print TMP $xls_str; \& close TMP; \& \& \& \& \& ############################################################################### \& # \& # Example 5. Write an Excel file to a string via IO::Scalar\*(Aqs newer interface. \& # Please refer to the IO::Scalar documentation for further details. \& # \& my $xls_str2; \& \& my $fh5 = IO::Scalar\->new(\e$xls_str2); \& \& \& my $workbook5 = Spreadsheet::WriteExcel\->new($fh5); \& my $worksheet5 = $workbook5\->add_worksheet(); \& \& $worksheet5\->write(0, 0, "Hi Excel 5"); \& $workbook5\->close(); # This is required before we use the scalar \& \& # The Excel file is now in $xls_str. As a demonstration, print it to a file. \& open TMP, "> fh_05.xls" or die "Couldn\*(Aqt open file: $!"; \& binmode TMP; \& print TMP $xls_str2; \& close TMP; .Ve .PP Download this example: .SS "Example: formula_result.pl" .IX Subsection "Example: formula_result.pl" Example of how to write Spreadsheet::WriteExcel formulas with a user specified result. .PP This is generally only required when writing a spreadsheet for an application other than Excel where the formula isn't evaluated. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # Example of how to write Spreadsheet::WriteExcel formulas with a user \& # specified result. \& # \& # This is generally only required when writing a spreadsheet for an \& # application other than Excel where the formula isn\*(Aqt evaluated. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2005, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqformula_result.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& my $format = $workbook\->add_format(color => \*(Aqblue\*(Aq); \& \& \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq=1+2\*(Aq); \& $worksheet\->write(\*(AqA2\*(Aq, \*(Aq=1+2\*(Aq, $format, 4); \& $worksheet\->write(\*(AqA3\*(Aq, \*(Aq="ABC"\*(Aq, undef, \*(AqDEF\*(Aq); \& $worksheet\->write(\*(AqA4\*(Aq, \*(Aq=IF(A1 > 1, TRUE, FALSE)\*(Aq, undef, \*(AqTRUE\*(Aq); \& $worksheet\->write(\*(AqA5\*(Aq, \*(Aq=1/0\*(Aq, undef, \*(Aq#DIV/0!\*(Aq); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: headers.pl" .IX Subsection "Example: headers.pl" This program shows several examples of how to set up headers and footers with Spreadsheet::WriteExcel. .PP The control characters used in the header/footer strings are: .PP .Vb 5 \& Control Category Description \& ======= ======== =========== \& &L Justification Left \& &C Center \& &R Right \& \& &P Information Page number \& &N Total number of pages \& &D Date \& &T Time \& &F File name \& &A Worksheet name \& \& &fontsize Font Font size \& &"font,style" Font name and style \& &U Single underline \& &E Double underline \& &S Strikethrough \& &X Superscript \& &Y Subscript \& \& && Miscellaneous Literal ampersand & .Ve .PP See the main Spreadsheet::WriteExcel documentation for more information. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ###################################################################### \& # \& # This program shows several examples of how to set up headers and \& # footers with Spreadsheet::WriteExcel. \& # \& # The control characters used in the header/footer strings are: \& # \& # Control Category Description \& # ======= ======== =========== \& # &L Justification Left \& # &C Center \& # &R Right \& # \& # &P Information Page number \& # &N Total number of pages \& # &D Date \& # &T Time \& # &F File name \& # &A Worksheet name \& # \& # &fontsize Font Font size \& # &"font,style" Font name and style \& # &U Single underline \& # &E Double underline \& # &S Strikethrough \& # &X Superscript \& # &Y Subscript \& # \& # && Miscellaneous Literal ampersand & \& # \& # See the main Spreadsheet::WriteExcel documentation for more information. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2002, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("headers.xls"); \& my $preview = "Select Print Preview to see the header and footer"; \& \& \& ###################################################################### \& # \& # A simple example to start \& # \& my $worksheet1 = $workbook\->add_worksheet(\*(AqSimple\*(Aq); \& \& my $header1 = \*(Aq&CHere is some centred text.\*(Aq; \& \& my $footer1 = \*(Aq&LHere is some left aligned text.\*(Aq; \& \& \& $worksheet1\->set_header($header1); \& $worksheet1\->set_footer($footer1); \& \& $worksheet1\->set_column(\*(AqA:A\*(Aq, 50); \& $worksheet1\->write(\*(AqA1\*(Aq, $preview); \& \& \& \& \& ###################################################################### \& # \& # This is an example of some of the header/footer variables. \& # \& my $worksheet2 = $workbook\->add_worksheet(\*(AqVariables\*(Aq); \& \& my $header2 = \*(Aq&LPage &P of &N\*(Aq. \& \*(Aq&CFilename: &F\*(Aq . \& \*(Aq&RSheetname: &A\*(Aq; \& \& my $footer2 = \*(Aq&LCurrent date: &D\*(Aq. \& \*(Aq&RCurrent time: &T\*(Aq; \& \& \& \& $worksheet2\->set_header($header2); \& $worksheet2\->set_footer($footer2); \& \& \& $worksheet2\->set_column(\*(AqA:A\*(Aq, 50); \& $worksheet2\->write(\*(AqA1\*(Aq, $preview); \& $worksheet2\->write(\*(AqA21\*(Aq, "Next sheet"); \& $worksheet2\->set_h_pagebreaks(20); \& \& \& \& ###################################################################### \& # \& # This example shows how to use more than one font \& # \& my $worksheet3 = $workbook\->add_worksheet(\*(AqMixed fonts\*(Aq); \& \& my $header3 = \*(Aq&C\*(Aq . \& \*(Aq&"Courier New,Bold"Hello \*(Aq . \& \*(Aq&"Arial,Italic"World\*(Aq; \& \& my $footer3 = \*(Aq&C\*(Aq . \& \*(Aq&"Symbol"e\*(Aq . \& \*(Aq&"Arial" = mc&X2\*(Aq; \& \& $worksheet3\->set_header($header3); \& $worksheet3\->set_footer($footer3); \& \& $worksheet3\->set_column(\*(AqA:A\*(Aq, 50); \& $worksheet3\->write(\*(AqA1\*(Aq, $preview); \& \& \& \& \& ###################################################################### \& # \& # Example of line wrapping \& # \& my $worksheet4 = $workbook\->add_worksheet(\*(AqWord wrap\*(Aq); \& \& my $header4 = "&CHeading 1\enHeading 2\enHeading 3"; \& \& $worksheet4\->set_header($header4); \& \& $worksheet4\->set_column(\*(AqA:A\*(Aq, 50); \& $worksheet4\->write(\*(AqA1\*(Aq, $preview); \& \& \& \& \& ###################################################################### \& # \& # Example of inserting a literal ampersand & \& # \& my $worksheet5 = $workbook\->add_worksheet(\*(AqAmpersand\*(Aq); \& \& my $header5 = "&CCuriouser && Curiouser \- Attorneys at Law"; \& \& $worksheet5\->set_header($header5); \& \& $worksheet5\->set_column(\*(AqA:A\*(Aq, 50); \& $worksheet5\->write(\*(AqA1\*(Aq, $preview); .Ve .PP Download this example: .SS "Example: hide_sheet.pl" .IX Subsection "Example: hide_sheet.pl" Example of how to hide a worksheet with Spreadsheet::WriteExcel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # Example of how to hide a worksheet with Spreadsheet::WriteExcel. \& # \& # reverse(\*(Aq(c)\*(Aq), April 2005, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqhidden.xls\*(Aq); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& \& # Sheet2 won\*(Aqt be visible until it is unhidden in Excel. \& $worksheet2\->hide(); \& \& $worksheet1\->write(0, 0, \*(AqSheet2 is hidden\*(Aq); \& $worksheet2\->write(0, 0, \*(AqHow did you find me?\*(Aq); \& $worksheet3\->write(0, 0, \*(AqSheet2 is hidden\*(Aq); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: hyperlink1.pl" .IX Subsection "Example: hyperlink1.pl" Example of how to use the WriteExcel module to write hyperlinks. .PP See also hyperlink2.pl for worksheet \s-1URL\s0 examples. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the WriteExcel module to write hyperlinks. \& # \& # See also hyperlink2.pl for worksheet URL examples. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("hyperlink.xls"); \& my $worksheet = $workbook\->add_worksheet(\*(AqHyperlinks\*(Aq); \& \& # Format the first column \& $worksheet\->set_column(\*(AqA:A\*(Aq, 30); \& $worksheet\->set_selection(\*(AqB1\*(Aq); \& \& \& # Add a sample format \& my $format = $workbook\->add_format(); \& $format\->set_size(12); \& $format\->set_bold(); \& $format\->set_color(\*(Aqred\*(Aq); \& $format\->set_underline(); \& \& \& # Write some hyperlinks \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq ); \& $worksheet\->write(\*(AqA3\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq, \*(AqPerl home\*(Aq ); \& $worksheet\->write(\*(AqA5\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq, undef, $format); \& $worksheet\->write(\*(AqA7\*(Aq, \*(Aqmailto:jmcnamara@cpan.org\*(Aq, \*(AqMail me\*(Aq); \& \& # Write a URL that isn\*(Aqt a hyperlink \& $worksheet\->write_string(\*(AqA9\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq); .Ve .PP Download this example: .SS "Example: hyperlink2.pl" .IX Subsection "Example: hyperlink2.pl" Example of how to use the WriteExcel module to write internal and internal hyperlinks. .PP If you wish to run this program and follow the hyperlinks you should create the following directory structure: .PP .Vb 3 \& C:\e \-\- Temp \-\-+\-\- Europe \& | \& \e\-\- Asia .Ve .PP See also hyperlink1.pl for web \s-1URL\s0 examples. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the WriteExcel module to write internal and internal \& # hyperlinks. \& # \& # If you wish to run this program and follow the hyperlinks you should create \& # the following directory structure: \& # \& # C:\e \-\- Temp \-\-+\-\- Europe \& # | \& # \e\-\- Asia \& # \& # \& # See also hyperlink1.pl for web URL examples. \& # \& # reverse(\*(Aq(c)\*(Aq), February 2002, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create three workbooks: \& # C:\eTemp\eEurope\eIreland.xls \& # C:\eTemp\eEurope\eItaly.xls \& # C:\eTemp\eAsia\eChina.xls \& # \& my $ireland = Spreadsheet::WriteExcel\->new(\*(AqC:\eTemp\eEurope\eIreland.xls\*(Aq); \& my $ire_links = $ireland\->add_worksheet(\*(AqLinks\*(Aq); \& my $ire_sales = $ireland\->add_worksheet(\*(AqSales\*(Aq); \& my $ire_data = $ireland\->add_worksheet(\*(AqProduct Data\*(Aq); \& \& my $italy = Spreadsheet::WriteExcel\->new(\*(AqC:\eTemp\eEurope\eItaly.xls\*(Aq); \& my $ita_links = $italy\->add_worksheet(\*(AqLinks\*(Aq); \& my $ita_sales = $italy\->add_worksheet(\*(AqSales\*(Aq); \& my $ita_data = $italy\->add_worksheet(\*(AqProduct Data\*(Aq); \& \& my $china = Spreadsheet::WriteExcel\->new(\*(AqC:\eTemp\eAsia\eChina.xls\*(Aq); \& my $cha_links = $china\->add_worksheet(\*(AqLinks\*(Aq); \& my $cha_sales = $china\->add_worksheet(\*(AqSales\*(Aq); \& my $cha_data = $china\->add_worksheet(\*(AqProduct Data\*(Aq); \& \& # Add a format \& my $format = $ireland\->add_format(color => \*(Aqgreen\*(Aq, bold => 1); \& $ire_links\->set_column(\*(AqA:B\*(Aq, 25); \& \& \& ############################################################################### \& # \& # Examples of internal links \& # \& $ire_links\->write(\*(AqA1\*(Aq, \*(AqInternal links\*(Aq, $format); \& \& # Internal link \& $ire_links\->write(\*(AqA2\*(Aq, \*(Aqinternal:Sales!A2\*(Aq); \& \& # Internal link to a range \& $ire_links\->write(\*(AqA3\*(Aq, \*(Aqinternal:Sales!A3:D3\*(Aq); \& \& # Internal link with an alternative string \& $ire_links\->write(\*(AqA4\*(Aq, \*(Aqinternal:Sales!A4\*(Aq, \*(AqLink\*(Aq); \& \& # Internal link with a format \& $ire_links\->write(\*(AqA5\*(Aq, \*(Aqinternal:Sales!A5\*(Aq, $format); \& \& # Internal link with an alternative string and format \& $ire_links\->write(\*(AqA6\*(Aq, \*(Aqinternal:Sales!A6\*(Aq, \*(AqLink\*(Aq, $format); \& \& # Internal link (spaces in worksheet name) \& $ire_links\->write(\*(AqA7\*(Aq, q{internal:\*(AqProduct Data\*(Aq!A7}); \& \& \& ############################################################################### \& # \& # Examples of external links \& # \& $ire_links\->write(\*(AqB1\*(Aq, \*(AqExternal links\*(Aq, $format); \& \& # External link to a local file \& $ire_links\->write(\*(AqB2\*(Aq, \*(Aqexternal:Italy.xls\*(Aq); \& \& # External link to a local file with worksheet \& $ire_links\->write(\*(AqB3\*(Aq, \*(Aqexternal:Italy.xls#Sales!B3\*(Aq); \& \& # External link to a local file with worksheet and alternative string \& $ire_links\->write(\*(AqB4\*(Aq, \*(Aqexternal:Italy.xls#Sales!B4\*(Aq, \*(AqLink\*(Aq); \& \& # External link to a local file with worksheet and format \& $ire_links\->write(\*(AqB5\*(Aq, \*(Aqexternal:Italy.xls#Sales!B5\*(Aq, $format); \& \& # External link to a remote file, absolute path \& $ire_links\->write(\*(AqB6\*(Aq, \*(Aqexternal:c:/Temp/Asia/China.xls\*(Aq); \& \& # External link to a remote file, relative path \& $ire_links\->write(\*(AqB7\*(Aq, \*(Aqexternal:../Asia/China.xls\*(Aq); \& \& # External link to a remote file with worksheet \& $ire_links\->write(\*(AqB8\*(Aq, \*(Aqexternal:c:/Temp/Asia/China.xls#Sales!B8\*(Aq); \& \& # External link to a remote file with worksheet (with spaces in the name) \& $ire_links\->write(\*(AqB9\*(Aq, q{external:c:/Temp/Asia/China.xls#\*(AqProduct Data\*(Aq!B9}); \& \& \& ############################################################################### \& # \& # Some utility links to return to the main sheet \& # \& $ire_sales\->write(\*(AqA2\*(Aq, \*(Aqinternal:Links!A2\*(Aq, \*(AqBack\*(Aq); \& $ire_sales\->write(\*(AqA3\*(Aq, \*(Aqinternal:Links!A3\*(Aq, \*(AqBack\*(Aq); \& $ire_sales\->write(\*(AqA4\*(Aq, \*(Aqinternal:Links!A4\*(Aq, \*(AqBack\*(Aq); \& $ire_sales\->write(\*(AqA5\*(Aq, \*(Aqinternal:Links!A5\*(Aq, \*(AqBack\*(Aq); \& $ire_sales\->write(\*(AqA6\*(Aq, \*(Aqinternal:Links!A6\*(Aq, \*(AqBack\*(Aq); \& $ire_data\-> write(\*(AqA7\*(Aq, \*(Aqinternal:Links!A7\*(Aq, \*(AqBack\*(Aq); \& \& $ita_links\->write(\*(AqA1\*(Aq, \*(Aqexternal:Ireland.xls#Links!B2\*(Aq, \*(AqBack\*(Aq); \& $ita_sales\->write(\*(AqB3\*(Aq, \*(Aqexternal:Ireland.xls#Links!B3\*(Aq, \*(AqBack\*(Aq); \& $ita_sales\->write(\*(AqB4\*(Aq, \*(Aqexternal:Ireland.xls#Links!B4\*(Aq, \*(AqBack\*(Aq); \& $ita_sales\->write(\*(AqB5\*(Aq, \*(Aqexternal:Ireland.xls#Links!B5\*(Aq, \*(AqBack\*(Aq); \& $cha_links\->write(\*(AqA1\*(Aq, \*(Aqexternal:../Europe/Ireland.xls#Links!B6\*(Aq, \*(AqBack\*(Aq); \& $cha_sales\->write(\*(AqB8\*(Aq, \*(Aqexternal:../Europe/Ireland.xls#Links!B8\*(Aq, \*(AqBack\*(Aq); \& $cha_data\-> write(\*(AqB9\*(Aq, \*(Aqexternal:../Europe/Ireland.xls#Links!B9\*(Aq, \*(AqBack\*(Aq); .Ve .PP Download this example: .SS "Example: images.pl" .IX Subsection "Example: images.pl" Example of how to insert images into an Excel worksheet using the Spreadsheet::WriteExcel \fBinsert_image()\fR method. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # Example of how to insert images into an Excel worksheet using the \& # Spreadsheet::WriteExcel insert_image() method. \& # \& # reverse(\*(Aq(c)\*(Aq), October 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook called simple.xls and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("images.xls"); \& my $worksheet1 = $workbook\->add_worksheet(\*(AqImage 1\*(Aq); \& my $worksheet2 = $workbook\->add_worksheet(\*(AqImage 2\*(Aq); \& my $worksheet3 = $workbook\->add_worksheet(\*(AqImage 3\*(Aq); \& my $worksheet4 = $workbook\->add_worksheet(\*(AqImage 4\*(Aq); \& \& # Insert a basic image \& $worksheet1\->write(\*(AqA10\*(Aq, "Image inserted into worksheet."); \& $worksheet1\->insert_image(\*(AqA1\*(Aq, \*(Aqrepublic.png\*(Aq); \& \& \& # Insert an image with an offset \& $worksheet2\->write(\*(AqA10\*(Aq, "Image inserted with an offset."); \& $worksheet2\->insert_image(\*(AqA1\*(Aq, \*(Aqrepublic.png\*(Aq, 32, 10); \& \& # Insert a scaled image \& $worksheet3\->write(\*(AqA10\*(Aq, "Image scaled: width x 2, height x 0.8."); \& $worksheet3\->insert_image(\*(AqA1\*(Aq, \*(Aqrepublic.png\*(Aq, 0, 0, 2, 0.8); \& \& # Insert an image over varied column and row sizes. \& $worksheet4\->set_column(\*(AqA:A\*(Aq, 5); \& $worksheet4\->set_column(\*(AqB:B\*(Aq, undef, undef, 1); # Hidden \& $worksheet4\->set_column(\*(AqC:D\*(Aq, 10); \& $worksheet4\->set_row(0, 30); \& $worksheet4\->set_row(3, 5); \& \& $worksheet4\->write(\*(AqA10\*(Aq, "Image inserted over scaled rows and columns."); \& $worksheet4\->insert_image(\*(AqA1\*(Aq, \*(Aqrepublic.png\*(Aq); .Ve .PP Download this example: .SS "Example: indent.pl" .IX Subsection "Example: indent.pl" A simple formatting example using Spreadsheet::WriteExcel. .PP This program demonstrates the indentation cell format. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple formatting example using Spreadsheet::WriteExcel. \& # \& # This program demonstrates the indentation cell format. \& # \& # reverse(\*(Aq(c)\*(Aq), May 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqindent.xls\*(Aq); \& \& my $worksheet = $workbook\->add_worksheet(); \& my $indent1 = $workbook\->add_format(indent => 1); \& my $indent2 = $workbook\->add_format(indent => 2); \& \& $worksheet\->set_column(\*(AqA:A\*(Aq, 40); \& \& \& $worksheet\->write(\*(AqA1\*(Aq, "This text is indented 1 level", $indent1); \& $worksheet\->write(\*(AqA2\*(Aq, "This text is indented 2 levels", $indent2); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: merge1.pl" .IX Subsection "Example: merge1.pl" Simple example of merging cells using the Spreadsheet::WriteExcel module. .PP This example merges three cells using the \*(L"Centre Across Selection\*(R" alignment which was the Excel 5 method of achieving a merge. For a more modern approach use the \fBmerge_range()\fR worksheet method instead. See the merge3.pl \- merge6.pl programs. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Simple example of merging cells using the Spreadsheet::WriteExcel module. \& # \& # This example merges three cells using the "Centre Across Selection" \& # alignment which was the Excel 5 method of achieving a merge. For a more \& # modern approach use the merge_range() worksheet method instead. \& # See the merge3.pl \- merge6.pl programs. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2002, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("merge1.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_column(\*(AqB:D\*(Aq, 20); \& $worksheet\->set_row(2, 30); \& \& \& # Create a merge format \& my $format = $workbook\->add_format(center_across => 1); \& \& \& # Only one cell should contain text, the others should be blank. \& $worksheet\->write (2, 1, "Center across selection", $format); \& $worksheet\->write_blank(2, 2, $format); \& $worksheet\->write_blank(2, 3, $format); .Ve .PP Download this example: .SS "Example: merge2.pl" .IX Subsection "Example: merge2.pl" Simple example of merging cells using the Spreadsheet::WriteExcel module .PP This example merges three cells using the \*(L"Centre Across Selection\*(R" alignment which was the Excel 5 method of achieving a merge. For a more modern approach use the \fBmerge_range()\fR worksheet method instead. See the merge3.pl \- merge6.pl programs. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Simple example of merging cells using the Spreadsheet::WriteExcel module \& # \& # This example merges three cells using the "Centre Across Selection" \& # alignment which was the Excel 5 method of achieving a merge. For a more \& # modern approach use the merge_range() worksheet method instead. \& # See the merge3.pl \- merge6.pl programs. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2002, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("merge2.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_column(1, 2, 30); \& $worksheet\->set_row(2, 40); \& \& \& # Create a merged format \& my $format = $workbook\->add_format( \& center_across => 1, \& bold => 1, \& size => 15, \& pattern => 1, \& border => 6, \& color => \*(Aqwhite\*(Aq, \& fg_color => \*(Aqgreen\*(Aq, \& border_color => \*(Aqyellow\*(Aq, \& align => \*(Aqvcenter\*(Aq, \& ); \& \& \& # Only one cell should contain text, the others should be blank. \& $worksheet\->write (2, 1, "Center across selection", $format); \& $worksheet\->write_blank(2, 2, $format); .Ve .PP Download this example: .SS "Example: merge3.pl" .IX Subsection "Example: merge3.pl" Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a merged cell. There are two options \fBwrite_url_range()\fR with a standard merge format or \fBmerge_range()\fR. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a \& # merged cell. There are two options write_url_range() with a standard merge \& # format or merge_range(). \& # \& # reverse(\*(Aq(c)\*(Aq), September 2002, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqmerge3.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_row($_, 30) for (1, 3, 6, 7); \& $worksheet\->set_column(\*(AqB:D\*(Aq, 20); \& \& \& ############################################################################### \& # \& # Example 1: Merge cells containing a hyperlink using write_url_range() \& # and the standard Excel 5+ merge property. \& # \& my $format1 = $workbook\->add_format( \& center_across => 1, \& border => 1, \& underline => 1, \& color => \*(Aqblue\*(Aq, \& ); \& \& # Write the cells to be merged \& $worksheet\->write_url_range(\*(AqB2:D2\*(Aq, \*(Aqhttp://www.perl.com\*(Aq, $format1); \& $worksheet\->write_blank(\*(AqC2\*(Aq, $format1); \& $worksheet\->write_blank(\*(AqD2\*(Aq, $format1); \& \& \& \& ############################################################################### \& # \& # Example 2: Merge cells containing a hyperlink using merge_range(). \& # \& my $format2 = $workbook\->add_format( \& border => 1, \& underline => 1, \& color => \*(Aqblue\*(Aq, \& align => \*(Aqcenter\*(Aq, \& valign => \*(Aqvcenter\*(Aq, \& ); \& \& # Merge 3 cells \& $worksheet\->merge_range(\*(AqB4:D4\*(Aq, \*(Aqhttp://www.perl.com\*(Aq, $format2); \& \& \& # Merge 3 cells over two rows \& $worksheet\->merge_range(\*(AqB7:D8\*(Aq, \*(Aqhttp://www.perl.com\*(Aq, $format2); .Ve .PP Download this example: .SS "Example: merge4.pl" .IX Subsection "Example: merge4.pl" Example of how to use the Spreadsheet::WriteExcel \fBmerge_range()\fR workbook method with complex formatting. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the Spreadsheet::WriteExcel merge_range() workbook \& # method with complex formatting. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2002, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqmerge4.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_row($_, 30) for (1..11); \& $worksheet\->set_column(\*(AqB:D\*(Aq, 20); \& \& \& ############################################################################### \& # \& # Example 1: Text centered vertically and horizontally \& # \& my $format1 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqvcenter\*(Aq, \& align => \*(Aqcenter\*(Aq, \& ); \& \& \& \& $worksheet\->merge_range(\*(AqB2:D3\*(Aq, \*(AqVertical and horizontal\*(Aq, $format1); \& \& \& ############################################################################### \& # \& # Example 2: Text aligned to the top and left \& # \& my $format2 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqtop\*(Aq, \& align => \*(Aqleft\*(Aq, \& ); \& \& \& \& $worksheet\->merge_range(\*(AqB5:D6\*(Aq, \*(AqAligned to the top and left\*(Aq, $format2); \& \& \& ############################################################################### \& # \& # Example 3: Text aligned to the bottom and right \& # \& my $format3 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqbottom\*(Aq, \& align => \*(Aqright\*(Aq, \& ); \& \& \& \& $worksheet\->merge_range(\*(AqB8:D9\*(Aq, \*(AqAligned to the bottom and right\*(Aq, $format3); \& \& \& ############################################################################### \& # \& # Example 4: Text justified (i.e. wrapped) in the cell \& # \& my $format4 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqtop\*(Aq, \& align => \*(Aqjustify\*(Aq, \& ); \& \& \& \& $worksheet\->merge_range(\*(AqB11:D12\*(Aq, \*(AqJustified: \*(Aq.\*(Aqso on and \*(Aq x18, $format4); .Ve .PP Download this example: .SS "Example: merge5.pl" .IX Subsection "Example: merge5.pl" Example of how to use the Spreadsheet::WriteExcel \fBmerge_cells()\fR workbook method with complex formatting and rotation. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook \& # method with complex formatting and rotation. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2002, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqmerge5.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_row($_, 36) for (3..8); \& $worksheet\->set_column($_, $_ , 15) for (1,3,5); \& \& \& ############################################################################### \& # \& # Rotation 1, letters run from top to bottom \& # \& my $format1 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqvcentre\*(Aq, \& align => \*(Aqcentre\*(Aq, \& rotation => 270, \& ); \& \& \& $worksheet\->merge_range(\*(AqB4:B9\*(Aq, \*(AqRotation 270\*(Aq, $format1); \& \& \& ############################################################################### \& # \& # Rotation 2, 90 deg anticlockwise \& # \& my $format2 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqvcentre\*(Aq, \& align => \*(Aqcentre\*(Aq, \& rotation => 90, \& ); \& \& \& $worksheet\->merge_range(\*(AqD4:D9\*(Aq, \*(AqRotation 90 deg\*(Aq, $format2); \& \& \& \& ############################################################################### \& # \& # Rotation 3, 90 deg clockwise \& # \& my $format3 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqvcentre\*(Aq, \& align => \*(Aqcentre\*(Aq, \& rotation => \-90, \& ); \& \& \& $worksheet\->merge_range(\*(AqF4:F9\*(Aq, \*(AqRotation \-90 deg\*(Aq, $format3); .Ve .PP Download this example: .SS "Example: merge6.pl" .IX Subsection "Example: merge6.pl" Example of how to use the Spreadsheet::WriteExcel \fBmerge_cells()\fR workbook method with Unicode strings. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook \& # method with Unicode strings. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), December 2005, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqmerge6.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_row($_, 36) for 2..9; \& $worksheet\->set_column(\*(AqB:D\*(Aq, 25); \& \& \& # Format for the merged cells. \& my $format = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& size => 20, \& valign => \*(Aqvcentre\*(Aq, \& align => \*(Aqleft\*(Aq, \& indent => 1, \& ); \& \& \& \& \& ############################################################################### \& # \& # Write an Ascii string. \& # \& \& $worksheet\->merge_range(\*(AqB3:D4\*(Aq, \*(AqASCII: A simple string\*(Aq, $format); \& \& \& \& \& ############################################################################### \& # \& # Write a UTF\-16 Unicode string. \& # \& \& # A phrase in Cyrillic encoded as UTF\-16BE. \& my $utf16_str = pack "H*", \*(Aq005500540046002d00310036003a0020\*(Aq. \& \*(Aq042d0442043e002004440440043004370430002004\*(Aq. \& \*(Aq3d043000200440044304410441043a043e043c0021\*(Aq; \& \& # Note the extra parameter at the end to indicate UTF\-16 encoding. \& $worksheet\->merge_range(\*(AqB6:D7\*(Aq, $utf16_str, $format, 1); \& \& \& \& \& ############################################################################### \& # \& # Write a UTF\-8 Unicode string. \& # \& \& if ($] >= 5.008) { \& my $smiley = chr 0x263a; \& $worksheet\->merge_range(\*(AqB9:D10\*(Aq, "UTF\-8: A Unicode smiley $smiley", \& $format); \& } \& else { \& $worksheet\->merge_range(\*(AqB9:D10\*(Aq, "UTF\-8: Requires Perl 5.8", $format); \& } \& \& \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: mod_perl1.pl" .IX Subsection "Example: mod_perl1.pl" Example of how to use the Spreadsheet::WriteExcel module to send an Excel file to a browser using mod_perl 1 and Apache .PP This module ties *XLS directly to Apache, and with the correct content\-disposition/types it will prompt the user to save the file, or open it at this location. .PP This script is a modification of the Spreadsheet::WriteExcel cgi.pl example. .PP Change the name of this file to Cgi.pm. Change the package location to where ever you locate this package. In the example below it is located in the WriteExcel directory. .PP Your httpd.conf entry for this module, should you choose to use it as a stand alone app, should look similar to the following: .PP .Vb 5 \& \& SetHandler perl\-script \& PerlHandler Spreadsheet::WriteExcel::Cgi \& PerlSendHeader On \& .Ve .PP The PerlHandler name above and the package name below *have* to match. .PP .Vb 10 \& ############################################################################### \& # \& # Example of how to use the Spreadsheet::WriteExcel module to send an Excel \& # file to a browser using mod_perl 1 and Apache \& # \& # This module ties *XLS directly to Apache, and with the correct \& # content\-disposition/types it will prompt the user to save \& # the file, or open it at this location. \& # \& # This script is a modification of the Spreadsheet::WriteExcel cgi.pl example. \& # \& # Change the name of this file to Cgi.pm. \& # Change the package location to where ever you locate this package. \& # In the example below it is located in the WriteExcel directory. \& # \& # Your httpd.conf entry for this module, should you choose to use it \& # as a stand alone app, should look similar to the following: \& # \& # \& # SetHandler perl\-script \& # PerlHandler Spreadsheet::WriteExcel::Cgi \& # PerlSendHeader On \& # \& # \& # The PerlHandler name above and the package name below *have* to match. \& \& # Apr 2001, Thomas Sullivan, webmaster@860.org \& # Feb 2001, John McNamara, jmcnamara@cpan.org \& \& package Spreadsheet::WriteExcel::Cgi; \& \& ########################################## \& # Pragma Definitions \& ########################################## \& use strict; \& \& ########################################## \& # Required Modules \& ########################################## \& use Apache::Constants qw(:common); \& use Apache::Request; \& use Apache::URI; # This may not be needed \& use Spreadsheet::WriteExcel; \& \& ########################################## \& # Main App Body \& ########################################## \& sub handler { \& # New apache object \& # Should you decide to use it. \& my $r = Apache::Request\->new(shift); \& \& # Set the filename and send the content type \& # This will appear when they save the spreadsheet \& my $filename ="cgitest.xls"; \& \& #################################################### \& ## Send the content type headers \& #################################################### \& print "Content\-disposition: attachment;filename=$filename\en"; \& print "Content\-type: application/vnd.ms\-excel\en\en"; \& \& #################################################### \& # Tie a filehandle to Apache\*(Aqs STDOUT. \& # Create a new workbook and add a worksheet. \& #################################################### \& tie *XLS => \*(AqApache\*(Aq; \& binmode(*XLS); \& \& my $workbook = Spreadsheet::WriteExcel\->new(\e*XLS); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Set the column width for column 1 \& $worksheet\->set_column(0, 0, 20); \& \& \& # Create a format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_size(15); \& $format\->set_color(\*(Aqblue\*(Aq); \& \& \& # Write to the workbook \& $worksheet\->write(0, 0, "Hi Excel!", $format); \& \& # You must close the workbook for Content\-disposition \& $workbook\->close(); \& } \& \& 1; .Ve .PP Download this example: .SS "Example: mod_perl2.pl" .IX Subsection "Example: mod_perl2.pl" Example of how to use the Spreadsheet::WriteExcel module to send an Excel file to a browser using mod_perl 2 and Apache. .PP This module ties *XLS directly to Apache, and with the correct content\-disposition/types it will prompt the user to save the file, or open it at this location. .PP This script is a modification of the Spreadsheet::WriteExcel cgi.pl example. .PP Change the name of this file to MP2Test.pm. Change the package location to where ever you locate this package. In the example below it is located in the WriteExcel directory. .PP Your httpd.conf entry for this module, should you choose to use it as a stand alone app, should look similar to the following: .PP .Vb 3 \& PerlModule Apache2::RequestRec \& PerlModule APR::Table \& PerlModule Apache2::RequestIO \& \& \& SetHandler perl\-script \& PerlResponseHandler Spreadsheet::WriteExcel::MP2Test \& .Ve .PP The PerlResponseHandler must match the package name below. .PP .Vb 10 \& ############################################################################### \& # \& # Example of how to use the Spreadsheet::WriteExcel module to send an Excel \& # file to a browser using mod_perl 2 and Apache. \& # \& # This module ties *XLS directly to Apache, and with the correct \& # content\-disposition/types it will prompt the user to save \& # the file, or open it at this location. \& # \& # This script is a modification of the Spreadsheet::WriteExcel cgi.pl example. \& # \& # Change the name of this file to MP2Test.pm. \& # Change the package location to where ever you locate this package. \& # In the example below it is located in the WriteExcel directory. \& # \& # Your httpd.conf entry for this module, should you choose to use it \& # as a stand alone app, should look similar to the following: \& # \& # PerlModule Apache2::RequestRec \& # PerlModule APR::Table \& # PerlModule Apache2::RequestIO \& # \& # \& # SetHandler perl\-script \& # PerlResponseHandler Spreadsheet::WriteExcel::MP2Test \& # \& # \& # The PerlResponseHandler must match the package name below. \& \& # Jun 2004, Matisse Enzer, matisse@matisse.net (mod_perl 2 version) \& # Apr 2001, Thomas Sullivan, webmaster@860.org \& # Feb 2001, John McNamara, jmcnamara@cpan.org \& \& package Spreadsheet::WriteExcel::MP2Test; \& \& ########################################## \& # Pragma Definitions \& ########################################## \& use strict; \& \& ########################################## \& # Required Modules \& ########################################## \& use Apache2::Const \-compile => qw( :common ); \& use Spreadsheet::WriteExcel; \& \& ########################################## \& # Main App Body \& ########################################## \& sub handler { \& my($r) = @_; # Apache request object is passed to handler in mod_perl 2 \& \& # Set the filename and send the content type \& # This will appear when they save the spreadsheet \& my $filename ="mod_perl2_test.xls"; \& \& #################################################### \& ## Send the content type headers the mod_perl 2 way \& #################################################### \& $r\->headers_out\->{\*(AqContent\-Disposition\*(Aq} = "attachment;filename=$filename"; \& $r\->content_type(\*(Aqapplication/vnd.ms\-excel\*(Aq); \& \& #################################################### \& # Tie a filehandle to Apache\*(Aqs STDOUT. \& # Create a new workbook and add a worksheet. \& #################################################### \& tie *XLS => $r; # The mod_perl 2 way. Tie to the Apache::RequestRec object \& binmode(*XLS); \& \& my $workbook = Spreadsheet::WriteExcel\->new(\e*XLS); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Set the column width for column 1 \& $worksheet\->set_column(0, 0, 20); \& \& \& # Create a format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_size(15); \& $format\->set_color(\*(Aqblue\*(Aq); \& \& \& # Write to the workbook \& $worksheet\->write(0, 0, \*(AqHi Excel! from \*(Aq . $r\->hostname , $format); \& \& # You must close the workbook for Content\-disposition \& $workbook\->close(); \& return Apache2::Const::OK; \& } \& \& 1; .Ve .PP Download this example: .SS "Example: outline.pl" .IX Subsection "Example: outline.pl" Example of how use Spreadsheet::WriteExcel to generate Excel outlines and grouping. .PP Excel allows you to group rows or columns so that they can be hidden or displayed with a single mouse click. This feature is referred to as outlines. .PP Outlines can reduce complex data down to a few salient sub-totals or summaries. .PP This feature is best viewed in Excel but the following is an \s-1ASCII\s0 representation of what a worksheet with three outlines might look like. Rows 3\-4 and rows 7\-8 are grouped at level 2. Rows 2\-9 are grouped at level 1. The lines at the left hand side are called outline level bars. .PP .Vb 10 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& _ | 1 | A | | | | ... \& | _ | 2 | B | | | | ... \& | | | 3 | (C) | | | | ... \& | | | 4 | (D) | | | | ... \& | \- | 5 | E | | | | ... \& | _ | 6 | F | | | | ... \& | | | 7 | (G) | | | | ... \& | | | 8 | (H) | | | | ... \& | \- | 9 | I | | | | ... \& \- | . | ... | ... | ... | ... | ... .Ve .PP Clicking the minus sign on each of the level 2 outlines will collapse and hide the data as shown in the next figure. The minus sign changes to a plus sign to indicate that the data in the outline is hidden. .PP .Vb 9 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& _ | 1 | A | | | | ... \& | | 2 | B | | | | ... \& | + | 5 | E | | | | ... \& | | 6 | F | | | | ... \& | + | 9 | I | | | | ... \& \- | . | ... | ... | ... | ... | ... .Ve .PP Clicking on the minus sign on the level 1 outline will collapse the remaining rows as follows: .PP .Vb 5 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 1 | A | | | | ... \& + | . | ... | ... | ... | ... | ... .Ve .PP See the main Spreadsheet::WriteExcel documentation for more information. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how use Spreadsheet::WriteExcel to generate Excel outlines and \& # grouping. \& # \& # \& # Excel allows you to group rows or columns so that they can be hidden or \& # displayed with a single mouse click. This feature is referred to as outlines. \& # \& # Outlines can reduce complex data down to a few salient sub\-totals or \& # summaries. \& # \& # This feature is best viewed in Excel but the following is an ASCII \& # representation of what a worksheet with three outlines might look like. \& # Rows 3\-4 and rows 7\-8 are grouped at level 2. Rows 2\-9 are grouped at \& # level 1. The lines at the left hand side are called outline level bars. \& # \& # \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # 1 2 3 | | A | B | C | D | ... \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # _ | 1 | A | | | | ... \& # | _ | 2 | B | | | | ... \& # | | | 3 | (C) | | | | ... \& # | | | 4 | (D) | | | | ... \& # | \- | 5 | E | | | | ... \& # | _ | 6 | F | | | | ... \& # | | | 7 | (G) | | | | ... \& # | | | 8 | (H) | | | | ... \& # | \- | 9 | I | | | | ... \& # \- | . | ... | ... | ... | ... | ... \& # \& # \& # Clicking the minus sign on each of the level 2 outlines will collapse and \& # hide the data as shown in the next figure. The minus sign changes to a plus \& # sign to indicate that the data in the outline is hidden. \& # \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # 1 2 3 | | A | B | C | D | ... \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # _ | 1 | A | | | | ... \& # | | 2 | B | | | | ... \& # | + | 5 | E | | | | ... \& # | | 6 | F | | | | ... \& # | + | 9 | I | | | | ... \& # \- | . | ... | ... | ... | ... | ... \& # \& # \& # Clicking on the minus sign on the level 1 outline will collapse the remaining \& # rows as follows: \& # \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # 1 2 3 | | A | B | C | D | ... \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # | 1 | A | | | | ... \& # + | . | ... | ... | ... | ... | ... \& # \& # See the main Spreadsheet::WriteExcel documentation for more information. \& # \& # reverse(\*(Aq(c)\*(Aq), April 2003, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add some worksheets \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqoutline.xls\*(Aq); \& my $worksheet1 = $workbook\->add_worksheet(\*(AqOutlined Rows\*(Aq); \& my $worksheet2 = $workbook\->add_worksheet(\*(AqCollapsed Rows\*(Aq); \& my $worksheet3 = $workbook\->add_worksheet(\*(AqOutline Columns\*(Aq); \& my $worksheet4 = $workbook\->add_worksheet(\*(AqOutline levels\*(Aq); \& \& # Add a general format \& my $bold = $workbook\->add_format(bold => 1); \& \& \& \& ############################################################################### \& # \& # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL() \& # functions so that it looks like the type of automatic outlines that are \& # generated when you use the Excel Data\->SubTotals menu item. \& # \& \& \& # For outlines the important parameters are $hidden and $level. Rows with the \& # same $level are grouped together. The group will be collapsed if $hidden is \& # non\-zero. $height and $XF are assigned default values if they are undef. \& # \& # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) \& # \& $worksheet1\->set_row(1, undef, undef, 0, 2); \& $worksheet1\->set_row(2, undef, undef, 0, 2); \& $worksheet1\->set_row(3, undef, undef, 0, 2); \& $worksheet1\->set_row(4, undef, undef, 0, 2); \& $worksheet1\->set_row(5, undef, undef, 0, 1); \& \& $worksheet1\->set_row(6, undef, undef, 0, 2); \& $worksheet1\->set_row(7, undef, undef, 0, 2); \& $worksheet1\->set_row(8, undef, undef, 0, 2); \& $worksheet1\->set_row(9, undef, undef, 0, 2); \& $worksheet1\->set_row(10, undef, undef, 0, 1); \& \& \& # Add a column format for clarity \& $worksheet1\->set_column(\*(AqA:A\*(Aq, 20); \& \& # Add the data, labels and formulas \& $worksheet1\->write(\*(AqA1\*(Aq, \*(AqRegion\*(Aq, $bold); \& $worksheet1\->write(\*(AqA2\*(Aq, \*(AqNorth\*(Aq); \& $worksheet1\->write(\*(AqA3\*(Aq, \*(AqNorth\*(Aq); \& $worksheet1\->write(\*(AqA4\*(Aq, \*(AqNorth\*(Aq); \& $worksheet1\->write(\*(AqA5\*(Aq, \*(AqNorth\*(Aq); \& $worksheet1\->write(\*(AqA6\*(Aq, \*(AqNorth Total\*(Aq, $bold); \& \& $worksheet1\->write(\*(AqB1\*(Aq, \*(AqSales\*(Aq, $bold); \& $worksheet1\->write(\*(AqB2\*(Aq, 1000); \& $worksheet1\->write(\*(AqB3\*(Aq, 1200); \& $worksheet1\->write(\*(AqB4\*(Aq, 900); \& $worksheet1\->write(\*(AqB5\*(Aq, 1200); \& $worksheet1\->write(\*(AqB6\*(Aq, \*(Aq=SUBTOTAL(9,B2:B5)\*(Aq, $bold); \& \& $worksheet1\->write(\*(AqA7\*(Aq, \*(AqSouth\*(Aq); \& $worksheet1\->write(\*(AqA8\*(Aq, \*(AqSouth\*(Aq); \& $worksheet1\->write(\*(AqA9\*(Aq, \*(AqSouth\*(Aq); \& $worksheet1\->write(\*(AqA10\*(Aq, \*(AqSouth\*(Aq); \& $worksheet1\->write(\*(AqA11\*(Aq, \*(AqSouth Total\*(Aq, $bold); \& \& $worksheet1\->write(\*(AqB7\*(Aq, 400); \& $worksheet1\->write(\*(AqB8\*(Aq, 600); \& $worksheet1\->write(\*(AqB9\*(Aq, 500); \& $worksheet1\->write(\*(AqB10\*(Aq, 600); \& $worksheet1\->write(\*(AqB11\*(Aq, \*(Aq=SUBTOTAL(9,B7:B10)\*(Aq, $bold); \& \& $worksheet1\->write(\*(AqA12\*(Aq, \*(AqGrand Total\*(Aq, $bold); \& $worksheet1\->write(\*(AqB12\*(Aq, \*(Aq=SUBTOTAL(9,B2:B10)\*(Aq, $bold); \& \& \& ############################################################################### \& # \& # Example 2: Create a worksheet with outlined rows. This is the same as the \& # previous example except that the rows are collapsed. \& # Note: We need to indicate the row that contains the collapsed symbol \*(Aq+\*(Aq \& # with the optional parameter, $collapsed. \& \& # The group will be collapsed if $hidden is non\-zero. \& # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) \& # \& $worksheet2\->set_row(1, undef, undef, 1, 2); \& $worksheet2\->set_row(2, undef, undef, 1, 2); \& $worksheet2\->set_row(3, undef, undef, 1, 2); \& $worksheet2\->set_row(4, undef, undef, 1, 2); \& $worksheet2\->set_row(5, undef, undef, 1, 1); \& \& $worksheet2\->set_row(6, undef, undef, 1, 2); \& $worksheet2\->set_row(7, undef, undef, 1, 2); \& $worksheet2\->set_row(8, undef, undef, 1, 2); \& $worksheet2\->set_row(9, undef, undef, 1, 2); \& $worksheet2\->set_row(10, undef, undef, 1, 1); \& $worksheet2\->set_row(11, undef, undef, 0, 0, 1); \& \& \& # Add a column format for clarity \& $worksheet2\->set_column(\*(AqA:A\*(Aq, 20); \& \& # Add the data, labels and formulas \& $worksheet2\->write(\*(AqA1\*(Aq, \*(AqRegion\*(Aq, $bold); \& $worksheet2\->write(\*(AqA2\*(Aq, \*(AqNorth\*(Aq); \& $worksheet2\->write(\*(AqA3\*(Aq, \*(AqNorth\*(Aq); \& $worksheet2\->write(\*(AqA4\*(Aq, \*(AqNorth\*(Aq); \& $worksheet2\->write(\*(AqA5\*(Aq, \*(AqNorth\*(Aq); \& $worksheet2\->write(\*(AqA6\*(Aq, \*(AqNorth Total\*(Aq, $bold); \& \& $worksheet2\->write(\*(AqB1\*(Aq, \*(AqSales\*(Aq, $bold); \& $worksheet2\->write(\*(AqB2\*(Aq, 1000); \& $worksheet2\->write(\*(AqB3\*(Aq, 1200); \& $worksheet2\->write(\*(AqB4\*(Aq, 900); \& $worksheet2\->write(\*(AqB5\*(Aq, 1200); \& $worksheet2\->write(\*(AqB6\*(Aq, \*(Aq=SUBTOTAL(9,B2:B5)\*(Aq, $bold); \& \& $worksheet2\->write(\*(AqA7\*(Aq, \*(AqSouth\*(Aq); \& $worksheet2\->write(\*(AqA8\*(Aq, \*(AqSouth\*(Aq); \& $worksheet2\->write(\*(AqA9\*(Aq, \*(AqSouth\*(Aq); \& $worksheet2\->write(\*(AqA10\*(Aq, \*(AqSouth\*(Aq); \& $worksheet2\->write(\*(AqA11\*(Aq, \*(AqSouth Total\*(Aq, $bold); \& \& $worksheet2\->write(\*(AqB7\*(Aq, 400); \& $worksheet2\->write(\*(AqB8\*(Aq, 600); \& $worksheet2\->write(\*(AqB9\*(Aq, 500); \& $worksheet2\->write(\*(AqB10\*(Aq, 600); \& $worksheet2\->write(\*(AqB11\*(Aq, \*(Aq=SUBTOTAL(9,B7:B10)\*(Aq, $bold); \& \& $worksheet2\->write(\*(AqA12\*(Aq, \*(AqGrand Total\*(Aq, $bold); \& $worksheet2\->write(\*(AqB12\*(Aq, \*(Aq=SUBTOTAL(9,B2:B10)\*(Aq, $bold); \& \& \& \& ############################################################################### \& # \& # Example 3: Create a worksheet with outlined columns. \& # \& my $data = [ \& [\*(AqMonth\*(Aq, \*(AqJan\*(Aq, \*(AqFeb\*(Aq, \*(AqMar\*(Aq, \*(AqApr\*(Aq, \*(AqMay\*(Aq, \*(AqJun\*(Aq,\*(Aq Total\*(Aq], \& [\*(AqNorth\*(Aq, 50, 20, 15, 25, 65, 80, ,\*(Aq=SUM(B2:G2)\*(Aq], \& [\*(AqSouth\*(Aq, 10, 20, 30, 50, 50, 50, ,\*(Aq=SUM(B3:G3)\*(Aq], \& [\*(AqEast\*(Aq, 45, 75, 50, 15, 75, 100, ,\*(Aq=SUM(B4:G4)\*(Aq], \& [\*(AqWest\*(Aq, 15, 15, 55, 35, 20, 50, ,\*(Aq=SUM(B5:G6)\*(Aq], \& ]; \& \& # Add bold format to the first row \& $worksheet3\->set_row(0, undef, $bold); \& \& # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) \& $worksheet3\->set_column(\*(AqA:A\*(Aq, 10, $bold ); \& $worksheet3\->set_column(\*(AqB:G\*(Aq, 5, undef, 0, 1); \& $worksheet3\->set_column(\*(AqH:H\*(Aq, 10); \& \& # Write the data and a formula \& $worksheet3\->write_col(\*(AqA1\*(Aq, $data); \& $worksheet3\->write(\*(AqH6\*(Aq, \*(Aq=SUM(H2:H5)\*(Aq, $bold); \& \& \& \& ############################################################################### \& # \& # Example 4: Show all possible outline levels. \& # \& my $levels = ["Level 1", "Level 2", "Level 3", "Level 4", \& "Level 5", "Level 6", "Level 7", "Level 6", \& "Level 5", "Level 4", "Level 3", "Level 2", "Level 1"]; \& \& \& $worksheet4\->write_col(\*(AqA1\*(Aq, $levels); \& \& $worksheet4\->set_row(0, undef, undef, undef, 1); \& $worksheet4\->set_row(1, undef, undef, undef, 2); \& $worksheet4\->set_row(2, undef, undef, undef, 3); \& $worksheet4\->set_row(3, undef, undef, undef, 4); \& $worksheet4\->set_row(4, undef, undef, undef, 5); \& $worksheet4\->set_row(5, undef, undef, undef, 6); \& $worksheet4\->set_row(6, undef, undef, undef, 7); \& $worksheet4\->set_row(7, undef, undef, undef, 6); \& $worksheet4\->set_row(8, undef, undef, undef, 5); \& $worksheet4\->set_row(9, undef, undef, undef, 4); \& $worksheet4\->set_row(10, undef, undef, undef, 3); \& $worksheet4\->set_row(11, undef, undef, undef, 2); \& $worksheet4\->set_row(12, undef, undef, undef, 1); \& \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: outline_collapsed.pl" .IX Subsection "Example: outline_collapsed.pl" Example of how use Spreadsheet::WriteExcel to generate Excel outlines and grouping. .PP These example focus mainly on collapsed outlines. See also the outlines.pl example program for more general examples. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how use Spreadsheet::WriteExcel to generate Excel outlines and \& # grouping. \& # \& # These example focus mainly on collapsed outlines. See also the \& # outlines.pl example program for more general examples. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2008, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add some worksheets \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqoutline_collapsed.xls\*(Aq); \& my $worksheet1 = $workbook\->add_worksheet(\*(AqOutlined Rows\*(Aq); \& my $worksheet2 = $workbook\->add_worksheet(\*(AqCollapsed Rows 1\*(Aq); \& my $worksheet3 = $workbook\->add_worksheet(\*(AqCollapsed Rows 2\*(Aq); \& my $worksheet4 = $workbook\->add_worksheet(\*(AqCollapsed Rows 3\*(Aq); \& my $worksheet5 = $workbook\->add_worksheet(\*(AqOutline Columns\*(Aq); \& my $worksheet6 = $workbook\->add_worksheet(\*(AqCollapsed Columns\*(Aq); \& \& \& # Add a general format \& my $bold = $workbook\->add_format(bold => 1); \& \& \& # \& # This function will generate the same data and sub\-totals on each worksheet. \& # \& sub create_sub_totals { \& \& my $worksheet = $_[0]; \& \& # Add a column format for clarity \& $worksheet\->set_column(\*(AqA:A\*(Aq, 20); \& \& # Add the data, labels and formulas \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqRegion\*(Aq, $bold); \& $worksheet\->write(\*(AqA2\*(Aq, \*(AqNorth\*(Aq); \& $worksheet\->write(\*(AqA3\*(Aq, \*(AqNorth\*(Aq); \& $worksheet\->write(\*(AqA4\*(Aq, \*(AqNorth\*(Aq); \& $worksheet\->write(\*(AqA5\*(Aq, \*(AqNorth\*(Aq); \& $worksheet\->write(\*(AqA6\*(Aq, \*(AqNorth Total\*(Aq, $bold); \& \& $worksheet\->write(\*(AqB1\*(Aq, \*(AqSales\*(Aq, $bold); \& $worksheet\->write(\*(AqB2\*(Aq, 1000); \& $worksheet\->write(\*(AqB3\*(Aq, 1200); \& $worksheet\->write(\*(AqB4\*(Aq, 900); \& $worksheet\->write(\*(AqB5\*(Aq, 1200); \& $worksheet\->write(\*(AqB6\*(Aq, \*(Aq=SUBTOTAL(9,B2:B5)\*(Aq, $bold); \& \& $worksheet\->write(\*(AqA7\*(Aq, \*(AqSouth\*(Aq); \& $worksheet\->write(\*(AqA8\*(Aq, \*(AqSouth\*(Aq); \& $worksheet\->write(\*(AqA9\*(Aq, \*(AqSouth\*(Aq); \& $worksheet\->write(\*(AqA10\*(Aq, \*(AqSouth\*(Aq); \& $worksheet\->write(\*(AqA11\*(Aq, \*(AqSouth Total\*(Aq, $bold); \& \& $worksheet\->write(\*(AqB7\*(Aq, 400); \& $worksheet\->write(\*(AqB8\*(Aq, 600); \& $worksheet\->write(\*(AqB9\*(Aq, 500); \& $worksheet\->write(\*(AqB10\*(Aq, 600); \& $worksheet\->write(\*(AqB11\*(Aq, \*(Aq=SUBTOTAL(9,B7:B10)\*(Aq, $bold); \& \& $worksheet\->write(\*(AqA12\*(Aq, \*(AqGrand Total\*(Aq, $bold); \& $worksheet\->write(\*(AqB12\*(Aq, \*(Aq=SUBTOTAL(9,B2:B10)\*(Aq, $bold); \& \& } \& \& \& ############################################################################### \& # \& # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL() \& # functions so that it looks like the type of automatic outlines that are \& # generated when you use the Excel Data\->SubTotals menu item. \& # \& \& # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) \& $worksheet1\->set_row(1, undef, undef, 0, 2); \& $worksheet1\->set_row(2, undef, undef, 0, 2); \& $worksheet1\->set_row(3, undef, undef, 0, 2); \& $worksheet1\->set_row(4, undef, undef, 0, 2); \& $worksheet1\->set_row(5, undef, undef, 0, 1); \& \& $worksheet1\->set_row(6, undef, undef, 0, 2); \& $worksheet1\->set_row(7, undef, undef, 0, 2); \& $worksheet1\->set_row(8, undef, undef, 0, 2); \& $worksheet1\->set_row(9, undef, undef, 0, 2); \& $worksheet1\->set_row(10, undef, undef, 0, 1); \& \& # Write the sub\-total data that is common to the row examples. \& create_sub_totals($worksheet1); \& \& \& ############################################################################### \& # \& # Example 2: Create a worksheet with collapsed outlined rows. \& # This is the same as the example 1 except that the all rows are collapsed. \& # Note: We need to indicate the row that contains the collapsed symbol \*(Aq+\*(Aq with \& # the optional parameter, $collapsed. \& \& $worksheet2\->set_row(1, undef, undef, 1, 2); \& $worksheet2\->set_row(2, undef, undef, 1, 2); \& $worksheet2\->set_row(3, undef, undef, 1, 2); \& $worksheet2\->set_row(4, undef, undef, 1, 2); \& $worksheet2\->set_row(5, undef, undef, 1, 1); \& \& $worksheet2\->set_row(6, undef, undef, 1, 2); \& $worksheet2\->set_row(7, undef, undef, 1, 2); \& $worksheet2\->set_row(8, undef, undef, 1, 2); \& $worksheet2\->set_row(9, undef, undef, 1, 2); \& $worksheet2\->set_row(10, undef, undef, 1, 1); \& \& $worksheet2\->set_row(11, undef, undef, 0, 0, 1); \& \& # Write the sub\-total data that is common to the row examples. \& create_sub_totals($worksheet2); \& \& \& ############################################################################### \& # \& # Example 3: Create a worksheet with collapsed outlined rows. \& # Same as the example 1 except that the two sub\-totals are collapsed. \& \& $worksheet3\->set_row(1, undef, undef, 1, 2); \& $worksheet3\->set_row(2, undef, undef, 1, 2); \& $worksheet3\->set_row(3, undef, undef, 1, 2); \& $worksheet3\->set_row(4, undef, undef, 1, 2); \& $worksheet3\->set_row(5, undef, undef, 0, 1, 1); \& \& $worksheet3\->set_row(6, undef, undef, 1, 2); \& $worksheet3\->set_row(7, undef, undef, 1, 2); \& $worksheet3\->set_row(8, undef, undef, 1, 2); \& $worksheet3\->set_row(9, undef, undef, 1, 2); \& $worksheet3\->set_row(10, undef, undef, 0, 1, 1); \& \& \& # Write the sub\-total data that is common to the row examples. \& create_sub_totals($worksheet3); \& \& \& ############################################################################### \& # \& # Example 4: Create a worksheet with outlined rows. \& # Same as the example 1 except that the two sub\-totals are collapsed. \& \& $worksheet4\->set_row(1, undef, undef, 1, 2); \& $worksheet4\->set_row(2, undef, undef, 1, 2); \& $worksheet4\->set_row(3, undef, undef, 1, 2); \& $worksheet4\->set_row(4, undef, undef, 1, 2); \& $worksheet4\->set_row(5, undef, undef, 1, 1, 1); \& \& $worksheet4\->set_row(6, undef, undef, 1, 2); \& $worksheet4\->set_row(7, undef, undef, 1, 2); \& $worksheet4\->set_row(8, undef, undef, 1, 2); \& $worksheet4\->set_row(9, undef, undef, 1, 2); \& $worksheet4\->set_row(10, undef, undef, 1, 1, 1); \& \& $worksheet4\->set_row(11, undef, undef, 0, 0, 1); \& \& # Write the sub\-total data that is common to the row examples. \& create_sub_totals($worksheet4); \& \& \& \& ############################################################################### \& # \& # Example 5: Create a worksheet with outlined columns. \& # \& my $data = [ \& [\*(AqMonth\*(Aq, \*(AqJan\*(Aq, \*(AqFeb\*(Aq, \*(AqMar\*(Aq, \*(AqApr\*(Aq, \*(AqMay\*(Aq, \*(AqJun\*(Aq,\*(Aq Total\*(Aq], \& [\*(AqNorth\*(Aq, 50, 20, 15, 25, 65, 80, ,\*(Aq=SUM(B2:G2)\*(Aq], \& [\*(AqSouth\*(Aq, 10, 20, 30, 50, 50, 50, ,\*(Aq=SUM(B3:G3)\*(Aq], \& [\*(AqEast\*(Aq, 45, 75, 50, 15, 75, 100, ,\*(Aq=SUM(B4:G4)\*(Aq], \& [\*(AqWest\*(Aq, 15, 15, 55, 35, 20, 50, ,\*(Aq=SUM(B5:G6)\*(Aq], \& ]; \& \& # Add bold format to the first row \& $worksheet5\->set_row(0, undef, $bold); \& \& # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) \& $worksheet5\->set_column(\*(AqA:A\*(Aq, 10, $bold ); \& $worksheet5\->set_column(\*(AqB:G\*(Aq, 5, undef, 0, 1); \& $worksheet5\->set_column(\*(AqH:H\*(Aq, 10 ); \& \& # Write the data and a formula \& $worksheet5\->write_col(\*(AqA1\*(Aq, $data); \& $worksheet5\->write(\*(AqH6\*(Aq, \*(Aq=SUM(H2:H5)\*(Aq, $bold); \& \& \& ############################################################################### \& # \& # Example 6: Create a worksheet with collapsed outlined columns. \& # This is the same as the previous example except collapsed columns. \& \& # Add bold format to the first row \& $worksheet6\->set_row(0, undef, $bold); \& \& # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) \& $worksheet6\->set_column(\*(AqA:A\*(Aq, 10, $bold ); \& $worksheet6\->set_column(\*(AqB:G\*(Aq, 5, undef, 1, 1 ); \& $worksheet6\->set_column(\*(AqH:H\*(Aq, 10, undef, 0, 0, 1); \& \& # Write the data and a formula \& $worksheet6\->write_col(\*(AqA1\*(Aq, $data); \& $worksheet6\->write(\*(AqH6\*(Aq, \*(Aq=SUM(H2:H5)\*(Aq, $bold); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: panes.pl" .IX Subsection "Example: panes.pl" Example of using the WriteExcel module to create worksheet panes. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # Example of using the WriteExcel module to create worksheet panes. \& # \& # reverse(\*(Aq(c)\*(Aq), May 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("panes.xls"); \& \& my $worksheet1 = $workbook\->add_worksheet(\*(AqPanes 1\*(Aq); \& my $worksheet2 = $workbook\->add_worksheet(\*(AqPanes 2\*(Aq); \& my $worksheet3 = $workbook\->add_worksheet(\*(AqPanes 3\*(Aq); \& my $worksheet4 = $workbook\->add_worksheet(\*(AqPanes 4\*(Aq); \& \& # Freeze panes \& $worksheet1\->freeze_panes(1, 0); # 1 row \& \& $worksheet2\->freeze_panes(0, 1); # 1 column \& $worksheet3\->freeze_panes(1, 1); # 1 row and column \& \& # Split panes. \& # The divisions must be specified in terms of row and column dimensions. \& # The default row height is 12.75 and the default column width is 8.43 \& # \& $worksheet4\->split_panes(12.75, 8.43, 1, 1); # 1 row and column \& \& \& ####################################################################### \& # \& # Set up some formatting and text to highlight the panes \& # \& \& my $header = $workbook\->add_format(); \& $header\->set_color(\*(Aqwhite\*(Aq); \& $header\->set_align(\*(Aqcenter\*(Aq); \& $header\->set_align(\*(Aqvcenter\*(Aq); \& $header\->set_pattern(); \& $header\->set_fg_color(\*(Aqgreen\*(Aq); \& \& my $center = $workbook\->add_format(); \& $center\->set_align(\*(Aqcenter\*(Aq); \& \& \& ####################################################################### \& # \& # Sheet 1 \& # \& \& $worksheet1\->set_column(\*(AqA:I\*(Aq, 16); \& $worksheet1\->set_row(0, 20); \& $worksheet1\->set_selection(\*(AqC3\*(Aq); \& \& for my $i (0..8){ \& $worksheet1\->write(0, $i, \*(AqScroll down\*(Aq, $header); \& } \& \& for my $i (1..100){ \& for my $j (0..8){ \& $worksheet1\->write($i, $j, $i+1, $center); \& } \& } \& \& \& ####################################################################### \& # \& # Sheet 2 \& # \& \& $worksheet2\->set_column(\*(AqA:A\*(Aq, 16); \& $worksheet2\->set_selection(\*(AqC3\*(Aq); \& \& for my $i (0..49){ \& $worksheet2\->set_row($i, 15); \& $worksheet2\->write($i, 0, \*(AqScroll right\*(Aq, $header); \& } \& \& for my $i (0..49){ \& for my $j (1..25){ \& $worksheet2\->write($i, $j, $j, $center); \& } \& } \& \& \& ####################################################################### \& # \& # Sheet 3 \& # \& \& $worksheet3\->set_column(\*(AqA:Z\*(Aq, 16); \& $worksheet3\->set_selection(\*(AqC3\*(Aq); \& \& for my $i (1..25){ \& $worksheet3\->write(0, $i, \*(AqScroll down\*(Aq, $header); \& } \& \& for my $i (1..49){ \& $worksheet3\->write($i, 0, \*(AqScroll right\*(Aq, $header); \& } \& \& for my $i (1..49){ \& for my $j (1..25){ \& $worksheet3\->write($i, $j, $j, $center); \& } \& } \& \& \& ####################################################################### \& # \& # Sheet 4 \& # \& \& $worksheet4\->set_selection(\*(AqC3\*(Aq); \& \& for my $i (1..25){ \& $worksheet4\->write(0, $i, \*(AqScroll\*(Aq, $center); \& } \& \& for my $i (1..49){ \& $worksheet4\->write($i, 0, \*(AqScroll\*(Aq, $center); \& } \& \& for my $i (1..49){ \& for my $j (1..25){ \& $worksheet4\->write($i, $j, $j, $center); \& } \& } .Ve .PP Download this example: .SS "Example: properties.pl" .IX Subsection "Example: properties.pl" An example of adding document properties to a Spreadsheet::WriteExcel file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # An example of adding document properties to a Spreadsheet::WriteExcel file. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2008, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqproperties.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& $workbook\->set_properties( \& title => \*(AqThis is an example spreadsheet\*(Aq, \& subject => \*(AqWith document properties\*(Aq, \& author => \*(AqJohn McNamara\*(Aq, \& manager => \*(AqDr. Heinz Doofenshmirtz \*(Aq, \& company => \*(Aqof Wolves\*(Aq, \& category => \*(AqExample spreadsheets\*(Aq, \& keywords => \*(AqSample, Example, Properties\*(Aq, \& comments => \*(AqCreated with Perl and Spreadsheet::WriteExcel\*(Aq, \& ); \& \& \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqSelect File\->Properties to see the file properties\*(Aq); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: protection.pl" .IX Subsection "Example: protection.pl" Example of cell locking and formula hiding in an Excel worksheet via the Spreadsheet::WriteExcel module. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ######################################################################## \& # \& # Example of cell locking and formula hiding in an Excel worksheet via \& # the Spreadsheet::WriteExcel module. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("protection.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Create some format objects \& my $locked = $workbook\->add_format(locked => 1); \& my $unlocked = $workbook\->add_format(locked => 0); \& my $hidden = $workbook\->add_format(hidden => 1); \& \& # Format the columns \& $worksheet\->set_column(\*(AqA:A\*(Aq, 42); \& $worksheet\->set_selection(\*(AqB3:B3\*(Aq); \& \& # Protect the worksheet \& $worksheet\->protect(); \& \& # Examples of cell locking and hiding \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqCell B1 is locked. It cannot be edited.\*(Aq); \& $worksheet\->write(\*(AqB1\*(Aq, \*(Aq=1+2\*(Aq, $locked); \& \& $worksheet\->write(\*(AqA2\*(Aq, \*(AqCell B2 is unlocked. It can be edited.\*(Aq); \& $worksheet\->write(\*(AqB2\*(Aq, \*(Aq=1+2\*(Aq, $unlocked); \& \& $worksheet\->write(\*(AqA3\*(Aq, "Cell B3 is hidden. The formula isn\*(Aqt visible."); \& $worksheet\->write(\*(AqB3\*(Aq, \*(Aq=1+2\*(Aq, $hidden); \& \& $worksheet\->write(\*(AqA5\*(Aq, \*(AqUse Menu\->Tools\->Protection\->Unprotect Sheet\*(Aq); \& $worksheet\->write(\*(AqA6\*(Aq, \*(Aqto remove the worksheet protection. \*(Aq); .Ve .PP Download this example: .SS "Example: repeat.pl" .IX Subsection "Example: repeat.pl" Example of writing repeated formulas. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ###################################################################### \& # \& # Example of writing repeated formulas. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2002, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("repeat.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& my $limit = 1000; \& \& # Write a column of numbers \& for my $row (0..$limit) { \& $worksheet\->write($row, 0, $row); \& } \& \& \& # Store a formula \& my $formula = $worksheet\->store_formula(\*(Aq=A1*5+4\*(Aq); \& \& \& # Write a column of formulas based on the stored formula \& for my $row (0..$limit) { \& $worksheet\->repeat_formula($row, 1, $formula, undef, \& qr/^A1$/, \*(AqA\*(Aq.($row+1)); \& } \& \& \& # Direct formula writing. As a speed comparison uncomment the \& # following and run the program again \& \& #for my $row (0..$limit) { \& # $worksheet\->write_formula($row, 2, \*(Aq=A\*(Aq.($row+1).\*(Aq*5+4\*(Aq); \& #} \& \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: right_to_left.pl" .IX Subsection "Example: right_to_left.pl" Example of how to change the default worksheet direction from left-to-right to right-to-left as required by some eastern verions of Excel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # Example of how to change the default worksheet direction from \& # left\-to\-right to right\-to\-left as required by some eastern verions \& # of Excel. \& # \& # reverse(\*(Aq(c)\*(Aq), January 2006, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new("right_to_left.xls"); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& \& $worksheet2\->right_to_left(); \& \& $worksheet1\->write(0, 0, \*(AqHello\*(Aq); # A1, B1, C1, ... \& $worksheet2\->write(0, 0, \*(AqHello\*(Aq); # ..., C1, B1, A1 .Ve .PP Download this example: .SS "Example: row_wrap.pl" .IX Subsection "Example: row_wrap.pl" Demonstrates how to wrap data from one worksheet onto another. .PP Excel has a row limit of 65536 rows. Sometimes the amount of row data to be written to a file is greater than this limit. In this case it is a useful technique to wrap the data from one worksheet onto the next so that we get something like the following: .PP .Vb 3 \& Sheet1 Row 1 \- 65536 \& Sheet2 Row 65537 \- 131072 \& Sheet3 Row 131073 \- ... .Ve .PP In order to achieve this we use a single worksheet reference and reinitialise it to point to a new worksheet when required. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # Demonstrates how to wrap data from one worksheet onto another. \& # \& # Excel has a row limit of 65536 rows. Sometimes the amount of row data to be \& # written to a file is greater than this limit. In this case it is a useful \& # technique to wrap the data from one worksheet onto the next so that we get \& # something like the following: \& # \& # Sheet1 Row 1 \- 65536 \& # Sheet2 Row 65537 \- 131072 \& # Sheet3 Row 131073 \- ... \& # \& # In order to achieve this we use a single worksheet reference and \& # reinitialise it to point to a new worksheet when required. \& # \& # reverse(\*(Aq(c)\*(Aq), May 2006, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqrow_wrap.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Worksheet formatting. \& $worksheet\->set_column(\*(AqA:A\*(Aq, 20); \& \& \& # For the sake of this example we will use a small row limit. In order to use \& # the entire row range set the $row_limit to 65536. \& my $row_limit = 10; \& my $row = 0; \& \& for my $count (1 .. 2 * $row_limit +10) { \& \& # When we hit the row limit we redirect the output \& # to a new worksheet and reset the row number. \& if ($row == $row_limit) { \& $worksheet = $workbook\->add_worksheet(); \& $row = 0; \& \& # Repeat any worksheet formatting. \& $worksheet\->set_column(\*(AqA:A\*(Aq, 20); \& } \& \& $worksheet\->write($row, 0, "This is row $count"); \& $row++; \& } .Ve .PP Download this example: .SS "Example: sales.pl" .IX Subsection "Example: sales.pl" Example of a sales worksheet to demonstrate several different features. Also uses functions from the Spreadsheet::WriteExcel::Utility module. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of a sales worksheet to demonstrate several different features. \& # Also uses functions from the L module. \& # \& # reverse(\*(Aq(c)\*(Aq), October 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& use Spreadsheet::WriteExcel::Utility; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("sales.xls"); \& my $worksheet = $workbook\->add_worksheet(\*(AqMay Sales\*(Aq); \& \& \& # Set up some formats \& my %heading = ( \& bold => 1, \& pattern => 1, \& fg_color => 19, \& border => 1, \& align => \*(Aqcenter\*(Aq, \& ); \& \& my %total = ( \& bold => 1, \& top => 1, \& num_format => \*(Aq$#,##0.00\*(Aq \& ); \& \& my $heading = $workbook\->add_format(%heading); \& my $total_format = $workbook\->add_format(%total); \& my $price_format = $workbook\->add_format(num_format => \*(Aq$#,##0.00\*(Aq); \& my $date_format = $workbook\->add_format(num_format => \*(Aqmmm d yyy\*(Aq); \& \& \& # Write the main headings \& $worksheet\->freeze_panes(1); # Freeze the first row \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqItem\*(Aq, $heading); \& $worksheet\->write(\*(AqB1\*(Aq, \*(AqQuantity\*(Aq, $heading); \& $worksheet\->write(\*(AqC1\*(Aq, \*(AqPrice\*(Aq, $heading); \& $worksheet\->write(\*(AqD1\*(Aq, \*(AqTotal\*(Aq, $heading); \& $worksheet\->write(\*(AqE1\*(Aq, \*(AqDate\*(Aq, $heading); \& \& # Set the column widths \& $worksheet\->set_column(\*(AqA:A\*(Aq, 25); \& $worksheet\->set_column(\*(AqB:B\*(Aq, 10); \& $worksheet\->set_column(\*(AqC:E\*(Aq, 16); \& \& \& # Extract the sales data from the _\|_DATA_\|_ section at the end of the file. \& # In reality this information would probably come from a database \& my @sales; \& \& foreach my $line () { \& chomp $line; \& next if $line eq \*(Aq\*(Aq; \& # Simple\-minded processing of CSV data. Refer to the Text::CSV_XS \& # and Text::xSV modules for a more complete CSV handling. \& my @items = split /,/, $line; \& push @sales, \e@items; \& } \& \& \& # Write out the items from each row \& my $row = 1; \& foreach my $sale (@sales) { \& \& $worksheet\->write($row, 0, @$sale[0]); \& $worksheet\->write($row, 1, @$sale[1]); \& $worksheet\->write($row, 2, @$sale[2], $price_format); \& \& # Create a formula like \*(Aq=B2*C2\*(Aq \& my $formula = \*(Aq=\*(Aq \& . xl_rowcol_to_cell($row, 1) \& . "*" \& . xl_rowcol_to_cell($row, 2); \& \& $worksheet\->write($row, 3, $formula, $price_format); \& \& # Parse the date \& my $date = xl_decode_date_US(@$sale[3]); \& $worksheet\->write($row, 4, $date, $date_format); \& $row++; \& } \& \& # Create a formula to sum the totals, like \*(Aq=SUM(D2:D6)\*(Aq \& my $total = \*(Aq=SUM(D2:\*(Aq \& . xl_rowcol_to_cell($row\-1, 3) \& . ")"; \& \& $worksheet\->write($row, 3, $total, $total_format); \& \& \& \& _\|_DATA_\|_ \& 586 card,20,125.50,5/12/01 \& Flat Screen Monitor,1,1300.00,5/12/01 \& 64 MB dimms,45,49.99,5/13/01 \& 15 GB HD,12,300.00,5/13/01 \& Speakers (pair),5,15.50,5/14/01 .Ve .PP Download this example: .SS "Example: sendmail.pl" .IX Subsection "Example: sendmail.pl" Example of how to use Mail::Sender to send a Spreadsheet::WriteExcel Excel file as an attachment. .PP The main thing is to ensure that you \fBclose()\fR the Worbook before you send it. .PP See the Mail::Sender module for further details. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use Mail::Sender to send a Spreadsheet::WriteExcel Excel \& # file as an attachment. \& # \& # The main thing is to ensure that you close() the Worbook before you send it. \& # \& # See the L module for further details. \& # \& # reverse(\*(Aq(c)\*(Aq), August 2002, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& use Mail::Sender; \& \& # Create an Excel file \& my $workbook = Spreadsheet::WriteExcel\->new("sendmail.xls"); \& my $worksheet = $workbook\->add_worksheet; \& \& $worksheet\->write(\*(AqA1\*(Aq, "Hello World!"); \& \& $workbook\->close(); # Must close before sending \& \& \& \& # Send the file. Change all variables to suit \& my $sender = new Mail::Sender \& { \& smtp => \*(Aq123.123.123.123\*(Aq, \& from => \*(AqSomeone\*(Aq \& }; \& \& $sender\->MailFile( \& { \& to => \*(Aqanother@mail.com\*(Aq, \& subject => \*(AqExcel file\*(Aq, \& msg => "Here is the data.\en", \& file => \*(Aqmail.xls\*(Aq, \& }); .Ve .PP Download this example: .SS "Example: stats_ext.pl" .IX Subsection "Example: stats_ext.pl" Example of formatting using the Spreadsheet::WriteExcel module .PP This is a simple example of how to use functions that reference cells in other worksheets within the same workbook. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of formatting using the Spreadsheet::WriteExcel module \& # \& # This is a simple example of how to use functions that reference cells in \& # other worksheets within the same workbook. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("stats_ext.xls"); \& my $worksheet1 = $workbook\->add_worksheet(\*(AqTest results\*(Aq); \& my $worksheet2 = $workbook\->add_worksheet(\*(AqData\*(Aq); \& \& # Set the column width for columns 1 \& $worksheet1\->set_column(\*(AqA:A\*(Aq, 20); \& \& \& # Create a format for the headings \& my $heading = $workbook\->add_format(); \& $heading\->set_bold(); \& \& # Create a numerical format \& my $numformat = $workbook\->add_format(); \& $numformat\->set_num_format(\*(Aq0.00\*(Aq); \& \& \& \& \& # Write some statistical functions \& $worksheet1\->write(\*(AqA1\*(Aq, \*(AqCount\*(Aq, $heading); \& $worksheet1\->write(\*(AqB1\*(Aq, \*(Aq=COUNT(Data!B2:B9)\*(Aq); \& \& $worksheet1\->write(\*(AqA2\*(Aq, \*(AqSum\*(Aq, $heading); \& $worksheet1\->write(\*(AqB2\*(Aq, \*(Aq=SUM(Data!B2:B9)\*(Aq); \& \& $worksheet1\->write(\*(AqA3\*(Aq, \*(AqAverage\*(Aq, $heading); \& $worksheet1\->write(\*(AqB3\*(Aq, \*(Aq=AVERAGE(Data!B2:B9)\*(Aq); \& \& $worksheet1\->write(\*(AqA4\*(Aq, \*(AqMin\*(Aq, $heading); \& $worksheet1\->write(\*(AqB4\*(Aq, \*(Aq=MIN(Data!B2:B9)\*(Aq); \& \& $worksheet1\->write(\*(AqA5\*(Aq, \*(AqMax\*(Aq, $heading); \& $worksheet1\->write(\*(AqB5\*(Aq, \*(Aq=MAX(Data!B2:B9)\*(Aq); \& \& $worksheet1\->write(\*(AqA6\*(Aq, \*(AqStandard Deviation\*(Aq, $heading); \& $worksheet1\->write(\*(AqB6\*(Aq, \*(Aq=STDEV(Data!B2:B9)\*(Aq); \& \& $worksheet1\->write(\*(AqA7\*(Aq, \*(AqKurtosis\*(Aq, $heading); \& $worksheet1\->write(\*(AqB7\*(Aq, \*(Aq=KURT(Data!B2:B9)\*(Aq); \& \& \& # Write the sample data \& $worksheet2\->write(\*(AqA1\*(Aq, \*(AqSample\*(Aq, $heading); \& $worksheet2\->write(\*(AqA2\*(Aq, 1); \& $worksheet2\->write(\*(AqA3\*(Aq, 2); \& $worksheet2\->write(\*(AqA4\*(Aq, 3); \& $worksheet2\->write(\*(AqA5\*(Aq, 4); \& $worksheet2\->write(\*(AqA6\*(Aq, 5); \& $worksheet2\->write(\*(AqA7\*(Aq, 6); \& $worksheet2\->write(\*(AqA8\*(Aq, 7); \& $worksheet2\->write(\*(AqA9\*(Aq, 8); \& \& $worksheet2\->write(\*(AqB1\*(Aq, \*(AqLength\*(Aq, $heading); \& $worksheet2\->write(\*(AqB2\*(Aq, 25.4, $numformat); \& $worksheet2\->write(\*(AqB3\*(Aq, 25.4, $numformat); \& $worksheet2\->write(\*(AqB4\*(Aq, 24.8, $numformat); \& $worksheet2\->write(\*(AqB5\*(Aq, 25.0, $numformat); \& $worksheet2\->write(\*(AqB6\*(Aq, 25.3, $numformat); \& $worksheet2\->write(\*(AqB7\*(Aq, 24.9, $numformat); \& $worksheet2\->write(\*(AqB8\*(Aq, 25.2, $numformat); \& $worksheet2\->write(\*(AqB9\*(Aq, 24.8, $numformat); .Ve .PP Download this example: .SS "Example: stocks.pl" .IX Subsection "Example: stocks.pl" Example of formatting using the Spreadsheet::WriteExcel module .PP This example shows how to use a conditional numerical format with colours to indicate if a share price has gone up or down. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of formatting using the Spreadsheet::WriteExcel module \& # \& # This example shows how to use a conditional numerical format \& # with colours to indicate if a share price has gone up or down. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("stocks.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Set the column width for columns 1, 2, 3 and 4 \& $worksheet\->set_column(0, 3, 15); \& \& \& # Create a format for the column headings \& my $header = $workbook\->add_format(); \& $header\->set_bold(); \& $header\->set_size(12); \& $header\->set_color(\*(Aqblue\*(Aq); \& \& \& # Create a format for the stock price \& my $f_price = $workbook\->add_format(); \& $f_price\->set_align(\*(Aqleft\*(Aq); \& $f_price\->set_num_format(\*(Aq$0.00\*(Aq); \& \& \& # Create a format for the stock volume \& my $f_volume = $workbook\->add_format(); \& $f_volume\->set_align(\*(Aqleft\*(Aq); \& $f_volume\->set_num_format(\*(Aq#,##0\*(Aq); \& \& \& # Create a format for the price change. This is an example of a conditional \& # format. The number is formatted as a percentage. If it is positive it is \& # formatted in green, if it is negative it is formatted in red and if it is \& # zero it is formatted as the default font colour (in this case black). \& # Note: the [Green] format produces an unappealing lime green. Try \& # [Color 10] instead for a dark green. \& # \& my $f_change = $workbook\->add_format(); \& $f_change\->set_align(\*(Aqleft\*(Aq); \& $f_change\->set_num_format(\*(Aq[Green]0.0%;[Red]\-0.0%;0.0%\*(Aq); \& \& \& # Write out the data \& $worksheet\->write(0, 0, \*(AqCompany\*(Aq, $header); \& $worksheet\->write(0, 1, \*(AqPrice\*(Aq, $header); \& $worksheet\->write(0, 2, \*(AqVolume\*(Aq, $header); \& $worksheet\->write(0, 3, \*(AqChange\*(Aq, $header); \& \& $worksheet\->write(1, 0, \*(AqDamage Inc.\*(Aq ); \& $worksheet\->write(1, 1, 30.25, $f_price); # $30.25 \& $worksheet\->write(1, 2, 1234567, $f_volume); # 1,234,567 \& $worksheet\->write(1, 3, 0.085, $f_change); # 8.5% in green \& \& $worksheet\->write(2, 0, \*(AqDump Corp.\*(Aq ); \& $worksheet\->write(2, 1, 1.56, $f_price); # $1.56 \& $worksheet\->write(2, 2, 7564, $f_volume); # 7,564 \& $worksheet\->write(2, 3, \-0.015, $f_change); # \-1.5% in red \& \& $worksheet\->write(3, 0, \*(AqRev Ltd.\*(Aq ); \& $worksheet\->write(3, 1, 0.13, $f_price); # $0.13 \& $worksheet\->write(3, 2, 321, $f_volume); # 321 \& $worksheet\->write(3, 3, 0, $f_change); # 0 in the font color (black) .Ve .PP Download this example: .SS "Example: tab_colors.pl" .IX Subsection "Example: tab_colors.pl" Example of how to set Excel worksheet tab colours. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # Example of how to set Excel worksheet tab colours. \& # \& # reverse(\*(Aq(c)\*(Aq), May 2006, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqtab_colors.xls\*(Aq); \& \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& my $worksheet4 = $workbook\->add_worksheet(); \& \& # Worksheet1 will have the default tab colour. \& $worksheet2\->set_tab_color(\*(Aqred\*(Aq); \& $worksheet3\->set_tab_color(\*(Aqgreen\*(Aq); \& $worksheet4\->set_tab_color(0x35); # Orange .Ve .PP Download this example: .SS "Example: textwrap.pl" .IX Subsection "Example: textwrap.pl" Example of formatting using the Spreadsheet::WriteExcel module .PP This example shows how to wrap text in a cell. There are two alternatives, vertical justification and text wrap. .PP With vertical justification the text is wrapped automatically to fit the column width. With text wrap you must specify a newline with an embedded \en. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of formatting using the Spreadsheet::WriteExcel module \& # \& # This example shows how to wrap text in a cell. There are two alternatives, \& # vertical justification and text wrap. \& # \& # With vertical justification the text is wrapped automatically to fit the \& # column width. With text wrap you must specify a newline with an embedded \en. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("textwrap.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Set the column width for columns 1, 2 and 3 \& $worksheet\->set_column(1, 1, 24); \& $worksheet\->set_column(2, 2, 34); \& $worksheet\->set_column(3, 3, 34); \& \& # Set the row height for rows 1, 4, and 6. The height of row 2 will adjust \& # automatically to fit the text. \& # \& $worksheet\->set_row(0, 30); \& $worksheet\->set_row(3, 40); \& $worksheet\->set_row(5, 80); \& \& \& # No newlines \& my $str1 = "For whatever we lose (like a you or a me) "; \& $str1 .= "it\*(Aqs always ourselves we find in the sea"; \& \& # Embedded newlines \& my $str2 = "For whatever we lose\en(like a you or a me)\en"; \& $str2 .= "it\*(Aqs always ourselves\enwe find in the sea"; \& \& \& # Create a format for the column headings \& my $header = $workbook\->add_format(); \& $header\->set_bold(); \& $header\->set_font("Courier New"); \& $header\->set_align(\*(Aqcenter\*(Aq); \& $header\->set_align(\*(Aqvcenter\*(Aq); \& \& # Create a "vertical justification" format \& my $format1 = $workbook\->add_format(); \& $format1\->set_align(\*(Aqvjustify\*(Aq); \& \& # Create a "text wrap" format \& my $format2 = $workbook\->add_format(); \& $format2\->set_text_wrap(); \& \& # Write the headers \& $worksheet\->write(0, 1, "set_align(\*(Aqvjustify\*(Aq)", $header); \& $worksheet\->write(0, 2, "set_align(\*(Aqvjustify\*(Aq)", $header); \& $worksheet\->write(0, 3, "set_text_wrap()", $header); \& \& # Write some examples \& $worksheet\->write(1, 1, $str1, $format1); \& $worksheet\->write(1, 2, $str1, $format1); \& $worksheet\->write(1, 3, $str2, $format2); \& \& $worksheet\->write(3, 1, $str1, $format1); \& $worksheet\->write(3, 2, $str1, $format1); \& $worksheet\->write(3, 3, $str2, $format2); \& \& $worksheet\->write(5, 1, $str1, $format1); \& $worksheet\->write(5, 2, $str1, $format1); \& $worksheet\->write(5, 3, $str2, $format2); .Ve .PP Download this example: .SS "Example: win32ole.pl" .IX Subsection "Example: win32ole.pl" This is a simple example of how to create an Excel file using the Win32::OLE module for the sake of comparison. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # This is a simple example of how to create an Excel file using the \& # Win32::OLE module for the sake of comparison. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Cwd; \& use Win32::OLE; \& use Win32::OLE::Const \*(AqMicrosoft Excel\*(Aq; \& \& \& my $application = Win32::OLE\->new("Excel.Application"); \& my $workbook = $application\->Workbooks\->Add; \& my $worksheet = $workbook\->Worksheets(1); \& \& $worksheet\->Cells(1,1)\->{Value} = "Hello World"; \& $worksheet\->Cells(2,1)\->{Value} = "One"; \& $worksheet\->Cells(3,1)\->{Value} = "Two"; \& $worksheet\->Cells(4,1)\->{Value} = 3; \& $worksheet\->Cells(5,1)\->{Value} = 4.0000001; \& \& # Add some formatting \& $worksheet\->Cells(1,1)\->Font\->{Bold} = "True"; \& $worksheet\->Cells(1,1)\->Font\->{Size} = 16; \& $worksheet\->Cells(1,1)\->Font\->{ColorIndex} = 3; \& $worksheet\->Columns("A:A")\->{ColumnWidth} = 25; \& \& # Write a hyperlink \& my $range = $worksheet\->Range("A7:A7"); \& $worksheet\->Hyperlinks\->Add({ Anchor => $range, Address => "http://www.perl.com/"}); \& \& # Get current directory using Cwd.pm \& my $dir = cwd(); \& \& $workbook\->SaveAs({ \& FileName => $dir . \*(Aq/win32ole.xls\*(Aq, \& FileFormat => xlNormal, \& }); \& $workbook\->Close; .Ve .PP Download this example: .SS "Example: write_arrays.pl" .IX Subsection "Example: write_arrays.pl" Example of how to use the Spreadsheet::WriteExcel module to write 1D and 2D arrays of data. .PP To find out more about array references refer(!!) to the perlref and perlreftut manpages. To find out more about 2D arrays or \*(L"list of lists\*(R" refer to the perllol manpage. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # Example of how to use the Spreadsheet::WriteExcel module to \& # write 1D and 2D arrays of data. \& # \& # To find out more about array references refer(!!) to the perlref and \& # perlreftut manpages. To find out more about 2D arrays or "list of \& # lists" refer to the perllol manpage. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2002, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("write_arrays.xls"); \& my $worksheet1 = $workbook\->add_worksheet(\*(AqExample 1\*(Aq); \& my $worksheet2 = $workbook\->add_worksheet(\*(AqExample 2\*(Aq); \& my $worksheet3 = $workbook\->add_worksheet(\*(AqExample 3\*(Aq); \& my $worksheet4 = $workbook\->add_worksheet(\*(AqExample 4\*(Aq); \& my $worksheet5 = $workbook\->add_worksheet(\*(AqExample 5\*(Aq); \& my $worksheet6 = $workbook\->add_worksheet(\*(AqExample 6\*(Aq); \& my $worksheet7 = $workbook\->add_worksheet(\*(AqExample 7\*(Aq); \& my $worksheet8 = $workbook\->add_worksheet(\*(AqExample 8\*(Aq); \& \& my $format = $workbook\->add_format(color => \*(Aqred\*(Aq, bold => 1); \& \& \& # Data arrays used in the following examples. \& # undef values are written as blank cells (with format if specified). \& # \& my @array = ( \*(Aqone\*(Aq, \*(Aqtwo\*(Aq, undef, \*(Aqfour\*(Aq ); \& \& my @array2d = ( \& [\*(Aqmaggie\*(Aq, \*(Aqmilly\*(Aq, \*(Aqmolly\*(Aq, \*(Aqmay\*(Aq ], \& [13, 14, 15, 16 ], \& [\*(Aqshell\*(Aq, \*(Aqstar\*(Aq, \*(Aqcrab\*(Aq, \*(Aqstone\*(Aq], \& ); \& \& \& # 1. Write a row of data using an array reference. \& $worksheet1\->write(\*(AqA1\*(Aq, \e@array); \& \& # 2. Same as 1. above using an anonymous array ref. \& $worksheet2\->write(\*(AqA1\*(Aq, [ @array ]); \& \& # 3. Write a row of data using an explicit write_row() method call. \& # This is the same as calling write() in Ex. 1 above. \& # \& $worksheet3\->write_row(\*(AqA1\*(Aq, \e@array); \& \& # 4. Write a column of data using the write_col() method call. \& $worksheet4\->write_col(\*(AqA1\*(Aq, \e@array); \& \& # 5. Write a column of data using a ref to an array ref, i.e. a 2D array. \& $worksheet5\->write(\*(AqA1\*(Aq, [ \e@array ]); \& \& # 6. Write a 2D array in col\-row order. \& $worksheet6\->write(\*(AqA1\*(Aq, \e@array2d); \& \& # 7. Write a 2D array in row\-col order. \& $worksheet7\->write_col(\*(AqA1\*(Aq, \e@array2d); \& \& # 8. Write a row of data with formatting. The blank cell is also formatted. \& $worksheet8\->write(\*(AqA1\*(Aq, \e@array, $format); .Ve .PP Download this example: .SS "Example: write_handler1.pl" .IX Subsection "Example: write_handler1.pl" Example of how to add a user defined data handler to the Spreadsheet:: WriteExcel \fBwrite()\fR method. .PP The following example shows how to add a handler for a 7 digit \s-1ID\s0 number. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add a user defined data handler to the Spreadsheet:: \& # WriteExcel write() method. \& # \& # The following example shows how to add a handler for a 7 digit ID number. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("write_handler1.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& ############################################################################### \& # \& # Add a handler for 7 digit id numbers. This is useful when you want a string \& # such as 0000001 written as a string instead of a number and thus preserve \& # the leading zeroes. \& # \& # Note: you can get the same effect using the keep_leading_zeros() method but \& # this serves as a simple example. \& # \& $worksheet\->add_write_handler(qr[^\ed{7}$], \e&write_my_id); \& \& \& ############################################################################### \& # \& # The following function processes the data when a match is found. \& # \& sub write_my_id { \& \& my $worksheet = shift; \& \& return $worksheet\->write_string(@_); \& } \& \& \& # This format maintains the cell as text even if it is edited. \& my $id_format = $workbook\->add_format(num_format => \*(Aq@\*(Aq); \& \& \& # Write some numbers in the user defined format \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq0000000\*(Aq, $id_format); \& $worksheet\->write(\*(AqA2\*(Aq, \*(Aq0000001\*(Aq, $id_format); \& $worksheet\->write(\*(AqA3\*(Aq, \*(Aq0004000\*(Aq, $id_format); \& $worksheet\->write(\*(AqA4\*(Aq, \*(Aq1234567\*(Aq, $id_format); \& \& # Write some numbers that don\*(Aqt match the defined format \& $worksheet\->write(\*(AqA6\*(Aq, \*(Aq000000\*(Aq, $id_format); \& $worksheet\->write(\*(AqA7\*(Aq, \*(Aq000001\*(Aq, $id_format); \& $worksheet\->write(\*(AqA8\*(Aq, \*(Aq004000\*(Aq, $id_format); \& $worksheet\->write(\*(AqA9\*(Aq, \*(Aq123456\*(Aq, $id_format); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_handler2.pl" .IX Subsection "Example: write_handler2.pl" Example of how to add a user defined data handler to the Spreadsheet:: WriteExcel \fBwrite()\fR method. .PP The following example shows how to add a handler for a 7 digit \s-1ID\s0 number. It adds an additional constraint to the write_handler1.pl in that it only filters data that isn't in the third column. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add a user defined data handler to the Spreadsheet:: \& # WriteExcel write() method. \& # \& # The following example shows how to add a handler for a 7 digit ID number. \& # It adds an additional constraint to the write_handler1.pl in that it only \& # filters data that isn\*(Aqt in the third column. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("write_handler2.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& ############################################################################### \& # \& # Add a handler for 7 digit id numbers. This is useful when you want a string \& # such as 0000001 written as a string instead of a number and thus preserve \& # the leading zeroes. \& # \& # Note: you can get the same effect using the keep_leading_zeros() method but \& # this serves as a simple example. \& # \& $worksheet\->add_write_handler(qr[^\ed{7}$], \e&write_my_id); \& \& \& ############################################################################### \& # \& # The following function processes the data when a match is found. The handler \& # is set up so that it only filters data if it is in the third column. \& # \& sub write_my_id { \& \& my $worksheet = shift; \& my $col = $_[1]; \& \& # col is zero based \& if ($col != 2) { \& return $worksheet\->write_string(@_); \& } \& else { \& # Reject the match and return control to write() \& return undef; \& } \& \& } \& \& \& # This format maintains the cell as text even if it is edited. \& my $id_format = $workbook\->add_format(num_format => \*(Aq@\*(Aq); \& \& \& # Write some numbers in the user defined format \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq0000000\*(Aq, $id_format); \& $worksheet\->write(\*(AqB1\*(Aq, \*(Aq0000001\*(Aq, $id_format); \& $worksheet\->write(\*(AqC1\*(Aq, \*(Aq0000002\*(Aq, $id_format); \& $worksheet\->write(\*(AqD1\*(Aq, \*(Aq0000003\*(Aq, $id_format); \& \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_handler3.pl" .IX Subsection "Example: write_handler3.pl" Example of how to add a user defined data handler to the Spreadsheet:: WriteExcel \fBwrite()\fR method. .PP The following example shows how to add a handler for dates in a specific format. .PP See write_handler4.pl for a more rigorous example with error handling. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add a user defined data handler to the Spreadsheet:: \& # WriteExcel write() method. \& # \& # The following example shows how to add a handler for dates in a specific \& # format. \& # \& # See write_handler4.pl for a more rigorous example with error handling. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("write_handler3.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& my $date_format = $workbook\->add_format(num_format => \*(Aqdd/mm/yy\*(Aq); \& \& \& ############################################################################### \& # \& # Add a handler to match dates in the following format: d/m/yyyy \& # \& # The day and month can be single or double digits. \& # \& $worksheet\->add_write_handler(qr[^\ed{1,2}/\ed{1,2}/\ed{4}$], \e&write_my_date); \& \& \& ############################################################################### \& # \& # The following function processes the data when a match is found. \& # See write_handler4.pl for a more rigorous example with error handling. \& # \& sub write_my_date { \& \& my $worksheet = shift; \& my @args = @_; \& \& my $token = $args[2]; \& $token =~ qr[^(\ed{1,2})/(\ed{1,2})/(\ed{4})$]; \& \& # Change to the date format required by write_date_time(). \& my $date = sprintf "%4d\-%02d\-%02dT", $3, $2, $1; \& \& $args[2] = $date; \& \& return $worksheet\->write_date_time(@args); \& } \& \& \& # Write some dates in the user defined format \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq22/12/2004\*(Aq, $date_format); \& $worksheet\->write(\*(AqA2\*(Aq, \*(Aq1/1/1995\*(Aq, $date_format); \& $worksheet\->write(\*(AqA3\*(Aq, \*(Aq01/01/1995\*(Aq, $date_format); \& \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_handler4.pl" .IX Subsection "Example: write_handler4.pl" Example of how to add a user defined data handler to the Spreadsheet:: WriteExcel \fBwrite()\fR method. .PP The following example shows how to add a handler for dates in a specific format. .PP This is a more rigorous version of write_handler3.pl. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add a user defined data handler to the Spreadsheet:: \& # WriteExcel write() method. \& # \& # The following example shows how to add a handler for dates in a specific \& # format. \& # \& # This is a more rigorous version of write_handler3.pl. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("write_handler4.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& my $date_format = $workbook\->add_format(num_format => \*(Aqdd/mm/yy\*(Aq); \& \& \& ############################################################################### \& # \& # Add a handler to match dates in the following formats: d/m/yy, d/m/yyyy \& # \& # The day and month can be single or double digits and the year can be 2 or 4 \& # digits. \& # \& $worksheet\->add_write_handler(qr[^\ed{1,2}/\ed{1,2}/\ed{2,4}$], \e&write_my_date); \& \& \& ############################################################################### \& # \& # The following function processes the data when a match is found. \& # \& sub write_my_date { \& \& my $worksheet = shift; \& my @args = @_; \& \& my $token = $args[2]; \& \& if ($token =~ qr[^(\ed{1,2})/(\ed{1,2})/(\ed{2,4})$]) { \& \& my $day = $1; \& my $mon = $2; \& my $year = $3; \& \& # Use a window for 2 digit dates. This will keep some ragged Perl \& # programmer employed in thirty years time. :\-) \& if (length $year == 2) { \& if ($year < 50) { \& $year += 2000; \& } \& else { \& $year += 1900; \& } \& } \& \& my $date = sprintf "%4d\-%02d\-%02dT", $year, $mon, $day; \& \& # Convert the ISO ISO8601 style string to an Excel date \& $date = $worksheet\->convert_date_time($date); \& \& if (defined $date) { \& # Date was valid \& $args[2] = $date; \& return $worksheet\->write_number(@args); \& } \& else { \& # Not a valid date therefore write as a string \& return $worksheet\->write_string(@args); \& } \& } \& else { \& # Shouldn\*(Aqt happen if the same match is used in the re and sub. \& return undef; \& } \& } \& \& \& # Write some dates in the user defined format \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq22/12/2004\*(Aq, $date_format); \& $worksheet\->write(\*(AqA2\*(Aq, \*(Aq22/12/04\*(Aq, $date_format); \& $worksheet\->write(\*(AqA3\*(Aq, \*(Aq2/12/04\*(Aq, $date_format); \& $worksheet\->write(\*(AqA4\*(Aq, \*(Aq2/5/04\*(Aq, $date_format); \& $worksheet\->write(\*(AqA5\*(Aq, \*(Aq2/5/95\*(Aq, $date_format); \& $worksheet\->write(\*(AqA6\*(Aq, \*(Aq2/5/1995\*(Aq, $date_format); \& \& # Some erroneous dates \& $worksheet\->write(\*(AqA8\*(Aq, \*(Aq2/5/1895\*(Aq, $date_format); # Date out of Excel range \& $worksheet\->write(\*(AqA9\*(Aq, \*(Aq29/2/2003\*(Aq, $date_format); # Invalid leap day \& $worksheet\->write(\*(AqA10\*(Aq,\*(Aq50/50/50\*(Aq, $date_format); # Matches but isn\*(Aqt a date \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_to_scalar.pl" .IX Subsection "Example: write_to_scalar.pl" An example of writing an Excel file to a Perl scalar using Spreadsheet:: WriteExcel and the new features of perl 5.8. .PP For an examples of how to write to a scalar in versions prior to perl 5.8 see the filehandle.pl program and IO:Scalar. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # An example of writing an Excel file to a Perl scalar using Spreadsheet:: \& # WriteExcel and the new features of perl 5.8. \& # \& # For an examples of how to write to a scalar in versions prior to perl 5.8 \& # see the filehandle.pl program and IO:Scalar. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& require 5.008; \& \& \& # Use perl 5.8\*(Aqs feature of using a scalar as a filehandle. \& my $fh; \& my $str = \*(Aq\*(Aq; \& open $fh, \*(Aq>\*(Aq, \e$str or die "Failed to open filehandle: $!"; \& \& \& # Or replace the previous three lines with this: \& # open my $fh, \*(Aq>\*(Aq, \emy $str or die "Failed to open filehandle: $!"; \& \& \& # Spreadsheet::WriteExce accepts filehandle as well as file names. \& my $workbook = Spreadsheet::WriteExcel\->new($fh); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->write(0, 0, "Hi Excel!"); \& \& $workbook\->close(); \& \& \& # The Excel file in now in $str. Remember to binmode() the output \& # filehandle before printing it. \& binmode STDOUT; \& print $str; \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_utf16.pl" .IX Subsection "Example: unicode_utf16.pl" A simple example of writing some Unicode text with Spreadsheet::WriteExcel. .PP This example shows \s-1UTF16\s0 encoding. With perl 5.8 it is also possible to use utf8 without modification. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of writing some Unicode text with Spreadsheet::WriteExcel. \& # \& # This example shows UTF16 encoding. With perl 5.8 it is also possible to use \& # utf8 without modification. \& # \& # reverse(\*(Aq(c)\*(Aq), May 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqunicode_utf16.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Write the Unicode smiley face (with increased font for legibility) \& my $smiley = pack "n", 0x263a; \& my $big_font = $workbook\->add_format(size => 40); \& \& $worksheet\->write_utf16be_string(\*(AqA3\*(Aq, $smiley, $big_font); \& \& \& # Write a phrase in Cyrillic \& my $uni_str = pack "H*", "042d0442043e002004440440043004370430002004". \& "3d043000200440044304410441043a043e043c0021"; \& \& $worksheet\->write_utf16be_string(\*(AqA5\*(Aq, $uni_str); \& \& \& $worksheet\->write_utf16be_string(\*(AqA7\*(Aq, pack "H*", "0074006500730074"); \& \& \& \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_utf16_japan.pl" .IX Subsection "Example: unicode_utf16_japan.pl" A simple example of writing some Unicode text with Spreadsheet::WriteExcel. .PP This creates an Excel file with the word Nippon in 3 character sets. .PP This example shows \s-1UTF16\s0 encoding. With perl 5.8 it is also possible to use utf8 without modification. .PP See also the unicode_2022_jp.pl and unicode_shift_jis.pl examples. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of writing some Unicode text with Spreadsheet::WriteExcel. \& # \& # This creates an Excel file with the word Nippon in 3 character sets. \& # \& # This example shows UTF16 encoding. With perl 5.8 it is also possible to use \& # utf8 without modification. \& # \& # See also the unicode_2022_jp.pl and unicode_shift_jis.pl examples. \& # \& # reverse(\*(Aq(c)\*(Aq), May 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqunicode_utf16_japan.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Set a Unicode font. \& my $uni_font = $workbook\->add_format(font => \*(AqArial Unicode MS\*(Aq); \& \& \& # Create some UTF\-16BE Unicode text. \& my $kanji = pack \*(Aqn*\*(Aq, 0x65e5, 0x672c; \& my $katakana = pack \*(Aqn*\*(Aq, 0xff86, 0xff8e, 0xff9d; \& my $hiragana = pack \*(Aqn*\*(Aq, 0x306b, 0x307b, 0x3093; \& \& \& \& $worksheet\->write_utf16be_string(\*(AqA1\*(Aq, $kanji, $uni_font); \& $worksheet\->write_utf16be_string(\*(AqA2\*(Aq, $katakana, $uni_font); \& $worksheet\->write_utf16be_string(\*(AqA3\*(Aq, $hiragana, $uni_font); \& \& \& $worksheet\->write(\*(AqB1\*(Aq, \*(AqKanji\*(Aq); \& $worksheet\->write(\*(AqB2\*(Aq, \*(AqKatakana\*(Aq); \& $worksheet\->write(\*(AqB3\*(Aq, \*(AqHiragana\*(Aq); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_cyrillic.pl" .IX Subsection "Example: unicode_cyrillic.pl" A simple example of writing some Russian cyrillic text using Spreadsheet::WriteExcel and perl 5.8. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of writing some Russian cyrillic text using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2005, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& # In this example we generate utf8 strings from character data but in a \& # real application we would expect them to come from an external source. \& # \& \& \& # Create a Russian worksheet name in utf8. \& my $sheet = pack "U*", 0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438, \& 0x0446, 0x0430; \& \& \& # Create a Russian string. \& my $str = pack "U*", 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, \& 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C, \& 0x0438, 0x0440, 0x0021; \& \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_cyrillic.xls"); \& my $worksheet = $workbook\->add_worksheet($sheet . \*(Aq1\*(Aq); \& \& $worksheet\->set_column(\*(AqA:A\*(Aq, 18); \& $worksheet\->write(\*(AqA1\*(Aq, $str); \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_list.pl" .IX Subsection "Example: unicode_list.pl" A simple example using Spreadsheet::WriteExcel to display all available Unicode characters in a font. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example using Spreadsheet::WriteExcel to display all available \& # Unicode characters in a font. \& # \& # reverse(\*(Aq(c)\*(Aq), May 2004, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqunicode_list.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Set a Unicode font. \& my $uni_font = $workbook\->add_format(font => \*(AqArial Unicode MS\*(Aq); \& \& # Ascii font for labels. \& my $courier = $workbook\->add_format(font => \*(AqCourier New\*(Aq); \& \& \& my $char = 0; \& \& # Loop through all 32768 UTF\-16BE characters. \& # \& for my $row (0 .. 2 ** 12 \-1) { \& for my $col (0 .. 31) { \& \& last if $char == 0xffff; \& \& if ($col % 2 == 0){ \& $worksheet\->write_string($row, $col, \& sprintf(\*(Aq0x%04X\*(Aq, $char), $courier); \& } \& else { \& $worksheet\->write_utf16be_string($row, $col, \& pack(\*(Aqn\*(Aq, $char++), $uni_font); \& } \& } \& } \& \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_2022_jp.pl" .IX Subsection "Example: unicode_2022_jp.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Japanese from a file with \s-1ISO\-2022\-JP\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Japanese from a file with ISO\-2022\-JP \& # encoded text. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_2022_jp.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& \& \& my $file = \*(Aqunicode_2022_jp.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(iso\-2022\-jp)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_8859_11.pl" .IX Subsection "Example: unicode_8859_11.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Thai from a file with \s-1ISO\-8859\-11\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Thai from a file with ISO\-8859\-11 encoded text. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_8859_11.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& \& \& my $file = \*(Aqunicode_8859_11.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(iso\-8859\-11)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_8859_7.pl" .IX Subsection "Example: unicode_8859_7.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Greek from a file with \s-1ISO\-8859\-7\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Greek from a file with ISO\-8859\-7 encoded text. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_8859_7.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& \& \& my $file = \*(Aqunicode_8859_7.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(iso\-8859\-7)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_big5.pl" .IX Subsection "Example: unicode_big5.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Chinese from a file with \s-1BIG5\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Chinese from a file with BIG5 encoded text. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_big5.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 80); \& \& \& my $file = \*(Aqunicode_big5.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(big5)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_cp1251.pl" .IX Subsection "Example: unicode_cp1251.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Russian from a file with \s-1CP1251\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Russian from a file with CP1251 encoded text. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_cp1251.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& \& \& my $file = \*(Aqunicode_cp1251.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(cp1251)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_cp1256.pl" .IX Subsection "Example: unicode_cp1256.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Arabic text from a \s-1CP\-1256\s0 encoded file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Arabic text from a CP\-1256 encoded file. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_cp1256.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& \& \& my $file = \*(Aqunicode_cp1256.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(cp1256)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_koi8r.pl" .IX Subsection "Example: unicode_koi8r.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Russian from a file with \s-1KOI8\-R\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Russian from a file with KOI8\-R encoded text. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_koi8r.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& \& \& my $file = \*(Aqunicode_koi8r.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(koi8\-r)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_polish_utf8.pl" .IX Subsection "Example: unicode_polish_utf8.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Polish from a file with \s-1UTF8\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Polish from a file with UTF8 encoded text. \& # \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_polish_utf8.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& \& \& my $file = \*(Aqunicode_polish_utf8.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(utf8)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_shift_jis.pl" .IX Subsection "Example: unicode_shift_jis.pl" A simple example of converting some Unicode text to an Excel file using Spreadsheet::WriteExcel and perl 5.8. .PP This example generates some Japenese text from a file with Shift-JIS encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Spreadsheet::WriteExcel and perl 5.8. \& # \& # This example generates some Japenese text from a file with Shift\-JIS \& # encoded text. \& # \& # reverse(\*(Aq(c)\*(Aq), September 2004, John McNamara, jmcnamara@cpan.org \& # \& \& \& \& # Perl 5.8 or later is required for proper utf8 handling. For older perl \& # versions you should use UTF16 and the write_utf16be_string() method. \& # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs. \& # \& require 5.008; \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& my $workbook = Spreadsheet::WriteExcel\->new("unicode_shift_jis.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column(\*(AqA:A\*(Aq, 50); \& \& \& my $file = \*(Aqunicode_shift_jis.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(shiftjis)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while () { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: csv2xls.pl" .IX Subsection "Example: csv2xls.pl" Example of how to use the WriteExcel module .PP Simple program to convert a \s-1CSV\s0 comma-separated value file to an Excel file. This is more or less an non-op since Excel can read \s-1CSV\s0 files. The program uses Text::CSV_XS to parse the \s-1CSV.\s0 .PP Usage: csv2xls.pl file.csv newfile.xls .PP \&\s-1NOTE:\s0 This is only a simple conversion utility for illustrative purposes. For converting a \s-1CSV\s0 or Tab separated or any other type of delimited text file to Excel I recommend the more rigorous csv2xls program that is part of H.Merijn Brand's Text::CSV_XS module distro. .PP See the examples/csv2xls link here: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the WriteExcel module \& # \& # Simple program to convert a CSV comma\-separated value file to an Excel file. \& # This is more or less an non\-op since Excel can read CSV files. \& # The program uses Text::CSV_XS to parse the CSV. \& # \& # Usage: csv2xls.pl file.csv newfile.xls \& # \& # \& # NOTE: This is only a simple conversion utility for illustrative purposes. \& # For converting a CSV or Tab separated or any other type of delimited \& # text file to Excel I recommend the more rigorous csv2xls program that is \& # part of H.Merijn Brand\*(Aqs Text::CSV_XS module distro. \& # \& # See the examples/csv2xls link here: \& # L \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& use Text::CSV_XS; \& \& # Check for valid number of arguments \& if (($#ARGV < 1) || ($#ARGV > 2)) { \& die("Usage: csv2xls csvfile.txt newfile.xls\en"); \& }; \& \& # Open the Comma Separated Variable file \& open (CSVFILE, $ARGV[0]) or die "$ARGV[0]: $!"; \& \& # Create a new Excel workbook \& my $workbook = Spreadsheet::WriteExcel\->new($ARGV[1]); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Create a new CSV parsing object \& my $csv = Text::CSV_XS\->new; \& \& # Row and column are zero indexed \& my $row = 0; \& \& \& while () { \& if ($csv\->parse($_)) { \& my @Fld = $csv\->fields; \& \& my $col = 0; \& foreach my $token (@Fld) { \& $worksheet\->write($row, $col, $token); \& $col++; \& } \& $row++; \& } \& else { \& my $err = $csv\->error_input; \& print "Text::CSV_XS parse() failed on argument: ", $err, "\en"; \& } \& } .Ve .PP Download this example: .SS "Example: tab2xls.pl" .IX Subsection "Example: tab2xls.pl" Example of how to use the WriteExcel module .PP The following converts a tab separated file into an Excel file .PP Usage: tab2xls.pl tabfile.txt newfile.xls .PP \&\s-1NOTE:\s0 This is only a simple conversion utility for illustrative purposes. For converting a \s-1CSV\s0 or Tab separated or any other type of delimited text file to Excel I recommend the more rigorous csv2xls program that is part of H.Merijn Brand's Text::CSV_XS module distro. .PP See the examples/csv2xls link here: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to use the WriteExcel module \& # \& # The following converts a tab separated file into an Excel file \& # \& # Usage: tab2xls.pl tabfile.txt newfile.xls \& # \& # \& # NOTE: This is only a simple conversion utility for illustrative purposes. \& # For converting a CSV or Tab separated or any other type of delimited \& # text file to Excel I recommend the more rigorous csv2xls program that is \& # part of H.Merijn Brand\*(Aqs Text::CSV_XS module distro. \& # \& # See the examples/csv2xls link here: \& # L \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& # Check for valid number of arguments \& if (($#ARGV < 1) || ($#ARGV > 2)) { \& die("Usage: tab2xls tabfile.txt newfile.xls\en"); \& }; \& \& \& # Open the tab delimited file \& open (TABFILE, $ARGV[0]) or die "$ARGV[0]: $!"; \& \& \& # Create a new Excel workbook \& my $workbook = Spreadsheet::WriteExcel\->new($ARGV[1]); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Row and column are zero indexed \& my $row = 0; \& \& while () { \& chomp; \& # Split on single tab \& my @Fld = split(\*(Aq\et\*(Aq, $_); \& \& my $col = 0; \& foreach my $token (@Fld) { \& $worksheet\->write($row, $col, $token); \& $col++; \& } \& $row++; \& } .Ve .PP Download this example: .SS "Example: datecalc1.pl" .IX Subsection "Example: datecalc1.pl" \&\s-1NOTE:\s0 An easier way of writing dates and times is to use the newer \fBwrite_date_time()\fR Worksheet method. See the date_time.pl example. .PP Demonstration of writing date/time cells to Excel spreadsheets, using UNIX/Perl time as source of date/time. .PP UNIX/Perl time is the time since the Epoch (00:00:00 \s-1GMT, 1\s0 Jan 1970) measured in seconds. .PP An Excel file can use exactly one of two different date/time systems. In these systems, a floating point number represents the number of days (and fractional parts of the day) since a start point. The floating point number is referred to as a 'serial'. .PP The two systems ('1900' and '1904') use different starting points: .PP .Vb 5 \& \*(Aq1900\*(Aq; \*(Aq1.00\*(Aq is 1 Jan 1900 BUT 1900 is erroneously regarded as \& a leap year \- see: \& http://support.microsoft.com/support/kb/articles/Q181/3/70.asp \& for the excuse^H^H^H^H^H^Hreason. \& \*(Aq1904\*(Aq; \*(Aq1.00\*(Aq is 2 Jan 1904. .Ve .PP The '1904' system is the default for Apple Macs. Windows versions of Excel have the option to use the '1904' system. .PP Note that Visual Basic's \*(L"DateSerial\*(R" function does \s-1NOT\s0 erroneously regard 1900 as a leap year, and thus its serials do not agree with the 1900 serials of Excel for dates before 1 Mar 1900. .PP Note that StarOffice (at least at version 5.2) does \s-1NOT\s0 erroneously regard 1900 as a leap year, and thus its serials do not agree with the 1900 serials of Excel for dates before 1 Mar 1900. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& \& ###################################################################### \& # \& # NOTE: An easier way of writing dates and times is to use the newer \& # write_date_time() Worksheet method. See the date_time.pl example. \& # \& ###################################################################### \& # \& # Demonstration of writing date/time cells to Excel spreadsheets, \& # using UNIX/Perl time as source of date/time. \& # \& ###################################################################### \& # \& # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970) \& # measured in seconds. \& # \& # An Excel file can use exactly one of two different date/time systems. \& # In these systems, a floating point number represents the number of days \& # (and fractional parts of the day) since a start point. The floating point \& # number is referred to as a \*(Aqserial\*(Aq. \& # \& # The two systems (\*(Aq1900\*(Aq and \*(Aq1904\*(Aq) use different starting points: \& # \& # \*(Aq1900\*(Aq; \*(Aq1.00\*(Aq is 1 Jan 1900 BUT 1900 is erroneously regarded as \& # a leap year \- see: \& # http://support.microsoft.com/support/kb/articles/Q181/3/70.asp \& # for the excuse^H^H^H^H^H^Hreason. \& # \*(Aq1904\*(Aq; \*(Aq1.00\*(Aq is 2 Jan 1904. \& # \& # The \*(Aq1904\*(Aq system is the default for Apple Macs. Windows versions of \& # Excel have the option to use the \*(Aq1904\*(Aq system. \& # \& # Note that Visual Basic\*(Aqs "DateSerial" function does NOT erroneously \& # regard 1900 as a leap year, and thus its serials do not agree with \& # the 1900 serials of Excel for dates before 1 Mar 1900. \& # \& # Note that StarOffice (at least at version 5.2) does NOT erroneously \& # regard 1900 as a leap year, and thus its serials do not agree with \& # the 1900 serials of Excel for dates before 1 Mar 1900. \& # \& \& # Copyright 2000, Andrew Benham, adsb@bigfoot.com \& # \& \& ###################################################################### \& # \& # Calculation description \& # ======================= \& # \& # 1900 system \& # \-\-\-\-\-\-\-\-\-\-\- \& # Unix time is \*(Aq0\*(Aq at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900. \& # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68) \& # were leap years with an extra day. \& # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and \& # 1 Jan 1970. \& # In the 1900 system, \*(Aq1\*(Aq is 1 Jan 1900, but as 1900 was not a leap year \& # 1 Jan 1900 should really be \*(Aq2\*(Aq, so 1 Jan 1970 is \*(Aq25569\*(Aq. \& # \& # 1904 system \& # \-\-\-\-\-\-\-\-\-\-\- \& # Unix time is \*(Aq0\*(Aq at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904. \& # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68) \& # were leap years with an extra day. \& # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and \& # 1 Jan 1970. \& # In the 1904 system, 2 Jan 1904 being \*(Aq1\*(Aq, 1 Jan 1970 is \*(Aq24107\*(Aq. \& # \& ###################################################################### \& # \& # Copyright (c) 2000, Andrew Benham. \& # This program is free software. It may be used, redistributed and/or \& # modified under the same terms as Perl itself. \& # \& # Andrew Benham, adsb@bigfoot.com \& # London, United Kingdom \& # 11 Nov 2000 \& # \& ###################################################################### \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& use Time::Local; \& \& use vars qw/$DATE_SYSTEM/; \& \& # Use 1900 date system on all platforms other than Apple Mac (for which \& # use 1904 date system). \& $DATE_SYSTEM = ($^O eq \*(AqMacOS\*(Aq) ? 1 : 0; \& \& my $workbook = Spreadsheet::WriteExcel\->new("dates.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& my $format_date = $workbook\->add_format(); \& $format_date\->set_num_format(\*(Aqd mmmm yyy\*(Aq); \& \& $worksheet\->set_column(0,1,21); \& \& $worksheet\->write_string (0,0,"The epoch (GMT)"); \& $worksheet\->write_number (0,1,&calc_serial(0,1),0x16); \& \& $worksheet\->write_string (1,0,"The epoch (localtime)"); \& $worksheet\->write_number (1,1,&calc_serial(0,0),0x16); \& \& $worksheet\->write_string (2,0,"Today"); \& $worksheet\->write_number (2,1,&calc_serial(),$format_date); \& \& my $christmas2000 = timelocal(0,0,0,25,11,100); \& $worksheet\->write_string (3,0,"Christmas 2000"); \& $worksheet\->write_number (3,1,&calc_serial($christmas2000),$format_date); \& \& $workbook\->close(); \& \& #\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # calc_serial() \& # \& # Called with (up to) 2 parameters. \& # 1. Unix timestamp. If omitted, uses current time. \& # 2. GMT flag. Set to \*(Aq1\*(Aq to return serial in GMT. \& # If omitted, returns serial in appropriate timezone. \& # \& # Returns date/time serial according to $DATE_SYSTEM selected \& #\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& sub calc_serial { \& my $time = (defined $_[0]) ? $_[0] : time(); \& my $gmtflag = (defined $_[1]) ? $_[1] : 0; \& \& # Divide timestamp by number of seconds in a day. \& # This gives a date serial with \*(Aq0\*(Aq on 1 Jan 1970. \& my $serial = $time / 86400; \& \& # Adjust the date serial by the offset appropriate to the \& # currently selected system (1900/1904). \& if ($DATE_SYSTEM == 0) { # use 1900 system \& $serial += 25569; \& } else { # use 1904 system \& $serial += 24107; \& } \& \& unless ($gmtflag) { \& # Now have a \*(Aqraw\*(Aq serial with the right offset. But this \& # gives a serial in GMT, which is false unless the timezone \& # is GMT. We need to adjust the serial by the appropriate \& # timezone offset. \& # Calculate the appropriate timezone offset by seeing what \& # the differences between localtime and gmtime for the given \& # time are. \& \& my @gmtime = gmtime($time); \& my @ltime = localtime($time); \& \& # For the first 7 elements of the two arrays, adjust the \& # date serial where the elements differ. \& for (0 .. 6) { \& my $diff = $ltime[$_] \- $gmtime[$_]; \& if ($diff) { \& $serial += _adjustment($diff,$_); \& } \& } \& } \& \& # Perpetuate the error that 1900 was a leap year by decrementing \& # the serial if we\*(Aqre using the 1900 system and the date is prior to \& # 1 Mar 1900. This has the effect of making serial value \*(Aq60\*(Aq \& # 29 Feb 1900. \& \& # This fix only has any effect if UNIX/Perl time on the platform \& # can represent 1900. Many can\*(Aqt. \& \& unless ($DATE_SYSTEM) { \& $serial\-\- if ($serial < 61); # \*(Aq61\*(Aq is 1 Mar 1900 \& } \& return $serial; \& } \& \& sub _adjustment { \& # Based on the difference in the localtime/gmtime array elements \& # number, return the adjustment required to the serial. \& \& # We only look at some elements of the localtime/gmtime arrays: \& # seconds unlikely to be different as all known timezones \& # have an offset of integral multiples of 15 minutes, \& # but it\*(Aqs easy to do. \& # minutes will be different for timezone offsets which are \& # not an exact number of hours. \& # hours very likely to be different. \& # weekday will differ when localtime/gmtime difference \& # straddles midnight. \& # \& # Assume that difference between localtime and gmtime is less than \& # 5 days, then don\*(Aqt have to do maths for day of month, month number, \& # year number, etc... \& \& my ($delta,$element) = @_; \& my $adjust = 0; \& \& if ($element == 0) { # Seconds \& $adjust = $delta/86400; # 60 * 60 * 24 \& } elsif ($element == 1) { # Minutes \& $adjust = $delta/1440; # 60 * 24 \& } elsif ($element == 2) { # Hours \& $adjust = $delta/24; # 24 \& } elsif ($element == 6) { # Day of week number \& # Catch difference straddling Sat/Sun in either direction \& $delta += 7 if ($delta < \-4); \& $delta \-= 7 if ($delta > 4); \& \& $adjust = $delta; \& } \& return $adjust; \& } .Ve .PP Download this example: .SS "Example: datecalc2.pl" .IX Subsection "Example: datecalc2.pl" Example of how to using the Date::Calc module to calculate Excel dates. .PP \&\s-1NOTE:\s0 An easier way of writing dates and times is to use the newer \fBwrite_date_time()\fR Worksheet method. See the date_time.pl example. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to using the Date::Calc module to calculate Excel dates. \& # \& # NOTE: An easier way of writing dates and times is to use the newer \& # write_date_time() Worksheet method. See the date_time.pl example. \& # \& # reverse(\*(Aq(c)\*(Aq), June 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& use Date::Calc qw(Delta_DHMS); # You may need to install this module. \& \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("excel_date2.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Expand the first column so that the date is visible. \& $worksheet\->set_column("A:A", 25); \& \& \& # Add a format for the date \& my $format = $workbook\->add_format(); \& $format\->set_num_format(\*(Aqd mmmm yyy HH:MM:SS\*(Aq); \& \& \& my $date; \& \& # Write some dates and times \& $date = excel_date(1900, 1, 1); \& $worksheet\->write("A1", $date, $format); \& \& $date = excel_date(2000, 1, 1); \& $worksheet\->write("A2", $date, $format); \& \& $date = excel_date(2000, 4, 17, 14, 33, 15); \& $worksheet\->write("A3", $date, $format); \& \& \& ############################################################################### \& # \& # excel_date($years, $months, $days, $hours, $minutes, $seconds) \& # \& # Create an Excel date in the 1900 format. All of the arguments are optional \& # but you should at least add $years. \& # \& # Corrects for Excel\*(Aqs missing leap day in 1900. See excel_time1.pl for an \& # explanation. \& # \& sub excel_date { \& \& my $years = $_[0] || 1900; \& my $months = $_[1] || 1; \& my $days = $_[2] || 1; \& my $hours = $_[3] || 0; \& my $minutes = $_[4] || 0; \& my $seconds = $_[5] || 0; \& \& my @date = ($years, $months, $days, $hours, $minutes, $seconds); \& my @epoch = (1899, 12, 31, 0, 0, 0); \& \& ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date); \& \& my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60); \& \& # Add a day for Excel\*(Aqs missing leap day in 1900 \& $date++ if ($date > 59); \& \& return $date; \& } \& \& ############################################################################### \& # \& # excel_date($years, $months, $days, $hours, $minutes, $seconds) \& # \& # Create an Excel date in the 1904 format. All of the arguments are optional \& # but you should at least add $years. \& # \& # You will also need to call $workbook\->set_1904() for this format to be valid. \& # \& sub excel_date_1904 { \& \& my $years = $_[0] || 1900; \& my $months = $_[1] || 1; \& my $days = $_[2] || 1; \& my $hours = $_[3] || 0; \& my $minutes = $_[4] || 0; \& my $seconds = $_[5] || 0; \& \& my @date = ($years, $months, $days, $hours, $minutes, $seconds); \& my @epoch = (1904, 1, 1, 0, 0, 0); \& \& ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date); \& \& my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60); \& \& return $date; \& } .Ve .PP Download this example: .SS "Example: lecxe.pl" .IX Subsection "Example: lecxe.pl" Utility program to convert an Excel file into a Spreadsheet::WriteExcel program using Win32::OLE .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& # \& # Utility program to convert an Excel file into a Spreadsheet::WriteExcel \& # program using Win32::OLE \& # \& \& # \& # lecxe program \& # by t0mas@netlords.net \& # \& # Version 0.01a Initial release (alpha) \& \& \& # Modules \& use strict; \& use Win32::OLE; \& use Win32::OLE::Const; \& use Getopt::Std; \& \& \& # Vars \& use vars qw(%opts); \& \& \& # Get options \& getopts(\*(Aqi:o:v\*(Aq,\e%opts); \& \& \& # Not enough options \& exit &usage unless ($opts{i} && $opts{o}); \& \& \& # Create Excel object \& my $Excel = new Win32::OLE("Excel.Application","Quit") or \& die "Can\*(Aqt start excel: $!"; \& \& \& # Get constants \& my $ExcelConst=Win32::OLE::Const\->Load("Microsoft Excel"); \& \& \& # Show Excel \& $Excel\->{Visible} = 1 if ($opts{v}); \& \& \& # Open infile \& my $Workbook = $Excel\->Workbooks\->Open({Filename=>$opts{i}}); \& \& \& # Open outfile \& open (OUTFILE,">$opts{o}") or die "Can\*(Aqt open outfile $opts{o}: $!"; \& \& \& # Print header for outfile \& print OUTFILE <<\*(AqEOH\*(Aq; \& #!/usr/bin/perl \-w \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& \& use vars qw($workbook %worksheets %formats); \& \& \& $workbook = Spreadsheet::WriteExcel\->new("_change_me_.xls"); \& \& \& EOH \& \& \& # Loop all sheets \& foreach my $sheetnum (1..$Excel\->Workbooks(1)\->Worksheets\->Count) { \& \& \& # Format sheet \& my $name=$Excel\->Workbooks(1)\->Worksheets($sheetnum)\->Name; \& print "Sheet $name\en" if ($opts{v}); \& print OUTFILE "# Sheet $name\en"; \& print OUTFILE "\e$worksheets{\*(Aq$name\*(Aq} = \e$workbook\->add_worksheet(\*(Aq$name\*(Aq);\en"; \& \& \& # Get usedrange of cells in worksheet \& my $usedrange=$Excel\->Workbooks(1)\->Worksheets($sheetnum)\->UsedRange; \& \& \& # Loop all columns in used range \& foreach my $j (1..$usedrange\->Columns\->Count){ \& \& \& # Format column \& print "Col $j\en" if ($opts{v}); \& my ($colwidth); \& $colwidth=$usedrange\->Columns($j)\->ColumnWidth; \& print OUTFILE "# Column $j\en"; \& print OUTFILE "\e$worksheets{\*(Aq$name\*(Aq}\->set_column(".($j\-1).",".($j\-1). \& ", $colwidth);\en"; \& \& \& # Loop all rows in used range \& foreach my $i (1..$usedrange\->Rows\->Count){ \& \& \& # Format row \& print "Row $i\en" if ($opts{v}); \& print OUTFILE "# Row $i\en"; \& do { \& my ($rowheight); \& $rowheight=$usedrange\->Rows($i)\->RowHeight; \& print OUTFILE "\e$worksheets{\*(Aq$name\*(Aq}\->set_row(".($i\-1). \& ", $rowheight);\en"; \& } if ($j==1); \& \& \& # Start creating cell format \& my $fname="\e$formats{\*(Aq".$name.\*(AqR\*(Aq.$i.\*(AqC\*(Aq.$j."\*(Aq}"; \& my $format="$fname=\e$workbook\->add_format();\en"; \& my $print_format=0; \& \& # Check for borders \& my @bfnames=qw(left right top bottom); \& foreach my $k (1..$usedrange\->Cells($i,$j)\->Borders\->Count) { \& my $lstyle=$usedrange\->Cells($i,$j)\->Borders($k)\->LineStyle; \& if ($lstyle > 0) { \& $format.=$fname."\->set_".$bfnames[$k\-1]."($lstyle);\en"; \& $print_format=1; \& } \& } \& \& \& # Check for font \& my ($fontattr,$prop,$func,%fontsets,$fontColor); \& %fontsets=(Name=>\*(Aqset_font\*(Aq, \& Size=>\*(Aqset_size\*(Aq); \& while (($prop,$func) = each %fontsets) { \& $fontattr=$usedrange\->Cells($i,$j)\->Font\->$prop; \& if ($fontattr ne "") { \& $format.=$fname."\->$func(\*(Aq$fontattr\*(Aq);\en"; \& $print_format=1; \& } \& \& \& } \& %fontsets=(Bold=>\*(Aqset_bold(1)\*(Aq, \& Italic=>\*(Aqset_italic(1)\*(Aq, \& Underline=>\*(Aqset_underline(1)\*(Aq, \& Strikethrough=>\*(Aqset_strikeout(1)\*(Aq, \& Superscript=>\*(Aqset_script(1)\*(Aq, \& Subscript=>\*(Aqset_script(2)\*(Aq, \& OutlineFont=>\*(Aqset_outline(1)\*(Aq, \& Shadow=>\*(Aqset_shadow(1)\*(Aq); \& while (($prop,$func) = each %fontsets) { \& $fontattr=$usedrange\->Cells($i,$j)\->Font\->$prop; \& if ($fontattr==1) { \& $format.=$fname."\->$func;\en" ; \& \& $print_format=1; \& } \& } \& $fontColor=$usedrange\->Cells($i,$j)\->Font\->ColorIndex(); \& if ($fontColor>0&&$fontColor!=$ExcelConst\->{xlColorIndexAutomatic}) { \& $format.=$fname."\->set_color(".($fontColor+7).");\en" ; \& $print_format=1; \& } \& \& \& \& # Check text alignment, merging and wrapping \& my ($halign,$valign,$merge,$wrap); \& $halign=$usedrange\->Cells($i,$j)\->HorizontalAlignment; \& my %hAligns=($ExcelConst\->{xlHAlignCenter}=>"\*(Aqcenter\*(Aq", \& $ExcelConst\->{xlHAlignJustify}=>"\*(Aqjustify\*(Aq", \& $ExcelConst\->{xlHAlignLeft}=>"\*(Aqleft\*(Aq", \& $ExcelConst\->{xlHAlignRight}=>"\*(Aqright\*(Aq", \& $ExcelConst\->{xlHAlignFill}=>"\*(Aqfill\*(Aq", \& $ExcelConst\->{xlHAlignCenterAcrossSelection}=>"\*(Aqmerge\*(Aq"); \& if ($halign!=$ExcelConst\->{xlHAlignGeneral}) { \& $format.=$fname."\->set_align($hAligns{$halign});\en"; \& $print_format=1; \& } \& $valign=$usedrange\->Cells($i,$j)\->VerticalAlignment; \& my %vAligns=($ExcelConst\->{xlVAlignBottom}=>"\*(Aqbottom\*(Aq", \& $ExcelConst\->{xlVAlignCenter}=>"\*(Aqvcenter\*(Aq", \& $ExcelConst\->{xlVAlignJustify}=>"\*(Aqvjustify\*(Aq", \& $ExcelConst\->{xlVAlignTop}=>"\*(Aqtop\*(Aq"); \& if ($valign) { \& $format.=$fname."\->set_align($vAligns{$valign});\en"; \& $print_format=1; \& } \& $merge=$usedrange\->Cells($i,$j)\->MergeCells; \& if ($merge==1) { \& $format.=$fname."\->set_merge();\en"; \& \& $print_format=1; \& } \& $wrap=$usedrange\->Cells($i,$j)\->WrapText; \& if ($wrap==1) { \& $format.=$fname."\->set_text_wrap(1);\en"; \& \& $print_format=1; \& } \& \& \& # Check patterns \& my ($pattern,%pats); \& %pats=(\-4142=>0,\-4125=>2,\-4126=>3,\-4124=>4,\-4128=>5,\-4166=>6, \& \-4121=>7,\-4162=>8); \& $pattern=$usedrange\->Cells($i,$j)\->Interior\->Pattern; \& if ($pattern&&$pattern!=$ExcelConst\->{xlPatternAutomatic}) { \& $pattern=$pats{$pattern} if ($pattern<0 && defined $pats{$pattern}); \& $format.=$fname."\->set_pattern($pattern);\en"; \& \& # Colors fg/bg \& my ($cIndex); \& $cIndex=$usedrange\->Cells($i,$j)\->Interior\->PatternColorIndex; \& if ($cIndex>0&&$cIndex!=$ExcelConst\->{xlColorIndexAutomatic}) { \& $format.=$fname."\->set_bg_color(".($cIndex+7).");\en"; \& } \& $cIndex=$usedrange\->Cells($i,$j)\->Interior\->ColorIndex; \& if ($cIndex>0&&$cIndex!=$ExcelConst\->{xlColorIndexAutomatic}) { \& $format.=$fname."\->set_fg_color(".($cIndex+7).");\en"; \& } \& $print_format=1; \& } \& \& \& # Check for number format \& my ($num_format); \& $num_format=$usedrange\->Cells($i,$j)\->NumberFormat; \& if ($num_format ne "") { \& $format.=$fname."\->set_num_format(\*(Aq$num_format\*(Aq);\en"; \& $print_format=1; \& } \& \& \& # Check for contents (text or formula) \& my ($contents); \& $contents=$usedrange\->Cells($i,$j)\->Formula; \& $contents=$usedrange\->Cells($i,$j)\->Text if ($contents eq ""); \& \& \& # Print cell \& if ($contents ne "" or $print_format) { \& print OUTFILE "# Cell($i,$j)\en"; \& print OUTFILE $format if ($print_format); \& print OUTFILE "\e$worksheets{\*(Aq$name\*(Aq}\->write(".($i\-1).",".($j\-1). \& ",\*(Aq$contents\*(Aq"; \& print OUTFILE ",$fname" if ($print_format); \& print OUTFILE ");\en"; \& } \& } \& } \& } \& \& \& # Famous last words... \& print OUTFILE "\e$workbook\->close();\en"; \& \& \& # Close outfile \& close (OUTFILE) or die "Can\*(Aqt close outfile $opts{o}: $!"; \& \& \& #################################################################### \& sub usage { \& printf STDERR "usage: $0 [options]\en". \& "\etOptions:\en". \& "\et\et\-v \etverbose mode\en" . \& "\et\et\-i \etname of input file\en" . \& "\et\et\-o \etname of output file\en"; \& } \& \& \& #################################################################### \& sub END { \& # Quit excel \& do { \& $Excel\->{DisplayAlerts} = 0; \& $Excel\->Quit; \& } if (defined $Excel); \& } \& \& \& _\|_END_\|_ \& \& \& =head1 NAME \& \& \& lecxe \- A Excel file to Spreadsheet::WriteExcel code converter \& \& \& =head1 DESCRIPTION \& \& \& This program takes an MS Excel workbook file as input and from \& that file, produces an output file with Perl code that uses the \& Spreadsheet::WriteExcel module to reproduce the original \& file. \& \& \& =head1 STUFF \& \& \& Additional hands\-on editing of the output file might be neccecary \& as: \& \& \& * This program always names the file produced by output script \& _change_me_.xls \& \& \& * Users of international Excel versions will have som work to do \& on list separators and numeric punctation characters. \& \& \& =head1 SEE ALSO \& \& \& L, L, L \& \& \& =head1 BUGS \& \& \& * Picks wrong color on cells sometimes. \& \& \& * Probably a few other... \& \& \& =head1 DISCLAIMER \& \& \& I do not guarantee B with this program. If you use it you \& are doing so B! I may or may not support this \& depending on my time schedule... \& \& \& =head1 AUTHOR \& \& \& t0mas@netlords.net \& \& \& =head1 COPYRIGHT \& \& \& Copyright 2001, t0mas@netlords.net \& \& \& This package is free software; you can redistribute it and/or \& modify it under the same terms as Perl itself. .Ve .PP Download this example: .SS "Example: convertA1.pl" .IX Subsection "Example: convertA1.pl" This program contains helper functions to deal with the Excel A1 cell reference notation. .PP These functions have been superseded by Spreadsheet::WriteExcel::Utility. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # This program contains helper functions to deal with the Excel A1 cell \& # reference notation. \& # \& # These functions have been superseded by L. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& \& print "\en"; \& print "Cell B7 is equivalent to ("; \& print join " ", cell_to_rowcol(\*(AqB7\*(Aq); \& print ") in row column notation.\en"; \& \& print "Cell \e$B7 is equivalent to ("; \& print join " ", cell_to_rowcol(\*(Aq$B7\*(Aq); \& print ") in row column notation.\en"; \& \& print "Cell B\e$7 is equivalent to ("; \& print join " ", cell_to_rowcol(\*(AqB$7\*(Aq); \& print ") in row column notation.\en"; \& \& print "Cell \e$B\e$7 is equivalent to ("; \& print join " ", cell_to_rowcol(\*(Aq$B$7\*(Aq); \& print ") in row column notation.\en\en"; \& \& print "Row and column (1999, 29) are equivalent to "; \& print rowcol_to_cell(1999, 29), ".\en"; \& \& print "Row and column (1999, 29, 0, 1) are equivalent to "; \& print rowcol_to_cell(1999, 29, 0, 1), ".\en\en"; \& \& print "The base cell is: Z7\en"; \& print "Increment the row: ", inc_cell_row(\*(AqZ7\*(Aq), "\en"; \& print "Decrement the row: ", dec_cell_row(\*(AqZ7\*(Aq), "\en"; \& print "Increment the column: ", inc_cell_col(\*(AqZ7\*(Aq), "\en"; \& print "Decrement the column: ", dec_cell_col(\*(AqZ7\*(Aq), "\en\en"; \& \& \& ############################################################################### \& # \& # rowcol_to_cell($row, $col, $row_absolute, $col_absolute) \& # \& # Convert a zero based row and column reference to a A1 reference. For example \& # (0, 2) to C1. $row_absolute, $col_absolute are optional. They are boolean \& # values used to indicate if the row or column value is absolute, i.e. if it is \& # prefixed by a $ sign: eg. (0, 2, 0, 1) converts to $C1. \& # \& # Returns: a cell reference string. \& # \& sub rowcol_to_cell { \& \& my $row = $_[0]; \& my $col = $_[1]; \& my $row_abs = $_[2] || 0; \& my $col_abs = $_[3] || 0; \& \& \& if ($row_abs) { \& $row_abs = \*(Aq$\*(Aq \& } \& else { \& $row_abs = \*(Aq\*(Aq \& } \& \& if ($col_abs) { \& $col_abs = \*(Aq$\*(Aq \& } \& else { \& $col_abs = \*(Aq\*(Aq \& } \& \& \& my $int = int ($col / 26); \& my $frac = $col % 26 +1; \& \& my $chr1 =\*(Aq\*(Aq; \& my $chr2 =\*(Aq\*(Aq; \& \& \& if ($frac != 0) { \& $chr2 = chr (ord(\*(AqA\*(Aq) + $frac \-1); \& } \& \& if ($int > 0) { \& $chr1 = chr (ord(\*(AqA\*(Aq) + $int \-1); \& } \& \& $row++; # Zero index to 1\-index \& \& return $col_abs . $chr1 . $chr2 . $row_abs. $row; \& } \& \& \& ############################################################################### \& # \& # cell_to_rowcol($cell_ref) \& # \& # Convert an Excel cell reference in A1 notation to a zero based row and column \& # reference; converts C1 to (0, 2, 0, 0). \& # \& # Returns: row, column, row_is_absolute, column_is_absolute \& # \& # \& sub cell_to_rowcol { \& \& my $cell = shift; \& \& $cell =~ /(\e$?)([A\-I]?[A\-Z])(\e$?)(\ed+)/; \& \& my $col_abs = $1 eq "" ? 0 : 1; \& my $col = $2; \& my $row_abs = $3 eq "" ? 0 : 1; \& my $row = $4; \& \& # Convert base26 column string to number \& # All your Base are belong to us. \& my @chars = split //, $col; \& my $expn = 0; \& $col = 0; \& \& while (@chars) { \& my $char = pop(@chars); # LS char first \& $col += (ord($char) \-ord(\*(AqA\*(Aq) +1) * (26**$expn); \& $expn++; \& } \& \& # Convert 1\-index to zero\-index \& $row\-\-; \& $col\-\-; \& \& return $row, $col, $row_abs, $col_abs; \& } \& \& \& ############################################################################### \& # \& # inc_cell_row($cell_ref) \& # \& # Increments the row number of an Excel cell reference in A1 notation. \& # For example C3 to C4 \& # \& # Returns: a cell reference string. \& # \& sub inc_cell_row { \& \& my $cell = shift; \& my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell); \& \& $row++; \& \& return rowcol_to_cell($row, $col, $row_abs, $col_abs); \& } \& \& \& ############################################################################### \& # \& # dec_cell_row($cell_ref) \& # \& # Decrements the row number of an Excel cell reference in A1 notation. \& # For example C4 to C3 \& # \& # Returns: a cell reference string. \& # \& sub dec_cell_row { \& \& my $cell = shift; \& my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell); \& \& $row\-\-; \& \& return rowcol_to_cell($row, $col, $row_abs, $col_abs); \& } \& \& \& ############################################################################### \& # \& # inc_cell_col($cell_ref) \& # \& # Increments the column number of an Excel cell reference in A1 notation. \& # For example C3 to D3 \& # \& # Returns: a cell reference string. \& # \& sub inc_cell_col { \& \& my $cell = shift; \& my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell); \& \& $col++; \& \& return rowcol_to_cell($row, $col, $row_abs, $col_abs); \& } \& \& \& ############################################################################### \& # \& # dec_cell_col($cell_ref) \& # \& # Decrements the column number of an Excel cell reference in A1 notation. \& # For example D3 to C3 \& # \& # Returns: a cell reference string. \& # \& sub dec_cell_col { \& \& my $cell = shift; \& my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell); \& \& $col\-\-; \& \& return rowcol_to_cell($row, $col, $row_abs, $col_abs); \& } .Ve .PP Download this example: .SS "Example: writeA1.pl" .IX Subsection "Example: writeA1.pl" This is an example of how to extend the Spreadsheet::WriteExcel module. .PP Code is appended to the Spreadsheet::WriteExcel::Worksheet module by reusing the package name. The new code provides a \fBwrite()\fR method that allows you to use Excels A1 style cell references. This is not particularly useful but it serves as an example of how the module can be extended without modifying the code directly. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # This is an example of how to extend the Spreadsheet::WriteExcel module. \& # \& # Code is appended to the Spreadsheet::WriteExcel::Worksheet module by reusing \& # the package name. The new code provides a write() method that allows you to \& # use Excels A1 style cell references. This is not particularly useful but it \& # serves as an example of how the module can be extended without modifying the \& # code directly. \& # \& # reverse(\*(Aq(c)\*(Aq), March 2001, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook called simple.xls and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new("writeA1.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Write numbers or text \& $worksheet\->write (0, 0, "Hello"); \& $worksheet\->writeA1("A3", "A3" ); \& $worksheet\->writeA1("A5", 1.2345 ); \& \& \& ############################################################################### \& # \& # The following will be appended to the Spreadsheet::WriteExcel::Worksheet \& # package. \& # \& \& package Spreadsheet::WriteExcel::Worksheet; \& \& ############################################################################### \& # \& # writeA1($cell, $token, $format) \& # \& # Convert $cell from Excel A1 notation to $row, $col notation and \& # call write() on $token. \& # \& # Returns: return value of called subroutine or \-4 for invalid cell \& # reference. \& # \& sub writeA1 { \& my $self = shift; \& my $cell = shift; \& my $col; \& my $row; \& \& if ($cell =~ /([A\-z]+)(\ed+)/) { \& ($row, $col) = _convertA1($2, $1); \& $self\->write($row, $col, @_); \& } else { \& return \-4; \& } \& } \& \& ############################################################################### \& # \& # _convertA1($row, $col) \& # \& # Convert Excel A1 notation to $row, $col notation. Convert base26 column \& # string to a number. \& # \& sub _convertA1 { \& my $row = $_[0]; \& my $col = $_[1]; # String in AA notation \& \& my @chars = split //, $col; \& my $expn = 0; \& $col = 0; \& \& while (@chars) { \& my $char = uc(pop(@chars)); # LS char first \& $col += (ord($char) \-ord(\*(AqA\*(Aq) +1) * (26**$expn); \& $expn++; \& } \& \& # Convert 1 index to 0 index \& $row\-\-; \& $col\-\-; \& \& return($row, $col); \& } .Ve .PP Download this example: .SH "AUTHOR" .IX Header "AUTHOR" John McNamara jmcnamara@cpan.org .PP Contributed examples contain the original author's name. .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright MM-MMX, John McNamara. .PP All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.