1Spreadsheet::WriteExcelU:s:eErxaCmopnltersi(b3u)ted PerlSpDroecaudmsehneteatt:i:oWnriteExcel::Examples(3)
2
3
4

NAME

6       Examples - Spreadsheet::WriteExcel example programs.
7

DESCRIPTION

9       This is a documentation only module showing the examples that are
10       included in the Spreadsheet::WriteExcel distribution.
11
12       This file was auto-generated via the gen_examples_pod.pl program that
13       is also included in the examples directory.
14

Example programs

16       The following is a list of the 85 example programs that are included in
17       the Spreadsheet::WriteExcel distribution.
18
19       •   "Example: a_simple.pl" A get started example with some basic
20           features.
21
22       •   "Example: demo.pl" A demo of some of the available features.
23
24       •   "Example: regions.pl" A simple example of multiple worksheets.
25
26       •   "Example: stats.pl" Basic formulas and functions.
27
28       •   "Example: formats.pl" All the available formatting on several
29           worksheets.
30
31       •   "Example: bug_report.pl" A template for submitting bug reports.
32
33       •   "Example: autofilter.pl" Examples of worksheet autofilters.
34
35       •   "Example: autofit.pl" Simulate Excel's autofit for column widths.
36
37       •   "Example: bigfile.pl" Write past the 7MB limit with
38           OLE::Storage_Lite.
39
40       •   "Example: cgi.pl" A simple CGI program.
41
42       •   "Example: chart_area.pl" A demo of area style charts.
43
44       •   "Example: chart_bar.pl" A demo of bar (vertical histogram) style
45           charts.
46
47       •   "Example: chart_column.pl" A demo of column (histogram) style
48           charts.
49
50       •   "Example: chart_line.pl" A demo of line style charts.
51
52       •   "Example: chart_pie.pl" A demo of pie style charts.
53
54       •   "Example: chart_scatter.pl" A demo of scatter style charts.
55
56       •   "Example: chart_stock.pl" A demo of stock style charts.
57
58       •   "Example: chess.pl" An example of reusing formatting via
59           properties.
60
61       •   "Example: colors.pl" A demo of the colour palette and named
62           colours.
63
64       •   "Example: comments1.pl" Add comments to worksheet cells.
65
66       •   "Example: comments2.pl" Add comments with advanced options.
67
68       •   "Example: copyformat.pl" Example of copying a cell format.
69
70       •   "Example: data_validate.pl" An example of data validation and
71           dropdown lists.
72
73       •   "Example: date_time.pl" Write dates and times with
74           write_date_time().
75
76       •   "Example: defined_name.pl" Example of how to create defined names.
77
78       •   "Example: diag_border.pl" A simple example of diagonal cell
79           borders.
80
81       •   "Example: easter_egg.pl" Expose the Excel97 flight simulator.
82
83       •   "Example: filehandle.pl" Examples of working with filehandles.
84
85       •   "Example: formula_result.pl" Formulas with user specified results.
86
87       •   "Example: headers.pl" Examples of worksheet headers and footers.
88
89       •   "Example: hide_sheet.pl" Simple example of hiding a worksheet.
90
91       •   "Example: hyperlink1.pl" Shows how to create web hyperlinks.
92
93       •   "Example: hyperlink2.pl" Examples of internal and external
94           hyperlinks.
95
96       •   "Example: images.pl" Adding images to worksheets.
97
98       •   "Example: indent.pl" An example of cell indentation.
99
100       •   "Example: merge1.pl" A simple example of cell merging.
101
102       •   "Example: merge2.pl" A simple example of cell merging with
103           formatting.
104
105       •   "Example: merge3.pl" Add hyperlinks to merged cells.
106
107       •   "Example: merge4.pl" An advanced example of merging with
108           formatting.
109
110       •   "Example: merge5.pl" An advanced example of merging with
111           formatting.
112
113       •   "Example: merge6.pl" An example of merging with Unicode strings.
114
115       •   "Example: mod_perl1.pl" A simple mod_perl 1 program.
116
117       •   "Example: mod_perl2.pl" A simple mod_perl 2 program.
118
119       •   "Example: outline.pl" An example of outlines and grouping.
120
121       •   "Example: outline_collapsed.pl" An example of collapsed outlines.
122
123       •   "Example: panes.pl" An examples of how to create panes.
124
125       •   "Example: properties.pl" Add document properties to a workbook.
126
127       •   "Example: protection.pl" Example of cell locking and formula
128           hiding.
129
130       •   "Example: repeat.pl" Example of writing repeated formulas.
131
132       •   "Example: right_to_left.pl" Change default sheet direction to right
133           to left.
134
135       •   "Example: row_wrap.pl" How to wrap data from one worksheet onto
136           another.
137
138       •   "Example: sales.pl" An example of a simple sales spreadsheet.
139
140       •   "Example: sendmail.pl" Send an Excel email attachment using
141           Mail::Sender.
142
143       •   "Example: stats_ext.pl" Same as stats.pl with external references.
144
145       •   "Example: stocks.pl" Demonstrates conditional formatting.
146
147       •   "Example: tab_colors.pl" Example of how to set worksheet tab
148           colours.
149
150       •   "Example: textwrap.pl" Demonstrates text wrapping options.
151
152       •   "Example: win32ole.pl" A sample Win32::OLE example for comparison.
153
154       •   "Example: write_arrays.pl" Example of writing 1D or 2D arrays of
155           data.
156
157       •   "Example: write_handler1.pl" Example of extending the write()
158           method. Step 1.
159
160       •   "Example: write_handler2.pl" Example of extending the write()
161           method. Step 2.
162
163       •   "Example: write_handler3.pl" Example of extending the write()
164           method. Step 3.
165
166       •   "Example: write_handler4.pl" Example of extending the write()
167           method. Step 4.
168
169       •   "Example: write_to_scalar.pl" Example of writing an Excel file to a
170           Perl scalar.
171
172       •   "Example: unicode_utf16.pl" Simple example of using Unicode UTF16
173           strings.
174
175       •   "Example: unicode_utf16_japan.pl" Write Japanese Unicode strings
176           using UTF-16.
177
178       •   "Example: unicode_cyrillic.pl" Write Russian Cyrillic strings using
179           UTF-8.
180
181       •   "Example: unicode_list.pl" List the chars in a Unicode font.
182
183       •   "Example: unicode_2022_jp.pl" Japanese: ISO-2022-JP to utf8 in perl
184           5.8.
185
186       •   "Example: unicode_8859_11.pl" Thai:     ISO-8859_11 to utf8 in perl
187           5.8.
188
189       •   "Example: unicode_8859_7.pl" Greek:    ISO-8859_7  to utf8 in perl
190           5.8.
191
192       •   "Example: unicode_big5.pl" Chinese:  BIG5        to utf8 in perl
193           5.8.
194
195       •   "Example: unicode_cp1251.pl" Russian:  CP1251      to utf8 in perl
196           5.8.
197
198       •   "Example: unicode_cp1256.pl" Arabic:   CP1256      to utf8 in perl
199           5.8.
200
201       •   "Example: unicode_koi8r.pl" Russian:  KOI8-R      to utf8 in perl
202           5.8.
203
204       •   "Example: unicode_polish_utf8.pl" Polish :  UTF8        to utf8 in
205           perl 5.8.
206
207       •   "Example: unicode_shift_jis.pl" Japanese: Shift JIS   to utf8 in
208           perl 5.8.
209
210       •   "Example: csv2xls.pl" Program to convert a CSV file to an Excel
211           file.
212
213       •   "Example: tab2xls.pl" Program to convert a tab separated file to
214           xls.
215
216       •   "Example: datecalc1.pl" Convert Unix/Perl time to Excel time.
217
218       •   "Example: datecalc2.pl" Calculate an Excel date using Date::Calc.
219
220       •   "Example: lecxe.pl" Convert Excel to WriteExcel using Win32::OLE.
221
222       •   "Example: convertA1.pl" Helper functions for dealing with A1
223           notation.
224
225       •   "Example: function_locale.pl" Add non-English function names to
226           Formula.pm.
227
228       •   "Example: writeA1.pl" Example of how to extend the module.
229
230   Example: a_simple.pl
231       A simple example of how to use the Spreadsheet::WriteExcel module to
232       write some  text and numbers to an Excel binary file.
233
234       Source code for this example:
235
236           #!/usr/bin/perl -w
237
238           ###############################################################################
239           #
240           # A simple example of how to use the Spreadsheet::WriteExcel module to write
241           # some  text and numbers to an Excel binary file.
242           #
243           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
244           #
245
246           use strict;
247           use Spreadsheet::WriteExcel;
248
249           # Create a new workbook called simple.xls and add a worksheet
250           my $workbook  = Spreadsheet::WriteExcel->new('a_simple.xls');
251           my $worksheet = $workbook->add_worksheet();
252
253           # The general syntax is write($row, $column, $token). Note that row and
254           # column are zero indexed
255           #
256
257           # Write some text
258           $worksheet->write(0, 0,  "Hi Excel!");
259
260
261           # Write some numbers
262           $worksheet->write(2, 0,  3);          # Writes 3
263           $worksheet->write(3, 0,  3.00000);    # Writes 3
264           $worksheet->write(4, 0,  3.00001);    # Writes 3.00001
265           $worksheet->write(5, 0,  3.14159);    # TeX revision no.?
266
267
268           # Write some formulas
269           $worksheet->write(7, 0,  '=A3 + A6');
270           $worksheet->write(8, 0,  '=IF(A5>3,"Yes", "No")');
271
272
273           # Write a hyperlink
274           $worksheet->write(10, 0, 'http://www.perl.com/');
275
276           __END__
277
278       Download this example:
279       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/a_simple.pl>
280
281   Example: demo.pl
282       A simple demo of some of the features of Spreadsheet::WriteExcel.
283
284       This program is used to create the project screenshot for Freshmeat:
285       <http://freshmeat.net/projects/writeexcel/>
286
287       Source code for this example:
288
289           #!/usr/bin/perl -w
290
291           #######################################################################
292           #
293           # A simple demo of some of the features of Spreadsheet::WriteExcel.
294           #
295           # This program is used to create the project screenshot for Freshmeat:
296           # L<http://freshmeat.net/projects/writeexcel/>
297           #
298           # reverse('(c)'), October 2001, John McNamara, jmcnamara@cpan.org
299           #
300
301           use strict;
302           use Spreadsheet::WriteExcel;
303
304           my $workbook   = Spreadsheet::WriteExcel->new("demo.xls");
305           my $worksheet  = $workbook->add_worksheet('Demo');
306           my $worksheet2 = $workbook->add_worksheet('Another sheet');
307           my $worksheet3 = $workbook->add_worksheet('And another');
308
309           my $bold       = $workbook->add_format(bold => 1);
310
311
312           #######################################################################
313           #
314           # Write a general heading
315           #
316           $worksheet->set_column('A:A', 36, $bold);
317           $worksheet->set_column('B:B', 20       );
318           $worksheet->set_row   (0,     40       );
319
320           my $heading  = $workbook->add_format(
321                                                   bold    => 1,
322                                                   color   => 'blue',
323                                                   size    => 16,
324                                                   merge   => 1,
325                                                   align  => 'vcenter',
326                                                   );
327
328           my @headings = ('Features of Spreadsheet::WriteExcel', '');
329           $worksheet->write_row('A1', \@headings, $heading);
330
331
332           #######################################################################
333           #
334           # Some text examples
335           #
336           my $text_format  = $workbook->add_format(
337                                                       bold    => 1,
338                                                       italic  => 1,
339                                                       color   => 'red',
340                                                       size    => 18,
341                                                       font    =>'Lucida Calligraphy'
342                                                   );
343
344           # A phrase in Cyrillic
345           my $unicode = pack "H*", "042d0442043e002004440440043004370430002004".
346                                    "3d043000200440044304410441043a043e043c0021";
347
348
349           $worksheet->write('A2', "Text");
350           $worksheet->write('B2', "Hello Excel");
351           $worksheet->write('A3', "Formatted text");
352           $worksheet->write('B3', "Hello Excel", $text_format);
353           $worksheet->write('A4', "Unicode text");
354           $worksheet->write_utf16be_string('B4', $unicode);
355
356           #######################################################################
357           #
358           # Some numeric examples
359           #
360           my $num1_format  = $workbook->add_format(num_format => '$#,##0.00');
361           my $num2_format  = $workbook->add_format(num_format => ' d mmmm yyy');
362
363
364           $worksheet->write('A5', "Numbers");
365           $worksheet->write('B5', 1234.56);
366           $worksheet->write('A6', "Formatted numbers");
367           $worksheet->write('B6', 1234.56, $num1_format);
368           $worksheet->write('A7', "Formatted numbers");
369           $worksheet->write('B7', 37257, $num2_format);
370
371
372           #######################################################################
373           #
374           # Formulae
375           #
376           $worksheet->set_selection('B8');
377           $worksheet->write('A8', 'Formulas and functions, "=SIN(PI()/4)"');
378           $worksheet->write('B8', '=SIN(PI()/4)');
379
380
381           #######################################################################
382           #
383           # Hyperlinks
384           #
385           $worksheet->write('A9', "Hyperlinks");
386           $worksheet->write('B9',  'http://www.perl.com/' );
387
388
389           #######################################################################
390           #
391           # Images
392           #
393           $worksheet->write('A10', "Images");
394           $worksheet->insert_image('B10', 'republic.png', 16, 8);
395
396
397           #######################################################################
398           #
399           # Misc
400           #
401           $worksheet->write('A18', "Page/printer setup");
402           $worksheet->write('A19', "Multiple worksheets");
403
404           __END__
405
406       Download this example:
407       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/demo.pl>
408
409   Example: regions.pl
410       An example of how to use the Spreadsheet:WriteExcel module to write a
411       basic Excel workbook with multiple worksheets.
412
413       Source code for this example:
414
415           #!/usr/bin/perl -w
416
417           ###############################################################################
418           #
419           # An example of how to use the Spreadsheet:WriteExcel module to write a basic
420           # Excel workbook with multiple worksheets.
421           #
422           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
423           #
424
425           use strict;
426           use Spreadsheet::WriteExcel;
427
428           # Create a new Excel workbook
429           my $workbook = Spreadsheet::WriteExcel->new("regions.xls");
430
431           # Add some worksheets
432           my $north = $workbook->add_worksheet("North");
433           my $south = $workbook->add_worksheet("South");
434           my $east  = $workbook->add_worksheet("East");
435           my $west  = $workbook->add_worksheet("West");
436
437           # Add a Format
438           my $format = $workbook->add_format();
439           $format->set_bold();
440           $format->set_color('blue');
441
442           # Add a caption to each worksheet
443           foreach my $worksheet ($workbook->sheets()) {
444               $worksheet->write(0, 0, "Sales", $format);
445           }
446
447           # Write some data
448           $north->write(0, 1, 200000);
449           $south->write(0, 1, 100000);
450           $east->write (0, 1, 150000);
451           $west->write (0, 1, 100000);
452
453           # Set the active worksheet
454           $south->activate();
455
456           # Set the width of the first column
457           $south->set_column(0, 0, 20);
458
459           # Set the active cell
460           $south->set_selection(0, 1);
461
462       Download this example:
463       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/regions.pl>
464
465   Example: stats.pl
466       A simple example of how to use functions with the
467       Spreadsheet::WriteExcel module.
468
469       Source code for this example:
470
471           #!/usr/bin/perl -w
472
473           ###############################################################################
474           #
475           # A simple example of how to use functions with the Spreadsheet::WriteExcel
476           # module.
477           #
478           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
479           #
480
481           use strict;
482           use Spreadsheet::WriteExcel;
483
484           # Create a new workbook and add a worksheet
485           my $workbook  = Spreadsheet::WriteExcel->new("stats.xls");
486           my $worksheet = $workbook->add_worksheet('Test data');
487
488           # Set the column width for columns 1
489           $worksheet->set_column(0, 0, 20);
490
491
492           # Create a format for the headings
493           my $format = $workbook->add_format();
494           $format->set_bold();
495
496
497           # Write the sample data
498           $worksheet->write(0, 0, 'Sample', $format);
499           $worksheet->write(0, 1, 1);
500           $worksheet->write(0, 2, 2);
501           $worksheet->write(0, 3, 3);
502           $worksheet->write(0, 4, 4);
503           $worksheet->write(0, 5, 5);
504           $worksheet->write(0, 6, 6);
505           $worksheet->write(0, 7, 7);
506           $worksheet->write(0, 8, 8);
507
508           $worksheet->write(1, 0, 'Length', $format);
509           $worksheet->write(1, 1, 25.4);
510           $worksheet->write(1, 2, 25.4);
511           $worksheet->write(1, 3, 24.8);
512           $worksheet->write(1, 4, 25.0);
513           $worksheet->write(1, 5, 25.3);
514           $worksheet->write(1, 6, 24.9);
515           $worksheet->write(1, 7, 25.2);
516           $worksheet->write(1, 8, 24.8);
517
518           # Write some statistical functions
519           $worksheet->write(4,  0, 'Count', $format);
520           $worksheet->write(4,  1, '=COUNT(B1:I1)');
521
522           $worksheet->write(5,  0, 'Sum', $format);
523           $worksheet->write(5,  1, '=SUM(B2:I2)');
524
525           $worksheet->write(6,  0, 'Average', $format);
526           $worksheet->write(6,  1, '=AVERAGE(B2:I2)');
527
528           $worksheet->write(7,  0, 'Min', $format);
529           $worksheet->write(7,  1, '=MIN(B2:I2)');
530
531           $worksheet->write(8,  0, 'Max', $format);
532           $worksheet->write(8,  1, '=MAX(B2:I2)');
533
534           $worksheet->write(9,  0, 'Standard Deviation', $format);
535           $worksheet->write(9,  1, '=STDEV(B2:I2)');
536
537           $worksheet->write(10, 0, 'Kurtosis', $format);
538           $worksheet->write(10, 1, '=KURT(B2:I2)');
539
540           __END__
541
542       Download this example:
543       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/stats.pl>
544
545   Example: formats.pl
546       Examples of formatting using the Spreadsheet::WriteExcel module.
547
548       This program demonstrates almost all possible formatting options. It is
549       worth running this program and viewing the output Excel file if you are
550       interested in the various formatting possibilities.
551
552       Source code for this example:
553
554           #!/usr/bin/perl -w
555
556           ###############################################################################
557           #
558           # Examples of formatting using the Spreadsheet::WriteExcel module.
559           #
560           # This program demonstrates almost all possible formatting options. It is worth
561           # running this program and viewing the output Excel file if you are interested
562           # in the various formatting possibilities.
563           #
564           # reverse('(c)'), September 2002, John McNamara, jmcnamara@cpan.org
565           #
566
567           use strict;
568           use Spreadsheet::WriteExcel;
569
570           my $workbook = Spreadsheet::WriteExcel->new('formats.xls');
571
572           # Some common formats
573           my $center  = $workbook->add_format(align => 'center');
574           my $heading = $workbook->add_format(align => 'center', bold => 1);
575
576           # The named colors
577           my %colors = (
578                           0x08, 'black',
579                           0x0C, 'blue',
580                           0x10, 'brown',
581                           0x0F, 'cyan',
582                           0x17, 'gray',
583                           0x11, 'green',
584                           0x0B, 'lime',
585                           0x0E, 'magenta',
586                           0x12, 'navy',
587                           0x35, 'orange',
588                           0x21, 'pink',
589                           0x14, 'purple',
590                           0x0A, 'red',
591                           0x16, 'silver',
592                           0x09, 'white',
593                           0x0D, 'yellow',
594                        );
595
596           # Call these subroutines to demonstrate different formatting options
597           intro();
598           fonts();
599           named_colors();
600           standard_colors();
601           numeric_formats();
602           borders();
603           patterns();
604           alignment();
605           misc();
606
607           # Note: this is required
608           $workbook->close();
609
610
611           ######################################################################
612           #
613           # Intro.
614           #
615           sub intro {
616
617               my $worksheet = $workbook->add_worksheet('Introduction');
618
619               $worksheet->set_column(0, 0, 60);
620
621               my $format = $workbook->add_format();
622               $format->set_bold();
623               $format->set_size(14);
624               $format->set_color('blue');
625               $format->set_align('center');
626
627               my $format2 = $workbook->add_format();
628               $format2->set_bold();
629               $format2->set_color('blue');
630
631               $worksheet->write(2, 0, 'This workbook demonstrates some of',  $format);
632               $worksheet->write(3, 0, 'the formatting options provided by',  $format);
633               $worksheet->write(4, 0, 'the Spreadsheet::WriteExcel module.', $format);
634
635               $worksheet->write('A7',  'Sections:', $format2);
636               $worksheet->write('A8',  "internal:Fonts!A1",             'Fonts'          );
637               $worksheet->write('A9',  "internal:'Named colors'!A1",    'Named colors'   );
638               $worksheet->write('A10', "internal:'Standard colors'!A1", 'Standard colors');
639               $worksheet->write('A11', "internal:'Numeric formats'!A1", 'Numeric formats');
640               $worksheet->write('A12', "internal:Borders!A1",           'Borders'        );
641               $worksheet->write('A13', "internal:Patterns!A1",          'Patterns'       );
642               $worksheet->write('A14', "internal:Alignment!A1",         'Alignment'      );
643               $worksheet->write('A15', "internal:Miscellaneous!A1",     'Miscellaneous'  );
644
645           }
646
647
648           ######################################################################
649           #
650           # Demonstrate the named colors.
651           #
652           sub named_colors {
653
654               my $worksheet = $workbook->add_worksheet('Named colors');
655
656               $worksheet->set_column(0, 3, 15);
657
658               $worksheet->write(0, 0, "Index", $heading);
659               $worksheet->write(0, 1, "Index", $heading);
660               $worksheet->write(0, 2, "Name",  $heading);
661               $worksheet->write(0, 3, "Color", $heading);
662
663               my $i = 1;
664
665               while (my($index, $color) = each %colors) {
666                   my $format = $workbook->add_format(
667                                                       bg_color => $color,
668                                                       pattern  => 1,
669                                                       border   => 1
670                                                    );
671
672                   $worksheet->write($i+1, 0, $index,                    $center);
673                   $worksheet->write($i+1, 1, sprintf("0x%02X", $index), $center);
674                   $worksheet->write($i+1, 2, $color,                    $center);
675                   $worksheet->write($i+1, 3, '',                        $format);
676                   $i++;
677               }
678           }
679
680
681           ######################################################################
682           #
683           # Demonstrate the standard Excel colors in the range 8..63.
684           #
685           sub standard_colors {
686
687               my $worksheet = $workbook->add_worksheet('Standard colors');
688
689               $worksheet->set_column(0, 3, 15);
690
691               $worksheet->write(0, 0, "Index", $heading);
692               $worksheet->write(0, 1, "Index", $heading);
693               $worksheet->write(0, 2, "Color", $heading);
694               $worksheet->write(0, 3, "Name",  $heading);
695
696               for my $i (8..63) {
697                   my $format = $workbook->add_format(
698                                                       bg_color => $i,
699                                                       pattern  => 1,
700                                                       border   => 1
701                                                    );
702
703                   $worksheet->write(($i -7), 0, $i,                    $center);
704                   $worksheet->write(($i -7), 1, sprintf("0x%02X", $i), $center);
705                   $worksheet->write(($i -7), 2, '',                    $format);
706
707                   # Add the  color names
708                   if (exists $colors{$i}) {
709                       $worksheet->write(($i -7), 3, $colors{$i}, $center);
710
711                   }
712               }
713           }
714
715
716           ######################################################################
717           #
718           # Demonstrate the standard numeric formats.
719           #
720           sub numeric_formats {
721
722               my $worksheet = $workbook->add_worksheet('Numeric formats');
723
724               $worksheet->set_column(0, 4, 15);
725               $worksheet->set_column(5, 5, 45);
726
727               $worksheet->write(0, 0, "Index",       $heading);
728               $worksheet->write(0, 1, "Index",       $heading);
729               $worksheet->write(0, 2, "Unformatted", $heading);
730               $worksheet->write(0, 3, "Formatted",   $heading);
731               $worksheet->write(0, 4, "Negative",    $heading);
732               $worksheet->write(0, 5, "Format",      $heading);
733
734               my @formats;
735               push @formats, [ 0x00, 1234.567,   0,         'General' ];
736               push @formats, [ 0x01, 1234.567,   0,         '0' ];
737               push @formats, [ 0x02, 1234.567,   0,         '0.00' ];
738               push @formats, [ 0x03, 1234.567,   0,         '#,##0' ];
739               push @formats, [ 0x04, 1234.567,   0,         '#,##0.00' ];
740               push @formats, [ 0x05, 1234.567,   -1234.567, '($#,##0_);($#,##0)' ];
741               push @formats, [ 0x06, 1234.567,   -1234.567, '($#,##0_);[Red]($#,##0)' ];
742               push @formats, [ 0x07, 1234.567,   -1234.567, '($#,##0.00_);($#,##0.00)' ];
743               push @formats, [ 0x08, 1234.567,   -1234.567, '($#,##0.00_);[Red]($#,##0.00)' ];
744               push @formats, [ 0x09, 0.567,      0,         '0%' ];
745               push @formats, [ 0x0a, 0.567,      0,         '0.00%' ];
746               push @formats, [ 0x0b, 1234.567,   0,         '0.00E+00' ];
747               push @formats, [ 0x0c, 0.75,       0,         '# ?/?' ];
748               push @formats, [ 0x0d, 0.3125,     0,         '# ??/??' ];
749               push @formats, [ 0x0e, 36892.521,  0,         'm/d/yy' ];
750               push @formats, [ 0x0f, 36892.521,  0,         'd-mmm-yy' ];
751               push @formats, [ 0x10, 36892.521,  0,         'd-mmm' ];
752               push @formats, [ 0x11, 36892.521,  0,         'mmm-yy' ];
753               push @formats, [ 0x12, 36892.521,  0,         'h:mm AM/PM' ];
754               push @formats, [ 0x13, 36892.521,  0,         'h:mm:ss AM/PM' ];
755               push @formats, [ 0x14, 36892.521,  0,         'h:mm' ];
756               push @formats, [ 0x15, 36892.521,  0,         'h:mm:ss' ];
757               push @formats, [ 0x16, 36892.521,  0,         'm/d/yy h:mm' ];
758               push @formats, [ 0x25, 1234.567,   -1234.567, '(#,##0_);(#,##0)' ];
759               push @formats, [ 0x26, 1234.567,   -1234.567, '(#,##0_);[Red](#,##0)' ];
760               push @formats, [ 0x27, 1234.567,   -1234.567, '(#,##0.00_);(#,##0.00)' ];
761               push @formats, [ 0x28, 1234.567,   -1234.567, '(#,##0.00_);[Red](#,##0.00)' ];
762               push @formats, [ 0x29, 1234.567,   -1234.567, '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)' ];
763               push @formats, [ 0x2a, 1234.567,   -1234.567, '_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)' ];
764               push @formats, [ 0x2b, 1234.567,   -1234.567, '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' ];
765               push @formats, [ 0x2c, 1234.567,   -1234.567, '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)' ];
766               push @formats, [ 0x2d, 36892.521,  0,         'mm:ss' ];
767               push @formats, [ 0x2e, 3.0153,     0,         '[h]:mm:ss' ];
768               push @formats, [ 0x2f, 36892.521,  0,         'mm:ss.0' ];
769               push @formats, [ 0x30, 1234.567,   0,         '##0.0E+0' ];
770               push @formats, [ 0x31, 1234.567,   0,         '@' ];
771
772               my $i;
773               foreach my $format (@formats){
774                   my $style = $workbook->add_format();
775                   $style->set_num_format($format->[0]);
776
777                   $i++;
778                   $worksheet->write($i, 0, $format->[0],                    $center);
779                   $worksheet->write($i, 1, sprintf("0x%02X", $format->[0]), $center);
780                   $worksheet->write($i, 2, $format->[1],                    $center);
781                   $worksheet->write($i, 3, $format->[1],                    $style);
782
783                   if ($format->[2]) {
784                       $worksheet->write($i, 4, $format->[2], $style);
785                   }
786
787                   $worksheet->write_string($i, 5, $format->[3]);
788               }
789           }
790
791
792           ######################################################################
793           #
794           # Demonstrate the font options.
795           #
796           sub fonts {
797
798               my $worksheet = $workbook->add_worksheet('Fonts');
799
800               $worksheet->set_column(0, 0, 30);
801               $worksheet->set_column(1, 1, 10);
802
803               $worksheet->write(0, 0, "Font name",   $heading);
804               $worksheet->write(0, 1, "Font size",   $heading);
805
806               my @fonts;
807               push @fonts, [ 10, 'Arial' ];
808               push @fonts, [ 12, 'Arial' ];
809               push @fonts, [ 14, 'Arial' ];
810               push @fonts, [ 12, 'Arial Black' ];
811               push @fonts, [ 12, 'Arial Narrow' ];
812               push @fonts, [ 12, 'Century Schoolbook' ];
813               push @fonts, [ 12, 'Courier' ];
814               push @fonts, [ 12, 'Courier New' ];
815               push @fonts, [ 12, 'Garamond' ];
816               push @fonts, [ 12, 'Impact' ];
817               push @fonts, [ 12, 'Lucida Handwriting'] ;
818               push @fonts, [ 12, 'Times New Roman' ];
819               push @fonts, [ 12, 'Symbol' ];
820               push @fonts, [ 12, 'Wingdings' ];
821               push @fonts, [ 12, 'A font that doesn\'t exist' ];
822
823               my $i;
824               foreach my $font (@fonts){
825                   my $format = $workbook->add_format();
826
827                   $format->set_size($font->[0]);
828                   $format->set_font($font->[1]);
829
830                   $i++;
831                   $worksheet->write($i, 0, $font->[1], $format);
832                   $worksheet->write($i, 1, $font->[0], $format);
833               }
834
835           }
836
837
838           ######################################################################
839           #
840           # Demonstrate the standard Excel border styles.
841           #
842           sub borders {
843
844               my $worksheet = $workbook->add_worksheet('Borders');
845
846               $worksheet->set_column(0, 4, 10);
847               $worksheet->set_column(5, 5, 40);
848
849               $worksheet->write(0, 0, "Index", $heading);
850               $worksheet->write(0, 1, "Index", $heading);
851               $worksheet->write(0, 3, "Style", $heading);
852               $worksheet->write(0, 5, "The style is highlighted in red for ", $heading);
853               $worksheet->write(1, 5, "emphasis, the default color is black.", $heading);
854
855               for my $i (0..13){
856                   my $format = $workbook->add_format();
857                   $format->set_border($i);
858                   $format->set_border_color('red');
859                   $format->set_align('center');
860
861                   $worksheet->write((2*($i+1)), 0, $i,                    $center);
862                   $worksheet->write((2*($i+1)), 1, sprintf("0x%02X", $i), $center);
863
864                   $worksheet->write((2*($i+1)), 3, "Border", $format);
865               }
866
867               $worksheet->write(30, 0, "Diag type", $heading);
868               $worksheet->write(30, 1, "Index", $heading);
869               $worksheet->write(30, 3, "Style", $heading);
870               $worksheet->write(30, 5, "Diagonal Boder styles", $heading);
871
872               for my $i (1..3){
873                   my $format = $workbook->add_format();
874                   $format->set_diag_type($i);
875                   $format->set_diag_border(1);
876                   $format->set_diag_color('red');
877                   $format->set_align('center');
878
879                   $worksheet->write((2*($i+15)), 0, $i,                     $center);
880                   $worksheet->write((2*($i+15)), 1, sprintf("0x%02X", $i),  $center);
881
882                   $worksheet->write((2*($i+15)), 3, "Border", $format);
883               }
884           }
885
886
887
888           ######################################################################
889           #
890           # Demonstrate the standard Excel cell patterns.
891           #
892           sub patterns {
893
894               my $worksheet = $workbook->add_worksheet('Patterns');
895
896               $worksheet->set_column(0, 4, 10);
897               $worksheet->set_column(5, 5, 50);
898
899               $worksheet->write(0, 0, "Index", $heading);
900               $worksheet->write(0, 1, "Index", $heading);
901               $worksheet->write(0, 3, "Pattern", $heading);
902
903               $worksheet->write(0, 5, "The background colour has been set to silver.", $heading);
904               $worksheet->write(1, 5, "The foreground colour has been set to green.",  $heading);
905
906               for my $i (0..18){
907                   my $format = $workbook->add_format();
908
909                   $format->set_pattern($i);
910                   $format->set_bg_color('silver');
911                   $format->set_fg_color('green');
912                   $format->set_align('center');
913
914                   $worksheet->write((2*($i+1)), 0, $i,                    $center);
915                   $worksheet->write((2*($i+1)), 1, sprintf("0x%02X", $i), $center);
916
917                   $worksheet->write((2*($i+1)), 3, "Pattern", $format);
918
919                   if ($i == 1) {
920                       $worksheet->write((2*($i+1)), 5, "This is solid colour, the most useful pattern.", $heading);
921                   }
922               }
923           }
924
925
926           ######################################################################
927           #
928           # Demonstrate the standard Excel cell alignments.
929           #
930           sub alignment {
931
932               my $worksheet = $workbook->add_worksheet('Alignment');
933
934               $worksheet->set_column(0, 7, 12);
935               $worksheet->set_row(0, 40);
936               $worksheet->set_selection(7, 0);
937
938               my $format01 = $workbook->add_format();
939               my $format02 = $workbook->add_format();
940               my $format03 = $workbook->add_format();
941               my $format04 = $workbook->add_format();
942               my $format05 = $workbook->add_format();
943               my $format06 = $workbook->add_format();
944               my $format07 = $workbook->add_format();
945               my $format08 = $workbook->add_format();
946               my $format09 = $workbook->add_format();
947               my $format10 = $workbook->add_format();
948               my $format11 = $workbook->add_format();
949               my $format12 = $workbook->add_format();
950               my $format13 = $workbook->add_format();
951               my $format14 = $workbook->add_format();
952               my $format15 = $workbook->add_format();
953               my $format16 = $workbook->add_format();
954               my $format17 = $workbook->add_format();
955
956               $format02->set_align('top');
957               $format03->set_align('bottom');
958               $format04->set_align('vcenter');
959               $format05->set_align('vjustify');
960               $format06->set_text_wrap();
961
962               $format07->set_align('left');
963               $format08->set_align('right');
964               $format09->set_align('center');
965               $format10->set_align('fill');
966               $format11->set_align('justify');
967               $format12->set_merge();
968
969               $format13->set_rotation(45);
970               $format14->set_rotation(-45);
971               $format15->set_rotation(270);
972
973               $format16->set_shrink();
974               $format17->set_indent(1);
975
976               $worksheet->write(0, 0, 'Vertical',     $heading);
977               $worksheet->write(0, 1, 'top',          $format02);
978               $worksheet->write(0, 2, 'bottom',       $format03);
979               $worksheet->write(0, 3, 'vcenter',      $format04);
980               $worksheet->write(0, 4, 'vjustify',     $format05);
981               $worksheet->write(0, 5, "text\nwrap",   $format06);
982
983               $worksheet->write(2, 0, 'Horizontal',   $heading);
984               $worksheet->write(2, 1, 'left',         $format07);
985               $worksheet->write(2, 2, 'right',        $format08);
986               $worksheet->write(2, 3, 'center',       $format09);
987               $worksheet->write(2, 4, 'fill',         $format10);
988               $worksheet->write(2, 5, 'justify',      $format11);
989
990               $worksheet->write(3, 1, 'merge',        $format12);
991               $worksheet->write(3, 2, '',             $format12);
992
993               $worksheet->write(3, 3, 'Shrink ' x 3,  $format16);
994               $worksheet->write(3, 4, 'Indent',       $format17);
995
996
997               $worksheet->write(5, 0, 'Rotation',     $heading);
998               $worksheet->write(5, 1, 'Rotate 45',    $format13);
999               $worksheet->write(6, 1, 'Rotate -45',   $format14);
1000               $worksheet->write(7, 1, 'Rotate 270',   $format15);
1001           }
1002
1003
1004           ######################################################################
1005           #
1006           # Demonstrate other miscellaneous features.
1007           #
1008           sub misc {
1009
1010               my $worksheet = $workbook->add_worksheet('Miscellaneous');
1011
1012               $worksheet->set_column(2, 2, 25);
1013
1014               my $format01 = $workbook->add_format();
1015               my $format02 = $workbook->add_format();
1016               my $format03 = $workbook->add_format();
1017               my $format04 = $workbook->add_format();
1018               my $format05 = $workbook->add_format();
1019               my $format06 = $workbook->add_format();
1020               my $format07 = $workbook->add_format();
1021
1022               $format01->set_underline(0x01);
1023               $format02->set_underline(0x02);
1024               $format03->set_underline(0x21);
1025               $format04->set_underline(0x22);
1026               $format05->set_font_strikeout();
1027               $format06->set_font_outline();
1028               $format07->set_font_shadow();
1029
1030               $worksheet->write(1,  2, 'Underline  0x01',          $format01);
1031               $worksheet->write(3,  2, 'Underline  0x02',          $format02);
1032               $worksheet->write(5,  2, 'Underline  0x21',          $format03);
1033               $worksheet->write(7,  2, 'Underline  0x22',          $format04);
1034               $worksheet->write(9,  2, 'Strikeout',                $format05);
1035               $worksheet->write(11, 2, 'Outline (Macintosh only)', $format06);
1036               $worksheet->write(13, 2, 'Shadow (Macintosh only)',  $format07);
1037           }
1038
1039           __END__
1040
1041       Download this example:
1042       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/formats.pl>
1043
1044   Example: bug_report.pl
1045       A template for submitting a bug report.
1046
1047       Run this program and read the output from the command line.
1048
1049           #!/usr/bin/perl -w
1050
1051
1052           ###############################################################################
1053           #
1054           # A template for submitting a bug report.
1055           #
1056           # Run this program and read the output from the command line.
1057           #
1058           # reverse('(c)'), March 2004, John McNamara, jmcnamara@cpan.org
1059           #
1060
1061
1062           use strict;
1063
1064           print << 'HINTS_1';
1065
1066           REPORTING A BUG OR ASKING A QUESTION
1067
1068               Feel free to report bugs or ask questions. However, to save time
1069               consider the following steps first:
1070
1071               Read the documentation:
1072
1073                   The Spreadsheet::WriteExcel documentation has been refined in
1074                   response to user questions. Therefore, if you have a question it is
1075                   possible that someone else has asked it before you and that it is
1076                   already addressed in the documentation. Since there is a lot of
1077                   documentation to get through you should at least read the table of
1078                   contents and search for keywords that you are interested in.
1079
1080               Look at the example programs:
1081
1082                   There are over 70 example programs shipped with the standard
1083                   Spreadsheet::WriteExcel distribution. Many of these were created in
1084                   response to user questions. Try to identify an example program that
1085                   corresponds to your query and adapt it to your needs.
1086
1087           HINTS_1
1088           print "Press enter ..."; <STDIN>;
1089
1090           print << 'HINTS_2';
1091
1092               If you submit a bug report here are some pointers.
1093
1094               1.  Put "WriteExcel:" at the beginning of the subject line. This helps
1095                   to filter genuine messages from spam.
1096
1097               2.  Describe the problems as clearly and as concisely as possible.
1098
1099               3.  Send a sample program. It is often easier to describe a problem in
1100                   code than in written prose.
1101
1102               4.  The sample program should be as small as possible to demonstrate the
1103                   problem. Don't copy and past large sections of your program. The
1104                   program should also be self contained and working.
1105
1106               A sample bug report is generated below. If you use this format then it
1107               will help to analyse your question and respond to it more quickly.
1108
1109               Please don't send patches without contacting the author first.
1110
1111
1112           HINTS_2
1113           print "Press enter ..."; <STDIN>;
1114
1115
1116           print << 'EMAIL';
1117
1118           =======================================================================
1119
1120           To:      John McNamara <jmcnamara@cpan.org>
1121           Subject: WriteExcel: Problem with something.
1122
1123           Hi John,
1124
1125           I am using Spreadsheet::WriteExcel and I have encountered a problem. I
1126           want it to do SOMETHING but the module appears to do SOMETHING_ELSE.
1127
1128           Here is some code that demonstrates the problem.
1129
1130               #!/usr/bin/perl -w
1131
1132               use strict;
1133               use Spreadsheet::WriteExcel;
1134
1135               my $workbook  = Spreadsheet::WriteExcel->new("reload.xls");
1136               my $worksheet = $workbook->add_worksheet();
1137
1138               $worksheet->write(0, 0, "Hi Excel!");
1139
1140               __END__
1141
1142
1143           I tested using Excel XX (or Gnumeric or OpenOffice.org).
1144
1145           My automatically generated system details are as follows:
1146           EMAIL
1147
1148
1149           print "\n    Perl version   : $]";
1150           print "\n    OS name        : $^O";
1151           print "\n    Module versions: (not all are required)\n";
1152
1153
1154           my @modules = qw(
1155                             Spreadsheet::WriteExcel
1156                             Spreadsheet::ParseExcel
1157                             OLE::Storage_Lite
1158                             Parse::RecDescent
1159                             File::Temp
1160                             Digest::MD4
1161                             Digest::Perl::MD4
1162                             Digest::MD5
1163                           );
1164
1165
1166           for my $module (@modules) {
1167               my $version;
1168               eval "require $module";
1169
1170               if (not $@) {
1171                   $version = $module->VERSION;
1172                   $version = '(unknown)' if not defined $version;
1173               }
1174               else {
1175                   $version = '(not installed)';
1176               }
1177
1178               printf "%21s%-24s\t%s\n", "", $module, $version;
1179           }
1180
1181
1182           print << "BYE";
1183           Yours etc.,
1184
1185           A. Person
1186           --
1187
1188           BYE
1189
1190           __END__
1191
1192       Download this example:
1193       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/bug_report.pl>
1194
1195   Example: autofilter.pl
1196       An example of how to create autofilters with Spreadsheet::WriteExcel.
1197
1198       An autofilter is a way of adding drop down lists to the headers of a 2D
1199       range of worksheet data. This is turn allow users to filter the data
1200       based on simple criteria so that some data is shown and some is hidden.
1201
1202       Source code for this example:
1203
1204           #!/usr/bin/perl -w
1205
1206           ###############################################################################
1207           #
1208           # An example of how to create autofilters with Spreadsheet::WriteExcel.
1209           #
1210           # An autofilter is a way of adding drop down lists to the headers of a 2D range
1211           # of worksheet data. This is turn allow users to filter the data based on
1212           # simple criteria so that some data is shown and some is hidden.
1213           #
1214           # reverse('(c)'), September 2007, John McNamara, jmcnamara@cpan.org
1215           #
1216
1217           use strict;
1218           use Spreadsheet::WriteExcel;
1219
1220           my $workbook   = Spreadsheet::WriteExcel->new('autofilter.xls');
1221
1222           die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
1223
1224           my $worksheet1 = $workbook->add_worksheet();
1225           my $worksheet2 = $workbook->add_worksheet();
1226           my $worksheet3 = $workbook->add_worksheet();
1227           my $worksheet4 = $workbook->add_worksheet();
1228           my $worksheet5 = $workbook->add_worksheet();
1229           my $worksheet6 = $workbook->add_worksheet();
1230
1231           my $bold       = $workbook->add_format(bold => 1);
1232
1233
1234           # Extract the data embedded at the end of this file.
1235           my @headings = split ' ', <DATA>;
1236           my @data;
1237           push @data, [split] while <DATA>;
1238
1239
1240           # Set up several sheets with the same data.
1241           for my $worksheet ($workbook->sheets()) {
1242               $worksheet->set_column('A:D', 12);
1243               $worksheet->set_row(0, 20, $bold);
1244               $worksheet->write('A1', \@headings);
1245           }
1246
1247
1248           ###############################################################################
1249           #
1250           # Example 1. Autofilter without conditions.
1251           #
1252
1253           $worksheet1->autofilter('A1:D51');
1254           $worksheet1->write('A2', [[@data]]);
1255
1256
1257           ###############################################################################
1258           #
1259           #
1260           # Example 2. Autofilter with a filter condition in the first column.
1261           #
1262
1263           # The range in this example is the same as above but in row-column notation.
1264           $worksheet2->autofilter(0, 0, 50, 3);
1265
1266           # The placeholder "Region" in the filter is ignored and can be any string
1267           # that adds clarity to the expression.
1268           #
1269           $worksheet2->filter_column(0, 'Region eq East');
1270
1271           #
1272           # Hide the rows that don't match the filter criteria.
1273           #
1274           my $row = 1;
1275
1276           for my $row_data (@data) {
1277               my $region = $row_data->[0];
1278
1279               if ($region eq 'East') {
1280                   # Row is visible.
1281               }
1282               else {
1283                   # Hide row.
1284                   $worksheet2->set_row($row, undef, undef, 1);
1285               }
1286
1287               $worksheet2->write($row++, 0, $row_data);
1288           }
1289
1290
1291           ###############################################################################
1292           #
1293           #
1294           # Example 3. Autofilter with a dual filter condition in one of the columns.
1295           #
1296
1297           $worksheet3->autofilter('A1:D51');
1298
1299           $worksheet3->filter_column('A', 'x eq East or x eq South');
1300
1301           #
1302           # Hide the rows that don't match the filter criteria.
1303           #
1304           $row = 1;
1305
1306           for my $row_data (@data) {
1307               my $region = $row_data->[0];
1308
1309               if ($region eq 'East' or $region eq 'South') {
1310                   # Row is visible.
1311               }
1312               else {
1313                   # Hide row.
1314                   $worksheet3->set_row($row, undef, undef, 1);
1315               }
1316
1317               $worksheet3->write($row++, 0, $row_data);
1318           }
1319
1320
1321           ###############################################################################
1322           #
1323           #
1324           # Example 4. Autofilter with filter conditions in two columns.
1325           #
1326
1327           $worksheet4->autofilter('A1:D51');
1328
1329           $worksheet4->filter_column('A', 'x eq East');
1330           $worksheet4->filter_column('C', 'x > 3000 and x < 8000' );
1331
1332           #
1333           # Hide the rows that don't match the filter criteria.
1334           #
1335           $row = 1;
1336
1337           for my $row_data (@data) {
1338               my $region = $row_data->[0];
1339               my $volume = $row_data->[2];
1340
1341               if ($region eq 'East' and
1342                   $volume >  3000   and $volume < 8000
1343               )
1344               {
1345                   # Row is visible.
1346               }
1347               else {
1348                   # Hide row.
1349                   $worksheet4->set_row($row, undef, undef, 1);
1350               }
1351
1352               $worksheet4->write($row++, 0, $row_data);
1353           }
1354
1355
1356           ###############################################################################
1357           #
1358           #
1359           # Example 5. Autofilter with filter for blanks.
1360           #
1361
1362           # Create a blank cell in our test data.
1363           $data[5]->[0] = '';
1364
1365
1366           $worksheet5->autofilter('A1:D51');
1367           $worksheet5->filter_column('A', 'x == Blanks');
1368
1369           #
1370           # Hide the rows that don't match the filter criteria.
1371           #
1372           $row = 1;
1373
1374           for my $row_data (@data) {
1375               my $region = $row_data->[0];
1376
1377               if ($region eq '')
1378               {
1379                   # Row is visible.
1380               }
1381               else {
1382                   # Hide row.
1383                   $worksheet5->set_row($row, undef, undef, 1);
1384               }
1385
1386               $worksheet5->write($row++, 0, $row_data);
1387           }
1388
1389
1390           ###############################################################################
1391           #
1392           #
1393           # Example 6. Autofilter with filter for non-blanks.
1394           #
1395
1396
1397           $worksheet6->autofilter('A1:D51');
1398           $worksheet6->filter_column('A', 'x == NonBlanks');
1399
1400           #
1401           # Hide the rows that don't match the filter criteria.
1402           #
1403           $row = 1;
1404
1405           for my $row_data (@data) {
1406               my $region = $row_data->[0];
1407
1408               if ($region ne '')
1409               {
1410                   # Row is visible.
1411               }
1412               else {
1413                   # Hide row.
1414                   $worksheet6->set_row($row, undef, undef, 1);
1415               }
1416
1417               $worksheet6->write($row++, 0, $row_data);
1418           }
1419
1420
1421
1422           __DATA__
1423           Region    Item      Volume    Month
1424           East      Apple     9000      July
1425           East      Apple     5000      July
1426           South     Orange    9000      September
1427           North     Apple     2000      November
1428           West      Apple     9000      November
1429           South     Pear      7000      October
1430           North     Pear      9000      August
1431           West      Orange    1000      December
1432           West      Grape     1000      November
1433           South     Pear      10000     April
1434           West      Grape     6000      January
1435           South     Orange    3000      May
1436           North     Apple     3000      December
1437           South     Apple     7000      February
1438           West      Grape     1000      December
1439           East      Grape     8000      February
1440           South     Grape     10000     June
1441           West      Pear      7000      December
1442           South     Apple     2000      October
1443           East      Grape     7000      December
1444           North     Grape     6000      April
1445           East      Pear      8000      February
1446           North     Apple     7000      August
1447           North     Orange    7000      July
1448           North     Apple     6000      June
1449           South     Grape     8000      September
1450           West      Apple     3000      October
1451           South     Orange    10000     November
1452           West      Grape     4000      July
1453           North     Orange    5000      August
1454           East      Orange    1000      November
1455           East      Orange    4000      October
1456           North     Grape     5000      August
1457           East      Apple     1000      December
1458           South     Apple     10000     March
1459           East      Grape     7000      October
1460           West      Grape     1000      September
1461           East      Grape     10000     October
1462           South     Orange    8000      March
1463           North     Apple     4000      July
1464           South     Orange    5000      July
1465           West      Apple     4000      June
1466           East      Apple     5000      April
1467           North     Pear      3000      August
1468           East      Grape     9000      November
1469           North     Orange    8000      October
1470           East      Apple     10000     June
1471           South     Pear      1000      December
1472           North     Grape     10000     July
1473           East      Grape     6000      February
1474
1475       Download this example:
1476       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/autofilter.pl>
1477
1478   Example: autofit.pl
1479       Simulate Excel's autofit for column widths.
1480
1481       Excel provides a function called Autofit (Format->Columns->Autofit)
1482       that adjusts column widths to match the length of the longest string in
1483       a column.  Excel calculates these widths at run time when it has access
1484       to information about string lengths and font information. This function
1485       is *not* a feature of the file format and thus cannot be implemented by
1486       Spreadsheet::WriteExcel.
1487
1488       However, we can make an attempt to simulate it by keeping track of the
1489       longest string written to each column and then adjusting the column
1490       widths prior to closing the file.
1491
1492       We keep track of the longest strings by adding a handler to the write()
1493       function. See add_handler() in the S::WE docs for more information.
1494
1495       The main problem with trying to simulate Autofit lies in defining a
1496       relationship between a string length and its width in a arbitrary font
1497       and size. We use two approaches below. The first is a simple direct
1498       relationship obtained by trial and error. The second is a slightly more
1499       sophisticated method using an external module. For more complicated
1500       applications you will probably have to work out your own methods.
1501
1502       Source code for this example:
1503
1504           #!/usr/bin/perl -w
1505
1506           ##############################################################################
1507           #
1508           # Simulate Excel's autofit for column widths.
1509           #
1510           # Excel provides a function called Autofit (Format->Columns->Autofit) that
1511           # adjusts column widths to match the length of the longest string in a column.
1512           # Excel calculates these widths at run time when it has access to information
1513           # about string lengths and font information. This function is *not* a feature
1514           # of the file format and thus cannot be implemented by Spreadsheet::WriteExcel.
1515           #
1516           # However, we can make an attempt to simulate it by keeping track of the
1517           # longest string written to each column and then adjusting the column widths
1518           # prior to closing the file.
1519           #
1520           # We keep track of the longest strings by adding a handler to the write()
1521           # function. See add_handler() in the S::WE docs for more information.
1522           #
1523           # The main problem with trying to simulate Autofit lies in defining a
1524           # relationship between a string length and its width in a arbitrary font and
1525           # size. We use two approaches below. The first is a simple direct relationship
1526           # obtained by trial and error. The second is a slightly more sophisticated
1527           # method using an external module. For more complicated applications you will
1528           # probably have to work out your own methods.
1529           #
1530           # reverse('(c)'), May 2006, John McNamara, jmcnamara@cpan.org
1531           #
1532
1533           use strict;
1534           use Spreadsheet::WriteExcel;
1535
1536           my $workbook    = Spreadsheet::WriteExcel->new('autofit.xls');
1537           my $worksheet   = $workbook->add_worksheet();
1538
1539
1540           ###############################################################################
1541           #
1542           # Add a handler to store the width of the longest string written to a column.
1543           # We use the stored width to simulate an autofit of the column widths.
1544           #
1545           # You should do this for every worksheet you want to autofit.
1546           #
1547           $worksheet->add_write_handler(qr[\w], \&store_string_widths);
1548
1549
1550
1551           $worksheet->write('A1', 'Hello');
1552           $worksheet->write('B1', 'Hello World');
1553           $worksheet->write('D1', 'Hello');
1554           $worksheet->write('F1', 'This is a long string as an example.');
1555
1556           # Run the autofit after you have finished writing strings to the workbook.
1557           autofit_columns($worksheet);
1558
1559
1560
1561           ###############################################################################
1562           #
1563           # Functions used for Autofit.
1564           #
1565           ###############################################################################
1566
1567           ###############################################################################
1568           #
1569           # Adjust the column widths to fit the longest string in the column.
1570           #
1571           sub autofit_columns {
1572
1573               my $worksheet = shift;
1574               my $col       = 0;
1575
1576               for my $width (@{$worksheet->{__col_widths}}) {
1577
1578                   $worksheet->set_column($col, $col, $width) if $width;
1579                   $col++;
1580               }
1581           }
1582
1583
1584           ###############################################################################
1585           #
1586           # The following function is a callback that was added via add_write_handler()
1587           # above. It modifies the write() function so that it stores the maximum
1588           # unwrapped width of a string in a column.
1589           #
1590           sub store_string_widths {
1591
1592               my $worksheet = shift;
1593               my $col       = $_[1];
1594               my $token     = $_[2];
1595
1596               # Ignore some tokens that we aren't interested in.
1597               return if not defined $token;       # Ignore undefs.
1598               return if $token eq '';             # Ignore blank cells.
1599               return if ref $token eq 'ARRAY';    # Ignore array refs.
1600               return if $token =~ /^=/;           # Ignore formula
1601
1602               # Ignore numbers
1603               return if $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
1604
1605               # Ignore various internal and external hyperlinks. In a real scenario
1606               # you may wish to track the length of the optional strings used with
1607               # urls.
1608               return if $token =~ m{^[fh]tt?ps?://};
1609               return if $token =~ m{^mailto:};
1610               return if $token =~ m{^(?:in|ex)ternal:};
1611
1612
1613               # We store the string width as data in the Worksheet object. We use
1614               # a double underscore key name to avoid conflicts with future names.
1615               #
1616               my $old_width    = $worksheet->{__col_widths}->[$col];
1617               my $string_width = string_width($token);
1618
1619               if (not defined $old_width or $string_width > $old_width) {
1620                   # You may wish to set a minimum column width as follows.
1621                   #return undef if $string_width < 10;
1622
1623                   $worksheet->{__col_widths}->[$col] = $string_width;
1624               }
1625
1626
1627               # Return control to write();
1628               return undef;
1629           }
1630
1631
1632           ###############################################################################
1633           #
1634           # Very simple conversion between string length and string width for Arial 10.
1635           # See below for a more sophisticated method.
1636           #
1637           sub string_width {
1638
1639               return 0.9 * length $_[0];
1640           }
1641
1642           __END__
1643
1644
1645
1646           ###############################################################################
1647           #
1648           # This function uses an external module to get a more accurate width for a
1649           # string. Note that in a real program you could "use" the module instead of
1650           # "require"-ing it and you could make the Font object global to avoid repeated
1651           # initialisation.
1652           #
1653           # Note also that the $pixel_width to $cell_width is specific to Arial. For
1654           # other fonts you should calculate appropriate relationships. A future version
1655           # of S::WE will provide a way of specifying column widths in pixels instead of
1656           # cell units in order to simplify this conversion.
1657           #
1658           sub string_width {
1659
1660               require Font::TTFMetrics;
1661
1662               my $arial        = Font::TTFMetrics->new('c:\windows\fonts\arial.ttf');
1663
1664               my $font_size    = 10;
1665               my $dpi          = 96;
1666               my $units_per_em = $arial->get_units_per_em();
1667               my $font_width   = $arial->string_width($_[0]);
1668
1669               # Convert to pixels as per TTFMetrics docs.
1670               my $pixel_width  = 6 + $font_width *$font_size *$dpi /(72 *$units_per_em);
1671
1672               # Add extra pixels for border around text.
1673               $pixel_width  += 6;
1674
1675               # Convert to cell width (for Arial) and for cell widths > 1.
1676               my $cell_width   = ($pixel_width -5) /7;
1677
1678               return $cell_width;
1679
1680           }
1681
1682           __END__
1683
1684       Download this example:
1685       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/autofit.pl>
1686
1687   Example: bigfile.pl
1688       Example of creating a Spreadsheet::WriteExcel that is larger than the
1689       default 7MB limit.
1690
1691       This is exactly that same as any other Spreadsheet::WriteExcel program
1692       except that is requires that the OLE::Storage module is installed.
1693
1694       Source code for this example:
1695
1696           #!/usr/bin/perl -w
1697
1698           ###############################################################################
1699           #
1700           # Example of creating a Spreadsheet::WriteExcel that is larger than the
1701           # default 7MB limit.
1702           #
1703           # This is exactly that same as any other Spreadsheet::WriteExcel program except
1704           # that is requires that the OLE::Storage module is installed.
1705           #
1706           # reverse('(c)'), Jan 2007, John McNamara, jmcnamara@cpan.org
1707
1708
1709           use strict;
1710           use Spreadsheet::WriteExcel;
1711
1712
1713           my $workbook  = Spreadsheet::WriteExcel->new('bigfile.xls');
1714           my $worksheet = $workbook->add_worksheet();
1715
1716           $worksheet->set_column(0, 50, 18);
1717
1718           for my $col (0 .. 50) {
1719               for my $row (0 .. 6000) {
1720                   $worksheet->write($row, $col, "Row: $row Col: $col");
1721               }
1722           }
1723
1724           __END__
1725
1726       Download this example:
1727       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/bigfile.pl>
1728
1729   Example: cgi.pl
1730       Example of how to use the Spreadsheet::WriteExcel module to send an
1731       Excel file to a browser in a CGI program.
1732
1733       On Windows the hash-bang line should be something like:
1734
1735           #!C:\Perl\bin\perl.exe
1736
1737       The "Content-Disposition" line will cause a prompt to be generated to
1738       save the file. If you want to stream the file to the browser instead,
1739       comment out that line as shown below.
1740
1741           #!/usr/bin/perl -w
1742
1743           ###############################################################################
1744           #
1745           # Example of how to use the Spreadsheet::WriteExcel module to send an Excel
1746           # file to a browser in a CGI program.
1747           #
1748           # On Windows the hash-bang line should be something like:
1749           #
1750           #     #!C:\Perl\bin\perl.exe
1751           #
1752           # The "Content-Disposition" line will cause a prompt to be generated to save
1753           # the file. If you want to stream the file to the browser instead, comment out
1754           # that line as shown below.
1755           #
1756           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
1757           #
1758
1759           use strict;
1760           use Spreadsheet::WriteExcel;
1761
1762           # Set the filename and send the content type
1763           my $filename ="cgitest.xls";
1764
1765           print "Content-type: application/vnd.ms-excel\n";
1766           # The Content-Disposition will generate a prompt to save the file. If you want
1767           # to stream the file to the browser, comment out the following line.
1768           print "Content-Disposition: attachment; filename=$filename\n";
1769           print "\n";
1770
1771           # Create a new workbook and add a worksheet. The special Perl filehandle - will
1772           # redirect the output to STDOUT
1773           #
1774           my $workbook  = Spreadsheet::WriteExcel->new(\*STDOUT);
1775           my $worksheet = $workbook->add_worksheet();
1776
1777
1778           # Set the column width for column 1
1779           $worksheet->set_column(0, 0, 20);
1780
1781
1782           # Create a format
1783           my $format = $workbook->add_format();
1784           $format->set_bold();
1785           $format->set_size(15);
1786           $format->set_color('blue');
1787
1788
1789           # Write to the workbook
1790           $worksheet->write(0, 0, "Hi Excel!", $format);
1791
1792           __END__
1793
1794       Download this example:
1795       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/cgi.pl>
1796
1797   Example: chart_area.pl
1798       A simple demo of Area charts in Spreadsheet::WriteExcel.
1799
1800       Source code for this example:
1801
1802           #!/usr/bin/perl -w
1803
1804           ###############################################################################
1805           #
1806           # A simple demo of Area charts in Spreadsheet::WriteExcel.
1807           #
1808           # reverse('(c)'), December 2009, John McNamara, jmcnamara@cpan.org
1809           #
1810
1811           use strict;
1812           use Spreadsheet::WriteExcel;
1813
1814           my $workbook  = Spreadsheet::WriteExcel->new( 'chart_area.xls' );
1815           my $worksheet = $workbook->add_worksheet();
1816           my $bold      = $workbook->add_format( bold => 1 );
1817
1818           # Add the worksheet data that the charts will refer to.
1819           my $headings = [ 'Category', 'Values 1', 'Values 2' ];
1820           my $data = [
1821               [ 2, 3, 4, 5, 6, 7 ],
1822               [ 1, 4, 5, 2, 1, 5 ],
1823               [ 3, 6, 7, 5, 4, 3 ],
1824           ];
1825
1826           $worksheet->write( 'A1', $headings, $bold );
1827           $worksheet->write( 'A2', $data );
1828
1829
1830           ###############################################################################
1831           #
1832           # Example 1. A minimal chart.
1833           #
1834           my $chart1 = $workbook->add_chart( type => 'area' );
1835
1836           # Add values only. Use the default categories.
1837           $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
1838
1839
1840           ###############################################################################
1841           #
1842           # Example 2. A minimal chart with user specified categories (X axis)
1843           #            and a series name.
1844           #
1845           my $chart2 = $workbook->add_chart( type => 'area' );
1846
1847           # Configure the series.
1848           $chart2->add_series(
1849               categories => '=Sheet1!$A$2:$A$7',
1850               values     => '=Sheet1!$B$2:$B$7',
1851               name       => 'Test data series 1',
1852           );
1853
1854
1855           ###############################################################################
1856           #
1857           # Example 3. Same as previous chart but with added title and axes labels.
1858           #
1859           my $chart3 = $workbook->add_chart( type => 'area' );
1860
1861           # Configure the series.
1862           $chart3->add_series(
1863               categories => '=Sheet1!$A$2:$A$7',
1864               values     => '=Sheet1!$B$2:$B$7',
1865               name       => 'Test data series 1',
1866           );
1867
1868           # Add some labels.
1869           $chart3->set_title( name => 'Results of sample analysis' );
1870           $chart3->set_x_axis( name => 'Sample number' );
1871           $chart3->set_y_axis( name => 'Sample length (cm)' );
1872
1873
1874           ###############################################################################
1875           #
1876           # Example 4. Same as previous chart but with an added series and with a
1877           #            user specified chart sheet name.
1878           #
1879           my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'area' );
1880
1881           # Configure the series.
1882           $chart4->add_series(
1883               categories => '=Sheet1!$A$2:$A$7',
1884               values     => '=Sheet1!$B$2:$B$7',
1885               name       => 'Test data series 1',
1886           );
1887
1888           # Add another series.
1889           $chart4->add_series(
1890               categories => '=Sheet1!$A$2:$A$7',
1891               values     => '=Sheet1!$C$2:$C$7',
1892               name       => 'Test data series 2',
1893           );
1894
1895           # Add some labels.
1896           $chart4->set_title( name => 'Results of sample analysis' );
1897           $chart4->set_x_axis( name => 'Sample number' );
1898           $chart4->set_y_axis( name => 'Sample length (cm)' );
1899
1900
1901           ###############################################################################
1902           #
1903           # Example 5. Same as Example 3 but as an embedded chart.
1904           #
1905           my $chart5 = $workbook->add_chart( type => 'area', embedded => 1 );
1906
1907           # Configure the series.
1908           $chart5->add_series(
1909               categories => '=Sheet1!$A$2:$A$7',
1910               values     => '=Sheet1!$B$2:$B$7',
1911               name       => 'Test data series 1',
1912           );
1913
1914           # Add some labels.
1915           $chart5->set_title( name => 'Results of sample analysis' );
1916           $chart5->set_x_axis( name => 'Sample number' );
1917           $chart5->set_y_axis( name => 'Sample length (cm)' );
1918
1919           # Insert the chart into the main worksheet.
1920           $worksheet->insert_chart( 'E2', $chart5 );
1921
1922           __END__
1923
1924       Download this example:
1925       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/chart_area.pl>
1926
1927   Example: chart_bar.pl
1928       A simple demo of Bar charts in Spreadsheet::WriteExcel.
1929
1930       Source code for this example:
1931
1932           #!/usr/bin/perl -w
1933
1934           ###############################################################################
1935           #
1936           # A simple demo of Bar charts in Spreadsheet::WriteExcel.
1937           #
1938           # reverse('(c)'), December 2009, John McNamara, jmcnamara@cpan.org
1939           #
1940
1941           use strict;
1942           use Spreadsheet::WriteExcel;
1943
1944           my $workbook  = Spreadsheet::WriteExcel->new( 'chart_bar.xls' );
1945           my $worksheet = $workbook->add_worksheet();
1946           my $bold      = $workbook->add_format( bold => 1 );
1947
1948           # Add the worksheet data that the charts will refer to.
1949           my $headings = [ 'Category', 'Values 1', 'Values 2' ];
1950           my $data = [
1951               [ 2, 3, 4, 5, 6, 7 ],
1952               [ 1, 4, 5, 2, 1, 5 ],
1953               [ 3, 6, 7, 5, 4, 3 ],
1954           ];
1955
1956           $worksheet->write( 'A1', $headings, $bold );
1957           $worksheet->write( 'A2', $data );
1958
1959
1960           ###############################################################################
1961           #
1962           # Example 1. A minimal chart.
1963           #
1964           my $chart1 = $workbook->add_chart( type => 'bar' );
1965
1966           # Add values only. Use the default categories.
1967           $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
1968
1969
1970           ###############################################################################
1971           #
1972           # Example 2. A minimal chart with user specified categories (X axis)
1973           #            and a series name.
1974           #
1975           my $chart2 = $workbook->add_chart( type => 'bar' );
1976
1977           # Configure the series.
1978           $chart2->add_series(
1979               categories => '=Sheet1!$A$2:$A$7',
1980               values     => '=Sheet1!$B$2:$B$7',
1981               name       => 'Test data series 1',
1982           );
1983
1984
1985           ###############################################################################
1986           #
1987           # Example 3. Same as previous chart but with added title and axes labels.
1988           #
1989           my $chart3 = $workbook->add_chart( type => 'bar' );
1990
1991           # Configure the series.
1992           $chart3->add_series(
1993               categories => '=Sheet1!$A$2:$A$7',
1994               values     => '=Sheet1!$B$2:$B$7',
1995               name       => 'Test data series 1',
1996           );
1997
1998           # Add some labels.
1999           $chart3->set_title( name => 'Results of sample analysis' );
2000           $chart3->set_x_axis( name => 'Sample number' );
2001           $chart3->set_y_axis( name => 'Sample length (cm)' );
2002
2003
2004           ###############################################################################
2005           #
2006           # Example 4. Same as previous chart but with an added series and with a
2007           #            user specified chart sheet name.
2008           #
2009           my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'bar' );
2010
2011           # Configure the series.
2012           $chart4->add_series(
2013               categories => '=Sheet1!$A$2:$A$7',
2014               values     => '=Sheet1!$B$2:$B$7',
2015               name       => 'Test data series 1',
2016           );
2017
2018           # Add another series.
2019           $chart4->add_series(
2020               categories => '=Sheet1!$A$2:$A$7',
2021               values     => '=Sheet1!$C$2:$C$7',
2022               name       => 'Test data series 2',
2023           );
2024
2025           # Add some labels.
2026           $chart4->set_title( name => 'Results of sample analysis' );
2027           $chart4->set_x_axis( name => 'Sample number' );
2028           $chart4->set_y_axis( name => 'Sample length (cm)' );
2029
2030
2031           ###############################################################################
2032           #
2033           # Example 5. Same as Example 3 but as an embedded chart.
2034           #
2035           my $chart5 = $workbook->add_chart( type => 'bar', embedded => 1 );
2036
2037           # Configure the series.
2038           $chart5->add_series(
2039               categories => '=Sheet1!$A$2:$A$7',
2040               values     => '=Sheet1!$B$2:$B$7',
2041               name       => 'Test data series 1',
2042           );
2043
2044           # Add some labels.
2045           $chart5->set_title( name => 'Results of sample analysis' );
2046           $chart5->set_x_axis( name => 'Sample number' );
2047           $chart5->set_y_axis( name => 'Sample length (cm)' );
2048
2049           # Insert the chart into the main worksheet.
2050           $worksheet->insert_chart( 'E2', $chart5 );
2051
2052           __END__
2053
2054       Download this example:
2055       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/chart_bar.pl>
2056
2057   Example: chart_column.pl
2058       A simple demo of Column charts in Spreadsheet::WriteExcel.
2059
2060       Source code for this example:
2061
2062           #!/usr/bin/perl -w
2063
2064           ###############################################################################
2065           #
2066           # A simple demo of Column charts in Spreadsheet::WriteExcel.
2067           #
2068           # reverse('(c)'), December 2009, John McNamara, jmcnamara@cpan.org
2069           #
2070
2071           use strict;
2072           use Spreadsheet::WriteExcel;
2073
2074           my $workbook  = Spreadsheet::WriteExcel->new( 'chart_column.xls' );
2075           my $worksheet = $workbook->add_worksheet();
2076           my $bold      = $workbook->add_format( bold => 1 );
2077
2078           # Add the worksheet data that the charts will refer to.
2079           my $headings = [ 'Category', 'Values 1', 'Values 2' ];
2080           my $data = [
2081               [ 2, 3, 4, 5, 6, 7 ],
2082               [ 1, 4, 5, 2, 1, 5 ],
2083               [ 3, 6, 7, 5, 4, 3 ],
2084           ];
2085
2086           $worksheet->write( 'A1', $headings, $bold );
2087           $worksheet->write( 'A2', $data );
2088
2089
2090           ###############################################################################
2091           #
2092           # Example 1. A minimal chart.
2093           #
2094           my $chart1 = $workbook->add_chart( type => 'column' );
2095
2096           # Add values only. Use the default categories.
2097           $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
2098
2099
2100           ###############################################################################
2101           #
2102           # Example 2. A minimal chart with user specified categories (X axis)
2103           #            and a series name.
2104           #
2105           my $chart2 = $workbook->add_chart( type => 'column' );
2106
2107           # Configure the series.
2108           $chart2->add_series(
2109               categories => '=Sheet1!$A$2:$A$7',
2110               values     => '=Sheet1!$B$2:$B$7',
2111               name       => 'Test data series 1',
2112           );
2113
2114
2115           ###############################################################################
2116           #
2117           # Example 3. Same as previous chart but with added title and axes labels.
2118           #
2119           my $chart3 = $workbook->add_chart( type => 'column' );
2120
2121           # Configure the series.
2122           $chart3->add_series(
2123               categories => '=Sheet1!$A$2:$A$7',
2124               values     => '=Sheet1!$B$2:$B$7',
2125               name       => 'Test data series 1',
2126           );
2127
2128           # Add some labels.
2129           $chart3->set_title( name => 'Results of sample analysis' );
2130           $chart3->set_x_axis( name => 'Sample number' );
2131           $chart3->set_y_axis( name => 'Sample length (cm)' );
2132
2133
2134           ###############################################################################
2135           #
2136           # Example 4. Same as previous chart but with an added series and with a
2137           #            user specified chart sheet name.
2138           #
2139           my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'column' );
2140
2141           # Configure the series.
2142           $chart4->add_series(
2143               categories => '=Sheet1!$A$2:$A$7',
2144               values     => '=Sheet1!$B$2:$B$7',
2145               name       => 'Test data series 1',
2146           );
2147
2148           # Add another series.
2149           $chart4->add_series(
2150               categories => '=Sheet1!$A$2:$A$7',
2151               values     => '=Sheet1!$C$2:$C$7',
2152               name       => 'Test data series 2',
2153           );
2154
2155           # Add some labels.
2156           $chart4->set_title( name => 'Results of sample analysis' );
2157           $chart4->set_x_axis( name => 'Sample number' );
2158           $chart4->set_y_axis( name => 'Sample length (cm)' );
2159
2160
2161           ###############################################################################
2162           #
2163           # Example 5. Same as Example 3 but as an embedded chart.
2164           #
2165           my $chart5 = $workbook->add_chart( type => 'column', embedded => 1 );
2166
2167           # Configure the series.
2168           $chart5->add_series(
2169               categories => '=Sheet1!$A$2:$A$7',
2170               values     => '=Sheet1!$B$2:$B$7',
2171               name       => 'Test data series 1',
2172           );
2173
2174           # Add some labels.
2175           $chart5->set_title( name => 'Results of sample analysis' );
2176           $chart5->set_x_axis( name => 'Sample number' );
2177           $chart5->set_y_axis( name => 'Sample length (cm)' );
2178
2179           # Insert the chart into the main worksheet.
2180           $worksheet->insert_chart( 'E2', $chart5 );
2181
2182           __END__
2183
2184       Download this example:
2185       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/chart_column.pl>
2186
2187   Example: chart_line.pl
2188       A simple demo of Line charts in Spreadsheet::WriteExcel.
2189
2190       Source code for this example:
2191
2192           #!/usr/bin/perl -w
2193
2194           ###############################################################################
2195           #
2196           # A simple demo of Line charts in Spreadsheet::WriteExcel.
2197           #
2198           # reverse('(c)'), December 2009, John McNamara, jmcnamara@cpan.org
2199           #
2200
2201           use strict;
2202           use Spreadsheet::WriteExcel;
2203
2204           my $workbook  = Spreadsheet::WriteExcel->new( 'chart_line.xls' );
2205           my $worksheet = $workbook->add_worksheet();
2206           my $bold      = $workbook->add_format( bold => 1 );
2207
2208           # Add the worksheet data that the charts will refer to.
2209           my $headings = [ 'Category', 'Values 1', 'Values 2' ];
2210           my $data = [
2211               [ 2, 3, 4, 5, 6, 7 ],
2212               [ 1, 4, 5, 2, 1, 5 ],
2213               [ 3, 6, 7, 5, 4, 3 ],
2214           ];
2215
2216           $worksheet->write( 'A1', $headings, $bold );
2217           $worksheet->write( 'A2', $data );
2218
2219
2220           ###############################################################################
2221           #
2222           # Example 1. A minimal chart.
2223           #
2224           my $chart1 = $workbook->add_chart( type => 'line' );
2225
2226           # Add values only. Use the default categories.
2227           $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
2228
2229
2230           ###############################################################################
2231           #
2232           # Example 2. A minimal chart with user specified categories (X axis)
2233           #            and a series name.
2234           #
2235           my $chart2 = $workbook->add_chart( type => 'line' );
2236
2237           # Configure the series.
2238           $chart2->add_series(
2239               categories => '=Sheet1!$A$2:$A$7',
2240               values     => '=Sheet1!$B$2:$B$7',
2241               name       => 'Test data series 1',
2242           );
2243
2244
2245           ###############################################################################
2246           #
2247           # Example 3. Same as previous chart but with added title and axes labels.
2248           #
2249           my $chart3 = $workbook->add_chart( type => 'line' );
2250
2251           # Configure the series.
2252           $chart3->add_series(
2253               categories => '=Sheet1!$A$2:$A$7',
2254               values     => '=Sheet1!$B$2:$B$7',
2255               name       => 'Test data series 1',
2256           );
2257
2258           # Add some labels.
2259           $chart3->set_title( name => 'Results of sample analysis' );
2260           $chart3->set_x_axis( name => 'Sample number' );
2261           $chart3->set_y_axis( name => 'Sample length (cm)' );
2262
2263
2264           ###############################################################################
2265           #
2266           # Example 4. Same as previous chart but with an added series and with a
2267           #            user specified chart sheet name.
2268           #
2269           my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'line' );
2270
2271           # Configure the series.
2272           $chart4->add_series(
2273               categories => '=Sheet1!$A$2:$A$7',
2274               values     => '=Sheet1!$B$2:$B$7',
2275               name       => 'Test data series 1',
2276           );
2277
2278           # Add another series.
2279           $chart4->add_series(
2280               categories => '=Sheet1!$A$2:$A$7',
2281               values     => '=Sheet1!$C$2:$C$7',
2282               name       => 'Test data series 2',
2283           );
2284
2285           # Add some labels.
2286           $chart4->set_title( name => 'Results of sample analysis' );
2287           $chart4->set_x_axis( name => 'Sample number' );
2288           $chart4->set_y_axis( name => 'Sample length (cm)' );
2289
2290
2291           ###############################################################################
2292           #
2293           # Example 5. Same as Example 3 but as an embedded chart.
2294           #
2295           my $chart5 = $workbook->add_chart( type => 'line', embedded => 1 );
2296
2297           # Configure the series.
2298           $chart5->add_series(
2299               categories => '=Sheet1!$A$2:$A$7',
2300               values     => '=Sheet1!$B$2:$B$7',
2301               name       => 'Test data series 1',
2302           );
2303
2304           # Add some labels.
2305           $chart5->set_title( name => 'Results of sample analysis' );
2306           $chart5->set_x_axis( name => 'Sample number' );
2307           $chart5->set_y_axis( name => 'Sample length (cm)' );
2308
2309           # Insert the chart into the main worksheet.
2310           $worksheet->insert_chart( 'E2', $chart5 );
2311
2312           __END__
2313
2314       Download this example:
2315       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/chart_line.pl>
2316
2317   Example: chart_pie.pl
2318       A simple demo of Pie charts in Spreadsheet::WriteExcel.
2319
2320       Source code for this example:
2321
2322           #!/usr/bin/perl -w
2323
2324           ###############################################################################
2325           #
2326           # A simple demo of Pie charts in Spreadsheet::WriteExcel.
2327           #
2328           # reverse('(c)'), December 2009, John McNamara, jmcnamara@cpan.org
2329           #
2330
2331           use strict;
2332           use Spreadsheet::WriteExcel;
2333
2334           my $workbook  = Spreadsheet::WriteExcel->new( 'chart_pie.xls' );
2335           my $worksheet = $workbook->add_worksheet();
2336           my $bold      = $workbook->add_format( bold => 1 );
2337
2338           # Add the worksheet data that the charts will refer to.
2339           my $headings = [ 'Category', 'Values' ];
2340           my $data = [
2341               [ 'Apple', 'Cherry', 'Pecan' ],
2342               [ 60,       30,       10     ],
2343           ];
2344
2345           $worksheet->write( 'A1', $headings, $bold );
2346           $worksheet->write( 'A2', $data );
2347
2348
2349           ###############################################################################
2350           #
2351           # Example 1. A minimal chart.
2352           #
2353           my $chart1 = $workbook->add_chart( type => 'pie' );
2354
2355           # Add values only. Use the default categories.
2356           $chart1->add_series( values => '=Sheet1!$B$2:$B$4' );
2357
2358
2359           ###############################################################################
2360           #
2361           # Example 2. A minimal chart with user specified categories and a series name.
2362           #
2363           my $chart2 = $workbook->add_chart( type => 'pie' );
2364
2365           # Configure the series.
2366           $chart2->add_series(
2367               categories => '=Sheet1!$A$2:$A$4',
2368               values     => '=Sheet1!$B$2:$B$4',
2369               name       => 'Pie sales data',
2370           );
2371
2372
2373           ###############################################################################
2374           #
2375           # Example 3. Same as previous chart but with an added title.
2376           #
2377           my $chart3 = $workbook->add_chart( type => 'pie' );
2378
2379           # Configure the series.
2380           $chart3->add_series(
2381               categories => '=Sheet1!$A$2:$A$4',
2382               values     => '=Sheet1!$B$2:$B$4',
2383               name       => 'Pie sales data',
2384           );
2385
2386           # Add a title.
2387           $chart3->set_title( name => 'Popular Pie Types' );
2388
2389
2390           ###############################################################################
2391           #
2392           # Example 4. Same as previous chart with a user specified chart sheet name.
2393           #
2394           my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'pie' );
2395
2396           # Configure the series.
2397           $chart4->add_series(
2398               categories => '=Sheet1!$A$2:$A$4',
2399               values     => '=Sheet1!$B$2:$B$4',
2400               name       => 'Pie sales data',
2401           );
2402
2403           # The other chart_*.pl examples add a second series in example 4 but additional
2404           # series aren't plotted in a pie chart.
2405
2406           # Add a title.
2407           $chart4->set_title( name => 'Popular Pie Types' );
2408
2409
2410           ###############################################################################
2411           #
2412           # Example 5. Same as Example 3 but as an embedded chart.
2413           #
2414           my $chart5 = $workbook->add_chart( type => 'pie', embedded => 1 );
2415
2416           # Configure the series.
2417           $chart5->add_series(
2418               categories => '=Sheet1!$A$2:$A$4',
2419               values     => '=Sheet1!$B$2:$B$4',
2420               name       => 'Pie sales data',
2421           );
2422
2423           # Add a title.
2424           $chart5->set_title( name => 'Popular Pie Types' );
2425
2426           # Insert the chart into the main worksheet.
2427           $worksheet->insert_chart( 'D2', $chart5 );
2428
2429           __END__
2430
2431       Download this example:
2432       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/chart_pie.pl>
2433
2434   Example: chart_scatter.pl
2435       A simple demo of Scatter charts in Spreadsheet::WriteExcel.
2436
2437       Source code for this example:
2438
2439           #!/usr/bin/perl -w
2440
2441           ###############################################################################
2442           #
2443           # A simple demo of Scatter charts in Spreadsheet::WriteExcel.
2444           #
2445           # reverse('(c)'), December 2009, John McNamara, jmcnamara@cpan.org
2446           #
2447
2448           use strict;
2449           use Spreadsheet::WriteExcel;
2450
2451           my $workbook  = Spreadsheet::WriteExcel->new( 'chart_scatter.xls' );
2452           my $worksheet = $workbook->add_worksheet();
2453           my $bold      = $workbook->add_format( bold => 1 );
2454
2455           # Add the worksheet data that the charts will refer to.
2456           my $headings = [ 'Category', 'Values 1', 'Values 2' ];
2457           my $data = [
2458               [ 2, 3, 4, 5, 6, 7 ],
2459               [ 1, 4, 5, 2, 1, 5 ],
2460               [ 3, 6, 7, 5, 4, 3 ],
2461           ];
2462
2463           $worksheet->write( 'A1', $headings, $bold );
2464           $worksheet->write( 'A2', $data );
2465
2466
2467           ###############################################################################
2468           #
2469           # Example 1. A minimal chart.
2470           #
2471           my $chart1 = $workbook->add_chart( type => 'scatter' );
2472
2473           # Add values only. Use the default categories.
2474           $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
2475
2476
2477           ###############################################################################
2478           #
2479           # Example 2. A minimal chart with user specified categories (X axis)
2480           #            and a series name.
2481           #
2482           my $chart2 = $workbook->add_chart( type => 'scatter' );
2483
2484           # Configure the series.
2485           $chart2->add_series(
2486               categories => '=Sheet1!$A$2:$A$7',
2487               values     => '=Sheet1!$B$2:$B$7',
2488               name       => 'Test data series 1',
2489           );
2490
2491
2492           ###############################################################################
2493           #
2494           # Example 3. Same as previous chart but with added title and axes labels.
2495           #
2496           my $chart3 = $workbook->add_chart( type => 'scatter' );
2497
2498           # Configure the series.
2499           $chart3->add_series(
2500               categories => '=Sheet1!$A$2:$A$7',
2501               values     => '=Sheet1!$B$2:$B$7',
2502               name       => 'Test data series 1',
2503           );
2504
2505           # Add some labels.
2506           $chart3->set_title( name => 'Results of sample analysis' );
2507           $chart3->set_x_axis( name => 'Sample number' );
2508           $chart3->set_y_axis( name => 'Sample length (cm)' );
2509
2510
2511           ###############################################################################
2512           #
2513           # Example 4. Same as previous chart but with an added series and with a
2514           #            user specified chart sheet name.
2515           #
2516           my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'scatter' );
2517
2518           # Configure the series.
2519           $chart4->add_series(
2520               categories => '=Sheet1!$A$2:$A$7',
2521               values     => '=Sheet1!$B$2:$B$7',
2522               name       => 'Test data series 1',
2523           );
2524
2525           # Add another series.
2526           $chart4->add_series(
2527               categories => '=Sheet1!$A$2:$A$7',
2528               values     => '=Sheet1!$C$2:$C$7',
2529               name       => 'Test data series 2',
2530           );
2531
2532           # Add some labels.
2533           $chart4->set_title( name => 'Results of sample analysis' );
2534           $chart4->set_x_axis( name => 'Sample number' );
2535           $chart4->set_y_axis( name => 'Sample length (cm)' );
2536
2537
2538           ###############################################################################
2539           #
2540           # Example 5. Same as Example 3 but as an embedded chart.
2541           #
2542           my $chart5 = $workbook->add_chart( type => 'scatter', embedded => 1 );
2543
2544           # Configure the series.
2545           $chart5->add_series(
2546               categories => '=Sheet1!$A$2:$A$7',
2547               values     => '=Sheet1!$B$2:$B$7',
2548               name       => 'Test data series 1',
2549           );
2550
2551           # Add some labels.
2552           $chart5->set_title( name => 'Results of sample analysis' );
2553           $chart5->set_x_axis( name => 'Sample number' );
2554           $chart5->set_y_axis( name => 'Sample length (cm)' );
2555
2556           # Insert the chart into the main worksheet.
2557           $worksheet->insert_chart( 'E2', $chart5 );
2558
2559           __END__
2560
2561       Download this example:
2562       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/chart_scatter.pl>
2563
2564   Example: chart_stock.pl
2565       A simple demo of Stock charts in Spreadsheet::WriteExcel.
2566
2567       Source code for this example:
2568
2569           #!/usr/bin/perl -w
2570
2571           ###############################################################################
2572           #
2573           # A simple demo of Stock charts in Spreadsheet::WriteExcel.
2574           #
2575           # reverse('(c)'), January 2010, John McNamara, jmcnamara@cpan.org
2576           #
2577
2578
2579           use strict;
2580           use Spreadsheet::WriteExcel;
2581
2582           my $workbook  = Spreadsheet::WriteExcel->new( 'chart_stock.xls' );
2583           my $worksheet = $workbook->add_worksheet();
2584
2585
2586           ###############################################################################
2587           #
2588           # Set up the data worksheet that the charts will refer to. We read the example
2589           # data from the __DATA__ section at the end of the file. This simulates
2590           # reading the data from a database or other source.
2591           #
2592           # The default Excel Stock chart is an Open-High-Low-Close chart. Therefore
2593           # we will need data for each of those series.
2594           #
2595           # The layout of the __DATA__ section is similar to the layout of the worksheet.
2596           #
2597
2598           # Add some formats.
2599           my $bold        = $workbook->add_format( bold       => 1 );
2600           my $date_format = $workbook->add_format( num_format => 'dd/mm/yyyy' );
2601
2602           # Increase the width of the column used for date to make it clearer.
2603           $worksheet->set_column( 'A:A', 12 );
2604
2605           # Read the data from the __DATA__ section at the end. In a real example this
2606           # would probably be a database query.
2607           my @stock_data;
2608
2609           while ( <DATA> ) {
2610               next unless /\S/;    # Skip blank lines.
2611               next if /^#/;        # Skip comments.
2612
2613               push @stock_data, [split];
2614           }
2615
2616           # Write the data to the worksheet.
2617           my $row = 0;
2618           my $col = 0;
2619
2620           my $headers = shift @stock_data;
2621           $worksheet->write( $row++, $col, $headers, $bold );
2622
2623           for my $stock_data ( @stock_data ) {
2624
2625               my @data = @$stock_data;
2626               my $date = shift @data;
2627
2628               $worksheet->write( $row, $col, $date, $date_format );
2629               $worksheet->write( $row, $col + 1, \@data );
2630
2631               $row++;
2632           }
2633
2634
2635           ###############################################################################
2636           #
2637           # Example 1. A default Open-High-Low-Close chart with series names, axes labels
2638           #            and a title.
2639           #
2640
2641           my $chart1 = $workbook->add_chart( type => 'stock' );
2642
2643           # Add a series for each of the Open-High-Low-Close columns. The categories are
2644           # the dates in the first column.
2645
2646           $chart1->add_series(
2647               categories => '=Sheet1!$A$2:$A$10',
2648               values     => '=Sheet1!$B$2:$B$10',
2649               name       => 'Open',
2650           );
2651
2652           $chart1->add_series(
2653               categories => '=Sheet1!$A$2:$A$10',
2654               values     => '=Sheet1!$C$2:$C$10',
2655               name       => 'High',
2656           );
2657
2658           $chart1->add_series(
2659               categories => '=Sheet1!$A$2:$A$10',
2660               values     => '=Sheet1!$D$2:$D$10',
2661               name       => 'Low',
2662           );
2663
2664           $chart1->add_series(
2665               categories => '=Sheet1!$A$2:$A$10',
2666               values     => '=Sheet1!$E$2:$E$10',
2667               name       => 'Close',
2668           );
2669
2670           # Add a chart title and axes labels.
2671           $chart1->set_title( name => 'Open-High-Low-Close', );
2672           $chart1->set_x_axis( name => 'Date', );
2673           $chart1->set_y_axis( name => 'Share price', );
2674
2675           ###############################################################################
2676           #
2677           # Example 2. Same as the previous as an embedded chart.
2678           #
2679
2680           my $chart2 = $workbook->add_chart( type => 'stock', embedded => 1 );
2681
2682           # Add a series for each of the Open-High-Low-Close columns. The categories are
2683           # the dates in the first column.
2684
2685           $chart2->add_series(
2686               categories => '=Sheet1!$A$2:$A$10',
2687               values     => '=Sheet1!$B$2:$B$10',
2688               name       => 'Open',
2689           );
2690
2691           $chart2->add_series(
2692               categories => '=Sheet1!$A$2:$A$10',
2693               values     => '=Sheet1!$C$2:$C$10',
2694               name       => 'High',
2695           );
2696
2697           $chart2->add_series(
2698               categories => '=Sheet1!$A$2:$A$10',
2699               values     => '=Sheet1!$D$2:$D$10',
2700               name       => 'Low',
2701           );
2702
2703           $chart2->add_series(
2704               categories => '=Sheet1!$A$2:$A$10',
2705               values     => '=Sheet1!$E$2:$E$10',
2706               name       => 'Close',
2707           );
2708
2709           # Add a chart title and axes labels.
2710           $chart2->set_title( name => 'Open-High-Low-Close', );
2711           $chart2->set_x_axis( name => 'Date', );
2712           $chart2->set_y_axis( name => 'Share price', );
2713
2714           # Insert the chart into the main worksheet.
2715           $worksheet->insert_chart( 'G2', $chart2 );
2716
2717
2718           __DATA__
2719           # Some sample stock data used for charting.
2720           Date        Open    High    Low     Close
2721           2009-08-19  100.00  104.06  95.96   100.34
2722           2009-08-20  101.01  109.08  100.50  108.31
2723           2009-08-23  110.75  113.48  109.05  109.40
2724           2009-08-24  111.24  111.60  103.57  104.87
2725           2009-08-25  104.96  108.00  103.88  106.00
2726           2009-08-26  104.95  107.95  104.66  107.91
2727           2009-08-27  108.10  108.62  105.69  106.15
2728           2009-08-30  105.28  105.49  102.01  102.01
2729           2009-08-31  102.30  103.71  102.16  102.37
2730
2731       Download this example:
2732       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/chart_stock.pl>
2733
2734   Example: chess.pl
2735       Example of formatting using the Spreadsheet::WriteExcel module via
2736       property hashes.
2737
2738       Setting format properties via hashes of values is useful when you have
2739       to deal with a large number of similar formats. Consider for example a
2740       chess board pattern with black squares, white unformatted squares and a
2741       border.
2742
2743       This relatively simple example requires 14 separate Format objects
2744       although there are only 5 different properties: black background, top
2745       border, bottom border, left border and right border.
2746
2747       Using property hashes it is possible to define these 5 sets of
2748       properties and then add them together to create the 14 Format
2749       configurations.
2750
2751       Source code for this example:
2752
2753           #!/usr/bin/perl -w
2754
2755           ########################################################################
2756           #
2757           # Example of formatting using the Spreadsheet::WriteExcel module via
2758           # property hashes.
2759           #
2760           # Setting format properties via hashes of values is useful when you have
2761           # to deal with a large number of similar formats. Consider for example a
2762           # chess board pattern with black squares, white unformatted squares and
2763           # a border.
2764           #
2765           # This relatively simple example requires 14 separate Format
2766           # objects although there are only 5 different properties: black
2767           # background, top border, bottom border, left border and right border.
2768           #
2769           # Using property hashes it is possible to define these 5 sets of
2770           # properties and then add them together to create the 14 Format
2771           # configurations.
2772           #
2773           # reverse('(c)'), July 2001, John McNamara, jmcnamara@cpan.org
2774           #
2775
2776           use strict;
2777           use Spreadsheet::WriteExcel;
2778
2779           my $workbook  = Spreadsheet::WriteExcel->new("chess.xls");
2780           my $worksheet = $workbook->add_worksheet();
2781
2782
2783           # Some row and column formatting
2784           $worksheet->set_column('B:I', 10);
2785
2786           for my $i (1..8) {
2787               $worksheet->set_row($i, 50);
2788           }
2789
2790
2791           # Define the property hashes
2792           #
2793           my %black = (
2794                           'fg_color'  => 'black',
2795                           'pattern'   => 1,
2796                       );
2797
2798           my %top     = ( 'top'    => 6 );
2799           my %bottom  = ( 'bottom' => 6 );
2800           my %left    = ( 'left'   => 6 );
2801           my %right   = ( 'right'  => 6 );
2802
2803
2804           # Define the formats
2805           #
2806           my $format01 = $workbook->add_format(%top,    %left          );
2807           my $format02 = $workbook->add_format(%top,    %black         );
2808           my $format03 = $workbook->add_format(%top,                   );
2809           my $format04 = $workbook->add_format(%top,    %right, %black );
2810
2811           my $format05 = $workbook->add_format(%left                   );
2812           my $format06 = $workbook->add_format(%black                  );
2813           my $format07 = $workbook->add_format(                        );
2814           my $format08 = $workbook->add_format(%right,  %black         );
2815           my $format09 = $workbook->add_format(%right                  );
2816           my $format10 = $workbook->add_format(%left,   %black         );
2817
2818           my $format11 = $workbook->add_format(%bottom, %left,  %black );
2819           my $format12 = $workbook->add_format(%bottom                 );
2820           my $format13 = $workbook->add_format(%bottom, %black         );
2821           my $format14 = $workbook->add_format(%bottom, %right         );
2822
2823
2824           # Draw the pattern
2825           $worksheet->write('B2', '', $format01);
2826           $worksheet->write('C2', '', $format02);
2827           $worksheet->write('D2', '', $format03);
2828           $worksheet->write('E2', '', $format02);
2829           $worksheet->write('F2', '', $format03);
2830           $worksheet->write('G2', '', $format02);
2831           $worksheet->write('H2', '', $format03);
2832           $worksheet->write('I2', '', $format04);
2833
2834           $worksheet->write('B3', '', $format10);
2835           $worksheet->write('C3', '', $format07);
2836           $worksheet->write('D3', '', $format06);
2837           $worksheet->write('E3', '', $format07);
2838           $worksheet->write('F3', '', $format06);
2839           $worksheet->write('G3', '', $format07);
2840           $worksheet->write('H3', '', $format06);
2841           $worksheet->write('I3', '', $format09);
2842
2843           $worksheet->write('B4', '', $format05);
2844           $worksheet->write('C4', '', $format06);
2845           $worksheet->write('D4', '', $format07);
2846           $worksheet->write('E4', '', $format06);
2847           $worksheet->write('F4', '', $format07);
2848           $worksheet->write('G4', '', $format06);
2849           $worksheet->write('H4', '', $format07);
2850           $worksheet->write('I4', '', $format08);
2851
2852           $worksheet->write('B5', '', $format10);
2853           $worksheet->write('C5', '', $format07);
2854           $worksheet->write('D5', '', $format06);
2855           $worksheet->write('E5', '', $format07);
2856           $worksheet->write('F5', '', $format06);
2857           $worksheet->write('G5', '', $format07);
2858           $worksheet->write('H5', '', $format06);
2859           $worksheet->write('I5', '', $format09);
2860
2861           $worksheet->write('B6', '', $format05);
2862           $worksheet->write('C6', '', $format06);
2863           $worksheet->write('D6', '', $format07);
2864           $worksheet->write('E6', '', $format06);
2865           $worksheet->write('F6', '', $format07);
2866           $worksheet->write('G6', '', $format06);
2867           $worksheet->write('H6', '', $format07);
2868           $worksheet->write('I6', '', $format08);
2869
2870           $worksheet->write('B7', '', $format10);
2871           $worksheet->write('C7', '', $format07);
2872           $worksheet->write('D7', '', $format06);
2873           $worksheet->write('E7', '', $format07);
2874           $worksheet->write('F7', '', $format06);
2875           $worksheet->write('G7', '', $format07);
2876           $worksheet->write('H7', '', $format06);
2877           $worksheet->write('I7', '', $format09);
2878
2879           $worksheet->write('B8', '', $format05);
2880           $worksheet->write('C8', '', $format06);
2881           $worksheet->write('D8', '', $format07);
2882           $worksheet->write('E8', '', $format06);
2883           $worksheet->write('F8', '', $format07);
2884           $worksheet->write('G8', '', $format06);
2885           $worksheet->write('H8', '', $format07);
2886           $worksheet->write('I8', '', $format08);
2887
2888           $worksheet->write('B9', '', $format11);
2889           $worksheet->write('C9', '', $format12);
2890           $worksheet->write('D9', '', $format13);
2891           $worksheet->write('E9', '', $format12);
2892           $worksheet->write('F9', '', $format13);
2893           $worksheet->write('G9', '', $format12);
2894           $worksheet->write('H9', '', $format13);
2895           $worksheet->write('I9', '', $format14);
2896
2897       Download this example:
2898       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/chess.pl>
2899
2900   Example: colors.pl
2901       Demonstrates Spreadsheet::WriteExcel's named colors and the Excel color
2902       palette.
2903
2904       The set_custom_color() Worksheet method can be used to override one of
2905       the built-in palette values with a more suitable colour. See the main
2906       docs.
2907
2908       Source code for this example:
2909
2910           #!/usr/bin/perl -w
2911
2912           ################################################################################
2913           #
2914           # Demonstrates Spreadsheet::WriteExcel's named colors and the Excel color
2915           # palette.
2916           #
2917           # The set_custom_color() Worksheet method can be used to override one of the
2918           # built-in palette values with a more suitable colour. See the main docs.
2919           #
2920           # reverse('(c)'), March 2002, John McNamara, jmcnamara@cpan.org
2921           #
2922
2923           use strict;
2924           use Spreadsheet::WriteExcel;
2925
2926           my $workbook = Spreadsheet::WriteExcel->new("colors.xls");
2927
2928           # Some common formats
2929           my $center  = $workbook->add_format(align => 'center');
2930           my $heading = $workbook->add_format(align => 'center', bold => 1);
2931
2932
2933           ######################################################################
2934           #
2935           # Demonstrate the named colors.
2936           #
2937
2938           my %colors = (
2939                           0x08, 'black',
2940                           0x0C, 'blue',
2941                           0x10, 'brown',
2942                           0x0F, 'cyan',
2943                           0x17, 'gray',
2944                           0x11, 'green',
2945                           0x0B, 'lime',
2946                           0x0E, 'magenta',
2947                           0x12, 'navy',
2948                           0x35, 'orange',
2949                           0x21, 'pink',
2950                           0x14, 'purple',
2951                           0x0A, 'red',
2952                           0x16, 'silver',
2953                           0x09, 'white',
2954                           0x0D, 'yellow',
2955                        );
2956
2957           my $worksheet1 = $workbook->add_worksheet('Named colors');
2958
2959           $worksheet1->set_column(0, 3, 15);
2960
2961           $worksheet1->write(0, 0, "Index", $heading);
2962           $worksheet1->write(0, 1, "Index", $heading);
2963           $worksheet1->write(0, 2, "Name",  $heading);
2964           $worksheet1->write(0, 3, "Color", $heading);
2965
2966           my $i = 1;
2967
2968           while (my($index, $color) = each %colors) {
2969               my $format = $workbook->add_format(
2970                                                   fg_color => $color,
2971                                                   pattern  => 1,
2972                                                   border   => 1
2973                                                );
2974
2975               $worksheet1->write($i+1, 0, $index,                    $center);
2976               $worksheet1->write($i+1, 1, sprintf("0x%02X", $index), $center);
2977               $worksheet1->write($i+1, 2, $color,                    $center);
2978               $worksheet1->write($i+1, 3, '',                        $format);
2979               $i++;
2980           }
2981
2982
2983           ######################################################################
2984           #
2985           # Demonstrate the standard Excel colors in the range 8..63.
2986           #
2987
2988           my $worksheet2 = $workbook->add_worksheet('Standard colors');
2989
2990           $worksheet2->set_column(0, 3, 15);
2991
2992           $worksheet2->write(0, 0, "Index", $heading);
2993           $worksheet2->write(0, 1, "Index", $heading);
2994           $worksheet2->write(0, 2, "Color", $heading);
2995           $worksheet2->write(0, 3, "Name",  $heading);
2996
2997           for my $i (8..63) {
2998               my $format = $workbook->add_format(
2999                                                   fg_color => $i,
3000                                                   pattern  => 1,
3001                                                   border   => 1
3002                                                );
3003
3004               $worksheet2->write(($i -7), 0, $i,                    $center);
3005               $worksheet2->write(($i -7), 1, sprintf("0x%02X", $i), $center);
3006               $worksheet2->write(($i -7), 2, '',                    $format);
3007
3008               # Add the  color names
3009               if (exists $colors{$i}) {
3010                   $worksheet2->write(($i -7), 3, $colors{$i}, $center);
3011
3012               }
3013           }
3014
3015           __END__
3016
3017       Download this example:
3018       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/colors.pl>
3019
3020   Example: comments1.pl
3021       This example demonstrates writing cell comments.
3022
3023       A cell comment is indicated in Excel by a small red triangle in the
3024       upper right-hand corner of the cell.
3025
3026       For more advanced comment options see comments2.pl.
3027
3028       Source code for this example:
3029
3030           #!/usr/bin/perl -w
3031
3032           ###############################################################################
3033           #
3034           # This example demonstrates writing cell comments.
3035           #
3036           # A cell comment is indicated in Excel by a small red triangle in the upper
3037           # right-hand corner of the cell.
3038           #
3039           # For more advanced comment options see comments2.pl.
3040           #
3041           # reverse('(c)'), November 2005, John McNamara, jmcnamara@cpan.org
3042           #
3043
3044           use strict;
3045           use Spreadsheet::WriteExcel;
3046
3047           my $workbook  = Spreadsheet::WriteExcel->new("comments1.xls");
3048           my $worksheet = $workbook->add_worksheet();
3049
3050
3051
3052           $worksheet->write        ('A1', 'Hello'            );
3053           $worksheet->write_comment('A1', 'This is a comment');
3054
3055           __END__
3056
3057       Download this example:
3058       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/comments1.pl>
3059
3060   Example: comments2.pl
3061       This example demonstrates writing cell comments.
3062
3063       A cell comment is indicated in Excel by a small red triangle in the
3064       upper right-hand corner of the cell.
3065
3066       Each of the worksheets demonstrates different features of cell
3067       comments.
3068
3069       Source code for this example:
3070
3071           #!/usr/bin/perl -w
3072
3073           ###############################################################################
3074           #
3075           # This example demonstrates writing cell comments.
3076           #
3077           # A cell comment is indicated in Excel by a small red triangle in the upper
3078           # right-hand corner of the cell.
3079           #
3080           # Each of the worksheets demonstrates different features of cell comments.
3081           #
3082           # reverse('(c)'), November 2005, John McNamara, jmcnamara@cpan.org
3083           #
3084
3085           use strict;
3086           use Spreadsheet::WriteExcel;
3087
3088           my $workbook   = Spreadsheet::WriteExcel->new("comments2.xls");
3089           my $text_wrap  = $workbook->add_format(text_wrap => 1, valign => 'top');
3090           my $worksheet1 = $workbook->add_worksheet();
3091           my $worksheet2 = $workbook->add_worksheet();
3092           my $worksheet3 = $workbook->add_worksheet();
3093           my $worksheet4 = $workbook->add_worksheet();
3094           my $worksheet5 = $workbook->add_worksheet();
3095           my $worksheet6 = $workbook->add_worksheet();
3096           my $worksheet7 = $workbook->add_worksheet();
3097           my $worksheet8 = $workbook->add_worksheet();
3098
3099
3100           # Variables that we will use in each example.
3101           my $cell_text = '';
3102           my $comment   = '';
3103
3104
3105
3106
3107           ###############################################################################
3108           #
3109           # Example 1. Demonstrates a simple cell comment without formatting and Unicode
3110           #            comments encoded as UTF-16 and as UTF-8.
3111           #
3112
3113           # Set up some formatting.
3114           $worksheet1->set_column('C:C', 25);
3115           $worksheet1->set_row(2, 50);
3116           $worksheet1->set_row(5, 50);
3117
3118
3119           # Simple ascii string.
3120           $cell_text = 'Hold the mouse over this cell to see the comment.';
3121
3122           $comment   = 'This is a comment.';
3123
3124           $worksheet1->write        ('C3', $cell_text, $text_wrap);
3125           $worksheet1->write_comment('C3', $comment);
3126
3127
3128           # UTF-16 string.
3129           $cell_text = 'This is a UTF-16 comment.';
3130
3131           $comment   = pack "n", 0x263a;
3132
3133           $worksheet1->write        ('C6', $cell_text, $text_wrap);
3134           $worksheet1->write_comment('C6', $comment, encoding => 1);
3135
3136
3137           # UTF-8 string in perl 5.8.
3138           if ($] >= 5.008) {
3139
3140               $worksheet1->set_row(8, 50);
3141               $cell_text = 'This is a UTF-8 string.';
3142               $comment   = chr 0x263a;
3143
3144               $worksheet1->write        ('C9', $cell_text, $text_wrap);
3145               $worksheet1->write_comment('C9', $comment);
3146           }
3147
3148
3149
3150           ###############################################################################
3151           #
3152           # Example 2. Demonstrates visible and hidden comments.
3153           #
3154
3155           # Set up some formatting.
3156           $worksheet2->set_column('C:C', 25);
3157           $worksheet2->set_row(2, 50);
3158           $worksheet2->set_row(5, 50);
3159
3160
3161           $cell_text = 'This cell comment is visible.';
3162
3163           $comment   = 'Hello.';
3164
3165           $worksheet2->write        ('C3', $cell_text, $text_wrap);
3166           $worksheet2->write_comment('C3', $comment, visible => 1);
3167
3168
3169           $cell_text = "This cell comment isn't visible (the default).";
3170
3171           $comment   = 'Hello.';
3172
3173           $worksheet2->write        ('C6', $cell_text, $text_wrap);
3174           $worksheet2->write_comment('C6', $comment);
3175
3176
3177
3178
3179           ###############################################################################
3180           #
3181           # Example 3. Demonstrates visible and hidden comments set at the worksheet
3182           #            level.
3183           #
3184
3185           # Set up some formatting.
3186           $worksheet3->set_column('C:C', 25);
3187           $worksheet3->set_row(2, 50);
3188           $worksheet3->set_row(5, 50);
3189           $worksheet3->set_row(8, 50);
3190
3191           # Make all comments on the worksheet visible.
3192           $worksheet3->show_comments();
3193
3194           $cell_text = 'This cell comment is visible, explicitly.';
3195
3196           $comment   = 'Hello.';
3197
3198           $worksheet3->write        ('C3', $cell_text, $text_wrap);
3199           $worksheet3->write_comment('C3', $comment, visible => 1);
3200
3201
3202           $cell_text = 'This cell comment is also visible because '.
3203                        'we used show_comments().';
3204
3205           $comment   = 'Hello.';
3206
3207           $worksheet3->write        ('C6', $cell_text, $text_wrap);
3208           $worksheet3->write_comment('C6', $comment);
3209
3210
3211           $cell_text = 'However, we can still override it locally.';
3212
3213           $comment   = 'Hello.';
3214
3215           $worksheet3->write        ('C9', $cell_text, $text_wrap);
3216           $worksheet3->write_comment('C9', $comment, visible => 0);
3217
3218
3219
3220
3221           ###############################################################################
3222           #
3223           # Example 4. Demonstrates changes to the comment box dimensions.
3224           #
3225
3226           # Set up some formatting.
3227           $worksheet4->set_column('C:C', 25);
3228           $worksheet4->set_row(2,  50);
3229           $worksheet4->set_row(5,  50);
3230           $worksheet4->set_row(8,  50);
3231           $worksheet4->set_row(15, 50);
3232
3233           $worksheet4->show_comments();
3234
3235           $cell_text = 'This cell comment is default size.';
3236
3237           $comment   = 'Hello.';
3238
3239           $worksheet4->write        ('C3', $cell_text, $text_wrap);
3240           $worksheet4->write_comment('C3', $comment);
3241
3242
3243           $cell_text = 'This cell comment is twice as wide.';
3244
3245           $comment   = 'Hello.';
3246
3247           $worksheet4->write        ('C6', $cell_text, $text_wrap);
3248           $worksheet4->write_comment('C6', $comment, x_scale => 2);
3249
3250
3251           $cell_text = 'This cell comment is twice as high.';
3252
3253           $comment   = 'Hello.';
3254
3255           $worksheet4->write        ('C9', $cell_text, $text_wrap);
3256           $worksheet4->write_comment('C9', $comment, y_scale => 2);
3257
3258
3259           $cell_text = 'This cell comment is scaled in both directions.';
3260
3261           $comment   = 'Hello.';
3262
3263           $worksheet4->write        ('C16', $cell_text, $text_wrap);
3264           $worksheet4->write_comment('C16', $comment, x_scale => 1.2, y_scale => 0.8);
3265
3266
3267           $cell_text = 'This cell comment has width and height specified in pixels.';
3268
3269           $comment   = 'Hello.';
3270
3271           $worksheet4->write        ('C19', $cell_text, $text_wrap);
3272           $worksheet4->write_comment('C19', $comment, width => 200, height => 20);
3273
3274
3275
3276           ###############################################################################
3277           #
3278           # Example 5. Demonstrates changes to the cell comment position.
3279           #
3280
3281           $worksheet5->set_column('C:C', 25);
3282           $worksheet5->set_row(2, 50);
3283           $worksheet5->set_row(5, 50);
3284           $worksheet5->set_row(8, 50);
3285           $worksheet5->set_row(11, 50);
3286
3287           $worksheet5->show_comments();
3288
3289           $cell_text = 'This cell comment is in the default position.';
3290
3291           $comment   = 'Hello.';
3292
3293           $worksheet5->write        ('C3', $cell_text, $text_wrap);
3294           $worksheet5->write_comment('C3', $comment);
3295
3296
3297           $cell_text = 'This cell comment has been moved to another cell.';
3298
3299           $comment   = 'Hello.';
3300
3301           $worksheet5->write        ('C6', $cell_text, $text_wrap);
3302           $worksheet5->write_comment('C6', $comment, start_cell => 'E4');
3303
3304
3305           $cell_text = 'This cell comment has been moved to another cell.';
3306
3307           $comment   = 'Hello.';
3308
3309           $worksheet5->write        ('C9', $cell_text, $text_wrap);
3310           $worksheet5->write_comment('C9', $comment, start_row => 8, start_col => 4);
3311
3312
3313           $cell_text = 'This cell comment has been shifted within its default cell.';
3314
3315           $comment   = 'Hello.';
3316
3317           $worksheet5->write        ('C12', $cell_text, $text_wrap);
3318           $worksheet5->write_comment('C12', $comment, x_offset => 30, y_offset => 12);
3319
3320
3321
3322           ###############################################################################
3323           #
3324           # Example 6. Demonstrates changes to the comment background colour.
3325           #
3326
3327           $worksheet6->set_column('C:C', 25);
3328           $worksheet6->set_row(2, 50);
3329           $worksheet6->set_row(5, 50);
3330           $worksheet6->set_row(8, 50);
3331
3332           $worksheet6->show_comments();
3333
3334           $cell_text = 'This cell comment has a different colour.';
3335
3336           $comment   = 'Hello.';
3337
3338           $worksheet6->write        ('C3', $cell_text, $text_wrap);
3339           $worksheet6->write_comment('C3', $comment, color => 'green');
3340
3341
3342           $cell_text = 'This cell comment has the default colour.';
3343
3344           $comment   = 'Hello.';
3345
3346           $worksheet6->write        ('C6', $cell_text, $text_wrap);
3347           $worksheet6->write_comment('C6', $comment);
3348
3349
3350           $cell_text = 'This cell comment has a different colour.';
3351
3352           $comment   = 'Hello.';
3353
3354           $worksheet6->write        ('C9', $cell_text, $text_wrap);
3355           $worksheet6->write_comment('C9', $comment, color => 0x35);
3356
3357
3358
3359
3360           ###############################################################################
3361           #
3362           # Example 7. Demonstrates how to set the cell comment author.
3363           #
3364
3365           $worksheet7->set_column('C:C', 30);
3366           $worksheet7->set_row(2,  50);
3367           $worksheet7->set_row(5,  50);
3368           $worksheet7->set_row(8,  50);
3369           $worksheet7->set_row(11, 50);
3370
3371           my $author = '';
3372           my $cell   = 'C3';
3373
3374           $cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
3375                        "by $author' (blank) in the status bar at the bottom";
3376
3377           $comment   = 'Hello.';
3378
3379           $worksheet7->write        ($cell, $cell_text, $text_wrap);
3380           $worksheet7->write_comment($cell, $comment);
3381
3382
3383           $author    = 'Perl';
3384           $cell      = 'C6';
3385           $cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
3386                        "by $author' in the status bar at the bottom";
3387
3388           $comment   = 'Hello.';
3389
3390           $worksheet7->write        ($cell, $cell_text, $text_wrap);
3391           $worksheet7->write_comment($cell, $comment, author => $author);
3392
3393
3394           $author    = pack "n", 0x20AC; # UTF-16 Euro
3395           $cell      = 'C9';
3396           $cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
3397                        "by Euro' in the status bar at the bottom";
3398
3399           $comment   = 'Hello.';
3400
3401           $worksheet7->write        ($cell, $cell_text, $text_wrap);
3402           $worksheet7->write_comment($cell, $comment, author          => $author,
3403                                                       author_encoding => 1      );
3404
3405           # UTF-8 string in perl 5.8.
3406           if ($] >= 5.008) {
3407               $author    = chr 0x20AC;
3408               $cell      = 'C12';
3409               $cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
3410                            "by $author' in the status bar at the bottom";
3411               $comment   = 'Hello.';
3412
3413               $worksheet7->write        ($cell, $cell_text, $text_wrap);
3414               $worksheet7->write_comment($cell, $comment, author => $author);
3415
3416           }
3417
3418
3419           ###############################################################################
3420           #
3421           # Example 8. Demonstrates the need to explicitly set the row height.
3422           #
3423
3424           # Set up some formatting.
3425           $worksheet8->set_column('C:C', 25);
3426           $worksheet8->set_row(2, 80);
3427
3428           $worksheet8->show_comments();
3429
3430
3431           $cell_text = 'The height of this row has been adjusted explicitly using ' .
3432                        'set_row(). The size of the comment box is adjusted '         .
3433                        'accordingly by WriteExcel.';
3434
3435           $comment   = 'Hello.';
3436
3437           $worksheet8->write        ('C3', $cell_text, $text_wrap);
3438           $worksheet8->write_comment('C3', $comment);
3439
3440
3441           $cell_text = 'The height of this row has been adjusted by Excel due to the '  .
3442                        'text wrap property being set. Unfortunately this means that '   .
3443                        'the height of the row is unknown to WriteExcel at run time '    .
3444                        "and thus the comment box is stretched as well.\n\n"             .
3445                        'Use set_row() to specify the row height explicitly to avoid '   .
3446                        'this problem.';
3447
3448           $comment   = 'Hello.';
3449
3450           $worksheet8->write        ('C6', $cell_text, $text_wrap);
3451           $worksheet8->write_comment('C6', $comment);
3452
3453           __END__
3454
3455       Download this example:
3456       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/comments2.pl>
3457
3458   Example: copyformat.pl
3459       Example of how to use the format copying method with
3460       Spreadsheet::WriteExcel.
3461
3462       This feature isn't required very often.
3463
3464       Source code for this example:
3465
3466           #!/usr/bin/perl -w
3467
3468           ###############################################################################
3469           #
3470           # Example of how to use the format copying method with Spreadsheet::WriteExcel.
3471           #
3472           # This feature isn't required very often.
3473           #
3474           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
3475           #
3476
3477           use strict;
3478           use Spreadsheet::WriteExcel;
3479
3480           # Create workbook1
3481           my $workbook1       = Spreadsheet::WriteExcel->new("workbook1.xls");
3482           my $worksheet1      = $workbook1->add_worksheet();
3483           my $format1a        = $workbook1->add_format();
3484           my $format1b        = $workbook1->add_format();
3485
3486           # Create workbook2
3487           my $workbook2       = Spreadsheet::WriteExcel->new("workbook2.xls");
3488           my $worksheet2      = $workbook2->add_worksheet();
3489           my $format2a        = $workbook2->add_format();
3490           my $format2b        = $workbook2->add_format();
3491
3492
3493           # Create a global format object that isn't tied to a workbook
3494           my $global_format   = Spreadsheet::WriteExcel::Format->new();
3495
3496           # Set the formatting
3497           $global_format->set_color('blue');
3498           $global_format->set_bold();
3499           $global_format->set_italic();
3500
3501           # Create another example format
3502           $format1b->set_color('red');
3503
3504           # Copy the global format properties to the worksheet formats
3505           $format1a->copy($global_format);
3506           $format2a->copy($global_format);
3507
3508           # Copy a format from worksheet1 to worksheet2
3509           $format2b->copy($format1b);
3510
3511           # Write some output
3512           $worksheet1->write(0, 0, "Ciao", $format1a);
3513           $worksheet1->write(1, 0, "Ciao", $format1b);
3514
3515           $worksheet2->write(0, 0, "Hello", $format2a);
3516           $worksheet2->write(1, 0, "Hello", $format2b);
3517
3518       Download this example:
3519       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/copyformat.pl>
3520
3521   Example: data_validate.pl
3522       Example of how to add data validation and dropdown lists to a
3523       Spreadsheet::WriteExcel file.
3524
3525       Data validation is a feature of Excel which allows you to restrict the
3526       data that a users enters in a cell and to display help and warning
3527       messages. It also allows you to restrict input to values in a drop down
3528       list.
3529
3530       Source code for this example:
3531
3532           #!/usr/bin/perl -w
3533
3534           ###############################################################################
3535           #
3536           # Example of how to add data validation and dropdown lists to a
3537           # Spreadsheet::WriteExcel file.
3538           #
3539           # Data validation is a feature of Excel which allows you to restrict the data
3540           # that a users enters in a cell and to display help and warning messages. It
3541           # also allows you to restrict input to values in a drop down list.
3542           #
3543           # reverse('(c)'), August 2008, John McNamara, jmcnamara@cpan.org
3544           #
3545
3546           use strict;
3547           use Spreadsheet::WriteExcel;
3548
3549           my $workbook  = Spreadsheet::WriteExcel->new('data_validate.xls');
3550           my $worksheet = $workbook->add_worksheet();
3551
3552           # Add a format for the header cells.
3553           my $header_format = $workbook->add_format(
3554                                                       border      => 1,
3555                                                       bg_color    => 43,
3556                                                       bold        => 1,
3557                                                       text_wrap   => 1,
3558                                                       valign      => 'vcenter',
3559                                                       indent      => 1,
3560                                                    );
3561
3562           # Set up layout of the worksheet.
3563           $worksheet->set_column('A:A', 64);
3564           $worksheet->set_column('B:B', 15);
3565           $worksheet->set_column('D:D', 15);
3566           $worksheet->set_row(0, 36);
3567           $worksheet->set_selection('B3');
3568
3569
3570           # Write the header cells and some data that will be used in the examples.
3571           my $row = 0;
3572           my $txt;
3573           my $heading1 = 'Some examples of data validation in Spreadsheet::WriteExcel';
3574           my $heading2 = 'Enter values in this column';
3575           my $heading3 = 'Sample Data';
3576
3577           $worksheet->write('A1', $heading1, $header_format);
3578           $worksheet->write('B1', $heading2, $header_format);
3579           $worksheet->write('D1', $heading3, $header_format);
3580
3581           $worksheet->write('D3', ['Integers',   1, 10]);
3582           $worksheet->write('D4', ['List data', 'open', 'high', 'close']);
3583           $worksheet->write('D5', ['Formula',   '=AND(F5=50,G5=60)', 50, 60]);
3584
3585
3586           #
3587           # Example 1. Limiting input to an integer in a fixed range.
3588           #
3589           $txt = 'Enter an integer between 1 and 10';
3590           $row += 2;
3591
3592           $worksheet->write($row, 0, $txt);
3593           $worksheet->data_validation($row, 1,
3594               {
3595                   validate        => 'integer',
3596                   criteria        => 'between',
3597                   minimum         => 1,
3598                   maximum         => 10,
3599               });
3600
3601
3602           #
3603           # Example 2. Limiting input to an integer outside a fixed range.
3604           #
3605           $txt = 'Enter an integer that is not between 1 and 10 (using cell references)';
3606           $row += 2;
3607
3608           $worksheet->write($row, 0, $txt);
3609           $worksheet->data_validation($row, 1,
3610               {
3611                   validate        => 'integer',
3612                   criteria        => 'not between',
3613                   minimum         => '=E3',
3614                   maximum         => '=F3',
3615               });
3616
3617
3618           #
3619           # Example 3. Limiting input to an integer greater than a fixed value.
3620           #
3621           $txt = 'Enter an integer greater than 0';
3622           $row += 2;
3623
3624           $worksheet->write($row, 0, $txt);
3625           $worksheet->data_validation($row, 1,
3626               {
3627                   validate        => 'integer',
3628                   criteria        => '>',
3629                   value           => 0,
3630               });
3631
3632
3633           #
3634           # Example 4. Limiting input to an integer less than a fixed value.
3635           #
3636           $txt = 'Enter an integer less than 10';
3637           $row += 2;
3638
3639           $worksheet->write($row, 0, $txt);
3640           $worksheet->data_validation($row, 1,
3641               {
3642                   validate        => 'integer',
3643                   criteria        => '<',
3644                   value           => 10,
3645               });
3646
3647
3648           #
3649           # Example 5. Limiting input to a decimal in a fixed range.
3650           #
3651           $txt = 'Enter a decimal between 0.1 and 0.5';
3652           $row += 2;
3653
3654           $worksheet->write($row, 0, $txt);
3655           $worksheet->data_validation($row, 1,
3656               {
3657                   validate        => 'decimal',
3658                   criteria        => 'between',
3659                   minimum         => 0.1,
3660                   maximum         => 0.5,
3661               });
3662
3663
3664           #
3665           # Example 6. Limiting input to a value in a dropdown list.
3666           #
3667           $txt = 'Select a value from a drop down list';
3668           $row += 2;
3669
3670           $worksheet->write($row, 0, $txt);
3671           $worksheet->data_validation($row, 1,
3672               {
3673                   validate        => 'list',
3674                   source          => ['open', 'high', 'close'],
3675               });
3676
3677
3678           #
3679           # Example 6. Limiting input to a value in a dropdown list.
3680           #
3681           $txt = 'Select a value from a drop down list (using a cell range)';
3682           $row += 2;
3683
3684           $worksheet->write($row, 0, $txt);
3685           $worksheet->data_validation($row, 1,
3686               {
3687                   validate        => 'list',
3688                   source          => '=E4:G4',
3689               });
3690
3691
3692           #
3693           # Example 7. Limiting input to a date in a fixed range.
3694           #
3695           $txt = 'Enter a date between 1/1/2008 and 12/12/2008';
3696           $row += 2;
3697
3698           $worksheet->write($row, 0, $txt);
3699           $worksheet->data_validation($row, 1,
3700               {
3701                   validate        => 'date',
3702                   criteria        => 'between',
3703                   minimum         => '2008-01-01T',
3704                   maximum         => '2008-12-12T',
3705               });
3706
3707
3708           #
3709           # Example 8. Limiting input to a time in a fixed range.
3710           #
3711           $txt = 'Enter a time between 6:00 and 12:00';
3712           $row += 2;
3713
3714           $worksheet->write($row, 0, $txt);
3715           $worksheet->data_validation($row, 1,
3716               {
3717                   validate        => 'time',
3718                   criteria        => 'between',
3719                   minimum         => 'T06:00',
3720                   maximum         => 'T12:00',
3721               });
3722
3723
3724           #
3725           # Example 9. Limiting input to a string greater than a fixed length.
3726           #
3727           $txt = 'Enter a string longer than 3 characters';
3728           $row += 2;
3729
3730           $worksheet->write($row, 0, $txt);
3731           $worksheet->data_validation($row, 1,
3732               {
3733                   validate        => 'length',
3734                   criteria        => '>',
3735                   value           => 3,
3736               });
3737
3738
3739           #
3740           # Example 10. Limiting input based on a formula.
3741           #
3742           $txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"';
3743           $row += 2;
3744
3745           $worksheet->write($row, 0, $txt);
3746           $worksheet->data_validation($row, 1,
3747               {
3748                   validate        => 'custom',
3749                   value           => '=AND(F5=50,G5=60)',
3750               });
3751
3752
3753           #
3754           # Example 11. Displaying and modify data validation messages.
3755           #
3756           $txt = 'Displays a message when you select the cell';
3757           $row += 2;
3758
3759           $worksheet->write($row, 0, $txt);
3760           $worksheet->data_validation($row, 1,
3761               {
3762                   validate      => 'integer',
3763                   criteria      => 'between',
3764                   minimum       => 1,
3765                   maximum       => 100,
3766                   input_title   => 'Enter an integer:',
3767                   input_message => 'between 1 and 100',
3768               });
3769
3770
3771           #
3772           # Example 12. Displaying and modify data validation messages.
3773           #
3774           $txt = 'Display a custom error message when integer isn\'t between 1 and 100';
3775           $row += 2;
3776
3777           $worksheet->write($row, 0, $txt);
3778           $worksheet->data_validation($row, 1,
3779               {
3780                   validate      => 'integer',
3781                   criteria      => 'between',
3782                   minimum       => 1,
3783                   maximum       => 100,
3784                   input_title   => 'Enter an integer:',
3785                   input_message => 'between 1 and 100',
3786                   error_title   => 'Input value is not valid!',
3787                   error_message => 'It should be an integer between 1 and 100',
3788               });
3789
3790
3791           #
3792           # Example 13. Displaying and modify data validation messages.
3793           #
3794           $txt = 'Display a custom information message when integer isn\'t between 1 and 100';
3795           $row += 2;
3796
3797           $worksheet->write($row, 0, $txt);
3798           $worksheet->data_validation($row, 1,
3799               {
3800                   validate      => 'integer',
3801                   criteria      => 'between',
3802                   minimum       => 1,
3803                   maximum       => 100,
3804                   input_title   => 'Enter an integer:',
3805                   input_message => 'between 1 and 100',
3806                   error_title   => 'Input value is not valid!',
3807                   error_message => 'It should be an integer between 1 and 100',
3808                   error_type    => 'information',
3809               });
3810
3811
3812           __END__
3813
3814       Download this example:
3815       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/data_validate.pl>
3816
3817   Example: date_time.pl
3818       Spreadsheet::WriteExcel example of writing dates and times using the
3819       write_date_time() Worksheet method.
3820
3821       Source code for this example:
3822
3823           #!/usr/bin/perl -w
3824
3825           ###############################################################################
3826           #
3827           # Spreadsheet::WriteExcel example of writing dates and times using the
3828           # write_date_time() Worksheet method.
3829           #
3830           # reverse('(c)'), August 2004, John McNamara, jmcnamara@cpan.org
3831           #
3832
3833           use strict;
3834           use Spreadsheet::WriteExcel;
3835
3836
3837           # Create a new workbook and add a worksheet
3838           my $workbook  = Spreadsheet::WriteExcel->new("date_time.xls");
3839           my $worksheet = $workbook->add_worksheet();
3840           my $bold      = $workbook->add_format(bold => 1);
3841           my $row       = 0;
3842
3843
3844           # Expand the first column so that the date is visible.
3845           $worksheet->set_column("A:B", 30);
3846
3847
3848           # Write the column headers
3849           $worksheet->write('A1', 'Formatted date', $bold);
3850           $worksheet->write('B1', 'Format',         $bold);
3851
3852
3853           # Examples date and time formats. In the output file compare how changing
3854           # the format codes change the appearance of the date.
3855           #
3856           my @date_formats = (
3857               'dd/mm/yy',
3858               'mm/dd/yy',
3859               '',
3860               'd mm yy',
3861               'dd mm yy',
3862               '',
3863               'dd m yy',
3864               'dd mm yy',
3865               'dd mmm yy',
3866               'dd mmmm yy',
3867               '',
3868               'dd mm y',
3869               'dd mm yyy',
3870               'dd mm yyyy',
3871               '',
3872               'd mmmm yyyy',
3873               '',
3874               'dd/mm/yy',
3875               'dd/mm/yy hh:mm',
3876               'dd/mm/yy hh:mm:ss',
3877               'dd/mm/yy hh:mm:ss.000',
3878               '',
3879               'hh:mm',
3880               'hh:mm:ss',
3881               'hh:mm:ss.000',
3882           );
3883
3884
3885           # Write the same date and time using each of the above formats. The empty
3886           # string formats create a blank line to make the example clearer.
3887           #
3888           for my $date_format (@date_formats) {
3889               $row++;
3890               next if $date_format eq '';
3891
3892               # Create a format for the date or time.
3893               my $format =  $workbook->add_format(
3894                                                   num_format => $date_format,
3895                                                   align      => 'left'
3896                                                  );
3897
3898               # Write the same date using different formats.
3899               $worksheet->write_date_time($row, 0, '2004-08-01T12:30:45.123', $format);
3900               $worksheet->write          ($row, 1, $date_format);
3901           }
3902
3903
3904           # The following is an example of an invalid date. It is written as a string
3905           # instead of a number. This is also Excel's default behaviour.
3906           #
3907           $row += 2;
3908           $worksheet->write_date_time($row, 0, '2004-13-01T12:30:45.123');
3909           $worksheet->write          ($row, 1, 'Invalid date. Written as string.', $bold);
3910
3911           __END__
3912
3913       Download this example:
3914       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/date_time.pl>
3915
3916   Example: defined_name.pl
3917       Example of how to create defined names in a Spreadsheet::WriteExcel
3918       file.
3919
3920       This method is used to defined a name that can be used to represent a
3921       value, a single cell or a range of cells in a workbook.
3922
3923       Source code for this example:
3924
3925           #!/usr/bin/perl -w
3926
3927           ###############################################################################
3928           #
3929           # Example of how to create defined names in a Spreadsheet::WriteExcel file.
3930           #
3931           # This method is used to defined a name that can be used to represent a value,
3932           # a single cell or a range of cells in a workbook.
3933           #
3934           # reverse('(c)'), September 2008, John McNamara, jmcnamara@cpan.org
3935           #
3936
3937           use strict;
3938           use Spreadsheet::WriteExcel;
3939
3940           my $workbook   = Spreadsheet::WriteExcel->new('defined_name.xls');
3941           my $worksheet1 = $workbook->add_worksheet();
3942           my $worksheet2 = $workbook->add_worksheet();
3943
3944
3945           $workbook->define_name('Exchange_rate', '=0.96');
3946           $workbook->define_name('Sales',         '=Sheet1!$G$1:$H$10');
3947           $workbook->define_name('Sheet2!Sales',  '=Sheet2!$G$1:$G$10');
3948
3949
3950           for my $worksheet ($workbook->sheets()) {
3951               $worksheet->set_column('A:A', 45);
3952               $worksheet->write('A2', 'This worksheet contains some defined names,');
3953               $worksheet->write('A3', 'See the Insert -> Name -> Define dialog.');
3954
3955           }
3956
3957
3958           $worksheet1->write('A4', '=Exchange_rate');
3959
3960           __END__
3961
3962       Download this example:
3963       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/defined_name.pl>
3964
3965   Example: diag_border.pl
3966       A simple formatting example that demonstrates how to add a diagonal
3967       cell border with Spreadsheet::WriteExcel
3968
3969       Source code for this example:
3970
3971           #!/usr/bin/perl -w
3972
3973           ##############################################################################
3974           #
3975           # A simple formatting example that demonstrates how to add a diagonal cell
3976           # border with Spreadsheet::WriteExcel
3977           #
3978           # reverse('(c)'), May 2004, John McNamara, jmcnamara@cpan.org
3979           #
3980
3981           use strict;
3982           use Spreadsheet::WriteExcel;
3983
3984
3985           my $workbook  = Spreadsheet::WriteExcel->new('diag_border.xls');
3986           my $worksheet = $workbook->add_worksheet();
3987
3988
3989           my $format1   = $workbook->add_format(diag_type       => '1');
3990
3991           my $format2   = $workbook->add_format(diag_type       => '2');
3992
3993           my $format3   = $workbook->add_format(diag_type       => '3');
3994
3995           my $format4   = $workbook->add_format(
3996                                                 diag_type       => '3',
3997                                                 diag_border     => '7',
3998                                                 diag_color      => 'red',
3999                                                );
4000
4001
4002           $worksheet->write('B3',  'Text', $format1);
4003           $worksheet->write('B6',  'Text', $format2);
4004           $worksheet->write('B9',  'Text', $format3);
4005           $worksheet->write('B12', 'Text', $format4);
4006
4007
4008
4009           __END__
4010
4011       Download this example:
4012       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/diag_border.pl>
4013
4014   Example: easter_egg.pl
4015       This uses the Win32::OLE module to expose the Flight Simulator easter
4016       egg in Excel 97 SR2.
4017
4018           #!/usr/bin/perl -w
4019
4020           ###############################################################################
4021           #
4022           # This uses the Win32::OLE module to expose the Flight Simulator easter egg
4023           # in Excel 97 SR2.
4024           #
4025           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
4026           #
4027
4028           use strict;
4029           use Win32::OLE;
4030
4031           my $application = Win32::OLE->new("Excel.Application");
4032           my $workbook    = $application->Workbooks->Add;
4033           my $worksheet   = $workbook->Worksheets(1);
4034
4035           $application->{Visible} = 1;
4036
4037           $worksheet->Range("L97:X97")->Select;
4038           $worksheet->Range("M97")->Activate;
4039
4040           my $message =  "Hold down Shift and Ctrl and click the ".
4041                          "Chart Wizard icon on the toolbar.\n\n".
4042                          "Use the mouse motion and buttons to control ".
4043                          "movement. Try to find the monolith. ".
4044                          "Close this dialog first.";
4045
4046           $application->InputBox($message);
4047
4048       Download this example:
4049       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/easter_egg.pl>
4050
4051   Example: filehandle.pl
4052       Example of using Spreadsheet::WriteExcel to write Excel files to
4053       different filehandles.
4054
4055       Source code for this example:
4056
4057           #!/usr/bin/perl -w
4058
4059           ###############################################################################
4060           #
4061           # Example of using Spreadsheet::WriteExcel to write Excel files to
4062           # different filehandles.
4063           #
4064           # reverse('(c)'), April 2003, John McNamara, jmcnamara@cpan.org
4065           #
4066
4067           use strict;
4068           use Spreadsheet::WriteExcel;
4069           use IO::Scalar;
4070
4071
4072
4073
4074           ###############################################################################
4075           #
4076           # Example 1. This demonstrates the standard way of creating an Excel file by
4077           # specifying a file name.
4078           #
4079
4080           my $workbook1  = Spreadsheet::WriteExcel->new('fh_01.xls');
4081           my $worksheet1 = $workbook1->add_worksheet();
4082
4083           $worksheet1->write(0, 0,  "Hi Excel!");
4084
4085
4086
4087
4088           ###############################################################################
4089           #
4090           # Example 2. Write an Excel file to an existing filehandle.
4091           #
4092
4093           open    TEST, "> fh_02.xls" or die "Couldn't open file: $!";
4094           binmode TEST; # Always do this regardless of whether the platform requires it.
4095
4096           my $workbook2  = Spreadsheet::WriteExcel->new(\*TEST);
4097           my $worksheet2 = $workbook2->add_worksheet();
4098
4099           $worksheet2->write(0, 0,  "Hi Excel!");
4100
4101
4102
4103
4104           ###############################################################################
4105           #
4106           # Example 3. Write an Excel file to an existing OO style filehandle.
4107           #
4108
4109           my $fh = FileHandle->new("> fh_03.xls")
4110                    or die "Couldn't open file: $!";
4111
4112           binmode($fh);
4113
4114           my $workbook3  = Spreadsheet::WriteExcel->new($fh);
4115           my $worksheet3 = $workbook3->add_worksheet();
4116
4117           $worksheet3->write(0, 0,  "Hi Excel!");
4118
4119
4120
4121
4122           ###############################################################################
4123           #
4124           # Example 4. Write an Excel file to a string via IO::Scalar. Please refer to
4125           # the IO::Scalar documentation for further details.
4126           #
4127
4128           my $xls_str;
4129
4130           tie *XLS, 'IO::Scalar', \$xls_str;
4131
4132           my $workbook4  = Spreadsheet::WriteExcel->new(\*XLS);
4133           my $worksheet4 = $workbook4->add_worksheet();
4134
4135           $worksheet4->write(0, 0, "Hi Excel 4");
4136           $workbook4->close(); # This is required before we use the scalar
4137
4138
4139           # The Excel file is now in $xls_str. As a demonstration, print it to a file.
4140           open    TMP, "> fh_04.xls" or die "Couldn't open file: $!";
4141           binmode TMP;
4142           print   TMP  $xls_str;
4143           close   TMP;
4144
4145
4146
4147
4148           ###############################################################################
4149           #
4150           # Example 5. Write an Excel file to a string via IO::Scalar's newer interface.
4151           # Please refer to the IO::Scalar documentation for further details.
4152           #
4153           my $xls_str2;
4154
4155           my $fh5 = IO::Scalar->new(\$xls_str2);
4156
4157
4158           my $workbook5  = Spreadsheet::WriteExcel->new($fh5);
4159           my $worksheet5 = $workbook5->add_worksheet();
4160
4161           $worksheet5->write(0, 0, "Hi Excel 5");
4162           $workbook5->close(); # This is required before we use the scalar
4163
4164           # The Excel file is now in $xls_str. As a demonstration, print it to a file.
4165           open    TMP, "> fh_05.xls" or die "Couldn't open file: $!";
4166           binmode TMP;
4167           print   TMP  $xls_str2;
4168           close   TMP;
4169
4170       Download this example:
4171       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/filehandle.pl>
4172
4173   Example: formula_result.pl
4174       Example of how to write Spreadsheet::WriteExcel formulas with a user
4175       specified result.
4176
4177       This is generally only required when writing a spreadsheet for an
4178       application other than Excel where the formula isn't evaluated.
4179
4180       Source code for this example:
4181
4182           #!/usr/bin/perl -w
4183
4184           #######################################################################
4185           #
4186           # Example of how to write Spreadsheet::WriteExcel formulas with a user
4187           # specified result.
4188           #
4189           # This is generally only required when writing a spreadsheet for an
4190           # application other than Excel where the formula isn't evaluated.
4191           #
4192           # reverse('(c)'), August 2005, John McNamara, jmcnamara@cpan.org
4193           #
4194
4195           use strict;
4196           use Spreadsheet::WriteExcel;
4197
4198           my $workbook  = Spreadsheet::WriteExcel->new('formula_result.xls');
4199           my $worksheet = $workbook->add_worksheet();
4200           my $format    = $workbook->add_format(color => 'blue');
4201
4202
4203           $worksheet->write('A1', '=1+2');
4204           $worksheet->write('A2', '=1+2',                     $format, 4);
4205           $worksheet->write('A3', '="ABC"',                   undef,   'DEF');
4206           $worksheet->write('A4', '=IF(A1 > 1, TRUE, FALSE)', undef,   'TRUE');
4207           $worksheet->write('A5', '=1/0',                     undef,   '#DIV/0!');
4208
4209
4210           __END__
4211
4212       Download this example:
4213       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/formula_result.pl>
4214
4215   Example: headers.pl
4216       This program shows several examples of how to set up headers and
4217       footers with Spreadsheet::WriteExcel.
4218
4219       The control characters used in the header/footer strings are:
4220
4221           Control             Category            Description
4222           =======             ========            ===========
4223           &L                  Justification       Left
4224           &C                                      Center
4225           &R                                      Right
4226
4227           &P                  Information         Page number
4228           &N                                      Total number of pages
4229           &D                                      Date
4230           &T                                      Time
4231           &F                                      File name
4232           &A                                      Worksheet name
4233
4234           &fontsize           Font                Font size
4235           &"font,style"                           Font name and style
4236           &U                                      Single underline
4237           &E                                      Double underline
4238           &S                                      Strikethrough
4239           &X                                      Superscript
4240           &Y                                      Subscript
4241
4242           &&                  Miscellaneous       Literal ampersand &
4243
4244       See the main Spreadsheet::WriteExcel documentation for more
4245       information.
4246
4247       Source code for this example:
4248
4249           #!/usr/bin/perl -w
4250
4251           ######################################################################
4252           #
4253           # This program shows several examples of how to set up headers and
4254           # footers with Spreadsheet::WriteExcel.
4255           #
4256           # The control characters used in the header/footer strings are:
4257           #
4258           #     Control             Category            Description
4259           #     =======             ========            ===========
4260           #     &L                  Justification       Left
4261           #     &C                                      Center
4262           #     &R                                      Right
4263           #
4264           #     &P                  Information         Page number
4265           #     &N                                      Total number of pages
4266           #     &D                                      Date
4267           #     &T                                      Time
4268           #     &F                                      File name
4269           #     &A                                      Worksheet name
4270           #
4271           #     &fontsize           Font                Font size
4272           #     &"font,style"                           Font name and style
4273           #     &U                                      Single underline
4274           #     &E                                      Double underline
4275           #     &S                                      Strikethrough
4276           #     &X                                      Superscript
4277           #     &Y                                      Subscript
4278           #
4279           #     &&                  Miscellaneous       Literal ampersand &
4280           #
4281           # See the main Spreadsheet::WriteExcel documentation for more information.
4282           #
4283           # reverse('(c)'), March 2002, John McNamara, jmcnamara@cpan.org
4284           #
4285
4286
4287           use strict;
4288           use Spreadsheet::WriteExcel;
4289
4290           my $workbook  = Spreadsheet::WriteExcel->new("headers.xls");
4291           my $preview   = "Select Print Preview to see the header and footer";
4292
4293
4294           ######################################################################
4295           #
4296           # A simple example to start
4297           #
4298           my $worksheet1  = $workbook->add_worksheet('Simple');
4299
4300           my $header1     = '&CHere is some centred text.';
4301
4302           my $footer1     = '&LHere is some left aligned text.';
4303
4304
4305           $worksheet1->set_header($header1);
4306           $worksheet1->set_footer($footer1);
4307
4308           $worksheet1->set_column('A:A', 50);
4309           $worksheet1->write('A1', $preview);
4310
4311
4312
4313
4314           ######################################################################
4315           #
4316           # This is an example of some of the header/footer variables.
4317           #
4318           my $worksheet2  = $workbook->add_worksheet('Variables');
4319
4320           my $header2     = '&LPage &P of &N'.
4321                             '&CFilename: &F' .
4322                             '&RSheetname: &A';
4323
4324           my $footer2     = '&LCurrent date: &D'.
4325                             '&RCurrent time: &T';
4326
4327
4328
4329           $worksheet2->set_header($header2);
4330           $worksheet2->set_footer($footer2);
4331
4332
4333           $worksheet2->set_column('A:A', 50);
4334           $worksheet2->write('A1', $preview);
4335           $worksheet2->write('A21', "Next sheet");
4336           $worksheet2->set_h_pagebreaks(20);
4337
4338
4339
4340           ######################################################################
4341           #
4342           # This example shows how to use more than one font
4343           #
4344           my $worksheet3 = $workbook->add_worksheet('Mixed fonts');
4345
4346           my $header3    = '&C' .
4347                            '&"Courier New,Bold"Hello ' .
4348                            '&"Arial,Italic"World';
4349
4350           my $footer3    = '&C' .
4351                            '&"Symbol"e' .
4352                            '&"Arial" = mc&X2';
4353
4354           $worksheet3->set_header($header3);
4355           $worksheet3->set_footer($footer3);
4356
4357           $worksheet3->set_column('A:A', 50);
4358           $worksheet3->write('A1', $preview);
4359
4360
4361
4362
4363           ######################################################################
4364           #
4365           # Example of line wrapping
4366           #
4367           my $worksheet4 = $workbook->add_worksheet('Word wrap');
4368
4369           my $header4    = "&CHeading 1\nHeading 2\nHeading 3";
4370
4371           $worksheet4->set_header($header4);
4372
4373           $worksheet4->set_column('A:A', 50);
4374           $worksheet4->write('A1', $preview);
4375
4376
4377
4378
4379           ######################################################################
4380           #
4381           # Example of inserting a literal ampersand &
4382           #
4383           my $worksheet5 = $workbook->add_worksheet('Ampersand');
4384
4385           my $header5    = "&CCuriouser && Curiouser - Attorneys at Law";
4386
4387           $worksheet5->set_header($header5);
4388
4389           $worksheet5->set_column('A:A', 50);
4390           $worksheet5->write('A1', $preview);
4391
4392       Download this example:
4393       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/headers.pl>
4394
4395   Example: hide_sheet.pl
4396       Example of how to hide a worksheet with Spreadsheet::WriteExcel.
4397
4398       Source code for this example:
4399
4400           #!/usr/bin/perl -w
4401
4402           #######################################################################
4403           #
4404           # Example of how to hide a worksheet with Spreadsheet::WriteExcel.
4405           #
4406           # reverse('(c)'), April 2005, John McNamara, jmcnamara@cpan.org
4407           #
4408
4409           use strict;
4410           use Spreadsheet::WriteExcel;
4411
4412           my $workbook   = Spreadsheet::WriteExcel->new('hidden.xls');
4413           my $worksheet1 = $workbook->add_worksheet();
4414           my $worksheet2 = $workbook->add_worksheet();
4415           my $worksheet3 = $workbook->add_worksheet();
4416
4417           # Sheet2 won't be visible until it is unhidden in Excel.
4418           $worksheet2->hide();
4419
4420           $worksheet1->write(0, 0, 'Sheet2 is hidden');
4421           $worksheet2->write(0, 0, 'How did you find me?');
4422           $worksheet3->write(0, 0, 'Sheet2 is hidden');
4423
4424
4425           __END__
4426
4427       Download this example:
4428       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/hide_sheet.pl>
4429
4430   Example: hyperlink1.pl
4431       Example of how to use the WriteExcel module to write hyperlinks.
4432
4433       See also hyperlink2.pl for worksheet URL examples.
4434
4435       Source code for this example:
4436
4437           #!/usr/bin/perl -w
4438
4439           ###############################################################################
4440           #
4441           # Example of how to use the WriteExcel module to write hyperlinks.
4442           #
4443           # See also hyperlink2.pl for worksheet URL examples.
4444           #
4445           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
4446           #
4447
4448           use strict;
4449           use Spreadsheet::WriteExcel;
4450
4451           # Create a new workbook and add a worksheet
4452           my $workbook  = Spreadsheet::WriteExcel->new("hyperlink.xls");
4453           my $worksheet = $workbook->add_worksheet('Hyperlinks');
4454
4455           # Format the first column
4456           $worksheet->set_column('A:A', 30);
4457           $worksheet->set_selection('B1');
4458
4459
4460           # Add a sample format
4461           my $format = $workbook->add_format();
4462           $format->set_size(12);
4463           $format->set_bold();
4464           $format->set_color('red');
4465           $format->set_underline();
4466
4467
4468           # Write some hyperlinks
4469           $worksheet->write('A1', 'http://www.perl.com/'                );
4470           $worksheet->write('A3', 'http://www.perl.com/', 'Perl home'   );
4471           $worksheet->write('A5', 'http://www.perl.com/', undef, $format);
4472           $worksheet->write('A7', 'mailto:jmcnamara@cpan.org', 'Mail me');
4473
4474           # Write a URL that isn't a hyperlink
4475           $worksheet->write_string('A9', 'http://www.perl.com/');
4476
4477       Download this example:
4478       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/hyperlink1.pl>
4479
4480   Example: hyperlink2.pl
4481       Example of how to use the WriteExcel module to write internal and
4482       internal hyperlinks.
4483
4484       If you wish to run this program and follow the hyperlinks you should
4485       create the following directory structure:
4486
4487           C:\ -- Temp --+-- Europe
4488                         |
4489                         \-- Asia
4490
4491       See also hyperlink1.pl for web URL examples.
4492
4493           #!/usr/bin/perl -w
4494
4495           ###############################################################################
4496           #
4497           # Example of how to use the WriteExcel module to write internal and internal
4498           # hyperlinks.
4499           #
4500           # If you wish to run this program and follow the hyperlinks you should create
4501           # the following directory structure:
4502           #
4503           #     C:\ -- Temp --+-- Europe
4504           #                   |
4505           #                   \-- Asia
4506           #
4507           #
4508           # See also hyperlink1.pl for web URL examples.
4509           #
4510           # reverse('(c)'), February 2002, John McNamara, jmcnamara@cpan.org
4511           #
4512
4513
4514           use strict;
4515           use Spreadsheet::WriteExcel;
4516
4517           # Create three workbooks:
4518           #   C:\Temp\Europe\Ireland.xls
4519           #   C:\Temp\Europe\Italy.xls
4520           #   C:\Temp\Asia\China.xls
4521           #
4522           my $ireland   = Spreadsheet::WriteExcel->new('C:\Temp\Europe\Ireland.xls');
4523           my $ire_links = $ireland->add_worksheet('Links');
4524           my $ire_sales = $ireland->add_worksheet('Sales');
4525           my $ire_data  = $ireland->add_worksheet('Product Data');
4526
4527           my $italy     = Spreadsheet::WriteExcel->new('C:\Temp\Europe\Italy.xls');
4528           my $ita_links = $italy->add_worksheet('Links');
4529           my $ita_sales = $italy->add_worksheet('Sales');
4530           my $ita_data  = $italy->add_worksheet('Product Data');
4531
4532           my $china     = Spreadsheet::WriteExcel->new('C:\Temp\Asia\China.xls');
4533           my $cha_links = $china->add_worksheet('Links');
4534           my $cha_sales = $china->add_worksheet('Sales');
4535           my $cha_data  = $china->add_worksheet('Product Data');
4536
4537           # Add a format
4538           my $format = $ireland->add_format(color => 'green', bold => 1);
4539           $ire_links->set_column('A:B', 25);
4540
4541
4542           ###############################################################################
4543           #
4544           # Examples of internal links
4545           #
4546           $ire_links->write('A1', 'Internal links', $format);
4547
4548           # Internal link
4549           $ire_links->write('A2', 'internal:Sales!A2');
4550
4551           # Internal link to a range
4552           $ire_links->write('A3', 'internal:Sales!A3:D3');
4553
4554           # Internal link with an alternative string
4555           $ire_links->write('A4', 'internal:Sales!A4', 'Link');
4556
4557           # Internal link with a format
4558           $ire_links->write('A5', 'internal:Sales!A5', $format);
4559
4560           # Internal link with an alternative string and format
4561           $ire_links->write('A6', 'internal:Sales!A6', 'Link', $format);
4562
4563           # Internal link (spaces in worksheet name)
4564           $ire_links->write('A7', q{internal:'Product Data'!A7});
4565
4566
4567           ###############################################################################
4568           #
4569           # Examples of external links
4570           #
4571           $ire_links->write('B1', 'External links', $format);
4572
4573           # External link to a local file
4574           $ire_links->write('B2', 'external:Italy.xls');
4575
4576           # External link to a local file with worksheet
4577           $ire_links->write('B3', 'external:Italy.xls#Sales!B3');
4578
4579           # External link to a local file with worksheet and alternative string
4580           $ire_links->write('B4', 'external:Italy.xls#Sales!B4', 'Link');
4581
4582           # External link to a local file with worksheet and format
4583           $ire_links->write('B5', 'external:Italy.xls#Sales!B5', $format);
4584
4585           # External link to a remote file, absolute path
4586           $ire_links->write('B6', 'external:c:/Temp/Asia/China.xls');
4587
4588           # External link to a remote file, relative path
4589           $ire_links->write('B7', 'external:../Asia/China.xls');
4590
4591           # External link to a remote file with worksheet
4592           $ire_links->write('B8', 'external:c:/Temp/Asia/China.xls#Sales!B8');
4593
4594           # External link to a remote file with worksheet (with spaces in the name)
4595           $ire_links->write('B9', q{external:c:/Temp/Asia/China.xls#'Product Data'!B9});
4596
4597
4598           ###############################################################################
4599           #
4600           # Some utility links to return to the main sheet
4601           #
4602           $ire_sales->write('A2', 'internal:Links!A2', 'Back');
4603           $ire_sales->write('A3', 'internal:Links!A3', 'Back');
4604           $ire_sales->write('A4', 'internal:Links!A4', 'Back');
4605           $ire_sales->write('A5', 'internal:Links!A5', 'Back');
4606           $ire_sales->write('A6', 'internal:Links!A6', 'Back');
4607           $ire_data-> write('A7', 'internal:Links!A7', 'Back');
4608
4609           $ita_links->write('A1', 'external:Ireland.xls#Links!B2', 'Back');
4610           $ita_sales->write('B3', 'external:Ireland.xls#Links!B3', 'Back');
4611           $ita_sales->write('B4', 'external:Ireland.xls#Links!B4', 'Back');
4612           $ita_sales->write('B5', 'external:Ireland.xls#Links!B5', 'Back');
4613           $cha_links->write('A1', 'external:../Europe/Ireland.xls#Links!B6', 'Back');
4614           $cha_sales->write('B8', 'external:../Europe/Ireland.xls#Links!B8', 'Back');
4615           $cha_data-> write('B9', 'external:../Europe/Ireland.xls#Links!B9', 'Back');
4616
4617       Download this example:
4618       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/hyperlink2.pl>
4619
4620   Example: images.pl
4621       Example of how to insert images into an Excel worksheet using the
4622       Spreadsheet::WriteExcel insert_image() method.
4623
4624       Source code for this example:
4625
4626           #!/usr/bin/perl -w
4627
4628           #######################################################################
4629           #
4630           # Example of how to insert images into an Excel worksheet using the
4631           # Spreadsheet::WriteExcel insert_image() method.
4632           #
4633           # reverse('(c)'), October 2001, John McNamara, jmcnamara@cpan.org
4634           #
4635
4636           use strict;
4637           use Spreadsheet::WriteExcel;
4638
4639           # Create a new workbook called simple.xls and add a worksheet
4640           my $workbook   = Spreadsheet::WriteExcel->new("images.xls");
4641           my $worksheet1 = $workbook->add_worksheet('Image 1');
4642           my $worksheet2 = $workbook->add_worksheet('Image 2');
4643           my $worksheet3 = $workbook->add_worksheet('Image 3');
4644           my $worksheet4 = $workbook->add_worksheet('Image 4');
4645
4646           # Insert a basic image
4647           $worksheet1->write('A10', "Image inserted into worksheet.");
4648           $worksheet1->insert_image('A1', 'republic.png');
4649
4650
4651           # Insert an image with an offset
4652           $worksheet2->write('A10', "Image inserted with an offset.");
4653           $worksheet2->insert_image('A1', 'republic.png', 32, 10);
4654
4655           # Insert a scaled image
4656           $worksheet3->write('A10', "Image scaled: width x 2, height x 0.8.");
4657           $worksheet3->insert_image('A1', 'republic.png', 0, 0, 2, 0.8);
4658
4659           # Insert an image over varied column and row sizes.
4660           $worksheet4->set_column('A:A', 5);
4661           $worksheet4->set_column('B:B', undef, undef, 1); # Hidden
4662           $worksheet4->set_column('C:D', 10);
4663           $worksheet4->set_row(0, 30);
4664           $worksheet4->set_row(3, 5);
4665
4666           $worksheet4->write('A10', "Image inserted over scaled rows and columns.");
4667           $worksheet4->insert_image('A1', 'republic.png');
4668
4669       Download this example:
4670       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/images.pl>
4671
4672   Example: indent.pl
4673       A simple formatting example using Spreadsheet::WriteExcel.
4674
4675       This program demonstrates the indentation cell format.
4676
4677       Source code for this example:
4678
4679           #!/usr/bin/perl -w
4680
4681           ##############################################################################
4682           #
4683           # A simple formatting example using Spreadsheet::WriteExcel.
4684           #
4685           # This program demonstrates the indentation cell format.
4686           #
4687           # reverse('(c)'), May 2004, John McNamara, jmcnamara@cpan.org
4688           #
4689
4690
4691           use strict;
4692           use Spreadsheet::WriteExcel;
4693
4694           my $workbook  = Spreadsheet::WriteExcel->new('indent.xls');
4695
4696           my $worksheet = $workbook->add_worksheet();
4697           my $indent1   = $workbook->add_format(indent => 1);
4698           my $indent2   = $workbook->add_format(indent => 2);
4699
4700           $worksheet->set_column('A:A', 40);
4701
4702
4703           $worksheet->write('A1', "This text is indented 1 level",  $indent1);
4704           $worksheet->write('A2', "This text is indented 2 levels", $indent2);
4705
4706
4707           __END__
4708
4709       Download this example:
4710       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/indent.pl>
4711
4712   Example: merge1.pl
4713       Simple example of merging cells using the Spreadsheet::WriteExcel
4714       module.
4715
4716       This example merges three cells using the "Centre Across Selection"
4717       alignment which was the Excel 5 method of achieving a merge. For a more
4718       modern approach use the merge_range() worksheet method instead.  See
4719       the merge3.pl - merge6.pl programs.
4720
4721       Source code for this example:
4722
4723           #!/usr/bin/perl -w
4724
4725           ###############################################################################
4726           #
4727           # Simple example of merging cells using the Spreadsheet::WriteExcel module.
4728           #
4729           # This example merges three cells using the "Centre Across Selection"
4730           # alignment which was the Excel 5 method of achieving a merge. For a more
4731           # modern approach use the merge_range() worksheet method instead.
4732           # See the merge3.pl - merge6.pl programs.
4733           #
4734           # reverse('(c)'), August 2002, John McNamara, jmcnamara@cpan.org
4735           #
4736
4737           use strict;
4738           use Spreadsheet::WriteExcel;
4739
4740           # Create a new workbook and add a worksheet
4741           my $workbook  = Spreadsheet::WriteExcel->new("merge1.xls");
4742           my $worksheet = $workbook->add_worksheet();
4743
4744
4745           # Increase the cell size of the merged cells to highlight the formatting.
4746           $worksheet->set_column('B:D', 20);
4747           $worksheet->set_row(2, 30);
4748
4749
4750           # Create a merge format
4751           my $format = $workbook->add_format(center_across => 1);
4752
4753
4754           # Only one cell should contain text, the others should be blank.
4755           $worksheet->write      (2, 1, "Center across selection", $format);
4756           $worksheet->write_blank(2, 2,                 $format);
4757           $worksheet->write_blank(2, 3,                 $format);
4758
4759       Download this example:
4760       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/merge1.pl>
4761
4762   Example: merge2.pl
4763       Simple example of merging cells using the Spreadsheet::WriteExcel
4764       module
4765
4766       This example merges three cells using the "Centre Across Selection"
4767       alignment which was the Excel 5 method of achieving a merge. For a more
4768       modern approach use the merge_range() worksheet method instead.  See
4769       the merge3.pl - merge6.pl programs.
4770
4771       Source code for this example:
4772
4773           #!/usr/bin/perl -w
4774
4775           ###############################################################################
4776           #
4777           # Simple example of merging cells using the Spreadsheet::WriteExcel module
4778           #
4779           # This example merges three cells using the "Centre Across Selection"
4780           # alignment which was the Excel 5 method of achieving a merge. For a more
4781           # modern approach use the merge_range() worksheet method instead.
4782           # See the merge3.pl - merge6.pl programs.
4783           #
4784           # reverse('(c)'), August 2002, John McNamara, jmcnamara@cpan.org
4785           #
4786
4787           use strict;
4788           use Spreadsheet::WriteExcel;
4789
4790           # Create a new workbook and add a worksheet
4791           my $workbook  = Spreadsheet::WriteExcel->new("merge2.xls");
4792           my $worksheet = $workbook->add_worksheet();
4793
4794
4795           # Increase the cell size of the merged cells to highlight the formatting.
4796           $worksheet->set_column(1, 2, 30);
4797           $worksheet->set_row(2, 40);
4798
4799
4800           # Create a merged format
4801           my $format = $workbook->add_format(
4802                                                   center_across   => 1,
4803                                                   bold            => 1,
4804                                                   size            => 15,
4805                                                   pattern         => 1,
4806                                                   border          => 6,
4807                                                   color           => 'white',
4808                                                   fg_color        => 'green',
4809                                                   border_color    => 'yellow',
4810                                                   align           => 'vcenter',
4811                                             );
4812
4813
4814           # Only one cell should contain text, the others should be blank.
4815           $worksheet->write      (2, 1, "Center across selection", $format);
4816           $worksheet->write_blank(2, 2,                            $format);
4817
4818       Download this example:
4819       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/merge2.pl>
4820
4821   Example: merge3.pl
4822       Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a
4823       merged cell. There are two options write_url_range() with a standard
4824       merge format or merge_range().
4825
4826       Source code for this example:
4827
4828           #!/usr/bin/perl -w
4829
4830           ###############################################################################
4831           #
4832           # Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a
4833           # merged cell. There are two options write_url_range() with a standard merge
4834           # format or merge_range().
4835           #
4836           # reverse('(c)'), September 2002, John McNamara, jmcnamara@cpan.org
4837           #
4838
4839           use strict;
4840           use Spreadsheet::WriteExcel;
4841
4842           # Create a new workbook and add a worksheet
4843           my $workbook  = Spreadsheet::WriteExcel->new('merge3.xls');
4844           my $worksheet = $workbook->add_worksheet();
4845
4846
4847           # Increase the cell size of the merged cells to highlight the formatting.
4848           $worksheet->set_row($_, 30) for (1, 3, 6, 7);
4849           $worksheet->set_column('B:D', 20);
4850
4851
4852           ###############################################################################
4853           #
4854           # Example 1: Merge cells containing a hyperlink using write_url_range()
4855           # and the standard Excel 5+ merge property.
4856           #
4857           my $format1 = $workbook->add_format(
4858                                               center_across   => 1,
4859                                               border          => 1,
4860                                               underline       => 1,
4861                                               color           => 'blue',
4862                                            );
4863
4864           # Write the cells to be merged
4865           $worksheet->write_url_range('B2:D2', 'http://www.perl.com', $format1);
4866           $worksheet->write_blank('C2', $format1);
4867           $worksheet->write_blank('D2', $format1);
4868
4869
4870
4871           ###############################################################################
4872           #
4873           # Example 2: Merge cells containing a hyperlink using merge_range().
4874           #
4875           my $format2 = $workbook->add_format(
4876                                               border      => 1,
4877                                               underline   => 1,
4878                                               color       => 'blue',
4879                                               align       => 'center',
4880                                               valign      => 'vcenter',
4881                                             );
4882
4883           # Merge 3 cells
4884           $worksheet->merge_range('B4:D4', 'http://www.perl.com', $format2);
4885
4886
4887           # Merge 3 cells over two rows
4888           $worksheet->merge_range('B7:D8', 'http://www.perl.com', $format2);
4889
4890       Download this example:
4891       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/merge3.pl>
4892
4893   Example: merge4.pl
4894       Example of how to use the Spreadsheet::WriteExcel merge_range()
4895       workbook method with complex formatting.
4896
4897       Source code for this example:
4898
4899           #!/usr/bin/perl -w
4900
4901           ###############################################################################
4902           #
4903           # Example of how to use the Spreadsheet::WriteExcel merge_range() workbook
4904           # method with complex formatting.
4905           #
4906           # reverse('(c)'), September 2002, John McNamara, jmcnamara@cpan.org
4907           #
4908
4909           use strict;
4910           use Spreadsheet::WriteExcel;
4911
4912           # Create a new workbook and add a worksheet
4913           my $workbook  = Spreadsheet::WriteExcel->new('merge4.xls');
4914           my $worksheet = $workbook->add_worksheet();
4915
4916
4917           # Increase the cell size of the merged cells to highlight the formatting.
4918           $worksheet->set_row($_, 30) for (1..11);
4919           $worksheet->set_column('B:D', 20);
4920
4921
4922           ###############################################################################
4923           #
4924           # Example 1: Text centered vertically and horizontally
4925           #
4926           my $format1 = $workbook->add_format(
4927                                               border  => 6,
4928                                               bold    => 1,
4929                                               color   => 'red',
4930                                               valign  => 'vcenter',
4931                                               align   => 'center',
4932                                              );
4933
4934
4935
4936           $worksheet->merge_range('B2:D3', 'Vertical and horizontal', $format1);
4937
4938
4939           ###############################################################################
4940           #
4941           # Example 2: Text aligned to the top and left
4942           #
4943           my $format2 = $workbook->add_format(
4944                                               border  => 6,
4945                                               bold    => 1,
4946                                               color   => 'red',
4947                                               valign  => 'top',
4948                                               align   => 'left',
4949                                             );
4950
4951
4952
4953           $worksheet->merge_range('B5:D6', 'Aligned to the top and left', $format2);
4954
4955
4956           ###############################################################################
4957           #
4958           # Example 3:  Text aligned to the bottom and right
4959           #
4960           my $format3 = $workbook->add_format(
4961                                               border  => 6,
4962                                               bold    => 1,
4963                                               color   => 'red',
4964                                               valign  => 'bottom',
4965                                               align   => 'right',
4966                                             );
4967
4968
4969
4970           $worksheet->merge_range('B8:D9', 'Aligned to the bottom and right', $format3);
4971
4972
4973           ###############################################################################
4974           #
4975           # Example 4:  Text justified (i.e. wrapped) in the cell
4976           #
4977           my $format4 = $workbook->add_format(
4978                                               border  => 6,
4979                                               bold    => 1,
4980                                               color   => 'red',
4981                                               valign  => 'top',
4982                                               align   => 'justify',
4983                                             );
4984
4985
4986
4987           $worksheet->merge_range('B11:D12', 'Justified: '.'so on and ' x18, $format4);
4988
4989       Download this example:
4990       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/merge4.pl>
4991
4992   Example: merge5.pl
4993       Example of how to use the Spreadsheet::WriteExcel merge_cells()
4994       workbook method with complex formatting and rotation.
4995
4996       Source code for this example:
4997
4998           #!/usr/bin/perl -w
4999
5000           ###############################################################################
5001           #
5002           # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
5003           # method with complex formatting and rotation.
5004           #
5005           #
5006           # reverse('(c)'), September 2002, John McNamara, jmcnamara@cpan.org
5007           #
5008
5009           use strict;
5010           use Spreadsheet::WriteExcel;
5011
5012           # Create a new workbook and add a worksheet
5013           my $workbook  = Spreadsheet::WriteExcel->new('merge5.xls');
5014           my $worksheet = $workbook->add_worksheet();
5015
5016
5017           # Increase the cell size of the merged cells to highlight the formatting.
5018           $worksheet->set_row($_, 36)         for (3..8);
5019           $worksheet->set_column($_, $_ , 15) for (1,3,5);
5020
5021
5022           ###############################################################################
5023           #
5024           # Rotation 1, letters run from top to bottom
5025           #
5026           my $format1 = $workbook->add_format(
5027                                               border      => 6,
5028                                               bold        => 1,
5029                                               color       => 'red',
5030                                               valign      => 'vcentre',
5031                                               align       => 'centre',
5032                                               rotation    => 270,
5033                                             );
5034
5035
5036           $worksheet->merge_range('B4:B9', 'Rotation 270', $format1);
5037
5038
5039           ###############################################################################
5040           #
5041           # Rotation 2, 90 deg anticlockwise
5042           #
5043           my $format2 = $workbook->add_format(
5044                                               border      => 6,
5045                                               bold        => 1,
5046                                               color       => 'red',
5047                                               valign      => 'vcentre',
5048                                               align       => 'centre',
5049                                               rotation    => 90,
5050                                             );
5051
5052
5053           $worksheet->merge_range('D4:D9', 'Rotation 90 deg', $format2);
5054
5055
5056
5057           ###############################################################################
5058           #
5059           # Rotation 3, 90 deg clockwise
5060           #
5061           my $format3 = $workbook->add_format(
5062                                               border      => 6,
5063                                               bold        => 1,
5064                                               color       => 'red',
5065                                               valign      => 'vcentre',
5066                                               align       => 'centre',
5067                                               rotation    => -90,
5068                                             );
5069
5070
5071           $worksheet->merge_range('F4:F9', 'Rotation -90 deg', $format3);
5072
5073       Download this example:
5074       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/merge5.pl>
5075
5076   Example: merge6.pl
5077       Example of how to use the Spreadsheet::WriteExcel merge_cells()
5078       workbook method with Unicode strings.
5079
5080       Source code for this example:
5081
5082           #!/usr/bin/perl -w
5083
5084           ###############################################################################
5085           #
5086           # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
5087           # method with Unicode strings.
5088           #
5089           #
5090           # reverse('(c)'), December 2005, John McNamara, jmcnamara@cpan.org
5091           #
5092
5093           use strict;
5094           use Spreadsheet::WriteExcel;
5095
5096           # Create a new workbook and add a worksheet
5097           my $workbook  = Spreadsheet::WriteExcel->new('merge6.xls');
5098           my $worksheet = $workbook->add_worksheet();
5099
5100
5101           # Increase the cell size of the merged cells to highlight the formatting.
5102           $worksheet->set_row($_, 36) for 2..9;
5103           $worksheet->set_column('B:D', 25);
5104
5105
5106           # Format for the merged cells.
5107           my $format = $workbook->add_format(
5108                                               border      => 6,
5109                                               bold        => 1,
5110                                               color       => 'red',
5111                                               size        => 20,
5112                                               valign      => 'vcentre',
5113                                               align       => 'left',
5114                                               indent      => 1,
5115                                             );
5116
5117
5118
5119
5120           ###############################################################################
5121           #
5122           # Write an Ascii string.
5123           #
5124
5125           $worksheet->merge_range('B3:D4', 'ASCII: A simple string', $format);
5126
5127
5128
5129
5130           ###############################################################################
5131           #
5132           # Write a UTF-16 Unicode string.
5133           #
5134
5135           # A phrase in Cyrillic encoded as UTF-16BE.
5136           my $utf16_str = pack "H*", '005500540046002d00310036003a0020'.
5137                                      '042d0442043e002004440440043004370430002004'.
5138                                      '3d043000200440044304410441043a043e043c0021';
5139
5140           # Note the extra parameter at the end to indicate UTF-16 encoding.
5141           $worksheet->merge_range('B6:D7', $utf16_str, $format, 1);
5142
5143
5144
5145
5146           ###############################################################################
5147           #
5148           # Write a UTF-8 Unicode string.
5149           #
5150
5151           if ($] >= 5.008) {
5152               my $smiley = chr 0x263a;
5153               $worksheet->merge_range('B9:D10', "UTF-8: A Unicode smiley $smiley",
5154                                                  $format);
5155           }
5156           else {
5157               $worksheet->merge_range('B9:D10', "UTF-8: Requires Perl 5.8", $format);
5158           }
5159
5160
5161
5162
5163           __END__
5164
5165       Download this example:
5166       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/merge6.pl>
5167
5168   Example: mod_perl1.pl
5169       Example of how to use the Spreadsheet::WriteExcel module to send an
5170       Excel file to a browser using mod_perl 1 and Apache
5171
5172       This module ties *XLS directly to Apache, and with the correct
5173       content-disposition/types it will prompt the user to save the file, or
5174       open it at this location.
5175
5176       This script is a modification of the Spreadsheet::WriteExcel cgi.pl
5177       example.
5178
5179       Change the name of this file to Cgi.pm.  Change the package location to
5180       where ever you locate this package.  In the example below it is located
5181       in the WriteExcel directory.
5182
5183       Your httpd.conf entry for this module, should you choose to use it as a
5184       stand alone app, should look similar to the following:
5185
5186           <Location /spreadsheet-test>
5187             SetHandler perl-script
5188             PerlHandler Spreadsheet::WriteExcel::Cgi
5189             PerlSendHeader On
5190           </Location>
5191
5192       The PerlHandler name above and the package name below *have* to match.
5193
5194           ###############################################################################
5195           #
5196           # Example of how to use the Spreadsheet::WriteExcel module to send an Excel
5197           # file to a browser using mod_perl 1 and Apache
5198           #
5199           # This module ties *XLS directly to Apache, and with the correct
5200           # content-disposition/types it will prompt the user to save
5201           # the file, or open it at this location.
5202           #
5203           # This script is a modification of the Spreadsheet::WriteExcel cgi.pl example.
5204           #
5205           # Change the name of this file to Cgi.pm.
5206           # Change the package location to where ever you locate this package.
5207           # In the example below it is located in the WriteExcel directory.
5208           #
5209           # Your httpd.conf entry for this module, should you choose to use it
5210           # as a stand alone app, should look similar to the following:
5211           #
5212           #     <Location /spreadsheet-test>
5213           #       SetHandler perl-script
5214           #       PerlHandler Spreadsheet::WriteExcel::Cgi
5215           #       PerlSendHeader On
5216           #     </Location>
5217           #
5218           # The PerlHandler name above and the package name below *have* to match.
5219
5220           # Apr 2001, Thomas Sullivan, webmaster@860.org
5221           # Feb 2001, John McNamara, jmcnamara@cpan.org
5222
5223           package Spreadsheet::WriteExcel::Cgi;
5224
5225           ##########################################
5226           # Pragma Definitions
5227           ##########################################
5228           use strict;
5229
5230           ##########################################
5231           # Required Modules
5232           ##########################################
5233           use Apache::Constants qw(:common);
5234           use Apache::Request;
5235           use Apache::URI; # This may not be needed
5236           use Spreadsheet::WriteExcel;
5237
5238           ##########################################
5239           # Main App Body
5240           ##########################################
5241           sub handler {
5242               # New apache object
5243               # Should you decide to use it.
5244               my $r = Apache::Request->new(shift);
5245
5246               # Set the filename and send the content type
5247               # This will appear when they save the spreadsheet
5248               my $filename ="cgitest.xls";
5249
5250               ####################################################
5251               ## Send the content type headers
5252               ####################################################
5253               print "Content-disposition: attachment;filename=$filename\n";
5254               print "Content-type: application/vnd.ms-excel\n\n";
5255
5256               ####################################################
5257               # Tie a filehandle to Apache's STDOUT.
5258               # Create a new workbook and add a worksheet.
5259               ####################################################
5260               tie *XLS => 'Apache';
5261               binmode(*XLS);
5262
5263               my $workbook  = Spreadsheet::WriteExcel->new(\*XLS);
5264               my $worksheet = $workbook->add_worksheet();
5265
5266
5267               # Set the column width for column 1
5268               $worksheet->set_column(0, 0, 20);
5269
5270
5271               # Create a format
5272               my $format = $workbook->add_format();
5273               $format->set_bold();
5274               $format->set_size(15);
5275               $format->set_color('blue');
5276
5277
5278               # Write to the workbook
5279               $worksheet->write(0, 0, "Hi Excel!", $format);
5280
5281               # You must close the workbook for Content-disposition
5282               $workbook->close();
5283           }
5284
5285           1;
5286
5287       Download this example:
5288       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/mod_perl1.pl>
5289
5290   Example: mod_perl2.pl
5291       Example of how to use the Spreadsheet::WriteExcel module to send an
5292       Excel file to a browser using mod_perl 2 and Apache.
5293
5294       This module ties *XLS directly to Apache, and with the correct
5295       content-disposition/types it will prompt the user to save the file, or
5296       open it at this location.
5297
5298       This script is a modification of the Spreadsheet::WriteExcel cgi.pl
5299       example.
5300
5301       Change the name of this file to MP2Test.pm.  Change the package
5302       location to where ever you locate this package.  In the example below
5303       it is located in the WriteExcel directory.
5304
5305       Your httpd.conf entry for this module, should you choose to use it as a
5306       stand alone app, should look similar to the following:
5307
5308           PerlModule Apache2::RequestRec
5309           PerlModule APR::Table
5310           PerlModule Apache2::RequestIO
5311
5312           <Location /spreadsheet-test>
5313              SetHandler perl-script
5314              PerlResponseHandler Spreadsheet::WriteExcel::MP2Test
5315           </Location>
5316
5317       The PerlResponseHandler must match the package name below.
5318
5319           ###############################################################################
5320           #
5321           # Example of how to use the Spreadsheet::WriteExcel module to send an Excel
5322           # file to a browser using mod_perl 2 and Apache.
5323           #
5324           # This module ties *XLS directly to Apache, and with the correct
5325           # content-disposition/types it will prompt the user to save
5326           # the file, or open it at this location.
5327           #
5328           # This script is a modification of the Spreadsheet::WriteExcel cgi.pl example.
5329           #
5330           # Change the name of this file to MP2Test.pm.
5331           # Change the package location to where ever you locate this package.
5332           # In the example below it is located in the WriteExcel directory.
5333           #
5334           # Your httpd.conf entry for this module, should you choose to use it
5335           # as a stand alone app, should look similar to the following:
5336           #
5337           #     PerlModule Apache2::RequestRec
5338           #     PerlModule APR::Table
5339           #     PerlModule Apache2::RequestIO
5340           #
5341           #     <Location /spreadsheet-test>
5342           #        SetHandler perl-script
5343           #        PerlResponseHandler Spreadsheet::WriteExcel::MP2Test
5344           #     </Location>
5345           #
5346           # The PerlResponseHandler must match the package name below.
5347
5348           # Jun 2004, Matisse Enzer, matisse@matisse.net  (mod_perl 2 version)
5349           # Apr 2001, Thomas Sullivan, webmaster@860.org
5350           # Feb 2001, John McNamara, jmcnamara@cpan.org
5351
5352           package Spreadsheet::WriteExcel::MP2Test;
5353
5354           ##########################################
5355           # Pragma Definitions
5356           ##########################################
5357           use strict;
5358
5359           ##########################################
5360           # Required Modules
5361           ##########################################
5362           use Apache2::Const -compile => qw( :common );
5363           use Spreadsheet::WriteExcel;
5364
5365           ##########################################
5366           # Main App Body
5367           ##########################################
5368           sub handler {
5369               my($r) = @_;  # Apache request object is passed to handler in mod_perl 2
5370
5371               # Set the filename and send the content type
5372               # This will appear when they save the spreadsheet
5373               my $filename ="mod_perl2_test.xls";
5374
5375               ####################################################
5376               ## Send the content type headers the mod_perl 2 way
5377               ####################################################
5378               $r->headers_out->{'Content-Disposition'} = "attachment;filename=$filename";
5379               $r->content_type('application/vnd.ms-excel');
5380
5381               ####################################################
5382               # Tie a filehandle to Apache's STDOUT.
5383               # Create a new workbook and add a worksheet.
5384               ####################################################
5385               tie *XLS => $r;  # The mod_perl 2 way. Tie to the Apache::RequestRec object
5386               binmode(*XLS);
5387
5388               my $workbook  = Spreadsheet::WriteExcel->new(\*XLS);
5389               my $worksheet = $workbook->add_worksheet();
5390
5391
5392               # Set the column width for column 1
5393               $worksheet->set_column(0, 0, 20);
5394
5395
5396               # Create a format
5397               my $format = $workbook->add_format();
5398               $format->set_bold();
5399               $format->set_size(15);
5400               $format->set_color('blue');
5401
5402
5403               # Write to the workbook
5404               $worksheet->write(0, 0, 'Hi Excel! from ' . $r->hostname , $format);
5405
5406               # You must close the workbook for Content-disposition
5407               $workbook->close();
5408               return Apache2::Const::OK;
5409           }
5410
5411           1;
5412
5413       Download this example:
5414       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/mod_perl2.pl>
5415
5416   Example: outline.pl
5417       Example of how use Spreadsheet::WriteExcel to generate Excel outlines
5418       and grouping.
5419
5420       Excel allows you to group rows or columns so that they can be hidden or
5421       displayed with a single mouse click. This feature is referred to as
5422       outlines.
5423
5424       Outlines can reduce complex data down to a few salient sub-totals or
5425       summaries.
5426
5427       This feature is best viewed in Excel but the following is an ASCII
5428       representation of what a worksheet with three outlines might look like.
5429       Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
5430       level 1. The lines at the left hand side are called outline level bars.
5431
5432                   ------------------------------------------
5433            1 2 3 |   |   A   |   B   |   C   |   D   |  ...
5434                   ------------------------------------------
5435             _    | 1 |   A   |       |       |       |  ...
5436            |  _  | 2 |   B   |       |       |       |  ...
5437            | |   | 3 |  (C)  |       |       |       |  ...
5438            | |   | 4 |  (D)  |       |       |       |  ...
5439            | -   | 5 |   E   |       |       |       |  ...
5440            |  _  | 6 |   F   |       |       |       |  ...
5441            | |   | 7 |  (G)  |       |       |       |  ...
5442            | |   | 8 |  (H)  |       |       |       |  ...
5443            | -   | 9 |   I   |       |       |       |  ...
5444            -     | . |  ...  |  ...  |  ...  |  ...  |  ...
5445
5446       Clicking the minus sign on each of the level 2 outlines will collapse
5447       and hide the data as shown in the next figure. The minus sign changes
5448       to a plus sign to indicate that the data in the outline is hidden.
5449
5450                   ------------------------------------------
5451            1 2 3 |   |   A   |   B   |   C   |   D   |  ...
5452                   ------------------------------------------
5453             _    | 1 |   A   |       |       |       |  ...
5454            |     | 2 |   B   |       |       |       |  ...
5455            | +   | 5 |   E   |       |       |       |  ...
5456            |     | 6 |   F   |       |       |       |  ...
5457            | +   | 9 |   I   |       |       |       |  ...
5458            -     | . |  ...  |  ...  |  ...  |  ...  |  ...
5459
5460       Clicking on the minus sign on the level 1 outline will collapse the
5461       remaining rows as follows:
5462
5463                   ------------------------------------------
5464            1 2 3 |   |   A   |   B   |   C   |   D   |  ...
5465                   ------------------------------------------
5466                  | 1 |   A   |       |       |       |  ...
5467            +     | . |  ...  |  ...  |  ...  |  ...  |  ...
5468
5469       See the main Spreadsheet::WriteExcel documentation for more
5470       information.
5471
5472       Source code for this example:
5473
5474           #!/usr/bin/perl -w
5475
5476           ###############################################################################
5477           #
5478           # Example of how use Spreadsheet::WriteExcel to generate Excel outlines and
5479           # grouping.
5480           #
5481           #
5482           # Excel allows you to group rows or columns so that they can be hidden or
5483           # displayed with a single mouse click. This feature is referred to as outlines.
5484           #
5485           # Outlines can reduce complex data down to a few salient sub-totals or
5486           # summaries.
5487           #
5488           # This feature is best viewed in Excel but the following is an ASCII
5489           # representation of what a worksheet with three outlines might look like.
5490           # Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
5491           # level 1. The lines at the left hand side are called outline level bars.
5492           #
5493           #
5494           #             ------------------------------------------
5495           #      1 2 3 |   |   A   |   B   |   C   |   D   |  ...
5496           #             ------------------------------------------
5497           #       _    | 1 |   A   |       |       |       |  ...
5498           #      |  _  | 2 |   B   |       |       |       |  ...
5499           #      | |   | 3 |  (C)  |       |       |       |  ...
5500           #      | |   | 4 |  (D)  |       |       |       |  ...
5501           #      | -   | 5 |   E   |       |       |       |  ...
5502           #      |  _  | 6 |   F   |       |       |       |  ...
5503           #      | |   | 7 |  (G)  |       |       |       |  ...
5504           #      | |   | 8 |  (H)  |       |       |       |  ...
5505           #      | -   | 9 |   I   |       |       |       |  ...
5506           #      -     | . |  ...  |  ...  |  ...  |  ...  |  ...
5507           #
5508           #
5509           # Clicking the minus sign on each of the level 2 outlines will collapse and
5510           # hide the data as shown in the next figure. The minus sign changes to a plus
5511           # sign to indicate that the data in the outline is hidden.
5512           #
5513           #             ------------------------------------------
5514           #      1 2 3 |   |   A   |   B   |   C   |   D   |  ...
5515           #             ------------------------------------------
5516           #       _    | 1 |   A   |       |       |       |  ...
5517           #      |     | 2 |   B   |       |       |       |  ...
5518           #      | +   | 5 |   E   |       |       |       |  ...
5519           #      |     | 6 |   F   |       |       |       |  ...
5520           #      | +   | 9 |   I   |       |       |       |  ...
5521           #      -     | . |  ...  |  ...  |  ...  |  ...  |  ...
5522           #
5523           #
5524           # Clicking on the minus sign on the level 1 outline will collapse the remaining
5525           # rows as follows:
5526           #
5527           #             ------------------------------------------
5528           #      1 2 3 |   |   A   |   B   |   C   |   D   |  ...
5529           #             ------------------------------------------
5530           #            | 1 |   A   |       |       |       |  ...
5531           #      +     | . |  ...  |  ...  |  ...  |  ...  |  ...
5532           #
5533           # See the main Spreadsheet::WriteExcel documentation for more information.
5534           #
5535           # reverse('(c)'), April 2003, John McNamara, jmcnamara@cpan.org
5536           #
5537
5538
5539
5540           use strict;
5541           use Spreadsheet::WriteExcel;
5542
5543           # Create a new workbook and add some worksheets
5544           my $workbook   = Spreadsheet::WriteExcel->new('outline.xls');
5545           my $worksheet1 = $workbook->add_worksheet('Outlined Rows');
5546           my $worksheet2 = $workbook->add_worksheet('Collapsed Rows');
5547           my $worksheet3 = $workbook->add_worksheet('Outline Columns');
5548           my $worksheet4 = $workbook->add_worksheet('Outline levels');
5549
5550           # Add a general format
5551           my $bold = $workbook->add_format(bold => 1);
5552
5553
5554
5555           ###############################################################################
5556           #
5557           # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
5558           # functions so that it looks like the type of automatic outlines that are
5559           # generated when you use the Excel Data->SubTotals menu item.
5560           #
5561
5562
5563           # For outlines the important parameters are $hidden and $level. Rows with the
5564           # same $level are grouped together. The group will be collapsed if $hidden is
5565           # non-zero. $height and $XF are assigned default values if they are undef.
5566           #
5567           # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
5568           #
5569           $worksheet1->set_row(1,  undef, undef, 0, 2);
5570           $worksheet1->set_row(2,  undef, undef, 0, 2);
5571           $worksheet1->set_row(3,  undef, undef, 0, 2);
5572           $worksheet1->set_row(4,  undef, undef, 0, 2);
5573           $worksheet1->set_row(5,  undef, undef, 0, 1);
5574
5575           $worksheet1->set_row(6,  undef, undef, 0, 2);
5576           $worksheet1->set_row(7,  undef, undef, 0, 2);
5577           $worksheet1->set_row(8,  undef, undef, 0, 2);
5578           $worksheet1->set_row(9,  undef, undef, 0, 2);
5579           $worksheet1->set_row(10, undef, undef, 0, 1);
5580
5581
5582           # Add a column format for clarity
5583           $worksheet1->set_column('A:A', 20);
5584
5585           # Add the data, labels and formulas
5586           $worksheet1->write('A1',  'Region', $bold);
5587           $worksheet1->write('A2',  'North');
5588           $worksheet1->write('A3',  'North');
5589           $worksheet1->write('A4',  'North');
5590           $worksheet1->write('A5',  'North');
5591           $worksheet1->write('A6',  'North Total', $bold);
5592
5593           $worksheet1->write('B1',  'Sales',  $bold);
5594           $worksheet1->write('B2',  1000);
5595           $worksheet1->write('B3',  1200);
5596           $worksheet1->write('B4',  900);
5597           $worksheet1->write('B5',  1200);
5598           $worksheet1->write('B6',  '=SUBTOTAL(9,B2:B5)', $bold);
5599
5600           $worksheet1->write('A7',  'South');
5601           $worksheet1->write('A8',  'South');
5602           $worksheet1->write('A9',  'South');
5603           $worksheet1->write('A10', 'South');
5604           $worksheet1->write('A11', 'South Total', $bold);
5605
5606           $worksheet1->write('B7',  400);
5607           $worksheet1->write('B8',  600);
5608           $worksheet1->write('B9',  500);
5609           $worksheet1->write('B10', 600);
5610           $worksheet1->write('B11', '=SUBTOTAL(9,B7:B10)', $bold);
5611
5612           $worksheet1->write('A12', 'Grand Total', $bold);
5613           $worksheet1->write('B12', '=SUBTOTAL(9,B2:B10)', $bold);
5614
5615
5616           ###############################################################################
5617           #
5618           # Example 2: Create a worksheet with outlined rows. This is the same as the
5619           # previous example except that the rows are collapsed.
5620           # Note: We need to indicate the row that contains the collapsed symbol '+'
5621           # with the optional parameter, $collapsed.
5622
5623           # The group will be collapsed if $hidden is non-zero.
5624           # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
5625           #
5626           $worksheet2->set_row(1,  undef, undef, 1, 2);
5627           $worksheet2->set_row(2,  undef, undef, 1, 2);
5628           $worksheet2->set_row(3,  undef, undef, 1, 2);
5629           $worksheet2->set_row(4,  undef, undef, 1, 2);
5630           $worksheet2->set_row(5,  undef, undef, 1, 1);
5631
5632           $worksheet2->set_row(6,  undef, undef, 1, 2);
5633           $worksheet2->set_row(7,  undef, undef, 1, 2);
5634           $worksheet2->set_row(8,  undef, undef, 1, 2);
5635           $worksheet2->set_row(9,  undef, undef, 1, 2);
5636           $worksheet2->set_row(10, undef, undef, 1, 1);
5637           $worksheet2->set_row(11, undef, undef, 0, 0, 1);
5638
5639
5640           # Add a column format for clarity
5641           $worksheet2->set_column('A:A', 20);
5642
5643           # Add the data, labels and formulas
5644           $worksheet2->write('A1',  'Region', $bold);
5645           $worksheet2->write('A2',  'North');
5646           $worksheet2->write('A3',  'North');
5647           $worksheet2->write('A4',  'North');
5648           $worksheet2->write('A5',  'North');
5649           $worksheet2->write('A6',  'North Total', $bold);
5650
5651           $worksheet2->write('B1',  'Sales',  $bold);
5652           $worksheet2->write('B2',  1000);
5653           $worksheet2->write('B3',  1200);
5654           $worksheet2->write('B4',  900);
5655           $worksheet2->write('B5',  1200);
5656           $worksheet2->write('B6',  '=SUBTOTAL(9,B2:B5)', $bold);
5657
5658           $worksheet2->write('A7',  'South');
5659           $worksheet2->write('A8',  'South');
5660           $worksheet2->write('A9',  'South');
5661           $worksheet2->write('A10', 'South');
5662           $worksheet2->write('A11', 'South Total', $bold);
5663
5664           $worksheet2->write('B7',  400);
5665           $worksheet2->write('B8',  600);
5666           $worksheet2->write('B9',  500);
5667           $worksheet2->write('B10', 600);
5668           $worksheet2->write('B11', '=SUBTOTAL(9,B7:B10)', $bold);
5669
5670           $worksheet2->write('A12', 'Grand Total', $bold);
5671           $worksheet2->write('B12', '=SUBTOTAL(9,B2:B10)', $bold);
5672
5673
5674
5675           ###############################################################################
5676           #
5677           # Example 3: Create a worksheet with outlined columns.
5678           #
5679           my $data = [
5680                       ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',' Total'],
5681                       ['North', 50,    20,    15,    25,    65,    80,    ,'=SUM(B2:G2)'],
5682                       ['South', 10,    20,    30,    50,    50,    50,    ,'=SUM(B3:G3)'],
5683                       ['East',  45,    75,    50,    15,    75,    100,   ,'=SUM(B4:G4)'],
5684                       ['West',  15,    15,    55,    35,    20,    50,    ,'=SUM(B5:G6)'],
5685                      ];
5686
5687           # Add bold format to the first row
5688           $worksheet3->set_row(0, undef, $bold);
5689
5690           # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
5691           $worksheet3->set_column('A:A', 10, $bold      );
5692           $worksheet3->set_column('B:G', 5,  undef, 0, 1);
5693           $worksheet3->set_column('H:H', 10);
5694
5695           # Write the data and a formula
5696           $worksheet3->write_col('A1', $data);
5697           $worksheet3->write('H6', '=SUM(H2:H5)', $bold);
5698
5699
5700
5701           ###############################################################################
5702           #
5703           # Example 4: Show all possible outline levels.
5704           #
5705           my $levels = ["Level 1", "Level 2", "Level 3", "Level 4",
5706                         "Level 5", "Level 6", "Level 7", "Level 6",
5707                         "Level 5", "Level 4", "Level 3", "Level 2", "Level 1"];
5708
5709
5710           $worksheet4->write_col('A1', $levels);
5711
5712           $worksheet4->set_row(0,  undef, undef, undef, 1);
5713           $worksheet4->set_row(1,  undef, undef, undef, 2);
5714           $worksheet4->set_row(2,  undef, undef, undef, 3);
5715           $worksheet4->set_row(3,  undef, undef, undef, 4);
5716           $worksheet4->set_row(4,  undef, undef, undef, 5);
5717           $worksheet4->set_row(5,  undef, undef, undef, 6);
5718           $worksheet4->set_row(6,  undef, undef, undef, 7);
5719           $worksheet4->set_row(7,  undef, undef, undef, 6);
5720           $worksheet4->set_row(8,  undef, undef, undef, 5);
5721           $worksheet4->set_row(9,  undef, undef, undef, 4);
5722           $worksheet4->set_row(10, undef, undef, undef, 3);
5723           $worksheet4->set_row(11, undef, undef, undef, 2);
5724           $worksheet4->set_row(12, undef, undef, undef, 1);
5725
5726
5727
5728           __END__
5729
5730       Download this example:
5731       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/outline.pl>
5732
5733   Example: outline_collapsed.pl
5734       Example of how use Spreadsheet::WriteExcel to generate Excel outlines
5735       and grouping.
5736
5737       These example focus mainly on collapsed outlines. See also the
5738       outlines.pl example program for more general examples.
5739
5740       Source code for this example:
5741
5742           #!/usr/bin/perl -w
5743
5744           ###############################################################################
5745           #
5746           # Example of how use Spreadsheet::WriteExcel to generate Excel outlines and
5747           # grouping.
5748           #
5749           # These example focus mainly on collapsed outlines. See also the
5750           # outlines.pl example program for more general examples.
5751           #
5752           # reverse('(c)'), March 2008, John McNamara, jmcnamara@cpan.org
5753           #
5754
5755           use strict;
5756           use Spreadsheet::WriteExcel;
5757
5758           # Create a new workbook and add some worksheets
5759           my $workbook   = Spreadsheet::WriteExcel->new('outline_collapsed.xls');
5760           my $worksheet1 = $workbook->add_worksheet('Outlined Rows');
5761           my $worksheet2 = $workbook->add_worksheet('Collapsed Rows 1');
5762           my $worksheet3 = $workbook->add_worksheet('Collapsed Rows 2');
5763           my $worksheet4 = $workbook->add_worksheet('Collapsed Rows 3');
5764           my $worksheet5 = $workbook->add_worksheet('Outline Columns');
5765           my $worksheet6 = $workbook->add_worksheet('Collapsed Columns');
5766
5767
5768           # Add a general format
5769           my $bold = $workbook->add_format(bold => 1);
5770
5771
5772           #
5773           # This function will generate the same data and sub-totals on each worksheet.
5774           #
5775           sub create_sub_totals {
5776
5777               my $worksheet = $_[0];
5778
5779               # Add a column format for clarity
5780               $worksheet->set_column('A:A', 20);
5781
5782               # Add the data, labels and formulas
5783               $worksheet->write('A1',  'Region', $bold);
5784               $worksheet->write('A2',  'North');
5785               $worksheet->write('A3',  'North');
5786               $worksheet->write('A4',  'North');
5787               $worksheet->write('A5',  'North');
5788               $worksheet->write('A6',  'North Total', $bold);
5789
5790               $worksheet->write('B1',  'Sales',  $bold);
5791               $worksheet->write('B2',  1000);
5792               $worksheet->write('B3',  1200);
5793               $worksheet->write('B4',  900);
5794               $worksheet->write('B5',  1200);
5795               $worksheet->write('B6',  '=SUBTOTAL(9,B2:B5)', $bold);
5796
5797               $worksheet->write('A7',  'South');
5798               $worksheet->write('A8',  'South');
5799               $worksheet->write('A9',  'South');
5800               $worksheet->write('A10', 'South');
5801               $worksheet->write('A11', 'South Total', $bold);
5802
5803               $worksheet->write('B7',  400);
5804               $worksheet->write('B8',  600);
5805               $worksheet->write('B9',  500);
5806               $worksheet->write('B10', 600);
5807               $worksheet->write('B11', '=SUBTOTAL(9,B7:B10)', $bold);
5808
5809               $worksheet->write('A12', 'Grand Total', $bold);
5810               $worksheet->write('B12', '=SUBTOTAL(9,B2:B10)', $bold);
5811
5812           }
5813
5814
5815           ###############################################################################
5816           #
5817           # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
5818           # functions so that it looks like the type of automatic outlines that are
5819           # generated when you use the Excel Data->SubTotals menu item.
5820           #
5821
5822           # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
5823           $worksheet1->set_row(1,  undef, undef, 0, 2);
5824           $worksheet1->set_row(2,  undef, undef, 0, 2);
5825           $worksheet1->set_row(3,  undef, undef, 0, 2);
5826           $worksheet1->set_row(4,  undef, undef, 0, 2);
5827           $worksheet1->set_row(5,  undef, undef, 0, 1);
5828
5829           $worksheet1->set_row(6,  undef, undef, 0, 2);
5830           $worksheet1->set_row(7,  undef, undef, 0, 2);
5831           $worksheet1->set_row(8,  undef, undef, 0, 2);
5832           $worksheet1->set_row(9,  undef, undef, 0, 2);
5833           $worksheet1->set_row(10, undef, undef, 0, 1);
5834
5835           # Write the sub-total data that is common to the row examples.
5836           create_sub_totals($worksheet1);
5837
5838
5839           ###############################################################################
5840           #
5841           # Example 2: Create a worksheet with collapsed outlined rows.
5842           # This is the same as the example 1  except that the all rows are collapsed.
5843           # Note: We need to indicate the row that contains the collapsed symbol '+' with
5844           # the optional parameter, $collapsed.
5845
5846           $worksheet2->set_row(1,  undef, undef, 1, 2);
5847           $worksheet2->set_row(2,  undef, undef, 1, 2);
5848           $worksheet2->set_row(3,  undef, undef, 1, 2);
5849           $worksheet2->set_row(4,  undef, undef, 1, 2);
5850           $worksheet2->set_row(5,  undef, undef, 1, 1);
5851
5852           $worksheet2->set_row(6,  undef, undef, 1, 2);
5853           $worksheet2->set_row(7,  undef, undef, 1, 2);
5854           $worksheet2->set_row(8,  undef, undef, 1, 2);
5855           $worksheet2->set_row(9,  undef, undef, 1, 2);
5856           $worksheet2->set_row(10, undef, undef, 1, 1);
5857
5858           $worksheet2->set_row(11, undef, undef, 0, 0, 1);
5859
5860           # Write the sub-total data that is common to the row examples.
5861           create_sub_totals($worksheet2);
5862
5863
5864           ###############################################################################
5865           #
5866           # Example 3: Create a worksheet with collapsed outlined rows.
5867           # Same as the example 1  except that the two sub-totals are collapsed.
5868
5869           $worksheet3->set_row(1,  undef, undef, 1, 2);
5870           $worksheet3->set_row(2,  undef, undef, 1, 2);
5871           $worksheet3->set_row(3,  undef, undef, 1, 2);
5872           $worksheet3->set_row(4,  undef, undef, 1, 2);
5873           $worksheet3->set_row(5,  undef, undef, 0, 1, 1);
5874
5875           $worksheet3->set_row(6,  undef, undef, 1, 2);
5876           $worksheet3->set_row(7,  undef, undef, 1, 2);
5877           $worksheet3->set_row(8,  undef, undef, 1, 2);
5878           $worksheet3->set_row(9,  undef, undef, 1, 2);
5879           $worksheet3->set_row(10, undef, undef, 0, 1, 1);
5880
5881
5882           # Write the sub-total data that is common to the row examples.
5883           create_sub_totals($worksheet3);
5884
5885
5886           ###############################################################################
5887           #
5888           # Example 4: Create a worksheet with outlined rows.
5889           # Same as the example 1  except that the two sub-totals are collapsed.
5890
5891           $worksheet4->set_row(1,  undef, undef, 1, 2);
5892           $worksheet4->set_row(2,  undef, undef, 1, 2);
5893           $worksheet4->set_row(3,  undef, undef, 1, 2);
5894           $worksheet4->set_row(4,  undef, undef, 1, 2);
5895           $worksheet4->set_row(5,  undef, undef, 1, 1, 1);
5896
5897           $worksheet4->set_row(6,  undef, undef, 1, 2);
5898           $worksheet4->set_row(7,  undef, undef, 1, 2);
5899           $worksheet4->set_row(8,  undef, undef, 1, 2);
5900           $worksheet4->set_row(9,  undef, undef, 1, 2);
5901           $worksheet4->set_row(10, undef, undef, 1, 1, 1);
5902
5903           $worksheet4->set_row(11, undef, undef, 0, 0, 1);
5904
5905           # Write the sub-total data that is common to the row examples.
5906           create_sub_totals($worksheet4);
5907
5908
5909
5910           ###############################################################################
5911           #
5912           # Example 5: Create a worksheet with outlined columns.
5913           #
5914           my $data = [
5915                       ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',' Total'],
5916                       ['North', 50,    20,    15,    25,    65,    80,    ,'=SUM(B2:G2)'],
5917                       ['South', 10,    20,    30,    50,    50,    50,    ,'=SUM(B3:G3)'],
5918                       ['East',  45,    75,    50,    15,    75,    100,   ,'=SUM(B4:G4)'],
5919                       ['West',  15,    15,    55,    35,    20,    50,    ,'=SUM(B5:G6)'],
5920                      ];
5921
5922           # Add bold format to the first row
5923           $worksheet5->set_row(0, undef, $bold);
5924
5925           # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
5926           $worksheet5->set_column('A:A', 10, $bold      );
5927           $worksheet5->set_column('B:G', 5,  undef, 0, 1);
5928           $worksheet5->set_column('H:H', 10             );
5929
5930           # Write the data and a formula
5931           $worksheet5->write_col('A1', $data);
5932           $worksheet5->write('H6', '=SUM(H2:H5)', $bold);
5933
5934
5935           ###############################################################################
5936           #
5937           # Example 6: Create a worksheet with collapsed outlined columns.
5938           # This is the same as the previous example except collapsed columns.
5939
5940           # Add bold format to the first row
5941           $worksheet6->set_row(0, undef, $bold);
5942
5943           # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
5944           $worksheet6->set_column('A:A', 10, $bold         );
5945           $worksheet6->set_column('B:G', 5,  undef, 1, 1   );
5946           $worksheet6->set_column('H:H', 10, undef, 0, 0, 1);
5947
5948           # Write the data and a formula
5949           $worksheet6->write_col('A1', $data);
5950           $worksheet6->write('H6', '=SUM(H2:H5)', $bold);
5951
5952
5953           __END__
5954
5955       Download this example:
5956       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/outline_collapsed.pl>
5957
5958   Example: panes.pl
5959       Example of using the WriteExcel module to create worksheet panes.
5960
5961       Source code for this example:
5962
5963           #!/usr/bin/perl -w
5964
5965           #######################################################################
5966           #
5967           # Example of using the WriteExcel module to create worksheet panes.
5968           #
5969           # reverse('(c)'), May 2001, John McNamara, jmcnamara@cpan.org
5970           #
5971
5972           use strict;
5973           use Spreadsheet::WriteExcel;
5974
5975           my $workbook  = Spreadsheet::WriteExcel->new("panes.xls");
5976
5977           my $worksheet1 = $workbook->add_worksheet('Panes 1');
5978           my $worksheet2 = $workbook->add_worksheet('Panes 2');
5979           my $worksheet3 = $workbook->add_worksheet('Panes 3');
5980           my $worksheet4 = $workbook->add_worksheet('Panes 4');
5981
5982           # Freeze panes
5983           $worksheet1->freeze_panes(1, 0); # 1 row
5984
5985           $worksheet2->freeze_panes(0, 1); # 1 column
5986           $worksheet3->freeze_panes(1, 1); # 1 row and column
5987
5988           # Split panes.
5989           # The divisions must be specified in terms of row and column dimensions.
5990           # The default row height is 12.75 and the default column width is 8.43
5991           #
5992           $worksheet4->split_panes(12.75, 8.43, 1, 1); # 1 row and column
5993
5994
5995           #######################################################################
5996           #
5997           # Set up some formatting and text to highlight the panes
5998           #
5999
6000           my $header = $workbook->add_format();
6001           $header->set_color('white');
6002           $header->set_align('center');
6003           $header->set_align('vcenter');
6004           $header->set_pattern();
6005           $header->set_fg_color('green');
6006
6007           my $center = $workbook->add_format();
6008           $center->set_align('center');
6009
6010
6011           #######################################################################
6012           #
6013           # Sheet 1
6014           #
6015
6016           $worksheet1->set_column('A:I', 16);
6017           $worksheet1->set_row(0, 20);
6018           $worksheet1->set_selection('C3');
6019
6020           for my $i (0..8){
6021               $worksheet1->write(0, $i, 'Scroll down', $header);
6022           }
6023
6024           for my $i (1..100){
6025               for my $j (0..8){
6026                   $worksheet1->write($i, $j, $i+1, $center);
6027               }
6028           }
6029
6030
6031           #######################################################################
6032           #
6033           # Sheet 2
6034           #
6035
6036           $worksheet2->set_column('A:A', 16);
6037           $worksheet2->set_selection('C3');
6038
6039           for my $i (0..49){
6040               $worksheet2->set_row($i, 15);
6041               $worksheet2->write($i, 0, 'Scroll right', $header);
6042           }
6043
6044           for my $i (0..49){
6045               for my $j (1..25){
6046                   $worksheet2->write($i, $j, $j, $center);
6047               }
6048           }
6049
6050
6051           #######################################################################
6052           #
6053           # Sheet 3
6054           #
6055
6056           $worksheet3->set_column('A:Z', 16);
6057           $worksheet3->set_selection('C3');
6058
6059           for my $i (1..25){
6060               $worksheet3->write(0, $i, 'Scroll down',  $header);
6061           }
6062
6063           for my $i (1..49){
6064               $worksheet3->write($i, 0, 'Scroll right', $header);
6065           }
6066
6067           for my $i (1..49){
6068               for my $j (1..25){
6069                   $worksheet3->write($i, $j, $j, $center);
6070               }
6071           }
6072
6073
6074           #######################################################################
6075           #
6076           # Sheet 4
6077           #
6078
6079           $worksheet4->set_selection('C3');
6080
6081           for my $i (1..25){
6082               $worksheet4->write(0, $i, 'Scroll', $center);
6083           }
6084
6085           for my $i (1..49){
6086               $worksheet4->write($i, 0, 'Scroll', $center);
6087           }
6088
6089           for my $i (1..49){
6090               for my $j (1..25){
6091                   $worksheet4->write($i, $j, $j, $center);
6092               }
6093           }
6094
6095       Download this example:
6096       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/panes.pl>
6097
6098   Example: properties.pl
6099       An example of adding document properties to a Spreadsheet::WriteExcel
6100       file.
6101
6102       Source code for this example:
6103
6104           #!/usr/bin/perl -w
6105
6106           ##############################################################################
6107           #
6108           # An example of adding document properties to a Spreadsheet::WriteExcel file.
6109           #
6110           # reverse('(c)'), August 2008, John McNamara, jmcnamara@cpan.org
6111           #
6112
6113           use strict;
6114           use Spreadsheet::WriteExcel;
6115
6116           my $workbook  = Spreadsheet::WriteExcel->new('properties.xls');
6117           my $worksheet = $workbook->add_worksheet();
6118
6119
6120           $workbook->set_properties(
6121               title    => 'This is an example spreadsheet',
6122               subject  => 'With document properties',
6123               author   => 'John McNamara',
6124               manager  => 'Dr. Heinz Doofenshmirtz ',
6125               company  => 'of Wolves',
6126               category => 'Example spreadsheets',
6127               keywords => 'Sample, Example, Properties',
6128               comments => 'Created with Perl and Spreadsheet::WriteExcel',
6129           );
6130
6131
6132           $worksheet->set_column('A:A', 50);
6133           $worksheet->write('A1', 'Select File->Properties to see the file properties');
6134
6135
6136           __END__
6137
6138       Download this example:
6139       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/properties.pl>
6140
6141   Example: protection.pl
6142       Example of cell locking and formula hiding in an Excel worksheet via
6143       the Spreadsheet::WriteExcel module.
6144
6145       Source code for this example:
6146
6147           #!/usr/bin/perl -w
6148
6149           ########################################################################
6150           #
6151           # Example of cell locking and formula hiding in an Excel worksheet via
6152           # the Spreadsheet::WriteExcel module.
6153           #
6154           # reverse('(c)'), August 2001, John McNamara, jmcnamara@cpan.org
6155           #
6156
6157           use strict;
6158           use Spreadsheet::WriteExcel;
6159
6160           my $workbook  = Spreadsheet::WriteExcel->new("protection.xls");
6161           my $worksheet = $workbook->add_worksheet();
6162
6163           # Create some format objects
6164           my $locked    = $workbook->add_format(locked => 1);
6165           my $unlocked  = $workbook->add_format(locked => 0);
6166           my $hidden    = $workbook->add_format(hidden => 1);
6167
6168           # Format the columns
6169           $worksheet->set_column('A:A', 42);
6170           $worksheet->set_selection('B3:B3');
6171
6172           # Protect the worksheet
6173           $worksheet->protect();
6174
6175           # Examples of cell locking and hiding
6176           $worksheet->write('A1', 'Cell B1 is locked. It cannot be edited.');
6177           $worksheet->write('B1', '=1+2', $locked);
6178
6179           $worksheet->write('A2', 'Cell B2 is unlocked. It can be edited.');
6180           $worksheet->write('B2', '=1+2', $unlocked);
6181
6182           $worksheet->write('A3', "Cell B3 is hidden. The formula isn't visible.");
6183           $worksheet->write('B3', '=1+2', $hidden);
6184
6185           $worksheet->write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet');
6186           $worksheet->write('A6', 'to remove the worksheet protection.   ');
6187
6188       Download this example:
6189       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/protection.pl>
6190
6191   Example: repeat.pl
6192       Example of writing repeated formulas.
6193
6194       Source code for this example:
6195
6196           #!/usr/bin/perl -w
6197
6198           ######################################################################
6199           #
6200           # Example of writing repeated formulas.
6201           #
6202           # reverse('(c)'), August 2002, John McNamara, jmcnamara@cpan.org
6203           #
6204
6205           use strict;
6206           use Spreadsheet::WriteExcel;
6207
6208           my $workbook  = Spreadsheet::WriteExcel->new("repeat.xls");
6209           my $worksheet = $workbook->add_worksheet();
6210
6211
6212           my $limit = 1000;
6213
6214           # Write a column of numbers
6215           for my $row (0..$limit) {
6216               $worksheet->write($row, 0,  $row);
6217           }
6218
6219
6220           # Store a formula
6221           my $formula = $worksheet->store_formula('=A1*5+4');
6222
6223
6224           # Write a column of formulas based on the stored formula
6225           for my $row (0..$limit) {
6226               $worksheet->repeat_formula($row, 1, $formula, undef,
6227                                                   qr/^A1$/, 'A'.($row+1));
6228           }
6229
6230
6231           # Direct formula writing. As a speed comparison uncomment the
6232           # following and run the program again
6233
6234           #for my $row (0..$limit) {
6235           #    $worksheet->write_formula($row, 2, '=A'.($row+1).'*5+4');
6236           #}
6237
6238
6239
6240           __END__
6241
6242       Download this example:
6243       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/repeat.pl>
6244
6245   Example: right_to_left.pl
6246       Example of how to change the default worksheet direction from left-to-
6247       right to right-to-left as required by some eastern verions of Excel.
6248
6249       Source code for this example:
6250
6251           #!/usr/bin/perl -w
6252
6253           #######################################################################
6254           #
6255           # Example of how to change the default worksheet direction from
6256           # left-to-right to right-to-left as required by some eastern verions
6257           # of Excel.
6258           #
6259           # reverse('(c)'), January 2006, John McNamara, jmcnamara@cpan.org
6260           #
6261
6262           use strict;
6263           use Spreadsheet::WriteExcel;
6264
6265           my $workbook   = Spreadsheet::WriteExcel->new("right_to_left.xls");
6266           my $worksheet1 = $workbook->add_worksheet();
6267           my $worksheet2 = $workbook->add_worksheet();
6268
6269           $worksheet2->right_to_left();
6270
6271           $worksheet1->write(0, 0, 'Hello'); #  A1, B1, C1, ...
6272           $worksheet2->write(0, 0, 'Hello'); # ..., C1, B1, A1
6273
6274       Download this example:
6275       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/right_to_left.pl>
6276
6277   Example: row_wrap.pl
6278       Demonstrates how to wrap data from one worksheet onto another.
6279
6280       Excel has a row limit of 65536 rows. Sometimes the amount of row data
6281       to be written to a file is greater than this limit. In this case it is
6282       a useful technique to wrap the data from one worksheet onto the next so
6283       that we get something like the following:
6284
6285         Sheet1  Row     1  -  65536
6286         Sheet2  Row 65537  - 131072
6287         Sheet3  Row 131073 - ...
6288
6289       In order to achieve this we use a single worksheet reference and
6290       reinitialise it to point to a new worksheet when required.
6291
6292       Source code for this example:
6293
6294           #!/usr/bin/perl -w
6295
6296           ##############################################################################
6297           #
6298           # Demonstrates how to wrap data from one worksheet onto another.
6299           #
6300           # Excel has a row limit of 65536 rows. Sometimes the amount of row data to be
6301           # written to a file is greater than this limit. In this case it is a useful
6302           # technique to wrap the data from one worksheet onto the next so that we get
6303           # something like the following:
6304           #
6305           #   Sheet1  Row     1  -  65536
6306           #   Sheet2  Row 65537  - 131072
6307           #   Sheet3  Row 131073 - ...
6308           #
6309           # In order to achieve this we use a single worksheet reference and
6310           # reinitialise it to point to a new worksheet when required.
6311           #
6312           # reverse('(c)'), May 2006, John McNamara, jmcnamara@cpan.org
6313           #
6314
6315
6316           use strict;
6317           use Spreadsheet::WriteExcel;
6318
6319           my $workbook  = Spreadsheet::WriteExcel->new('row_wrap.xls');
6320           my $worksheet = $workbook->add_worksheet();
6321
6322
6323           # Worksheet formatting.
6324           $worksheet->set_column('A:A', 20);
6325
6326
6327           # For the sake of this example we will use a small row limit. In order to use
6328           # the entire row range set the $row_limit to 65536.
6329           my $row_limit = 10;
6330           my $row       = 0;
6331
6332           for my $count (1 .. 2 * $row_limit +10) {
6333
6334               # When we hit the row limit we redirect the output
6335               # to a new worksheet and reset the row number.
6336               if ($row == $row_limit) {
6337                   $worksheet = $workbook->add_worksheet();
6338                   $row = 0;
6339
6340                   # Repeat any worksheet formatting.
6341                   $worksheet->set_column('A:A', 20);
6342               }
6343
6344               $worksheet->write($row, 0,  "This is row $count");
6345               $row++;
6346           }
6347
6348       Download this example:
6349       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/row_wrap.pl>
6350
6351   Example: sales.pl
6352       Example of a sales worksheet to demonstrate several different features.
6353       Also uses functions from the Spreadsheet::WriteExcel::Utility module.
6354
6355       Source code for this example:
6356
6357           #!/usr/bin/perl -w
6358
6359           ###############################################################################
6360           #
6361           # Example of a sales worksheet to demonstrate several different features.
6362           # Also uses functions from the L<Spreadsheet::WriteExcel::Utility> module.
6363           #
6364           # reverse('(c)'), October 2001, John McNamara, jmcnamara@cpan.org
6365           #
6366
6367           use strict;
6368           use Spreadsheet::WriteExcel;
6369           use Spreadsheet::WriteExcel::Utility;
6370
6371           # Create a new workbook and add a worksheet
6372           my $workbook        = Spreadsheet::WriteExcel->new("sales.xls");
6373           my $worksheet       = $workbook->add_worksheet('May Sales');
6374
6375
6376           # Set up some formats
6377           my %heading         =   (
6378                                       bold        => 1,
6379                                       pattern     => 1,
6380                                       fg_color    => 19,
6381                                       border      => 1,
6382                                       align       => 'center',
6383                                   );
6384
6385           my %total           =   (
6386                                   bold        => 1,
6387                                   top         => 1,
6388                                   num_format  => '$#,##0.00'
6389                                   );
6390
6391           my $heading         = $workbook->add_format(%heading);
6392           my $total_format    = $workbook->add_format(%total);
6393           my $price_format    = $workbook->add_format(num_format => '$#,##0.00');
6394           my $date_format     = $workbook->add_format(num_format => 'mmm d yyy');
6395
6396
6397           # Write the main headings
6398           $worksheet->freeze_panes(1); # Freeze the first row
6399           $worksheet->write('A1', 'Item',     $heading);
6400           $worksheet->write('B1', 'Quantity', $heading);
6401           $worksheet->write('C1', 'Price',    $heading);
6402           $worksheet->write('D1', 'Total',    $heading);
6403           $worksheet->write('E1', 'Date',     $heading);
6404
6405           # Set the column widths
6406           $worksheet->set_column('A:A', 25);
6407           $worksheet->set_column('B:B', 10);
6408           $worksheet->set_column('C:E', 16);
6409
6410
6411           # Extract the sales data from the __DATA__ section at the end of the file.
6412           # In reality this information would probably come from a database
6413           my @sales;
6414
6415           foreach my $line (<DATA>) {
6416               chomp $line;
6417               next if $line eq '';
6418               # Simple-minded processing of CSV data. Refer to the Text::CSV_XS
6419               # and Text::xSV modules for a more complete CSV handling.
6420               my @items = split /,/, $line;
6421               push @sales, \@items;
6422           }
6423
6424
6425           # Write out the items from each row
6426           my $row = 1;
6427           foreach my $sale (@sales) {
6428
6429               $worksheet->write($row, 0, @$sale[0]);
6430               $worksheet->write($row, 1, @$sale[1]);
6431               $worksheet->write($row, 2, @$sale[2], $price_format);
6432
6433               # Create a formula like '=B2*C2'
6434               my $formula =   '='
6435                               . xl_rowcol_to_cell($row, 1)
6436                               . "*"
6437                               . xl_rowcol_to_cell($row, 2);
6438
6439               $worksheet->write($row, 3, $formula, $price_format);
6440
6441               # Parse the date
6442               my $date = xl_decode_date_US(@$sale[3]);
6443               $worksheet->write($row, 4, $date, $date_format);
6444               $row++;
6445           }
6446
6447           # Create a formula to sum the totals, like '=SUM(D2:D6)'
6448           my $total = '=SUM(D2:'
6449                       . xl_rowcol_to_cell($row-1, 3)
6450                       . ")";
6451
6452           $worksheet->write($row, 3, $total, $total_format);
6453
6454
6455
6456           __DATA__
6457           586 card,20,125.50,5/12/01
6458           Flat Screen Monitor,1,1300.00,5/12/01
6459           64 MB dimms,45,49.99,5/13/01
6460           15 GB HD,12,300.00,5/13/01
6461           Speakers (pair),5,15.50,5/14/01
6462
6463       Download this example:
6464       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/sales.pl>
6465
6466   Example: sendmail.pl
6467       Example of how to use Mail::Sender to send a Spreadsheet::WriteExcel
6468       Excel file as an attachment.
6469
6470       The main thing is to ensure that you close() the Worbook before you
6471       send it.
6472
6473       See the Mail::Sender module for further details.
6474
6475           #!/usr/bin/perl -w
6476
6477           ###############################################################################
6478           #
6479           # Example of how to use Mail::Sender to send a Spreadsheet::WriteExcel Excel
6480           # file as an attachment.
6481           #
6482           # The main thing is to ensure that you close() the Worbook before you send it.
6483           #
6484           # See the L<Mail::Sender> module for further details.
6485           #
6486           # reverse('(c)'), August 2002, John McNamara, jmcnamara@cpan.org
6487           #
6488
6489
6490           use strict;
6491           use Spreadsheet::WriteExcel;
6492           use Mail::Sender;
6493
6494           # Create an Excel file
6495           my $workbook  = Spreadsheet::WriteExcel->new("sendmail.xls");
6496           my $worksheet = $workbook->add_worksheet;
6497
6498           $worksheet->write('A1', "Hello World!");
6499
6500           $workbook->close(); # Must close before sending
6501
6502
6503
6504           # Send the file.  Change all variables to suit
6505           my $sender = new Mail::Sender
6506           {
6507               smtp => '123.123.123.123',
6508               from => 'Someone'
6509           };
6510
6511           $sender->MailFile(
6512           {
6513               to      => 'another@mail.com',
6514               subject => 'Excel file',
6515               msg     => "Here is the data.\n",
6516               file    => 'mail.xls',
6517           });
6518
6519       Download this example:
6520       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/sendmail.pl>
6521
6522   Example: stats_ext.pl
6523       Example of formatting using the Spreadsheet::WriteExcel module
6524
6525       This is a simple example of how to use functions that reference cells
6526       in other worksheets within the same workbook.
6527
6528       Source code for this example:
6529
6530           #!/usr/bin/perl -w
6531
6532           ###############################################################################
6533           #
6534           # Example of formatting using the Spreadsheet::WriteExcel module
6535           #
6536           # This is a simple example of how to use functions that reference cells in
6537           # other worksheets within the same workbook.
6538           #
6539           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
6540           #
6541
6542           use strict;
6543           use Spreadsheet::WriteExcel;
6544
6545           # Create a new workbook and add a worksheet
6546           my $workbook  = Spreadsheet::WriteExcel->new("stats_ext.xls");
6547           my $worksheet1 = $workbook->add_worksheet('Test results');
6548           my $worksheet2 = $workbook->add_worksheet('Data');
6549
6550           # Set the column width for columns 1
6551           $worksheet1->set_column('A:A', 20);
6552
6553
6554           # Create a format for the headings
6555           my $heading = $workbook->add_format();
6556           $heading->set_bold();
6557
6558           # Create a numerical format
6559           my $numformat = $workbook->add_format();
6560           $numformat->set_num_format('0.00');
6561
6562
6563
6564
6565           # Write some statistical functions
6566           $worksheet1->write('A1', 'Count', $heading);
6567           $worksheet1->write('B1', '=COUNT(Data!B2:B9)');
6568
6569           $worksheet1->write('A2', 'Sum', $heading);
6570           $worksheet1->write('B2', '=SUM(Data!B2:B9)');
6571
6572           $worksheet1->write('A3', 'Average', $heading);
6573           $worksheet1->write('B3', '=AVERAGE(Data!B2:B9)');
6574
6575           $worksheet1->write('A4', 'Min', $heading);
6576           $worksheet1->write('B4', '=MIN(Data!B2:B9)');
6577
6578           $worksheet1->write('A5', 'Max', $heading);
6579           $worksheet1->write('B5', '=MAX(Data!B2:B9)');
6580
6581           $worksheet1->write('A6', 'Standard Deviation', $heading);
6582           $worksheet1->write('B6', '=STDEV(Data!B2:B9)');
6583
6584           $worksheet1->write('A7', 'Kurtosis', $heading);
6585           $worksheet1->write('B7', '=KURT(Data!B2:B9)');
6586
6587
6588           # Write the sample data
6589           $worksheet2->write('A1', 'Sample', $heading);
6590           $worksheet2->write('A2', 1);
6591           $worksheet2->write('A3', 2);
6592           $worksheet2->write('A4', 3);
6593           $worksheet2->write('A5', 4);
6594           $worksheet2->write('A6', 5);
6595           $worksheet2->write('A7', 6);
6596           $worksheet2->write('A8', 7);
6597           $worksheet2->write('A9', 8);
6598
6599           $worksheet2->write('B1', 'Length', $heading);
6600           $worksheet2->write('B2', 25.4, $numformat);
6601           $worksheet2->write('B3', 25.4, $numformat);
6602           $worksheet2->write('B4', 24.8, $numformat);
6603           $worksheet2->write('B5', 25.0, $numformat);
6604           $worksheet2->write('B6', 25.3, $numformat);
6605           $worksheet2->write('B7', 24.9, $numformat);
6606           $worksheet2->write('B8', 25.2, $numformat);
6607           $worksheet2->write('B9', 24.8, $numformat);
6608
6609       Download this example:
6610       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/stats_ext.pl>
6611
6612   Example: stocks.pl
6613       Example of formatting using the Spreadsheet::WriteExcel module
6614
6615       This example shows how to use a conditional numerical format with
6616       colours to indicate if a share price has gone up or down.
6617
6618       Source code for this example:
6619
6620           #!/usr/bin/perl -w
6621
6622           ###############################################################################
6623           #
6624           # Example of formatting using the Spreadsheet::WriteExcel module
6625           #
6626           # This example shows how to use a conditional numerical format
6627           # with colours to indicate if a share price has gone up or down.
6628           #
6629           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
6630           #
6631
6632           use strict;
6633           use Spreadsheet::WriteExcel;
6634
6635           # Create a new workbook and add a worksheet
6636           my $workbook  = Spreadsheet::WriteExcel->new("stocks.xls");
6637           my $worksheet = $workbook->add_worksheet();
6638
6639           # Set the column width for columns 1, 2, 3 and 4
6640           $worksheet->set_column(0, 3, 15);
6641
6642
6643           # Create a format for the column headings
6644           my $header = $workbook->add_format();
6645           $header->set_bold();
6646           $header->set_size(12);
6647           $header->set_color('blue');
6648
6649
6650           # Create a format for the stock price
6651           my $f_price = $workbook->add_format();
6652           $f_price->set_align('left');
6653           $f_price->set_num_format('$0.00');
6654
6655
6656           # Create a format for the stock volume
6657           my $f_volume = $workbook->add_format();
6658           $f_volume->set_align('left');
6659           $f_volume->set_num_format('#,##0');
6660
6661
6662           # Create a format for the price change. This is an example of a conditional
6663           # format. The number is formatted as a percentage. If it is positive it is
6664           # formatted in green, if it is negative it is formatted in red and if it is
6665           # zero it is formatted as the default font colour (in this case black).
6666           # Note: the [Green] format produces an unappealing lime green. Try
6667           # [Color 10] instead for a dark green.
6668           #
6669           my $f_change = $workbook->add_format();
6670           $f_change->set_align('left');
6671           $f_change->set_num_format('[Green]0.0%;[Red]-0.0%;0.0%');
6672
6673
6674           # Write out the data
6675           $worksheet->write(0, 0, 'Company', $header);
6676           $worksheet->write(0, 1, 'Price',   $header);
6677           $worksheet->write(0, 2, 'Volume',  $header);
6678           $worksheet->write(0, 3, 'Change',  $header);
6679
6680           $worksheet->write(1, 0, 'Damage Inc.'     );
6681           $worksheet->write(1, 1, 30.25,     $f_price);  # $30.25
6682           $worksheet->write(1, 2, 1234567,   $f_volume); # 1,234,567
6683           $worksheet->write(1, 3, 0.085,     $f_change); # 8.5% in green
6684
6685           $worksheet->write(2, 0, 'Dump Corp.'      );
6686           $worksheet->write(2, 1, 1.56,      $f_price);  # $1.56
6687           $worksheet->write(2, 2, 7564,      $f_volume); # 7,564
6688           $worksheet->write(2, 3, -0.015,    $f_change); # -1.5% in red
6689
6690           $worksheet->write(3, 0, 'Rev Ltd.'        );
6691           $worksheet->write(3, 1, 0.13,      $f_price);  # $0.13
6692           $worksheet->write(3, 2, 321,       $f_volume); # 321
6693           $worksheet->write(3, 3, 0,         $f_change); # 0 in the font color (black)
6694
6695       Download this example:
6696       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/stocks.pl>
6697
6698   Example: tab_colors.pl
6699       Example of how to set Excel worksheet tab colours.
6700
6701       Source code for this example:
6702
6703           #!/usr/bin/perl -w
6704
6705           #######################################################################
6706           #
6707           # Example of how to set Excel worksheet tab colours.
6708           #
6709           # reverse('(c)'), May 2006, John McNamara, jmcnamara@cpan.org
6710           #
6711
6712           use strict;
6713           use Spreadsheet::WriteExcel;
6714
6715
6716           my $workbook   = Spreadsheet::WriteExcel->new('tab_colors.xls');
6717
6718           my $worksheet1 = $workbook->add_worksheet();
6719           my $worksheet2 = $workbook->add_worksheet();
6720           my $worksheet3 = $workbook->add_worksheet();
6721           my $worksheet4 = $workbook->add_worksheet();
6722
6723           # Worksheet1 will have the default tab colour.
6724           $worksheet2->set_tab_color('red');
6725           $worksheet3->set_tab_color('green');
6726           $worksheet4->set_tab_color(0x35); # Orange
6727
6728       Download this example:
6729       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/tab_colors.pl>
6730
6731   Example: textwrap.pl
6732       Example of formatting using the Spreadsheet::WriteExcel module
6733
6734       This example shows how to wrap text in a cell. There are two
6735       alternatives, vertical justification and text wrap.
6736
6737       With vertical justification the text is wrapped automatically to fit
6738       the column width. With text wrap you must specify a newline with an
6739       embedded \n.
6740
6741       Source code for this example:
6742
6743           #!/usr/bin/perl -w
6744
6745           ###############################################################################
6746           #
6747           # Example of formatting using the Spreadsheet::WriteExcel module
6748           #
6749           # This example shows how to wrap text in a cell. There are two alternatives,
6750           # vertical justification and text wrap.
6751           #
6752           # With vertical justification the text is wrapped automatically to fit the
6753           # column width. With text wrap you must specify a newline with an embedded \n.
6754           #
6755           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
6756           #
6757
6758           use strict;
6759           use Spreadsheet::WriteExcel;
6760
6761           # Create a new workbook and add a worksheet
6762           my $workbook  = Spreadsheet::WriteExcel->new("textwrap.xls");
6763           my $worksheet = $workbook->add_worksheet();
6764
6765           # Set the column width for columns 1, 2 and 3
6766           $worksheet->set_column(1, 1, 24);
6767           $worksheet->set_column(2, 2, 34);
6768           $worksheet->set_column(3, 3, 34);
6769
6770           # Set the row height for rows 1, 4, and 6. The height of row 2 will adjust
6771           # automatically to fit the text.
6772           #
6773           $worksheet->set_row(0, 30);
6774           $worksheet->set_row(3, 40);
6775           $worksheet->set_row(5, 80);
6776
6777
6778           # No newlines
6779           my $str1  = "For whatever we lose (like a you or a me) ";
6780           $str1    .= "it's always ourselves we find in the sea";
6781
6782           # Embedded newlines
6783           my $str2  = "For whatever we lose\n(like a you or a me)\n";
6784              $str2 .= "it's always ourselves\nwe find in the sea";
6785
6786
6787           # Create a format for the column headings
6788           my $header = $workbook->add_format();
6789           $header->set_bold();
6790           $header->set_font("Courier New");
6791           $header->set_align('center');
6792           $header->set_align('vcenter');
6793
6794           # Create a "vertical justification" format
6795           my $format1 = $workbook->add_format();
6796           $format1->set_align('vjustify');
6797
6798           # Create a "text wrap" format
6799           my $format2 = $workbook->add_format();
6800           $format2->set_text_wrap();
6801
6802           # Write the headers
6803           $worksheet->write(0, 1, "set_align('vjustify')", $header);
6804           $worksheet->write(0, 2, "set_align('vjustify')", $header);
6805           $worksheet->write(0, 3, "set_text_wrap()", $header);
6806
6807           # Write some examples
6808           $worksheet->write(1, 1, $str1, $format1);
6809           $worksheet->write(1, 2, $str1, $format1);
6810           $worksheet->write(1, 3, $str2, $format2);
6811
6812           $worksheet->write(3, 1, $str1, $format1);
6813           $worksheet->write(3, 2, $str1, $format1);
6814           $worksheet->write(3, 3, $str2, $format2);
6815
6816           $worksheet->write(5, 1, $str1, $format1);
6817           $worksheet->write(5, 2, $str1, $format1);
6818           $worksheet->write(5, 3, $str2, $format2);
6819
6820       Download this example:
6821       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/textwrap.pl>
6822
6823   Example: win32ole.pl
6824       This is a simple example of how to create an Excel file using the
6825       Win32::OLE module for the sake of comparison.
6826
6827           #!/usr/bin/perl -w
6828
6829           ###############################################################################
6830           #
6831           # This is a simple example of how to create an Excel file using the
6832           # Win32::OLE module for the sake of comparison.
6833           #
6834           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
6835           #
6836
6837           use strict;
6838           use Cwd;
6839           use Win32::OLE;
6840           use Win32::OLE::Const 'Microsoft Excel';
6841
6842
6843           my $application = Win32::OLE->new("Excel.Application");
6844           my $workbook    = $application->Workbooks->Add;
6845           my $worksheet   = $workbook->Worksheets(1);
6846
6847           $worksheet->Cells(1,1)->{Value} = "Hello World";
6848           $worksheet->Cells(2,1)->{Value} = "One";
6849           $worksheet->Cells(3,1)->{Value} = "Two";
6850           $worksheet->Cells(4,1)->{Value} =  3;
6851           $worksheet->Cells(5,1)->{Value} =  4.0000001;
6852
6853           # Add some formatting
6854           $worksheet->Cells(1,1)->Font->{Bold}       = "True";
6855           $worksheet->Cells(1,1)->Font->{Size}       = 16;
6856           $worksheet->Cells(1,1)->Font->{ColorIndex} = 3;
6857           $worksheet->Columns("A:A")->{ColumnWidth}  = 25;
6858
6859           # Write a hyperlink
6860           my $range = $worksheet->Range("A7:A7");
6861           $worksheet->Hyperlinks->Add({ Anchor => $range, Address => "http://www.perl.com/"});
6862
6863           # Get current directory using Cwd.pm
6864           my $dir = cwd();
6865
6866           $workbook->SaveAs({
6867                               FileName   => $dir . '/win32ole.xls',
6868                               FileFormat => xlNormal,
6869                             });
6870           $workbook->Close;
6871
6872       Download this example:
6873       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/win32ole.pl>
6874
6875   Example: write_arrays.pl
6876       Example of how to use the Spreadsheet::WriteExcel module to write 1D
6877       and 2D arrays of data.
6878
6879       To find out more about array references refer(!!) to the perlref and
6880       perlreftut manpages. To find out more about 2D arrays or "list of
6881       lists" refer to the perllol manpage.
6882
6883       Source code for this example:
6884
6885           #!/usr/bin/perl -w
6886
6887           #######################################################################
6888           #
6889           # Example of how to use the Spreadsheet::WriteExcel module to
6890           # write 1D and 2D arrays of data.
6891           #
6892           # To find out more about array references refer(!!) to the perlref and
6893           # perlreftut manpages. To find out more about 2D arrays or "list of
6894           # lists" refer to the perllol manpage.
6895           #
6896           # reverse('(c)'), March 2002, John McNamara, jmcnamara@cpan.org
6897           #
6898
6899
6900           use strict;
6901           use Spreadsheet::WriteExcel;
6902
6903
6904           my $workbook   = Spreadsheet::WriteExcel->new("write_arrays.xls");
6905           my $worksheet1 = $workbook->add_worksheet('Example 1');
6906           my $worksheet2 = $workbook->add_worksheet('Example 2');
6907           my $worksheet3 = $workbook->add_worksheet('Example 3');
6908           my $worksheet4 = $workbook->add_worksheet('Example 4');
6909           my $worksheet5 = $workbook->add_worksheet('Example 5');
6910           my $worksheet6 = $workbook->add_worksheet('Example 6');
6911           my $worksheet7 = $workbook->add_worksheet('Example 7');
6912           my $worksheet8 = $workbook->add_worksheet('Example 8');
6913
6914           my $format     = $workbook->add_format(color => 'red', bold => 1);
6915
6916
6917           # Data arrays used in the following examples.
6918           # undef values are written as blank cells (with format if specified).
6919           #
6920           my @array   =   ( 'one', 'two', undef, 'four' );
6921
6922           my @array2d =   (
6923                               ['maggie', 'milly', 'molly', 'may'  ],
6924                               [13,       14,      15,      16     ],
6925                               ['shell',  'star',  'crab',  'stone'],
6926                           );
6927
6928
6929           # 1. Write a row of data using an array reference.
6930           $worksheet1->write('A1', \@array);
6931
6932           # 2. Same as 1. above using an anonymous array ref.
6933           $worksheet2->write('A1', [ @array ]);
6934
6935           # 3. Write a row of data using an explicit write_row() method call.
6936           #    This is the same as calling write() in Ex. 1 above.
6937           #
6938           $worksheet3->write_row('A1', \@array);
6939
6940           # 4. Write a column of data using the write_col() method call.
6941           $worksheet4->write_col('A1', \@array);
6942
6943           # 5. Write a column of data using a ref to an array ref, i.e. a 2D array.
6944           $worksheet5->write('A1', [ \@array ]);
6945
6946           # 6. Write a 2D array in col-row order.
6947           $worksheet6->write('A1', \@array2d);
6948
6949           # 7. Write a 2D array in row-col order.
6950           $worksheet7->write_col('A1', \@array2d);
6951
6952           # 8. Write a row of data with formatting. The blank cell is also formatted.
6953           $worksheet8->write('A1', \@array, $format);
6954
6955       Download this example:
6956       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/write_arrays.pl>
6957
6958   Example: write_handler1.pl
6959       Example of how to add a user defined data handler to the Spreadsheet::
6960       WriteExcel write() method.
6961
6962       The following example shows how to add a handler for a 7 digit ID
6963       number.
6964
6965       Source code for this example:
6966
6967           #!/usr/bin/perl -w
6968
6969           ###############################################################################
6970           #
6971           # Example of how to add a user defined data handler to the Spreadsheet::
6972           # WriteExcel write() method.
6973           #
6974           # The following example shows how to add a handler for a 7 digit ID number.
6975           #
6976           #
6977           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
6978           #
6979
6980           use strict;
6981           use Spreadsheet::WriteExcel;
6982
6983
6984           my $workbook    = Spreadsheet::WriteExcel->new("write_handler1.xls");
6985           my $worksheet   = $workbook->add_worksheet();
6986
6987
6988           ###############################################################################
6989           #
6990           # Add a handler for 7 digit id numbers. This is useful when you want a string
6991           # such as 0000001 written as a string instead of a number and thus preserve
6992           # the leading zeroes.
6993           #
6994           # Note: you can get the same effect using the keep_leading_zeros() method but
6995           # this serves as a simple example.
6996           #
6997           $worksheet->add_write_handler(qr[^\d{7}$], \&write_my_id);
6998
6999
7000           ###############################################################################
7001           #
7002           # The following function processes the data when a match is found.
7003           #
7004           sub write_my_id {
7005
7006               my $worksheet = shift;
7007
7008               return $worksheet->write_string(@_);
7009           }
7010
7011
7012           # This format maintains the cell as text even if it is edited.
7013           my $id_format   = $workbook->add_format(num_format => '@');
7014
7015
7016           # Write some numbers in the user defined format
7017           $worksheet->write('A1', '0000000', $id_format);
7018           $worksheet->write('A2', '0000001', $id_format);
7019           $worksheet->write('A3', '0004000', $id_format);
7020           $worksheet->write('A4', '1234567', $id_format);
7021
7022           # Write some numbers that don't match the defined format
7023           $worksheet->write('A6', '000000',  $id_format);
7024           $worksheet->write('A7', '000001',  $id_format);
7025           $worksheet->write('A8', '004000',  $id_format);
7026           $worksheet->write('A9', '123456',  $id_format);
7027
7028
7029           __END__
7030
7031       Download this example:
7032       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/write_handler1.pl>
7033
7034   Example: write_handler2.pl
7035       Example of how to add a user defined data handler to the Spreadsheet::
7036       WriteExcel write() method.
7037
7038       The following example shows how to add a handler for a 7 digit ID
7039       number.  It adds an additional constraint to the write_handler1.pl in
7040       that it only filters data that isn't in the third column.
7041
7042       Source code for this example:
7043
7044           #!/usr/bin/perl -w
7045
7046           ###############################################################################
7047           #
7048           # Example of how to add a user defined data handler to the Spreadsheet::
7049           # WriteExcel write() method.
7050           #
7051           # The following example shows how to add a handler for a 7 digit ID number.
7052           # It adds an additional constraint to the write_handler1.pl in that it only
7053           # filters data that isn't in the third column.
7054           #
7055           #
7056           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7057           #
7058
7059           use strict;
7060           use Spreadsheet::WriteExcel;
7061
7062
7063           my $workbook    = Spreadsheet::WriteExcel->new("write_handler2.xls");
7064           my $worksheet   = $workbook->add_worksheet();
7065
7066
7067           ###############################################################################
7068           #
7069           # Add a handler for 7 digit id numbers. This is useful when you want a string
7070           # such as 0000001 written as a string instead of a number and thus preserve
7071           # the leading zeroes.
7072           #
7073           # Note: you can get the same effect using the keep_leading_zeros() method but
7074           # this serves as a simple example.
7075           #
7076           $worksheet->add_write_handler(qr[^\d{7}$], \&write_my_id);
7077
7078
7079           ###############################################################################
7080           #
7081           # The following function processes the data when a match is found. The handler
7082           # is set up so that it only filters data if it is in the third column.
7083           #
7084           sub write_my_id {
7085
7086               my $worksheet = shift;
7087               my $col       = $_[1];
7088
7089               # col is zero based
7090               if ($col != 2) {
7091                   return $worksheet->write_string(@_);
7092               }
7093               else {
7094                   # Reject the match and return control to write()
7095                   return undef;
7096               }
7097
7098           }
7099
7100
7101           # This format maintains the cell as text even if it is edited.
7102           my $id_format   = $workbook->add_format(num_format => '@');
7103
7104
7105           # Write some numbers in the user defined format
7106           $worksheet->write('A1', '0000000', $id_format);
7107           $worksheet->write('B1', '0000001', $id_format);
7108           $worksheet->write('C1', '0000002', $id_format);
7109           $worksheet->write('D1', '0000003', $id_format);
7110
7111
7112
7113           __END__
7114
7115       Download this example:
7116       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/write_handler2.pl>
7117
7118   Example: write_handler3.pl
7119       Example of how to add a user defined data handler to the Spreadsheet::
7120       WriteExcel write() method.
7121
7122       The following example shows how to add a handler for dates in a
7123       specific format.
7124
7125       See write_handler4.pl for a more rigorous example with error handling.
7126
7127       Source code for this example:
7128
7129           #!/usr/bin/perl -w
7130
7131           ###############################################################################
7132           #
7133           # Example of how to add a user defined data handler to the Spreadsheet::
7134           # WriteExcel write() method.
7135           #
7136           # The following example shows how to add a handler for dates in a specific
7137           # format.
7138           #
7139           # See write_handler4.pl for a more rigorous example with error handling.
7140           #
7141           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7142           #
7143
7144           use strict;
7145           use Spreadsheet::WriteExcel;
7146
7147
7148           my $workbook    = Spreadsheet::WriteExcel->new("write_handler3.xls");
7149           my $worksheet   = $workbook->add_worksheet();
7150           my $date_format = $workbook->add_format(num_format => 'dd/mm/yy');
7151
7152
7153           ###############################################################################
7154           #
7155           # Add a handler to match dates in the following format: d/m/yyyy
7156           #
7157           # The day and month can be single or double digits.
7158           #
7159           $worksheet->add_write_handler(qr[^\d{1,2}/\d{1,2}/\d{4}$], \&write_my_date);
7160
7161
7162           ###############################################################################
7163           #
7164           # The following function processes the data when a match is found.
7165           # See write_handler4.pl for a more rigorous example with error handling.
7166           #
7167           sub write_my_date {
7168
7169               my $worksheet = shift;
7170               my @args      = @_;
7171
7172               my $token     = $args[2];
7173                  $token     =~ qr[^(\d{1,2})/(\d{1,2})/(\d{4})$];
7174
7175               # Change to the date format required by write_date_time().
7176               my $date = sprintf "%4d-%02d-%02dT", $3, $2, $1;
7177
7178               $args[2] = $date;
7179
7180               return $worksheet->write_date_time(@args);
7181           }
7182
7183
7184           # Write some dates in the user defined format
7185           $worksheet->write('A1', '22/12/2004', $date_format);
7186           $worksheet->write('A2', '1/1/1995',   $date_format);
7187           $worksheet->write('A3', '01/01/1995', $date_format);
7188
7189
7190
7191           __END__
7192
7193       Download this example:
7194       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/write_handler3.pl>
7195
7196   Example: write_handler4.pl
7197       Example of how to add a user defined data handler to the Spreadsheet::
7198       WriteExcel write() method.
7199
7200       The following example shows how to add a handler for dates in a
7201       specific format.
7202
7203       This is a more rigorous version of write_handler3.pl.
7204
7205       Source code for this example:
7206
7207           #!/usr/bin/perl -w
7208
7209           ###############################################################################
7210           #
7211           # Example of how to add a user defined data handler to the Spreadsheet::
7212           # WriteExcel write() method.
7213           #
7214           # The following example shows how to add a handler for dates in a specific
7215           # format.
7216           #
7217           # This is a more rigorous version of write_handler3.pl.
7218           #
7219           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7220           #
7221
7222           use strict;
7223           use Spreadsheet::WriteExcel;
7224
7225
7226           my $workbook    = Spreadsheet::WriteExcel->new("write_handler4.xls");
7227           my $worksheet   = $workbook->add_worksheet();
7228           my $date_format = $workbook->add_format(num_format => 'dd/mm/yy');
7229
7230
7231           ###############################################################################
7232           #
7233           # Add a handler to match dates in the following formats: d/m/yy, d/m/yyyy
7234           #
7235           # The day and month can be single or double digits and the year can be  2 or 4
7236           # digits.
7237           #
7238           $worksheet->add_write_handler(qr[^\d{1,2}/\d{1,2}/\d{2,4}$], \&write_my_date);
7239
7240
7241           ###############################################################################
7242           #
7243           # The following function processes the data when a match is found.
7244           #
7245           sub write_my_date {
7246
7247               my $worksheet = shift;
7248               my @args      = @_;
7249
7250               my $token     = $args[2];
7251
7252               if ($token =~  qr[^(\d{1,2})/(\d{1,2})/(\d{2,4})$]) {
7253
7254                   my $day  = $1;
7255                   my $mon  = $2;
7256                   my $year = $3;
7257
7258                   # Use a window for 2 digit dates. This will keep some ragged Perl
7259                   # programmer employed in thirty years time. :-)
7260                   if (length $year == 2) {
7261                       if ($year < 50) {
7262                           $year += 2000;
7263                       }
7264                       else {
7265                           $year += 1900;
7266                       }
7267                   }
7268
7269                   my $date = sprintf "%4d-%02d-%02dT", $year, $mon, $day;
7270
7271                   # Convert the ISO ISO8601 style string to an Excel date
7272                   $date = $worksheet->convert_date_time($date);
7273
7274                   if (defined $date) {
7275                       # Date was valid
7276                       $args[2] = $date;
7277                       return $worksheet->write_number(@args);
7278                   }
7279                   else {
7280                       # Not a valid date therefore write as a string
7281                       return $worksheet->write_string(@args);
7282                   }
7283               }
7284               else {
7285                   # Shouldn't happen if the same match is used in the re and sub.
7286                   return undef;
7287               }
7288           }
7289
7290
7291           # Write some dates in the user defined format
7292           $worksheet->write('A1', '22/12/2004', $date_format);
7293           $worksheet->write('A2', '22/12/04',   $date_format);
7294           $worksheet->write('A3', '2/12/04',    $date_format);
7295           $worksheet->write('A4', '2/5/04',     $date_format);
7296           $worksheet->write('A5', '2/5/95',     $date_format);
7297           $worksheet->write('A6', '2/5/1995',   $date_format);
7298
7299           # Some erroneous dates
7300           $worksheet->write('A8', '2/5/1895',   $date_format); # Date out of Excel range
7301           $worksheet->write('A9', '29/2/2003',  $date_format); # Invalid leap day
7302           $worksheet->write('A10','50/50/50',   $date_format); # Matches but isn't a date
7303
7304
7305           __END__
7306
7307       Download this example:
7308       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/write_handler4.pl>
7309
7310   Example: write_to_scalar.pl
7311       An example of writing an Excel file to a Perl scalar using
7312       Spreadsheet:: WriteExcel and the new features of perl 5.8.
7313
7314       For an examples of how to write to a scalar in versions prior to perl
7315       5.8 see the filehandle.pl program and IO:Scalar.
7316
7317           #!/usr/bin/perl -w
7318
7319           ##############################################################################
7320           #
7321           # An example of writing an Excel file to a Perl scalar using Spreadsheet::
7322           # WriteExcel and the new features of perl 5.8.
7323           #
7324           # For an examples of how to write to a scalar in versions prior to perl 5.8
7325           # see the filehandle.pl program and IO:Scalar.
7326           #
7327           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7328           #
7329
7330           use strict;
7331           use Spreadsheet::WriteExcel;
7332
7333           require 5.008;
7334
7335
7336           # Use perl 5.8's feature of using a scalar as a filehandle.
7337           my   $fh;
7338           my   $str = '';
7339           open $fh, '>', \$str or die "Failed to open filehandle: $!";
7340
7341
7342           # Or replace the previous three lines with this:
7343           # open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
7344
7345
7346           # Spreadsheet::WriteExce accepts filehandle as well as file names.
7347           my $workbook  = Spreadsheet::WriteExcel->new($fh);
7348           my $worksheet = $workbook->add_worksheet();
7349
7350           $worksheet->write(0, 0,  "Hi Excel!");
7351
7352           $workbook->close();
7353
7354
7355           # The Excel file in now in $str. Remember to binmode() the output
7356           # filehandle before printing it.
7357           binmode STDOUT;
7358           print $str;
7359
7360
7361           __END__
7362
7363       Download this example:
7364       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/write_to_scalar.pl>
7365
7366   Example: unicode_utf16.pl
7367       A simple example of writing some Unicode text with
7368       Spreadsheet::WriteExcel.
7369
7370       This example shows UTF16 encoding. With perl 5.8 it is also possible to
7371       use utf8 without modification.
7372
7373       Source code for this example:
7374
7375           #!/usr/bin/perl -w
7376
7377           ##############################################################################
7378           #
7379           # A simple example of writing some Unicode text with Spreadsheet::WriteExcel.
7380           #
7381           # This example shows UTF16 encoding. With perl 5.8 it is also possible to use
7382           # utf8 without modification.
7383           #
7384           # reverse('(c)'), May 2004, John McNamara, jmcnamara@cpan.org
7385           #
7386
7387
7388           use strict;
7389           use Spreadsheet::WriteExcel;
7390
7391
7392           my $workbook  = Spreadsheet::WriteExcel->new('unicode_utf16.xls');
7393           my $worksheet = $workbook->add_worksheet();
7394
7395
7396           # Write the Unicode smiley face (with increased font for legibility)
7397           my $smiley    = pack "n", 0x263a;
7398           my $big_font  = $workbook->add_format(size => 40);
7399
7400           $worksheet->write_utf16be_string('A3', $smiley, $big_font);
7401
7402
7403           # Write a phrase in Cyrillic
7404           my $uni_str = pack "H*", "042d0442043e002004440440043004370430002004".
7405                                    "3d043000200440044304410441043a043e043c0021";
7406
7407           $worksheet->write_utf16be_string('A5', $uni_str);
7408
7409
7410           $worksheet->write_utf16be_string('A7', pack "H*", "0074006500730074");
7411
7412
7413
7414
7415
7416           __END__
7417
7418       Download this example:
7419       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_utf16.pl>
7420
7421   Example: unicode_utf16_japan.pl
7422       A simple example of writing some Unicode text with
7423       Spreadsheet::WriteExcel.
7424
7425       This creates an Excel file with the word Nippon in 3 character sets.
7426
7427       This example shows UTF16 encoding. With perl 5.8 it is also possible to
7428       use utf8 without modification.
7429
7430       See also the unicode_2022_jp.pl and unicode_shift_jis.pl examples.
7431
7432       Source code for this example:
7433
7434           #!/usr/bin/perl -w
7435
7436           ##############################################################################
7437           #
7438           # A simple example of writing some Unicode text with Spreadsheet::WriteExcel.
7439           #
7440           # This creates an Excel file with the word Nippon in 3 character sets.
7441           #
7442           # This example shows UTF16 encoding. With perl 5.8 it is also possible to use
7443           # utf8 without modification.
7444           #
7445           # See also the unicode_2022_jp.pl and unicode_shift_jis.pl examples.
7446           #
7447           # reverse('(c)'), May 2004, John McNamara, jmcnamara@cpan.org
7448           #
7449
7450
7451           use strict;
7452           use Spreadsheet::WriteExcel;
7453
7454
7455           my $workbook  = Spreadsheet::WriteExcel->new('unicode_utf16_japan.xls');
7456           my $worksheet = $workbook->add_worksheet();
7457
7458
7459           # Set a Unicode font.
7460           my $uni_font  = $workbook->add_format(font => 'Arial Unicode MS');
7461
7462
7463           # Create some UTF-16BE Unicode text.
7464           my $kanji     = pack 'n*', 0x65e5, 0x672c;
7465           my $katakana  = pack 'n*', 0xff86, 0xff8e, 0xff9d;
7466           my $hiragana  = pack 'n*', 0x306b, 0x307b, 0x3093;
7467
7468
7469
7470           $worksheet->write_utf16be_string('A1', $kanji,    $uni_font);
7471           $worksheet->write_utf16be_string('A2', $katakana, $uni_font);
7472           $worksheet->write_utf16be_string('A3', $hiragana, $uni_font);
7473
7474
7475           $worksheet->write('B1', 'Kanji');
7476           $worksheet->write('B2', 'Katakana');
7477           $worksheet->write('B3', 'Hiragana');
7478
7479
7480           __END__
7481
7482       Download this example:
7483       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_utf16_japan.pl>
7484
7485   Example: unicode_cyrillic.pl
7486       A simple example of writing some Russian cyrillic text using
7487       Spreadsheet::WriteExcel and perl 5.8.
7488
7489       Source code for this example:
7490
7491           #!/usr/bin/perl -w
7492
7493           ##############################################################################
7494           #
7495           # A simple example of writing some Russian cyrillic text using
7496           # Spreadsheet::WriteExcel and perl 5.8.
7497           #
7498           # reverse('(c)'), March 2005, John McNamara, jmcnamara@cpan.org
7499           #
7500
7501
7502
7503           # Perl 5.8 or later is required for proper utf8 handling. For older perl
7504           # versions you should use UTF16 and the write_utf16be_string() method.
7505           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
7506           #
7507           require 5.008;
7508
7509           use strict;
7510           use Spreadsheet::WriteExcel;
7511
7512
7513           # In this example we generate utf8 strings from character data but in a
7514           # real application we would expect them to come from an external source.
7515           #
7516
7517
7518           # Create a Russian worksheet name in utf8.
7519           my $sheet   = pack "U*", 0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438,
7520                                    0x0446, 0x0430;
7521
7522
7523           # Create a Russian string.
7524           my $str     = pack "U*", 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441,
7525                                    0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C,
7526                                    0x0438, 0x0440, 0x0021;
7527
7528
7529
7530           my $workbook  = Spreadsheet::WriteExcel->new("unicode_cyrillic.xls");
7531           my $worksheet = $workbook->add_worksheet($sheet . '1');
7532
7533              $worksheet->set_column('A:A', 18);
7534              $worksheet->write('A1', $str);
7535
7536
7537           __END__
7538
7539       Download this example:
7540       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_cyrillic.pl>
7541
7542   Example: unicode_list.pl
7543       A simple example using Spreadsheet::WriteExcel to display all available
7544       Unicode characters in a font.
7545
7546       Source code for this example:
7547
7548           #!/usr/bin/perl -w
7549
7550           ##############################################################################
7551           #
7552           # A simple example using Spreadsheet::WriteExcel to display all available
7553           # Unicode characters in a font.
7554           #
7555           # reverse('(c)'), May 2004, John McNamara, jmcnamara@cpan.org
7556           #
7557
7558           use strict;
7559           use Spreadsheet::WriteExcel;
7560
7561
7562           my $workbook  = Spreadsheet::WriteExcel->new('unicode_list.xls');
7563           my $worksheet = $workbook->add_worksheet();
7564
7565
7566           # Set a Unicode font.
7567           my $uni_font  = $workbook->add_format(font => 'Arial Unicode MS');
7568
7569           # Ascii font for labels.
7570           my $courier   = $workbook->add_format(font => 'Courier New');
7571
7572
7573           my $char = 0;
7574
7575           # Loop through all 32768 UTF-16BE characters.
7576           #
7577           for my $row (0 .. 2 ** 12 -1) {
7578               for my $col (0 .. 31) {
7579
7580                   last if $char == 0xffff;
7581
7582                   if ($col % 2 == 0){
7583                       $worksheet->write_string($row, $col,
7584                                                      sprintf('0x%04X', $char), $courier);
7585                   }
7586                   else {
7587                       $worksheet->write_utf16be_string($row, $col,
7588                                                       pack('n', $char++), $uni_font);
7589                   }
7590               }
7591           }
7592
7593
7594
7595           __END__
7596
7597       Download this example:
7598       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_list.pl>
7599
7600   Example: unicode_2022_jp.pl
7601       A simple example of converting some Unicode text to an Excel file using
7602       Spreadsheet::WriteExcel and perl 5.8.
7603
7604       This example generates some Japanese from a file with ISO-2022-JP
7605       encoded text.
7606
7607       Source code for this example:
7608
7609           #!/usr/bin/perl -w
7610
7611           ##############################################################################
7612           #
7613           # A simple example of converting some Unicode text to an Excel file using
7614           # Spreadsheet::WriteExcel and perl 5.8.
7615           #
7616           # This example generates some Japanese from a file with ISO-2022-JP
7617           # encoded text.
7618           #
7619           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7620           #
7621
7622
7623
7624           # Perl 5.8 or later is required for proper utf8 handling. For older perl
7625           # versions you should use UTF16 and the write_utf16be_string() method.
7626           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
7627           #
7628           require 5.008;
7629
7630           use strict;
7631           use Spreadsheet::WriteExcel;
7632
7633
7634           my $workbook  = Spreadsheet::WriteExcel->new("unicode_2022_jp.xls");
7635           my $worksheet = $workbook->add_worksheet();
7636              $worksheet->set_column('A:A', 50);
7637
7638
7639           my $file = 'unicode_2022_jp.txt';
7640
7641           open FH, '<:encoding(iso-2022-jp)', $file  or die "Couldn't open $file: $!\n";
7642
7643           my $row = 0;
7644
7645           while (<FH>) {
7646               next if /^#/; # Ignore the comments in the sample file.
7647               chomp;
7648               $worksheet->write($row++, 0,  $_);
7649           }
7650
7651
7652           __END__
7653
7654       Download this example:
7655       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_2022_jp.pl>
7656
7657   Example: unicode_8859_11.pl
7658       A simple example of converting some Unicode text to an Excel file using
7659       Spreadsheet::WriteExcel and perl 5.8.
7660
7661       This example generates some Thai from a file with ISO-8859-11 encoded
7662       text.
7663
7664       Source code for this example:
7665
7666           #!/usr/bin/perl -w
7667
7668           ##############################################################################
7669           #
7670           # A simple example of converting some Unicode text to an Excel file using
7671           # Spreadsheet::WriteExcel and perl 5.8.
7672           #
7673           # This example generates some Thai from a file with ISO-8859-11 encoded text.
7674           #
7675           #
7676           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7677           #
7678
7679
7680
7681           # Perl 5.8 or later is required for proper utf8 handling. For older perl
7682           # versions you should use UTF16 and the write_utf16be_string() method.
7683           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
7684           #
7685           require 5.008;
7686
7687           use strict;
7688           use Spreadsheet::WriteExcel;
7689
7690
7691           my $workbook  = Spreadsheet::WriteExcel->new("unicode_8859_11.xls");
7692           my $worksheet = $workbook->add_worksheet();
7693              $worksheet->set_column('A:A', 50);
7694
7695
7696           my $file = 'unicode_8859_11.txt';
7697
7698           open FH, '<:encoding(iso-8859-11)', $file  or die "Couldn't open $file: $!\n";
7699
7700           my $row = 0;
7701
7702           while (<FH>) {
7703               next if /^#/; # Ignore the comments in the sample file.
7704               chomp;
7705               $worksheet->write($row++, 0,  $_);
7706           }
7707
7708
7709           __END__
7710
7711       Download this example:
7712       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_8859_11.pl>
7713
7714   Example: unicode_8859_7.pl
7715       A simple example of converting some Unicode text to an Excel file using
7716       Spreadsheet::WriteExcel and perl 5.8.
7717
7718       This example generates some Greek from a file with ISO-8859-7 encoded
7719       text.
7720
7721       Source code for this example:
7722
7723           #!/usr/bin/perl -w
7724
7725           ##############################################################################
7726           #
7727           # A simple example of converting some Unicode text to an Excel file using
7728           # Spreadsheet::WriteExcel and perl 5.8.
7729           #
7730           # This example generates some Greek from a file with ISO-8859-7 encoded text.
7731           #
7732           #
7733           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7734           #
7735
7736
7737
7738           # Perl 5.8 or later is required for proper utf8 handling. For older perl
7739           # versions you should use UTF16 and the write_utf16be_string() method.
7740           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
7741           #
7742           require 5.008;
7743
7744           use strict;
7745           use Spreadsheet::WriteExcel;
7746
7747
7748           my $workbook  = Spreadsheet::WriteExcel->new("unicode_8859_7.xls");
7749           my $worksheet = $workbook->add_worksheet();
7750              $worksheet->set_column('A:A', 50);
7751
7752
7753           my $file = 'unicode_8859_7.txt';
7754
7755           open FH, '<:encoding(iso-8859-7)', $file  or die "Couldn't open $file: $!\n";
7756
7757           my $row = 0;
7758
7759           while (<FH>) {
7760               next if /^#/; # Ignore the comments in the sample file.
7761               chomp;
7762               $worksheet->write($row++, 0,  $_);
7763           }
7764
7765
7766           __END__
7767
7768       Download this example:
7769       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_8859_7.pl>
7770
7771   Example: unicode_big5.pl
7772       A simple example of converting some Unicode text to an Excel file using
7773       Spreadsheet::WriteExcel and perl 5.8.
7774
7775       This example generates some Chinese from a file with BIG5 encoded text.
7776
7777       Source code for this example:
7778
7779           #!/usr/bin/perl -w
7780
7781           ##############################################################################
7782           #
7783           # A simple example of converting some Unicode text to an Excel file using
7784           # Spreadsheet::WriteExcel and perl 5.8.
7785           #
7786           # This example generates some Chinese from a file with BIG5 encoded text.
7787           #
7788           #
7789           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7790           #
7791
7792
7793
7794           # Perl 5.8 or later is required for proper utf8 handling. For older perl
7795           # versions you should use UTF16 and the write_utf16be_string() method.
7796           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
7797           #
7798           require 5.008;
7799
7800           use strict;
7801           use Spreadsheet::WriteExcel;
7802
7803
7804           my $workbook  = Spreadsheet::WriteExcel->new("unicode_big5.xls");
7805           my $worksheet = $workbook->add_worksheet();
7806              $worksheet->set_column('A:A', 80);
7807
7808
7809           my $file = 'unicode_big5.txt';
7810
7811           open FH, '<:encoding(big5)', $file  or die "Couldn't open $file: $!\n";
7812
7813           my $row = 0;
7814
7815           while (<FH>) {
7816               next if /^#/; # Ignore the comments in the sample file.
7817               chomp;
7818               $worksheet->write($row++, 0,  $_);
7819           }
7820
7821
7822           __END__
7823
7824       Download this example:
7825       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_big5.pl>
7826
7827   Example: unicode_cp1251.pl
7828       A simple example of converting some Unicode text to an Excel file using
7829       Spreadsheet::WriteExcel and perl 5.8.
7830
7831       This example generates some Russian from a file with CP1251 encoded
7832       text.
7833
7834       Source code for this example:
7835
7836           #!/usr/bin/perl -w
7837
7838           ##############################################################################
7839           #
7840           # A simple example of converting some Unicode text to an Excel file using
7841           # Spreadsheet::WriteExcel and perl 5.8.
7842           #
7843           # This example generates some Russian from a file with CP1251 encoded text.
7844           #
7845           #
7846           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7847           #
7848
7849
7850
7851           # Perl 5.8 or later is required for proper utf8 handling. For older perl
7852           # versions you should use UTF16 and the write_utf16be_string() method.
7853           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
7854           #
7855           require 5.008;
7856
7857           use strict;
7858           use Spreadsheet::WriteExcel;
7859
7860
7861           my $workbook  = Spreadsheet::WriteExcel->new("unicode_cp1251.xls");
7862           my $worksheet = $workbook->add_worksheet();
7863              $worksheet->set_column('A:A', 50);
7864
7865
7866           my $file = 'unicode_cp1251.txt';
7867
7868           open FH, '<:encoding(cp1251)', $file  or die "Couldn't open $file: $!\n";
7869
7870           my $row = 0;
7871
7872           while (<FH>) {
7873               next if /^#/; # Ignore the comments in the sample file.
7874               chomp;
7875               $worksheet->write($row++, 0,  $_);
7876           }
7877
7878
7879           __END__
7880
7881       Download this example:
7882       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_cp1251.pl>
7883
7884   Example: unicode_cp1256.pl
7885       A simple example of converting some Unicode text to an Excel file using
7886       Spreadsheet::WriteExcel and perl 5.8.
7887
7888       This example generates some Arabic text from a CP-1256 encoded file.
7889
7890       Source code for this example:
7891
7892           #!/usr/bin/perl -w
7893
7894           ##############################################################################
7895           #
7896           # A simple example of converting some Unicode text to an Excel file using
7897           # Spreadsheet::WriteExcel and perl 5.8.
7898           #
7899           # This example generates some Arabic text from a CP-1256 encoded file.
7900           #
7901           #
7902           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7903           #
7904
7905
7906
7907           # Perl 5.8 or later is required for proper utf8 handling. For older perl
7908           # versions you should use UTF16 and the write_utf16be_string() method.
7909           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
7910           #
7911           require 5.008;
7912
7913           use strict;
7914           use Spreadsheet::WriteExcel;
7915
7916
7917           my $workbook  = Spreadsheet::WriteExcel->new("unicode_cp1256.xls");
7918           my $worksheet = $workbook->add_worksheet();
7919              $worksheet->set_column('A:A', 50);
7920
7921
7922           my $file = 'unicode_cp1256.txt';
7923
7924           open FH, '<:encoding(cp1256)', $file  or die "Couldn't open $file: $!\n";
7925
7926           my $row = 0;
7927
7928           while (<FH>) {
7929               next if /^#/; # Ignore the comments in the sample file.
7930               chomp;
7931               $worksheet->write($row++, 0,  $_);
7932           }
7933
7934
7935           __END__
7936
7937       Download this example:
7938       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_cp1256.pl>
7939
7940   Example: unicode_koi8r.pl
7941       A simple example of converting some Unicode text to an Excel file using
7942       Spreadsheet::WriteExcel and perl 5.8.
7943
7944       This example generates some Russian from a file with KOI8-R encoded
7945       text.
7946
7947       Source code for this example:
7948
7949           #!/usr/bin/perl -w
7950
7951           ##############################################################################
7952           #
7953           # A simple example of converting some Unicode text to an Excel file using
7954           # Spreadsheet::WriteExcel and perl 5.8.
7955           #
7956           # This example generates some Russian from a file with KOI8-R encoded text.
7957           #
7958           #
7959           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
7960           #
7961
7962
7963
7964           # Perl 5.8 or later is required for proper utf8 handling. For older perl
7965           # versions you should use UTF16 and the write_utf16be_string() method.
7966           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
7967           #
7968           require 5.008;
7969
7970           use strict;
7971           use Spreadsheet::WriteExcel;
7972
7973
7974           my $workbook  = Spreadsheet::WriteExcel->new("unicode_koi8r.xls");
7975           my $worksheet = $workbook->add_worksheet();
7976              $worksheet->set_column('A:A', 50);
7977
7978
7979           my $file = 'unicode_koi8r.txt';
7980
7981           open FH, '<:encoding(koi8-r)', $file  or die "Couldn't open $file: $!\n";
7982
7983           my $row = 0;
7984
7985           while (<FH>) {
7986               next if /^#/; # Ignore the comments in the sample file.
7987               chomp;
7988               $worksheet->write($row++, 0,  $_);
7989           }
7990
7991
7992           __END__
7993
7994       Download this example:
7995       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_koi8r.pl>
7996
7997   Example: unicode_polish_utf8.pl
7998       A simple example of converting some Unicode text to an Excel file using
7999       Spreadsheet::WriteExcel and perl 5.8.
8000
8001       This example generates some Polish from a file with UTF8 encoded text.
8002
8003       Source code for this example:
8004
8005           #!/usr/bin/perl -w
8006
8007           ##############################################################################
8008           #
8009           # A simple example of converting some Unicode text to an Excel file using
8010           # Spreadsheet::WriteExcel and perl 5.8.
8011           #
8012           # This example generates some Polish from a file with UTF8 encoded text.
8013           #
8014           #
8015           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
8016           #
8017
8018
8019
8020           # Perl 5.8 or later is required for proper utf8 handling. For older perl
8021           # versions you should use UTF16 and the write_utf16be_string() method.
8022           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
8023           #
8024           require 5.008;
8025
8026           use strict;
8027           use Spreadsheet::WriteExcel;
8028
8029
8030           my $workbook  = Spreadsheet::WriteExcel->new("unicode_polish_utf8.xls");
8031           my $worksheet = $workbook->add_worksheet();
8032              $worksheet->set_column('A:A', 50);
8033
8034
8035           my $file = 'unicode_polish_utf8.txt';
8036
8037           open FH, '<:encoding(utf8)', $file  or die "Couldn't open $file: $!\n";
8038
8039           my $row = 0;
8040
8041           while (<FH>) {
8042               next if /^#/; # Ignore the comments in the sample file.
8043               chomp;
8044               $worksheet->write($row++, 0,  $_);
8045           }
8046
8047
8048           __END__
8049
8050       Download this example:
8051       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_polish_utf8.pl>
8052
8053   Example: unicode_shift_jis.pl
8054       A simple example of converting some Unicode text to an Excel file using
8055       Spreadsheet::WriteExcel and perl 5.8.
8056
8057       This example generates some Japenese text from a file with Shift-JIS
8058       encoded text.
8059
8060       Source code for this example:
8061
8062           #!/usr/bin/perl -w
8063
8064           ##############################################################################
8065           #
8066           # A simple example of converting some Unicode text to an Excel file using
8067           # Spreadsheet::WriteExcel and perl 5.8.
8068           #
8069           # This example generates some Japenese text from a file with Shift-JIS
8070           # encoded text.
8071           #
8072           # reverse('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
8073           #
8074
8075
8076
8077           # Perl 5.8 or later is required for proper utf8 handling. For older perl
8078           # versions you should use UTF16 and the write_utf16be_string() method.
8079           # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
8080           #
8081           require 5.008;
8082
8083           use strict;
8084           use Spreadsheet::WriteExcel;
8085
8086
8087           my $workbook  = Spreadsheet::WriteExcel->new("unicode_shift_jis.xls");
8088           my $worksheet = $workbook->add_worksheet();
8089              $worksheet->set_column('A:A', 50);
8090
8091
8092           my $file = 'unicode_shift_jis.txt';
8093
8094           open FH, '<:encoding(shiftjis)', $file  or die "Couldn't open $file: $!\n";
8095
8096           my $row = 0;
8097
8098           while (<FH>) {
8099               next if /^#/; # Ignore the comments in the sample file.
8100               chomp;
8101               $worksheet->write($row++, 0,  $_);
8102           }
8103
8104
8105           __END__
8106
8107       Download this example:
8108       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/unicode_shift_jis.pl>
8109
8110   Example: csv2xls.pl
8111       Example of how to use the WriteExcel module
8112
8113       Simple program to convert a CSV comma-separated value file to an Excel
8114       file.  This is more or less an non-op since Excel can read CSV files.
8115       The program uses Text::CSV_XS to parse the CSV.
8116
8117       Usage: csv2xls.pl file.csv newfile.xls
8118
8119       NOTE: This is only a simple conversion utility for illustrative
8120       purposes.  For converting a CSV or Tab separated or any other type of
8121       delimited text file to Excel I recommend the more rigorous csv2xls
8122       program that is part of H.Merijn Brand's Text::CSV_XS module distro.
8123
8124       See the examples/csv2xls link here:
8125           <http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
8126
8127           #!/usr/bin/perl -w
8128
8129           ###############################################################################
8130           #
8131           # Example of how to use the WriteExcel module
8132           #
8133           # Simple program to convert a CSV comma-separated value file to an Excel file.
8134           # This is more or less an non-op since Excel can read CSV files.
8135           # The program uses Text::CSV_XS to parse the CSV.
8136           #
8137           # Usage: csv2xls.pl file.csv newfile.xls
8138           #
8139           #
8140           # NOTE: This is only a simple conversion utility for illustrative purposes.
8141           # For converting a CSV or Tab separated or any other type of delimited
8142           # text file to Excel I recommend the more rigorous csv2xls program that is
8143           # part of H.Merijn Brand's Text::CSV_XS module distro.
8144           #
8145           # See the examples/csv2xls link here:
8146           #     L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
8147           #
8148           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
8149           #
8150
8151           use strict;
8152           use Spreadsheet::WriteExcel;
8153           use Text::CSV_XS;
8154
8155           # Check for valid number of arguments
8156           if (($#ARGV < 1) || ($#ARGV > 2)) {
8157              die("Usage: csv2xls csvfile.txt newfile.xls\n");
8158           };
8159
8160           # Open the Comma Separated Variable file
8161           open (CSVFILE, $ARGV[0]) or die "$ARGV[0]: $!";
8162
8163           # Create a new Excel workbook
8164           my $workbook  = Spreadsheet::WriteExcel->new($ARGV[1]);
8165           my $worksheet = $workbook->add_worksheet();
8166
8167           # Create a new CSV parsing object
8168           my $csv = Text::CSV_XS->new;
8169
8170           # Row and column are zero indexed
8171           my $row = 0;
8172
8173
8174           while (<CSVFILE>) {
8175               if ($csv->parse($_)) {
8176                   my @Fld = $csv->fields;
8177
8178                   my $col = 0;
8179                   foreach my $token (@Fld) {
8180                       $worksheet->write($row, $col, $token);
8181                       $col++;
8182                   }
8183                   $row++;
8184               }
8185               else {
8186                   my $err = $csv->error_input;
8187                   print "Text::CSV_XS parse() failed on argument: ", $err, "\n";
8188               }
8189           }
8190
8191       Download this example:
8192       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/csv2xls.pl>
8193
8194   Example: tab2xls.pl
8195       Example of how to use the WriteExcel module
8196
8197       The following converts a tab separated file into an Excel file
8198
8199       Usage: tab2xls.pl tabfile.txt newfile.xls
8200
8201       NOTE: This is only a simple conversion utility for illustrative
8202       purposes.  For converting a CSV or Tab separated or any other type of
8203       delimited text file to Excel I recommend the more rigorous csv2xls
8204       program that is part of H.Merijn Brand's Text::CSV_XS module distro.
8205
8206       See the examples/csv2xls link here:
8207           <http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
8208
8209           #!/usr/bin/perl -w
8210
8211           ###############################################################################
8212           #
8213           # Example of how to use the WriteExcel module
8214           #
8215           # The following converts a tab separated file into an Excel file
8216           #
8217           # Usage: tab2xls.pl tabfile.txt newfile.xls
8218           #
8219           #
8220           # NOTE: This is only a simple conversion utility for illustrative purposes.
8221           # For converting a CSV or Tab separated or any other type of delimited
8222           # text file to Excel I recommend the more rigorous csv2xls program that is
8223           # part of H.Merijn Brand's Text::CSV_XS module distro.
8224           #
8225           # See the examples/csv2xls link here:
8226           #     L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
8227           #
8228           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
8229           #
8230
8231
8232           use strict;
8233           use Spreadsheet::WriteExcel;
8234
8235
8236           # Check for valid number of arguments
8237           if (($#ARGV < 1) || ($#ARGV > 2)) {
8238               die("Usage: tab2xls tabfile.txt newfile.xls\n");
8239           };
8240
8241
8242           # Open the tab delimited file
8243           open (TABFILE, $ARGV[0]) or die "$ARGV[0]: $!";
8244
8245
8246           # Create a new Excel workbook
8247           my $workbook  = Spreadsheet::WriteExcel->new($ARGV[1]);
8248           my $worksheet = $workbook->add_worksheet();
8249
8250           # Row and column are zero indexed
8251           my $row = 0;
8252
8253           while (<TABFILE>) {
8254               chomp;
8255               # Split on single tab
8256               my @Fld = split('\t', $_);
8257
8258               my $col = 0;
8259               foreach my $token (@Fld) {
8260                   $worksheet->write($row, $col, $token);
8261                   $col++;
8262               }
8263               $row++;
8264           }
8265
8266       Download this example:
8267       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/tab2xls.pl>
8268
8269   Example: datecalc1.pl
8270       NOTE: An easier way of writing dates and times is to use the newer
8271             write_date_time() Worksheet method. See the date_time.pl example.
8272
8273       Demonstration of writing date/time cells to Excel spreadsheets, using
8274       UNIX/Perl time as source of date/time.
8275
8276       UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
8277       measured in seconds.
8278
8279       An Excel file can use exactly one of two different date/time systems.
8280       In these systems, a floating point number represents the number of days
8281       (and fractional parts of the day) since a start point. The floating
8282       point number is referred to as a 'serial'.
8283
8284       The two systems ('1900' and '1904') use different starting points:
8285
8286        '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
8287                a leap year - see:
8288                  http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
8289                for the excuse^H^H^H^H^H^Hreason.
8290        '1904'; '1.00' is 2 Jan 1904.
8291
8292       The '1904' system is the default for Apple Macs. Windows versions of
8293       Excel have the option to use the '1904' system.
8294
8295       Note that Visual Basic's "DateSerial" function does NOT erroneously
8296       regard 1900 as a leap year, and thus its serials do not agree with the
8297       1900 serials of Excel for dates before 1 Mar 1900.
8298
8299       Note that StarOffice (at least at version 5.2) does NOT erroneously
8300       regard 1900 as a leap year, and thus its serials do not agree with the
8301       1900 serials of Excel for dates before 1 Mar 1900.
8302
8303           #!/usr/bin/perl -w
8304
8305
8306           ######################################################################
8307           #
8308           # NOTE: An easier way of writing dates and times is to use the newer
8309           #       write_date_time() Worksheet method. See the date_time.pl example.
8310           #
8311           ######################################################################
8312           #
8313           # Demonstration of writing date/time cells to Excel spreadsheets,
8314           # using UNIX/Perl time as source of date/time.
8315           #
8316           ######################################################################
8317           #
8318           # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
8319           # measured in seconds.
8320           #
8321           # An Excel file can use exactly one of two different date/time systems.
8322           # In these systems, a floating point number represents the number of days
8323           # (and fractional parts of the day) since a start point. The floating point
8324           # number is referred to as a 'serial'.
8325           #
8326           # The two systems ('1900' and '1904') use different starting points:
8327           #
8328           #  '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
8329           #          a leap year - see:
8330           #            http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
8331           #          for the excuse^H^H^H^H^H^Hreason.
8332           #  '1904'; '1.00' is 2 Jan 1904.
8333           #
8334           # The '1904' system is the default for Apple Macs. Windows versions of
8335           # Excel have the option to use the '1904' system.
8336           #
8337           # Note that Visual Basic's "DateSerial" function does NOT erroneously
8338           # regard 1900 as a leap year, and thus its serials do not agree with
8339           # the 1900 serials of Excel for dates before 1 Mar 1900.
8340           #
8341           # Note that StarOffice (at least at version 5.2) does NOT erroneously
8342           # regard 1900 as a leap year, and thus its serials do not agree with
8343           # the 1900 serials of Excel for dates before 1 Mar 1900.
8344           #
8345
8346           # Copyright 2000, Andrew Benham, adsb@bigfoot.com
8347           #
8348
8349           ######################################################################
8350           #
8351           # Calculation description
8352           # =======================
8353           #
8354           # 1900 system
8355           # -----------
8356           # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
8357           # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
8358           # were leap years with an extra day.
8359           # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
8360           # 1 Jan 1970.
8361           # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
8362           # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
8363           #
8364           # 1904 system
8365           # -----------
8366           # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
8367           # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
8368           # were leap years with an extra day.
8369           # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
8370           # 1 Jan 1970.
8371           # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
8372           #
8373           ######################################################################
8374           #
8375           # Copyright (c) 2000, Andrew Benham.
8376           # This program is free software. It may be used, redistributed and/or
8377           # modified under the same terms as Perl itself.
8378           #
8379           # Andrew Benham, adsb@bigfoot.com
8380           # London, United Kingdom
8381           # 11 Nov 2000
8382           #
8383           ######################################################################
8384
8385
8386           use strict;
8387           use Spreadsheet::WriteExcel;
8388
8389           use Time::Local;
8390
8391           use vars qw/$DATE_SYSTEM/;
8392
8393           # Use 1900 date system on all platforms other than Apple Mac (for which
8394           # use 1904 date system).
8395           $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
8396
8397           my $workbook = Spreadsheet::WriteExcel->new("dates.xls");
8398           my $worksheet = $workbook->add_worksheet();
8399
8400           my $format_date =  $workbook->add_format();
8401           $format_date->set_num_format('d mmmm yyy');
8402
8403           $worksheet->set_column(0,1,21);
8404
8405           $worksheet->write_string (0,0,"The epoch (GMT)");
8406           $worksheet->write_number (0,1,&calc_serial(0,1),0x16);
8407
8408           $worksheet->write_string (1,0,"The epoch (localtime)");
8409           $worksheet->write_number (1,1,&calc_serial(0,0),0x16);
8410
8411           $worksheet->write_string (2,0,"Today");
8412           $worksheet->write_number (2,1,&calc_serial(),$format_date);
8413
8414           my $christmas2000 = timelocal(0,0,0,25,11,100);
8415           $worksheet->write_string (3,0,"Christmas 2000");
8416           $worksheet->write_number (3,1,&calc_serial($christmas2000),$format_date);
8417
8418           $workbook->close();
8419
8420           #-----------------------------------------------------------
8421           # calc_serial()
8422           #
8423           # Called with (up to) 2 parameters.
8424           #   1.  Unix timestamp.  If omitted, uses current time.
8425           #   2.  GMT flag. Set to '1' to return serial in GMT.
8426           #       If omitted, returns serial in appropriate timezone.
8427           #
8428           # Returns date/time serial according to $DATE_SYSTEM selected
8429           #-----------------------------------------------------------
8430           sub calc_serial {
8431               my $time = (defined $_[0]) ? $_[0] : time();
8432               my $gmtflag = (defined $_[1]) ? $_[1] : 0;
8433
8434               # Divide timestamp by number of seconds in a day.
8435               # This gives a date serial with '0' on 1 Jan 1970.
8436               my $serial = $time / 86400;
8437
8438               # Adjust the date serial by the offset appropriate to the
8439               # currently selected system (1900/1904).
8440               if ($DATE_SYSTEM == 0) {        # use 1900 system
8441                       $serial += 25569;
8442               } else {                        # use 1904 system
8443                       $serial += 24107;
8444               }
8445
8446               unless ($gmtflag) {
8447                       # Now have a 'raw' serial with the right offset. But this
8448                       # gives a serial in GMT, which is false unless the timezone
8449                       # is GMT. We need to adjust the serial by the appropriate
8450                       # timezone offset.
8451                       # Calculate the appropriate timezone offset by seeing what
8452                       # the differences between localtime and gmtime for the given
8453                       # time are.
8454
8455                       my @gmtime = gmtime($time);
8456                       my @ltime  = localtime($time);
8457
8458                       # For the first 7 elements of the two arrays, adjust the
8459                       # date serial where the elements differ.
8460                       for (0 .. 6) {
8461                               my $diff = $ltime[$_] - $gmtime[$_];
8462                               if ($diff) {
8463                                       $serial += _adjustment($diff,$_);
8464                               }
8465                       }
8466               }
8467
8468               # Perpetuate the error that 1900 was a leap year by decrementing
8469               # the serial if we're using the 1900 system and the date is prior to
8470               # 1 Mar 1900. This has the effect of making serial value '60'
8471               # 29 Feb 1900.
8472
8473               # This fix only has any effect if UNIX/Perl time on the platform
8474               # can represent 1900. Many can't.
8475
8476               unless ($DATE_SYSTEM) {
8477                       $serial-- if ($serial < 61);    # '61' is 1 Mar 1900
8478               }
8479               return $serial;
8480           }
8481
8482           sub _adjustment {
8483               # Based on the difference in the localtime/gmtime array elements
8484               # number, return the adjustment required to the serial.
8485
8486               # We only look at some elements of the localtime/gmtime arrays:
8487               #    seconds    unlikely to be different as all known timezones
8488               #               have an offset of integral multiples of 15 minutes,
8489               #               but it's easy to do.
8490               #    minutes    will be different for timezone offsets which are
8491               #               not an exact number of hours.
8492               #    hours      very likely to be different.
8493               #    weekday    will differ when localtime/gmtime difference
8494               #               straddles midnight.
8495               #
8496               # Assume that difference between localtime and gmtime is less than
8497               # 5 days, then don't have to do maths for day of month, month number,
8498               # year number, etc...
8499
8500               my ($delta,$element) = @_;
8501               my $adjust = 0;
8502
8503               if ($element == 0) {            # Seconds
8504                       $adjust = $delta/86400;         # 60 * 60 * 24
8505               } elsif ($element == 1) {       # Minutes
8506                       $adjust = $delta/1440;          # 60 * 24
8507               } elsif ($element == 2) {       # Hours
8508                       $adjust = $delta/24;            # 24
8509               } elsif ($element == 6) {       # Day of week number
8510                       # Catch difference straddling Sat/Sun in either direction
8511                       $delta += 7 if ($delta < -4);
8512                       $delta -= 7 if ($delta > 4);
8513
8514                       $adjust = $delta;
8515               }
8516               return $adjust;
8517           }
8518
8519       Download this example:
8520       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/datecalc1.pl>
8521
8522   Example: datecalc2.pl
8523       Example of how to using the Date::Calc module to calculate Excel dates.
8524
8525       NOTE: An easier way of writing dates and times is to use the newer
8526             write_date_time() Worksheet method. See the date_time.pl example.
8527
8528           #!/usr/bin/perl -w
8529
8530           ###############################################################################
8531           #
8532           # Example of how to using the Date::Calc module to calculate Excel dates.
8533           #
8534           # NOTE: An easier way of writing dates and times is to use the newer
8535           #       write_date_time() Worksheet method. See the date_time.pl example.
8536           #
8537           # reverse('(c)'), June 2001, John McNamara, jmcnamara@cpan.org
8538           #
8539
8540           use strict;
8541           use Spreadsheet::WriteExcel;
8542           use Date::Calc qw(Delta_DHMS); # You may need to install this module.
8543
8544
8545           # Create a new workbook and add a worksheet
8546           my $workbook = Spreadsheet::WriteExcel->new("excel_date2.xls");
8547           my $worksheet = $workbook->add_worksheet();
8548
8549           # Expand the first column so that the date is visible.
8550           $worksheet->set_column("A:A", 25);
8551
8552
8553           # Add a format for the date
8554           my $format =  $workbook->add_format();
8555           $format->set_num_format('d mmmm yyy HH:MM:SS');
8556
8557
8558           my $date;
8559
8560           # Write some dates and times
8561           $date =  excel_date(1900, 1, 1);
8562           $worksheet->write("A1", $date, $format);
8563
8564           $date =  excel_date(2000, 1, 1);
8565           $worksheet->write("A2", $date, $format);
8566
8567           $date =  excel_date(2000, 4, 17, 14, 33, 15);
8568           $worksheet->write("A3", $date, $format);
8569
8570
8571           ###############################################################################
8572           #
8573           # excel_date($years, $months, $days, $hours, $minutes, $seconds)
8574           #
8575           # Create an Excel date in the 1900 format. All of the arguments are optional
8576           # but you should at least add $years.
8577           #
8578           # Corrects for Excel's missing leap day in 1900. See excel_time1.pl for an
8579           # explanation.
8580           #
8581           sub excel_date {
8582
8583               my $years   = $_[0] || 1900;
8584               my $months  = $_[1] || 1;
8585               my $days    = $_[2] || 1;
8586               my $hours   = $_[3] || 0;
8587               my $minutes = $_[4] || 0;
8588               my $seconds = $_[5] || 0;
8589
8590               my @date = ($years, $months, $days, $hours, $minutes, $seconds);
8591               my @epoch = (1899, 12, 31, 0, 0, 0);
8592
8593               ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date);
8594
8595               my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60);
8596
8597               # Add a day for Excel's missing leap day in 1900
8598               $date++ if ($date > 59);
8599
8600               return $date;
8601           }
8602
8603           ###############################################################################
8604           #
8605           # excel_date($years, $months, $days, $hours, $minutes, $seconds)
8606           #
8607           # Create an Excel date in the 1904 format. All of the arguments are optional
8608           # but you should at least add $years.
8609           #
8610           # You will also need to call $workbook->set_1904() for this format to be valid.
8611           #
8612           sub excel_date_1904 {
8613
8614               my $years   = $_[0] || 1900;
8615               my $months  = $_[1] || 1;
8616               my $days    = $_[2] || 1;
8617               my $hours   = $_[3] || 0;
8618               my $minutes = $_[4] || 0;
8619               my $seconds = $_[5] || 0;
8620
8621               my @date = ($years, $months, $days, $hours, $minutes, $seconds);
8622               my @epoch = (1904, 1, 1, 0, 0, 0);
8623
8624               ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date);
8625
8626               my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60);
8627
8628               return $date;
8629           }
8630
8631       Download this example:
8632       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/datecalc2.pl>
8633
8634   Example: lecxe.pl
8635       Utility program to convert an Excel file into a Spreadsheet::WriteExcel
8636       program using Win32::OLE
8637
8638           #!/usr/bin/perl -w
8639
8640           #
8641           # Utility program to convert an Excel file into a Spreadsheet::WriteExcel
8642           # program using Win32::OLE
8643           #
8644
8645           #
8646           # lecxe program
8647           # by t0mas@netlords.net
8648           #
8649           # Version  0.01a    Initial release (alpha)
8650
8651
8652           # Modules
8653           use strict;
8654           use Win32::OLE;
8655           use Win32::OLE::Const;
8656           use Getopt::Std;
8657
8658
8659           # Vars
8660           use vars qw(%opts);
8661
8662
8663           # Get options
8664           getopts('i:o:v',\%opts);
8665
8666
8667           # Not enough options
8668           exit &usage unless ($opts{i} && $opts{o});
8669
8670
8671           # Create Excel object
8672           my $Excel = new Win32::OLE("Excel.Application","Quit") or
8673                   die "Can't start excel: $!";
8674
8675
8676           # Get constants
8677           my $ExcelConst=Win32::OLE::Const->Load("Microsoft Excel");
8678
8679
8680           # Show Excel
8681           $Excel->{Visible} = 1 if ($opts{v});
8682
8683
8684           # Open infile
8685           my $Workbook = $Excel->Workbooks->Open({Filename=>$opts{i}});
8686
8687
8688           # Open outfile
8689           open (OUTFILE,">$opts{o}") or die "Can't open outfile $opts{o}: $!";
8690
8691
8692           # Print header for outfile
8693           print OUTFILE <<'EOH';
8694           #!/usr/bin/perl -w
8695
8696
8697           use strict;
8698           use Spreadsheet::WriteExcel;
8699
8700
8701           use vars qw($workbook %worksheets %formats);
8702
8703
8704           $workbook = Spreadsheet::WriteExcel->new("_change_me_.xls");
8705
8706
8707           EOH
8708
8709
8710           # Loop all sheets
8711           foreach my $sheetnum (1..$Excel->Workbooks(1)->Worksheets->Count) {
8712
8713
8714                   # Format sheet
8715                   my $name=$Excel->Workbooks(1)->Worksheets($sheetnum)->Name;
8716                   print "Sheet $name\n" if ($opts{v});
8717                   print OUTFILE "# Sheet $name\n";
8718                   print OUTFILE "\$worksheets{'$name'} = \$workbook->add_worksheet('$name');\n";
8719
8720
8721                   # Get usedrange of cells in worksheet
8722                   my $usedrange=$Excel->Workbooks(1)->Worksheets($sheetnum)->UsedRange;
8723
8724
8725                   # Loop all columns in used range
8726                   foreach my $j (1..$usedrange->Columns->Count){
8727
8728
8729                           # Format column
8730                           print "Col $j\n" if ($opts{v});
8731                           my ($colwidth);
8732                           $colwidth=$usedrange->Columns($j)->ColumnWidth;
8733                           print OUTFILE "# Column $j\n";
8734                           print OUTFILE "\$worksheets{'$name'}->set_column(".($j-1).",".($j-1).
8735                                   ", $colwidth);\n";
8736
8737
8738                           # Loop all rows in used range
8739                           foreach my $i (1..$usedrange->Rows->Count){
8740
8741
8742                                   # Format row
8743                                   print "Row $i\n" if ($opts{v});
8744                                   print OUTFILE "# Row $i\n";
8745                                   do {
8746                                           my ($rowheight);
8747                                           $rowheight=$usedrange->Rows($i)->RowHeight;
8748                                           print OUTFILE "\$worksheets{'$name'}->set_row(".($i-1).
8749                                                   ", $rowheight);\n";
8750                                   } if ($j==1);
8751
8752
8753                                   # Start creating cell format
8754                                   my $fname="\$formats{'".$name.'R'.$i.'C'.$j."'}";
8755                                   my $format="$fname=\$workbook->add_format();\n";
8756                                   my $print_format=0;
8757
8758                                   # Check for borders
8759                                   my @bfnames=qw(left right top bottom);
8760                                   foreach my $k (1..$usedrange->Cells($i,$j)->Borders->Count) {
8761                                           my $lstyle=$usedrange->Cells($i,$j)->Borders($k)->LineStyle;
8762                                           if ($lstyle > 0) {
8763                                                   $format.=$fname."->set_".$bfnames[$k-1]."($lstyle);\n";
8764                                                   $print_format=1;
8765                                           }
8766                                   }
8767
8768
8769                                   # Check for font
8770                                   my ($fontattr,$prop,$func,%fontsets,$fontColor);
8771                                   %fontsets=(Name=>'set_font',
8772                                                           Size=>'set_size');
8773                                   while (($prop,$func) = each %fontsets) {
8774                                           $fontattr=$usedrange->Cells($i,$j)->Font->$prop;
8775                                           if ($fontattr ne "") {
8776                                                   $format.=$fname."->$func('$fontattr');\n";
8777                                                   $print_format=1;
8778                                           }
8779
8780
8781                                   }
8782                                   %fontsets=(Bold=>'set_bold(1)',
8783                                                           Italic=>'set_italic(1)',
8784                                                           Underline=>'set_underline(1)',
8785                                                           Strikethrough=>'set_strikeout(1)',
8786                                                           Superscript=>'set_script(1)',
8787                                                           Subscript=>'set_script(2)',
8788                                                           OutlineFont=>'set_outline(1)',
8789                                                           Shadow=>'set_shadow(1)');
8790                                   while (($prop,$func) = each %fontsets) {
8791                                           $fontattr=$usedrange->Cells($i,$j)->Font->$prop;
8792                                           if ($fontattr==1) {
8793                                                   $format.=$fname."->$func;\n" ;
8794
8795                                                   $print_format=1;
8796                                           }
8797                                   }
8798                                   $fontColor=$usedrange->Cells($i,$j)->Font->ColorIndex();
8799                                   if ($fontColor>0&&$fontColor!=$ExcelConst->{xlColorIndexAutomatic}) {
8800                                           $format.=$fname."->set_color(".($fontColor+7).");\n" ;
8801                                           $print_format=1;
8802                                   }
8803
8804
8805
8806                                   # Check text alignment, merging and wrapping
8807                                   my ($halign,$valign,$merge,$wrap);
8808                                   $halign=$usedrange->Cells($i,$j)->HorizontalAlignment;
8809                                   my %hAligns=($ExcelConst->{xlHAlignCenter}=>"'center'",
8810                                           $ExcelConst->{xlHAlignJustify}=>"'justify'",
8811                                           $ExcelConst->{xlHAlignLeft}=>"'left'",
8812                                           $ExcelConst->{xlHAlignRight}=>"'right'",
8813                                           $ExcelConst->{xlHAlignFill}=>"'fill'",
8814                                           $ExcelConst->{xlHAlignCenterAcrossSelection}=>"'merge'");
8815                                   if ($halign!=$ExcelConst->{xlHAlignGeneral}) {
8816                                           $format.=$fname."->set_align($hAligns{$halign});\n";
8817                                           $print_format=1;
8818                                   }
8819                                   $valign=$usedrange->Cells($i,$j)->VerticalAlignment;
8820                                   my %vAligns=($ExcelConst->{xlVAlignBottom}=>"'bottom'",
8821                                           $ExcelConst->{xlVAlignCenter}=>"'vcenter'",
8822                                           $ExcelConst->{xlVAlignJustify}=>"'vjustify'",
8823                                           $ExcelConst->{xlVAlignTop}=>"'top'");
8824                                   if ($valign) {
8825                                           $format.=$fname."->set_align($vAligns{$valign});\n";
8826                                           $print_format=1;
8827                                   }
8828                                   $merge=$usedrange->Cells($i,$j)->MergeCells;
8829                                   if ($merge==1) {
8830                                           $format.=$fname."->set_merge();\n";
8831
8832                                           $print_format=1;
8833                                   }
8834                                   $wrap=$usedrange->Cells($i,$j)->WrapText;
8835                                   if ($wrap==1) {
8836                                           $format.=$fname."->set_text_wrap(1);\n";
8837
8838                                           $print_format=1;
8839                                   }
8840
8841
8842                                   # Check patterns
8843                                   my ($pattern,%pats);
8844                                   %pats=(-4142=>0,-4125=>2,-4126=>3,-4124=>4,-4128=>5,-4166=>6,
8845                                                   -4121=>7,-4162=>8);
8846                                   $pattern=$usedrange->Cells($i,$j)->Interior->Pattern;
8847                                   if ($pattern&&$pattern!=$ExcelConst->{xlPatternAutomatic}) {
8848                                           $pattern=$pats{$pattern} if ($pattern<0 && defined $pats{$pattern});
8849                                           $format.=$fname."->set_pattern($pattern);\n";
8850
8851                                           # Colors fg/bg
8852                                           my ($cIndex);
8853                                           $cIndex=$usedrange->Cells($i,$j)->Interior->PatternColorIndex;
8854                                           if ($cIndex>0&&$cIndex!=$ExcelConst->{xlColorIndexAutomatic}) {
8855                                                   $format.=$fname."->set_bg_color(".($cIndex+7).");\n";
8856                                           }
8857                                           $cIndex=$usedrange->Cells($i,$j)->Interior->ColorIndex;
8858                                           if ($cIndex>0&&$cIndex!=$ExcelConst->{xlColorIndexAutomatic}) {
8859                                                   $format.=$fname."->set_fg_color(".($cIndex+7).");\n";
8860                                           }
8861                                           $print_format=1;
8862                                   }
8863
8864
8865                                   # Check for number format
8866                                   my ($num_format);
8867                                   $num_format=$usedrange->Cells($i,$j)->NumberFormat;
8868                                   if ($num_format ne "") {
8869                                           $format.=$fname."->set_num_format('$num_format');\n";
8870                                           $print_format=1;
8871                                   }
8872
8873
8874                                   # Check for contents (text or formula)
8875                                   my ($contents);
8876                                   $contents=$usedrange->Cells($i,$j)->Formula;
8877                                   $contents=$usedrange->Cells($i,$j)->Text if ($contents eq "");
8878
8879
8880                                   # Print cell
8881                                   if ($contents ne "" or $print_format) {
8882                                           print OUTFILE "# Cell($i,$j)\n";
8883                                           print OUTFILE $format if ($print_format);
8884                                           print OUTFILE "\$worksheets{'$name'}->write(".($i-1).",".($j-1).
8885                                                   ",'$contents'";
8886                                           print OUTFILE ",$fname" if ($print_format);
8887                                           print OUTFILE ");\n";
8888                                   }
8889                           }
8890                   }
8891           }
8892
8893
8894           # Famous last words...
8895           print OUTFILE "\$workbook->close();\n";
8896
8897
8898           # Close outfile
8899           close (OUTFILE) or die "Can't close outfile $opts{o}: $!";
8900
8901
8902           ####################################################################
8903           sub usage {
8904                   printf STDERR "usage: $0 [options]\n".
8905                           "\tOptions:\n".
8906                           "\t\t-v       \tverbose mode\n" .
8907                           "\t\t-i <name>\tname of input file\n" .
8908                           "\t\t-o <name>\tname of output file\n";
8909           }
8910
8911
8912           ####################################################################
8913           sub END {
8914                   # Quit excel
8915                   do {
8916                           $Excel->{DisplayAlerts} = 0;
8917                           $Excel->Quit;
8918                   } if (defined $Excel);
8919           }
8920
8921
8922           __END__
8923
8924
8925           =head1 NAME
8926
8927
8928           lecxe - A Excel file to Spreadsheet::WriteExcel code converter
8929
8930
8931           =head1 DESCRIPTION
8932
8933
8934           This program takes an MS Excel workbook file as input and from
8935           that file, produces an output file with Perl code that uses the
8936           Spreadsheet::WriteExcel module to reproduce the original
8937           file.
8938
8939
8940           =head1 STUFF
8941
8942
8943           Additional hands-on editing of the output file might be neccecary
8944           as:
8945
8946
8947           * This program always names the file produced by output script
8948             _change_me_.xls
8949
8950
8951           * Users of international Excel versions will have som work to do
8952             on list separators and numeric punctation characters.
8953
8954
8955           =head1 SEE ALSO
8956
8957
8958           L<Win32::OLE>, L<Win32::OLE::Variant>, L<Spreadsheet::WriteExcel>
8959
8960
8961           =head1 BUGS
8962
8963
8964           * Picks wrong color on cells sometimes.
8965
8966
8967           * Probably a few other...
8968
8969
8970           =head1 DISCLAIMER
8971
8972
8973           I do not guarantee B<ANYTHING> with this program. If you use it you
8974           are doing so B<AT YOUR OWN RISK>! I may or may not support this
8975           depending on my time schedule...
8976
8977
8978           =head1 AUTHOR
8979
8980
8981           t0mas@netlords.net
8982
8983
8984           =head1 COPYRIGHT
8985
8986
8987           Copyright 2001, t0mas@netlords.net
8988
8989
8990           This package is free software; you can redistribute it and/or
8991           modify it under the same terms as Perl itself.
8992
8993       Download this example:
8994       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/lecxe.pl>
8995
8996   Example: convertA1.pl
8997       This program contains helper functions to deal with the Excel A1 cell
8998       reference  notation.
8999
9000       These functions have been superseded by
9001       Spreadsheet::WriteExcel::Utility.
9002
9003           #!/usr/bin/perl -w
9004
9005           ###############################################################################
9006           #
9007           # This program contains helper functions to deal with the Excel A1 cell
9008           # reference  notation.
9009           #
9010           # These functions have been superseded by L<Spreadsheet::WriteExcel::Utility>.
9011           #
9012           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
9013           #
9014
9015           use strict;
9016
9017           print "\n";
9018           print "Cell B7   is equivalent to (";
9019           print join " ", cell_to_rowcol('B7');
9020           print ") in row column notation.\n";
9021
9022           print "Cell \$B7  is equivalent to (";
9023           print join " ", cell_to_rowcol('$B7');
9024           print ") in row column notation.\n";
9025
9026           print "Cell B\$7  is equivalent to (";
9027           print join " ", cell_to_rowcol('B$7');
9028           print ") in row column notation.\n";
9029
9030           print "Cell \$B\$7 is equivalent to (";
9031           print join " ", cell_to_rowcol('$B$7');
9032           print ") in row column notation.\n\n";
9033
9034           print "Row and column (1999, 29)       are equivalent to ";
9035           print rowcol_to_cell(1999, 29),   ".\n";
9036
9037           print "Row and column (1999, 29, 0, 1) are equivalent to ";
9038           print rowcol_to_cell(1999, 29, 0, 1),   ".\n\n";
9039
9040           print "The base cell is:     Z7\n";
9041           print "Increment the row:    ", inc_cell_row('Z7'), "\n";
9042           print "Decrement the row:    ", dec_cell_row('Z7'), "\n";
9043           print "Increment the column: ", inc_cell_col('Z7'), "\n";
9044           print "Decrement the column: ", dec_cell_col('Z7'), "\n\n";
9045
9046
9047           ###############################################################################
9048           #
9049           # rowcol_to_cell($row, $col, $row_absolute, $col_absolute)
9050           #
9051           # Convert a zero based row and column reference to a A1 reference. For example
9052           # (0, 2) to C1. $row_absolute, $col_absolute are optional. They are boolean
9053           # values used to indicate if the row or column value is absolute, i.e. if it is
9054           # prefixed by a $ sign: eg. (0, 2, 0, 1) converts to $C1.
9055           #
9056           # Returns: a cell reference string.
9057           #
9058           sub rowcol_to_cell {
9059
9060               my $row     = $_[0];
9061               my $col     = $_[1];
9062               my $row_abs = $_[2] || 0;
9063               my $col_abs = $_[3] || 0;
9064
9065
9066               if ($row_abs) {
9067                   $row_abs = '$'
9068               }
9069               else {
9070                   $row_abs = ''
9071               }
9072
9073               if ($col_abs) {
9074                   $col_abs = '$'
9075               }
9076               else {
9077                   $col_abs = ''
9078               }
9079
9080
9081               my $int  = int ($col / 26);
9082               my $frac = $col % 26 +1;
9083
9084               my $chr1 ='';
9085               my $chr2 ='';
9086
9087
9088               if ($frac != 0) {
9089                   $chr2 = chr (ord('A') + $frac -1);
9090               }
9091
9092               if ($int > 0) {
9093                   $chr1 = chr (ord('A') + $int  -1);
9094               }
9095
9096               $row++;     # Zero index to 1-index
9097
9098               return $col_abs . $chr1 . $chr2 . $row_abs. $row;
9099           }
9100
9101
9102           ###############################################################################
9103           #
9104           # cell_to_rowcol($cell_ref)
9105           #
9106           # Convert an Excel cell reference in A1 notation to a zero based row and column
9107           # reference; converts C1 to (0, 2, 0, 0).
9108           #
9109           # Returns: row, column, row_is_absolute, column_is_absolute
9110           #
9111           #
9112           sub cell_to_rowcol {
9113
9114               my $cell = shift;
9115
9116               $cell =~ /(\$?)([A-I]?[A-Z])(\$?)(\d+)/;
9117
9118               my $col_abs = $1 eq "" ? 0 : 1;
9119               my $col     = $2;
9120               my $row_abs = $3 eq "" ? 0 : 1;
9121               my $row     = $4;
9122
9123               # Convert base26 column string to number
9124               # All your Base are belong to us.
9125               my @chars  = split //, $col;
9126               my $expn   = 0;
9127               $col       = 0;
9128
9129               while (@chars) {
9130                   my $char = pop(@chars); # LS char first
9131                   $col += (ord($char) -ord('A') +1) * (26**$expn);
9132                   $expn++;
9133               }
9134
9135               # Convert 1-index to zero-index
9136               $row--;
9137               $col--;
9138
9139               return $row, $col, $row_abs, $col_abs;
9140           }
9141
9142
9143           ###############################################################################
9144           #
9145           # inc_cell_row($cell_ref)
9146           #
9147           # Increments the row number of an Excel cell reference in A1 notation.
9148           # For example C3 to C4
9149           #
9150           # Returns: a cell reference string.
9151           #
9152           sub inc_cell_row {
9153
9154               my $cell = shift;
9155               my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
9156
9157               $row++;
9158
9159               return rowcol_to_cell($row, $col, $row_abs, $col_abs);
9160           }
9161
9162
9163           ###############################################################################
9164           #
9165           # dec_cell_row($cell_ref)
9166           #
9167           # Decrements the row number of an Excel cell reference in A1 notation.
9168           # For example C4 to C3
9169           #
9170           # Returns: a cell reference string.
9171           #
9172           sub dec_cell_row {
9173
9174               my $cell = shift;
9175               my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
9176
9177               $row--;
9178
9179               return rowcol_to_cell($row, $col, $row_abs, $col_abs);
9180           }
9181
9182
9183           ###############################################################################
9184           #
9185           # inc_cell_col($cell_ref)
9186           #
9187           # Increments the column number of an Excel cell reference in A1 notation.
9188           # For example C3 to D3
9189           #
9190           # Returns: a cell reference string.
9191           #
9192           sub inc_cell_col {
9193
9194               my $cell = shift;
9195               my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
9196
9197               $col++;
9198
9199               return rowcol_to_cell($row, $col, $row_abs, $col_abs);
9200           }
9201
9202
9203           ###############################################################################
9204           #
9205           # dec_cell_col($cell_ref)
9206           #
9207           # Decrements the column number of an Excel cell reference in A1 notation.
9208           # For example D3 to C3
9209           #
9210           # Returns: a cell reference string.
9211           #
9212           sub dec_cell_col {
9213
9214               my $cell = shift;
9215               my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
9216
9217               $col--;
9218
9219               return rowcol_to_cell($row, $col, $row_abs, $col_abs);
9220           }
9221
9222       Download this example:
9223       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/convertA1.pl>
9224
9225   Example: writeA1.pl
9226       This is an example of how to extend the Spreadsheet::WriteExcel module.
9227
9228       Code is appended to the Spreadsheet::WriteExcel::Worksheet module by
9229       reusing the package name. The new code provides a write() method that
9230       allows you to use Excels A1 style cell references.  This is not
9231       particularly useful but it serves as an example of how the module can
9232       be extended without modifying the code directly.
9233
9234           #!/usr/bin/perl -w
9235
9236           ###############################################################################
9237           #
9238           # This is an example of how to extend the Spreadsheet::WriteExcel module.
9239           #
9240           # Code is appended to the Spreadsheet::WriteExcel::Worksheet module by reusing
9241           # the package name. The new code provides a write() method that allows you to
9242           # use Excels A1 style cell references.  This is not particularly useful but it
9243           # serves as an example of how the module can be extended without modifying the
9244           # code directly.
9245           #
9246           # reverse('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
9247           #
9248
9249           use strict;
9250           use Spreadsheet::WriteExcel;
9251
9252           # Create a new workbook called simple.xls and add a worksheet
9253           my $workbook  = Spreadsheet::WriteExcel->new("writeA1.xls");
9254           my $worksheet = $workbook->add_worksheet();
9255
9256           # Write numbers or text
9257           $worksheet->write  (0, 0, "Hello");
9258           $worksheet->writeA1("A3", "A3"   );
9259           $worksheet->writeA1("A5", 1.2345 );
9260
9261
9262           ###############################################################################
9263           #
9264           # The following will be appended to the Spreadsheet::WriteExcel::Worksheet
9265           # package.
9266           #
9267
9268           package Spreadsheet::WriteExcel::Worksheet;
9269
9270           ###############################################################################
9271           #
9272           # writeA1($cell, $token, $format)
9273           #
9274           # Convert $cell from Excel A1 notation to $row, $col notation and
9275           # call write() on $token.
9276           #
9277           # Returns: return value of called subroutine or -4 for invalid cell
9278           # reference.
9279           #
9280           sub writeA1 {
9281               my $self = shift;
9282               my $cell = shift;
9283               my $col;
9284               my $row;
9285
9286               if ($cell =~ /([A-z]+)(\d+)/) {
9287                  ($row, $col) = _convertA1($2, $1);
9288                  $self->write($row, $col, @_);
9289               } else {
9290                   return -4;
9291               }
9292           }
9293
9294           ###############################################################################
9295           #
9296           # _convertA1($row, $col)
9297           #
9298           # Convert Excel A1 notation to $row, $col notation. Convert base26 column
9299           # string to a number.
9300           #
9301           sub _convertA1 {
9302               my $row    = $_[0];
9303               my $col    = $_[1]; # String in AA notation
9304
9305               my @chars  = split //, $col;
9306               my $expn   = 0;
9307               $col       = 0;
9308
9309               while (@chars) {
9310                   my $char = uc(pop(@chars)); # LS char first
9311                   $col += (ord($char) -ord('A') +1) * (26**$expn);
9312                   $expn++;
9313               }
9314
9315               # Convert 1 index to 0 index
9316               $row--;
9317               $col--;
9318
9319               return($row, $col);
9320           }
9321
9322       Download this example:
9323       <http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.40/examples/writeA1.pl>
9324

AUTHOR

9326       John McNamara jmcnamara@cpan.org
9327
9328       Contributed examples contain the original author's name.
9329
9331       Copyright MM-MMX, John McNamara.
9332
9333       All Rights Reserved. This module is free software. It may be used,
9334       redistributed and/or modified under the same terms as Perl itself.
9335
9336
9337
9338perl v5.34.0                      2021-07-2S2preadsheet::WriteExcel::Examples(3)
Impressum