1Excel::Writer::XLSX::ExUasmeprleCso(n3t)ributed Perl DocEuxmceenlt:a:tWirointer::XLSX::Examples(3)
2
3
4
6 Examples - Excel::Writer::XLSX example programs.
7
9 This is a documentation only module showing the examples that are
10 included in the Excel::Writer::XLSX 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
16 The following is a list of the 88 example programs that are included in
17 the Excel::Writer::XLSX distribution.
18
19 · "Example: a_simple.pl" A simple demo of some of the features.
20
21 · "Example: bug_report.pl" A template for submitting bug reports.
22
23 · "Example: demo.pl" A demo of some of the available features.
24
25 · "Example: formats.pl" All the available formatting on several
26 worksheets.
27
28 · "Example: regions.pl" A simple example of multiple worksheets.
29
30 · "Example: stats.pl" Basic formulas and functions.
31
32 · "Example: autofilter.pl" Examples of worksheet autofilters.
33
34 · "Example: array_formula.pl" Examples of how to write array
35 formulas.
36
37 · "Example: cgi.pl" A simple CGI program.
38
39 · "Example: chart_area.pl" A demo of area style charts.
40
41 · "Example: chart_bar.pl" A demo of bar (vertical histogram) style
42 charts.
43
44 · "Example: chart_column.pl" A demo of column (histogram) style
45 charts.
46
47 · "Example: chart_line.pl" A demo of line style charts.
48
49 · "Example: chart_pie.pl" A demo of pie style charts.
50
51 · "Example: chart_doughnut.pl" A demo of dougnut style charts.
52
53 · "Example: chart_radar.pl" A demo of radar style charts.
54
55 · "Example: chart_scatter.pl" A demo of scatter style charts.
56
57 · "Example: chart_secondary_axis.pl" A demo of a line chart with a
58 secondary axis.
59
60 · "Example: chart_combined.pl" A demo of a combined column and line
61 chart.
62
63 · "Example: chart_pareto.pl" A demo of a combined Pareto chart.
64
65 · "Example: chart_stock.pl" A demo of stock style charts.
66
67 · "Example: chart_data_table.pl" A demo of a chart with a data table
68 on the axis.
69
70 · "Example: chart_data_tools.pl" A demo of charts with data
71 highlighting options.
72
73 · "Example: chart_clustered.pl" A demo of a chart with a clustered
74 axis.
75
76 · "Example: chart_styles.pl" A demo of the available chart styles.
77
78 · "Example: chart_gauge.pl" A demo of a gauge style chart.
79
80 · "Example: colors.pl" A demo of the colour palette and named
81 colours.
82
83 · "Example: comments1.pl" Add comments to worksheet cells.
84
85 · "Example: comments2.pl" Add comments with advanced options.
86
87 · "Example: conditional_format.pl" Add conditional formats to a range
88 of cells.
89
90 · "Example: data_validate.pl" An example of data validation and
91 dropdown lists.
92
93 · "Example: date_time.pl" Write dates and times with
94 write_date_time().
95
96 · "Example: defined_name.pl" Example of how to create defined names.
97
98 · "Example: diag_border.pl" A simple example of diagonal cell
99 borders.
100
101 · "Example: filehandle.pl" Examples of working with filehandles.
102
103 · "Example: headers.pl" Examples of worksheet headers and footers.
104
105 · "Example: hide_row_col.pl" Example of hiding rows and columns.
106
107 · "Example: hide_sheet.pl" Simple example of hiding a worksheet.
108
109 · "Example: hyperlink1.pl" Shows how to create web hyperlinks.
110
111 · "Example: hyperlink2.pl" Examples of internal and external
112 hyperlinks.
113
114 · "Example: indent.pl" An example of cell indentation.
115
116 · "Example: macros.pl" An example of adding macros from an existing
117 file.
118
119 · "Example: merge1.pl" A simple example of cell merging.
120
121 · "Example: merge2.pl" A simple example of cell merging with
122 formatting.
123
124 · "Example: merge3.pl" Add hyperlinks to merged cells.
125
126 · "Example: merge4.pl" An advanced example of merging with
127 formatting.
128
129 · "Example: merge5.pl" An advanced example of merging with
130 formatting.
131
132 · "Example: merge6.pl" An example of merging with Unicode strings.
133
134 · "Example: mod_perl1.pl" A simple mod_perl 1 program.
135
136 · "Example: mod_perl2.pl" A simple mod_perl 2 program.
137
138 · "Example: outline.pl" An example of outlines and grouping.
139
140 · "Example: outline_collapsed.pl" An example of collapsed outlines.
141
142 · "Example: panes.pl" An example of how to create panes.
143
144 · "Example: properties.pl" Add document properties to a workbook.
145
146 · "Example: protection.pl" Example of cell locking and formula
147 hiding.
148
149 · "Example: rich_strings.pl" Example of strings with multiple
150 formats.
151
152 · "Example: right_to_left.pl" Change default sheet direction to right
153 to left.
154
155 · "Example: sales.pl" An example of a simple sales spreadsheet.
156
157 · "Example: shape1.pl" Insert shapes in worksheet.
158
159 · "Example: shape2.pl" Insert shapes in worksheet. With properties.
160
161 · "Example: shape3.pl" Insert shapes in worksheet. Scaled.
162
163 · "Example: shape4.pl" Insert shapes in worksheet. With modification.
164
165 · "Example: shape5.pl" Insert shapes in worksheet. With connections.
166
167 · "Example: shape6.pl" Insert shapes in worksheet. With connections.
168
169 · "Example: shape7.pl" Insert shapes in worksheet. One to many
170 connections.
171
172 · "Example: shape8.pl" Insert shapes in worksheet. One to many
173 connections.
174
175 · "Example: shape_all.pl" Demo of all the available shape and
176 connector types.
177
178 · "Example: sparklines1.pl" Simple sparklines demo.
179
180 · "Example: sparklines2.pl" Sparklines demo showing formatting
181 options.
182
183 · "Example: stats_ext.pl" Same as stats.pl with external references.
184
185 · "Example: stocks.pl" Demonstrates conditional formatting.
186
187 · "Example: tab_colors.pl" Example of how to set worksheet tab
188 colours.
189
190 · "Example: tables.pl" Add Excel tables to a worksheet.
191
192 · "Example: write_handler1.pl" Example of extending the write()
193 method. Step 1.
194
195 · "Example: write_handler2.pl" Example of extending the write()
196 method. Step 2.
197
198 · "Example: write_handler3.pl" Example of extending the write()
199 method. Step 3.
200
201 · "Example: write_handler4.pl" Example of extending the write()
202 method. Step 4.
203
204 · "Example: write_to_scalar.pl" Example of writing an Excel file to a
205 Perl scalar.
206
207 · "Example: unicode_2022_jp.pl" Japanese: ISO-2022-JP.
208
209 · "Example: unicode_8859_11.pl" Thai: ISO-8859_11.
210
211 · "Example: unicode_8859_7.pl" Greek: ISO-8859_7.
212
213 · "Example: unicode_big5.pl" Chinese: BIG5.
214
215 · "Example: unicode_cp1251.pl" Russian: CP1251.
216
217 · "Example: unicode_cp1256.pl" Arabic: CP1256.
218
219 · "Example: unicode_cyrillic.pl" Russian: Cyrillic.
220
221 · "Example: unicode_koi8r.pl" Russian: KOI8-R.
222
223 · "Example: unicode_polish_utf8.pl" Polish : UTF8.
224
225 · "Example: unicode_shift_jis.pl" Japanese: Shift JIS.
226
227 Example: a_simple.pl
228 A simple example of how to use the Excel::Writer::XLSX module to write
229 text and numbers to an Excel xlsx file.
230
231 Source code for this example:
232
233 #!/usr/bin/perl -w
234
235 #######################################################################
236 #
237 # A simple example of how to use the Excel::Writer::XLSX module to
238 # write text and numbers to an Excel xlsx file.
239 #
240 # reverse ('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
241 #
242
243 use strict;
244 use Excel::Writer::XLSX;
245
246 # Create a new workbook called simple.xls and add a worksheet
247 my $workbook = Excel::Writer::XLSX->new( 'a_simple.xlsx' );
248 my $worksheet = $workbook->add_worksheet();
249
250 # The general syntax is write($row, $column, $token). Note that row and
251 # column are zero indexed
252 #
253
254 # Write some text
255 $worksheet->write( 0, 0, "Hi Excel!" );
256
257
258 # Write some numbers
259 $worksheet->write( 2, 0, 3 ); # Writes 3
260 $worksheet->write( 3, 0, 3.00000 ); # Writes 3
261 $worksheet->write( 4, 0, 3.00001 ); # Writes 3.00001
262 $worksheet->write( 5, 0, 3.14159 ); # TeX revision no.?
263
264
265 # Write some formulas
266 $worksheet->write( 7, 0, '=A3 + A6' );
267 $worksheet->write( 8, 0, '=IF(A5>3,"Yes", "No")' );
268
269
270 # Write a hyperlink
271 $worksheet->write( 10, 0, 'http://www.perl.com/' );
272
273 $workbook->close();
274
275 __END__
276
277 Download this example:
278 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/a_simple.pl>
279
280 Example: bug_report.pl
281 A template for submitting a bug report.
282
283 Run this program and read the output from the command line.
284
285 #!/usr/bin/perl -w
286
287 ###############################################################################
288 #
289 # A template for submitting a bug report.
290 #
291 # Run this program and read the output from the command line.
292 #
293 # reverse ('(c)'), March 2004, John McNamara, jmcnamara@cpan.org
294 #
295
296
297 use strict;
298
299 print << 'HINTS_1';
300
301 REPORTING A BUG OR ASKING A QUESTION
302
303 Feel free to report bugs or ask questions. However, to save time
304 consider the following steps first:
305
306 Read the documentation:
307
308 The Excel::Writer::XLSX documentation has been refined in
309 response to user questions. Therefore, if you have a question it is
310 possible that someone else has asked it before you and that it is
311 already addressed in the documentation. Since there is a lot of
312 documentation to get through you should at least read the table of
313 contents and search for keywords that you are interested in.
314
315 Look at the example programs:
316
317 There are over 80 example programs shipped with the standard
318 Excel::Writer::XLSX distribution. Many of these were created
319 in response to user questions. Try to identify an example program
320 that corresponds to your query and adapt it to your needs.
321
322 HINTS_1
323 print "Press enter ..."; <STDIN>;
324
325 print << 'HINTS_2';
326
327 If you submit a bug report here are some pointers.
328
329 1. Put "Excel::Writer::XLSX:" at the beginning of the subject line.
330 This helps to filter genuine messages from spam.
331
332 2. Describe the problems as clearly and as concisely as possible.
333
334 3. Send a sample program. It is often easier to describe a problem in
335 code than in written prose.
336
337 4. The sample program should be as small as possible to demonstrate the
338 problem. Don't copy and past large sections of your program. The
339 program should also be self contained and working.
340
341 A sample bug report is generated below. If you use this format then it
342 will help to analyse your question and respond to it more quickly.
343
344 Please don't send patches without contacting the author first.
345
346
347 HINTS_2
348 print "Press enter ..."; <STDIN>;
349
350
351 print << 'EMAIL';
352
353 =======================================================================
354
355 To: John McNamara <jmcnamara@cpan.org>
356 Subject: Excel::Writer::XLSX: Problem with something.
357
358 Hi John,
359
360 I am using Excel::Writer::XLSX and I have encountered a problem. I
361 want it to do SOMETHING but the module appears to do SOMETHING_ELSE.
362
363 Here is some code that demonstrates the problem.
364
365 #!/usr/bin/perl -w
366
367 use strict;
368 use Excel::Writer::XLSX;
369
370 my $workbook = Excel::Writer::XLSX->new("reload.xls");
371 my $worksheet = $workbook->add_worksheet();
372
373 $worksheet->write(0, 0, "Hi Excel!");
374
375 $workbook->close();
376
377 __END__
378
379 My automatically generated system details are as follows:
380 EMAIL
381
382
383 print "\n Perl version : $]";
384 print "\n OS name : $^O";
385 print "\n Module versions: (not all are required)\n";
386
387
388 my @modules = qw(
389 Excel::Writer::XLSX
390 Spreadsheet::WriteExcel
391 Archive::Zip
392 XML::Writer
393 IO::File
394 File::Temp
395 );
396
397
398 for my $module (@modules) {
399 my $version;
400 eval "require $module";
401
402 if (not $@) {
403 $version = $module->VERSION;
404 $version = '(unknown)' if not defined $version;
405 }
406 else {
407 $version = '(not installed)';
408 }
409
410 printf "%21s%-24s\t%s\n", "", $module, $version;
411 }
412
413
414 print << "BYE";
415 Yours etc.,
416
417 A. Person
418 --
419
420 BYE
421
422 __END__
423
424 Download this example:
425 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/bug_report.pl>
426
427 Example: demo.pl
428 A simple demo of some of the features of Excel::Writer::XLSX.
429
430 This program is used to create the project screenshot for Freshmeat:
431 <http://freshmeat.net/projects/writeexcel/>
432
433 Source code for this example:
434
435 #!/usr/bin/perl -w
436
437 #######################################################################
438 #
439 # A simple demo of some of the features of Excel::Writer::XLSX.
440 #
441 # This program is used to create the project screenshot for Freshmeat:
442 # L<http://freshmeat.net/projects/writeexcel/>
443 #
444 # reverse ('(c)'), October 2001, John McNamara, jmcnamara@cpan.org
445 #
446
447 use strict;
448 use Excel::Writer::XLSX;
449
450 my $workbook = Excel::Writer::XLSX->new( 'demo.xlsx' );
451 my $worksheet = $workbook->add_worksheet( 'Demo' );
452 my $worksheet2 = $workbook->add_worksheet( 'Another sheet' );
453 my $worksheet3 = $workbook->add_worksheet( 'And another' );
454
455 my $bold = $workbook->add_format( bold => 1 );
456
457
458 #######################################################################
459 #
460 # Write a general heading
461 #
462 $worksheet->set_column( 'A:A', 36, $bold );
463 $worksheet->set_column( 'B:B', 20 );
464 $worksheet->set_row( 0, 40 );
465
466 my $heading = $workbook->add_format(
467 bold => 1,
468 color => 'blue',
469 size => 16,
470 merge => 1,
471 align => 'vcenter',
472 );
473
474 my @headings = ( 'Features of Excel::Writer::XLSX', '' );
475 $worksheet->write_row( 'A1', \@headings, $heading );
476
477
478 #######################################################################
479 #
480 # Some text examples
481 #
482 my $text_format = $workbook->add_format(
483 bold => 1,
484 italic => 1,
485 color => 'red',
486 size => 18,
487 font => 'Lucida Calligraphy'
488 );
489
490
491 $worksheet->write( 'A2', "Text" );
492 $worksheet->write( 'B2', "Hello Excel" );
493 $worksheet->write( 'A3', "Formatted text" );
494 $worksheet->write( 'B3', "Hello Excel", $text_format );
495 $worksheet->write( 'A4', "Unicode text" );
496 $worksheet->write( 'B4', "\x{0410} \x{0411} \x{0412} \x{0413} \x{0414}" );
497
498 #######################################################################
499 #
500 # Some numeric examples
501 #
502 my $num1_format = $workbook->add_format( num_format => '$#,##0.00' );
503 my $num2_format = $workbook->add_format( num_format => ' d mmmm yyy' );
504
505
506 $worksheet->write( 'A5', "Numbers" );
507 $worksheet->write( 'B5', 1234.56 );
508 $worksheet->write( 'A6', "Formatted numbers" );
509 $worksheet->write( 'B6', 1234.56, $num1_format );
510 $worksheet->write( 'A7', "Formatted numbers" );
511 $worksheet->write( 'B7', 37257, $num2_format );
512
513
514 #######################################################################
515 #
516 # Formulae
517 #
518 $worksheet->set_selection( 'B8' );
519 $worksheet->write( 'A8', 'Formulas and functions, "=SIN(PI()/4)"' );
520 $worksheet->write( 'B8', '=SIN(PI()/4)' );
521
522
523 #######################################################################
524 #
525 # Hyperlinks
526 #
527 $worksheet->write( 'A9', "Hyperlinks" );
528 $worksheet->write( 'B9', 'http://www.perl.com/' );
529
530
531 #######################################################################
532 #
533 # Images
534 #
535 $worksheet->write( 'A10', "Images" );
536 $worksheet->insert_image( 'B10', 'republic.png',
537 { x_offset => 16, y_offset => 8 } );
538
539
540 #######################################################################
541 #
542 # Misc
543 #
544 $worksheet->write( 'A18', "Page/printer setup" );
545 $worksheet->write( 'A19', "Multiple worksheets" );
546
547 $workbook->close();
548
549 __END__
550
551 Download this example:
552 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/demo.pl>
553
554 Example: formats.pl
555 Examples of formatting using the Excel::Writer::XLSX module.
556
557 This program demonstrates almost all possible formatting options. It is
558 worth running this program and viewing the output Excel file if you are
559 interested in the various formatting possibilities.
560
561 Source code for this example:
562
563 #!/usr/bin/perl -w
564
565 ###############################################################################
566 #
567 # Examples of formatting using the Excel::Writer::XLSX module.
568 #
569 # This program demonstrates almost all possible formatting options. It is worth
570 # running this program and viewing the output Excel file if you are interested
571 # in the various formatting possibilities.
572 #
573 # reverse ('(c)'), September 2002, John McNamara, jmcnamara@cpan.org
574 #
575
576 use strict;
577 use Excel::Writer::XLSX;
578
579 my $workbook = Excel::Writer::XLSX->new( 'formats.xlsx' );
580
581 # Some common formats
582 my $center = $workbook->add_format( align => 'center' );
583 my $heading = $workbook->add_format( align => 'center', bold => 1 );
584
585 # The named colors
586 my %colors = (
587 0x08, 'black',
588 0x0C, 'blue',
589 0x10, 'brown',
590 0x0F, 'cyan',
591 0x17, 'gray',
592 0x11, 'green',
593 0x0B, 'lime',
594 0x0E, 'magenta',
595 0x12, 'navy',
596 0x35, 'orange',
597 0x21, 'pink',
598 0x14, 'purple',
599 0x0A, 'red',
600 0x16, 'silver',
601 0x09, 'white',
602 0x0D, 'yellow',
603
604 );
605
606 # Call these subroutines to demonstrate different formatting options
607 intro();
608 fonts();
609 named_colors();
610 standard_colors();
611 numeric_formats();
612 borders();
613 patterns();
614 alignment();
615 misc();
616
617 # Note: this is required
618 $workbook->close();
619
620
621 ######################################################################
622 #
623 # Intro.
624 #
625 sub intro {
626
627 my $worksheet = $workbook->add_worksheet( 'Introduction' );
628
629 $worksheet->set_column( 0, 0, 60 );
630
631 my $format = $workbook->add_format();
632 $format->set_bold();
633 $format->set_size( 14 );
634 $format->set_color( 'blue' );
635 $format->set_align( 'center' );
636
637 my $format2 = $workbook->add_format();
638 $format2->set_bold();
639 $format2->set_color( 'blue' );
640
641 my $format3 = $workbook->add_format(
642 color => 'blue',
643 underline => 1,
644 );
645
646 $worksheet->write( 2, 0, 'This workbook demonstrates some of', $format );
647 $worksheet->write( 3, 0, 'the formatting options provided by', $format );
648 $worksheet->write( 4, 0, 'the Excel::Writer::XLSX module.', $format );
649 $worksheet->write( 'A7', 'Sections:', $format2 );
650
651 $worksheet->write( 'A8', "internal:Fonts!A1", 'Fonts', $format3 );
652
653 $worksheet->write( 'A9', "internal:'Named colors'!A1",
654 'Named colors', $format3 );
655
656 $worksheet->write(
657 'A10',
658 "internal:'Standard colors'!A1",
659 'Standard colors', $format3
660 );
661
662 $worksheet->write(
663 'A11',
664 "internal:'Numeric formats'!A1",
665 'Numeric formats', $format3
666 );
667
668 $worksheet->write( 'A12', "internal:Borders!A1", 'Borders', $format3 );
669 $worksheet->write( 'A13', "internal:Patterns!A1", 'Patterns', $format3 );
670 $worksheet->write( 'A14', "internal:Alignment!A1", 'Alignment', $format3 );
671 $worksheet->write( 'A15', "internal:Miscellaneous!A1", 'Miscellaneous',
672 $format3 );
673
674 }
675
676
677 ######################################################################
678 #
679 # Demonstrate the named colors.
680 #
681 sub named_colors {
682
683 my $worksheet = $workbook->add_worksheet( 'Named colors' );
684
685 $worksheet->set_column( 0, 3, 15 );
686
687 $worksheet->write( 0, 0, "Index", $heading );
688 $worksheet->write( 0, 1, "Index", $heading );
689 $worksheet->write( 0, 2, "Name", $heading );
690 $worksheet->write( 0, 3, "Color", $heading );
691
692 my $i = 1;
693
694 while ( my ( $index, $color ) = each %colors ) {
695 my $format = $workbook->add_format(
696 bg_color => $color,
697 pattern => 1,
698 border => 1
699 );
700
701 $worksheet->write( $i + 1, 0, $index, $center );
702 $worksheet->write( $i + 1, 1, sprintf( "0x%02X", $index ), $center );
703 $worksheet->write( $i + 1, 2, $color, $center );
704 $worksheet->write( $i + 1, 3, '', $format );
705 $i++;
706 }
707 }
708
709
710 ######################################################################
711 #
712 # Demonstrate the standard Excel colors in the range 8..63.
713 #
714 sub standard_colors {
715
716 my $worksheet = $workbook->add_worksheet( 'Standard colors' );
717
718 $worksheet->set_column( 0, 3, 15 );
719
720 $worksheet->write( 0, 0, "Index", $heading );
721 $worksheet->write( 0, 1, "Index", $heading );
722 $worksheet->write( 0, 2, "Color", $heading );
723 $worksheet->write( 0, 3, "Name", $heading );
724
725 for my $i ( 8 .. 63 ) {
726 my $format = $workbook->add_format(
727 bg_color => $i,
728 pattern => 1,
729 border => 1
730 );
731
732 $worksheet->write( ( $i - 7 ), 0, $i, $center );
733 $worksheet->write( ( $i - 7 ), 1, sprintf( "0x%02X", $i ), $center );
734 $worksheet->write( ( $i - 7 ), 2, '', $format );
735
736 # Add the color names
737 if ( exists $colors{$i} ) {
738 $worksheet->write( ( $i - 7 ), 3, $colors{$i}, $center );
739
740 }
741 }
742 }
743
744
745 ######################################################################
746 #
747 # Demonstrate the standard numeric formats.
748 #
749 sub numeric_formats {
750
751 my $worksheet = $workbook->add_worksheet( 'Numeric formats' );
752
753 $worksheet->set_column( 0, 4, 15 );
754 $worksheet->set_column( 5, 5, 45 );
755
756 $worksheet->write( 0, 0, "Index", $heading );
757 $worksheet->write( 0, 1, "Index", $heading );
758 $worksheet->write( 0, 2, "Unformatted", $heading );
759 $worksheet->write( 0, 3, "Formatted", $heading );
760 $worksheet->write( 0, 4, "Negative", $heading );
761 $worksheet->write( 0, 5, "Format", $heading );
762
763 #<<<
764 my @formats;
765 push @formats, [ 0x00, 1234.567, 0, 'General' ];
766 push @formats, [ 0x01, 1234.567, 0, '0' ];
767 push @formats, [ 0x02, 1234.567, 0, '0.00' ];
768 push @formats, [ 0x03, 1234.567, 0, '#,##0' ];
769 push @formats, [ 0x04, 1234.567, 0, '#,##0.00' ];
770 push @formats, [ 0x05, 1234.567, -1234.567, '($#,##0_);($#,##0)' ];
771 push @formats, [ 0x06, 1234.567, -1234.567, '($#,##0_);[Red]($#,##0)' ];
772 push @formats, [ 0x07, 1234.567, -1234.567, '($#,##0.00_);($#,##0.00)' ];
773 push @formats, [ 0x08, 1234.567, -1234.567, '($#,##0.00_);[Red]($#,##0.00)' ];
774 push @formats, [ 0x09, 0.567, 0, '0%' ];
775 push @formats, [ 0x0a, 0.567, 0, '0.00%' ];
776 push @formats, [ 0x0b, 1234.567, 0, '0.00E+00' ];
777 push @formats, [ 0x0c, 0.75, 0, '# ?/?' ];
778 push @formats, [ 0x0d, 0.3125, 0, '# ??/??' ];
779 push @formats, [ 0x0e, 36892.521, 0, 'm/d/yy' ];
780 push @formats, [ 0x0f, 36892.521, 0, 'd-mmm-yy' ];
781 push @formats, [ 0x10, 36892.521, 0, 'd-mmm' ];
782 push @formats, [ 0x11, 36892.521, 0, 'mmm-yy' ];
783 push @formats, [ 0x12, 36892.521, 0, 'h:mm AM/PM' ];
784 push @formats, [ 0x13, 36892.521, 0, 'h:mm:ss AM/PM' ];
785 push @formats, [ 0x14, 36892.521, 0, 'h:mm' ];
786 push @formats, [ 0x15, 36892.521, 0, 'h:mm:ss' ];
787 push @formats, [ 0x16, 36892.521, 0, 'm/d/yy h:mm' ];
788 push @formats, [ 0x25, 1234.567, -1234.567, '(#,##0_);(#,##0)' ];
789 push @formats, [ 0x26, 1234.567, -1234.567, '(#,##0_);[Red](#,##0)' ];
790 push @formats, [ 0x27, 1234.567, -1234.567, '(#,##0.00_);(#,##0.00)' ];
791 push @formats, [ 0x28, 1234.567, -1234.567, '(#,##0.00_);[Red](#,##0.00)' ];
792 push @formats, [ 0x29, 1234.567, -1234.567, '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)' ];
793 push @formats, [ 0x2a, 1234.567, -1234.567, '_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)' ];
794 push @formats, [ 0x2b, 1234.567, -1234.567, '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' ];
795 push @formats, [ 0x2c, 1234.567, -1234.567, '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)' ];
796 push @formats, [ 0x2d, 36892.521, 0, 'mm:ss' ];
797 push @formats, [ 0x2e, 3.0153, 0, '[h]:mm:ss' ];
798 push @formats, [ 0x2f, 36892.521, 0, 'mm:ss.0' ];
799 push @formats, [ 0x30, 1234.567, 0, '##0.0E+0' ];
800 push @formats, [ 0x31, 1234.567, 0, '@' ];
801 #>>>
802
803 my $i;
804 foreach my $format ( @formats ) {
805 my $style = $workbook->add_format();
806 $style->set_num_format( $format->[0] );
807
808 $i++;
809 $worksheet->write( $i, 0, $format->[0], $center );
810 $worksheet->write( $i, 1, sprintf( "0x%02X", $format->[0] ), $center );
811 $worksheet->write( $i, 2, $format->[1], $center );
812 $worksheet->write( $i, 3, $format->[1], $style );
813
814 if ( $format->[2] ) {
815 $worksheet->write( $i, 4, $format->[2], $style );
816 }
817
818 $worksheet->write_string( $i, 5, $format->[3] );
819 }
820 }
821
822
823 ######################################################################
824 #
825 # Demonstrate the font options.
826 #
827 sub fonts {
828
829 my $worksheet = $workbook->add_worksheet( 'Fonts' );
830
831 $worksheet->set_column( 0, 0, 30 );
832 $worksheet->set_column( 1, 1, 10 );
833
834 $worksheet->write( 0, 0, "Font name", $heading );
835 $worksheet->write( 0, 1, "Font size", $heading );
836
837 my @fonts;
838 push @fonts, [ 10, 'Arial' ];
839 push @fonts, [ 12, 'Arial' ];
840 push @fonts, [ 14, 'Arial' ];
841 push @fonts, [ 12, 'Arial Black' ];
842 push @fonts, [ 12, 'Arial Narrow' ];
843 push @fonts, [ 12, 'Century Schoolbook' ];
844 push @fonts, [ 12, 'Courier' ];
845 push @fonts, [ 12, 'Courier New' ];
846 push @fonts, [ 12, 'Garamond' ];
847 push @fonts, [ 12, 'Impact' ];
848 push @fonts, [ 12, 'Lucida Handwriting' ];
849 push @fonts, [ 12, 'Times New Roman' ];
850 push @fonts, [ 12, 'Symbol' ];
851 push @fonts, [ 12, 'Wingdings' ];
852 push @fonts, [ 12, 'A font that doesn\'t exist' ];
853
854 my $i;
855 foreach my $font ( @fonts ) {
856 my $format = $workbook->add_format();
857
858 $format->set_size( $font->[0] );
859 $format->set_font( $font->[1] );
860
861 $i++;
862 $worksheet->write( $i, 0, $font->[1], $format );
863 $worksheet->write( $i, 1, $font->[0], $format );
864 }
865
866 }
867
868
869 ######################################################################
870 #
871 # Demonstrate the standard Excel border styles.
872 #
873 sub borders {
874
875 my $worksheet = $workbook->add_worksheet( 'Borders' );
876
877 $worksheet->set_column( 0, 4, 10 );
878 $worksheet->set_column( 5, 5, 40 );
879
880 $worksheet->write( 0, 0, "Index", $heading );
881 $worksheet->write( 0, 1, "Index", $heading );
882 $worksheet->write( 0, 3, "Style", $heading );
883 $worksheet->write( 0, 5, "The style is highlighted in red for ", $heading );
884 $worksheet->write( 1, 5, "emphasis, the default color is black.",
885 $heading );
886
887 for my $i ( 0 .. 13 ) {
888 my $format = $workbook->add_format();
889 $format->set_border( $i );
890 $format->set_border_color( 'red' );
891 $format->set_align( 'center' );
892
893 $worksheet->write( ( 2 * ( $i + 1 ) ), 0, $i, $center );
894 $worksheet->write( ( 2 * ( $i + 1 ) ),
895 1, sprintf( "0x%02X", $i ), $center );
896
897 $worksheet->write( ( 2 * ( $i + 1 ) ), 3, "Border", $format );
898 }
899
900 $worksheet->write( 30, 0, "Diag type", $heading );
901 $worksheet->write( 30, 1, "Index", $heading );
902 $worksheet->write( 30, 3, "Style", $heading );
903 $worksheet->write( 30, 5, "Diagonal Boder styles", $heading );
904
905 for my $i ( 1 .. 3 ) {
906 my $format = $workbook->add_format();
907 $format->set_diag_type( $i );
908 $format->set_diag_border( 1 );
909 $format->set_diag_color( 'red' );
910 $format->set_align( 'center' );
911
912 $worksheet->write( ( 2 * ( $i + 15 ) ), 0, $i, $center );
913 $worksheet->write( ( 2 * ( $i + 15 ) ),
914 1, sprintf( "0x%02X", $i ), $center );
915
916 $worksheet->write( ( 2 * ( $i + 15 ) ), 3, "Border", $format );
917 }
918 }
919
920
921 ######################################################################
922 #
923 # Demonstrate the standard Excel cell patterns.
924 #
925 sub patterns {
926
927 my $worksheet = $workbook->add_worksheet( 'Patterns' );
928
929 $worksheet->set_column( 0, 4, 10 );
930 $worksheet->set_column( 5, 5, 50 );
931
932 $worksheet->write( 0, 0, "Index", $heading );
933 $worksheet->write( 0, 1, "Index", $heading );
934 $worksheet->write( 0, 3, "Pattern", $heading );
935
936 $worksheet->write( 0, 5, "The background colour has been set to silver.",
937 $heading );
938 $worksheet->write( 1, 5, "The foreground colour has been set to green.",
939 $heading );
940
941 for my $i ( 0 .. 18 ) {
942 my $format = $workbook->add_format();
943
944 $format->set_pattern( $i );
945 $format->set_bg_color( 'silver' );
946 $format->set_fg_color( 'green' );
947 $format->set_align( 'center' );
948
949 $worksheet->write( ( 2 * ( $i + 1 ) ), 0, $i, $center );
950 $worksheet->write( ( 2 * ( $i + 1 ) ),
951 1, sprintf( "0x%02X", $i ), $center );
952
953 $worksheet->write( ( 2 * ( $i + 1 ) ), 3, "Pattern", $format );
954
955 if ( $i == 1 ) {
956 $worksheet->write( ( 2 * ( $i + 1 ) ),
957 5, "This is solid colour, the most useful pattern.", $heading );
958 }
959 }
960 }
961
962
963 ######################################################################
964 #
965 # Demonstrate the standard Excel cell alignments.
966 #
967 sub alignment {
968
969 my $worksheet = $workbook->add_worksheet( 'Alignment' );
970
971 $worksheet->set_column( 0, 7, 12 );
972 $worksheet->set_row( 0, 40 );
973 $worksheet->set_selection( 7, 0 );
974
975 my $format01 = $workbook->add_format();
976 my $format02 = $workbook->add_format();
977 my $format03 = $workbook->add_format();
978 my $format04 = $workbook->add_format();
979 my $format05 = $workbook->add_format();
980 my $format06 = $workbook->add_format();
981 my $format07 = $workbook->add_format();
982 my $format08 = $workbook->add_format();
983 my $format09 = $workbook->add_format();
984 my $format10 = $workbook->add_format();
985 my $format11 = $workbook->add_format();
986 my $format12 = $workbook->add_format();
987 my $format13 = $workbook->add_format();
988 my $format14 = $workbook->add_format();
989 my $format15 = $workbook->add_format();
990 my $format16 = $workbook->add_format();
991 my $format17 = $workbook->add_format();
992
993 $format02->set_align( 'top' );
994 $format03->set_align( 'bottom' );
995 $format04->set_align( 'vcenter' );
996 $format05->set_align( 'vjustify' );
997 $format06->set_text_wrap();
998
999 $format07->set_align( 'left' );
1000 $format08->set_align( 'right' );
1001 $format09->set_align( 'center' );
1002 $format10->set_align( 'fill' );
1003 $format11->set_align( 'justify' );
1004 $format12->set_merge();
1005
1006 $format13->set_rotation( 45 );
1007 $format14->set_rotation( -45 );
1008 $format15->set_rotation( 270 );
1009
1010 $format16->set_shrink();
1011 $format17->set_indent( 1 );
1012
1013 $worksheet->write( 0, 0, 'Vertical', $heading );
1014 $worksheet->write( 0, 1, 'top', $format02 );
1015 $worksheet->write( 0, 2, 'bottom', $format03 );
1016 $worksheet->write( 0, 3, 'vcenter', $format04 );
1017 $worksheet->write( 0, 4, 'vjustify', $format05 );
1018 $worksheet->write( 0, 5, "text\nwrap", $format06 );
1019
1020 $worksheet->write( 2, 0, 'Horizontal', $heading );
1021 $worksheet->write( 2, 1, 'left', $format07 );
1022 $worksheet->write( 2, 2, 'right', $format08 );
1023 $worksheet->write( 2, 3, 'center', $format09 );
1024 $worksheet->write( 2, 4, 'fill', $format10 );
1025 $worksheet->write( 2, 5, 'justify', $format11 );
1026
1027 $worksheet->write( 3, 1, 'merge', $format12 );
1028 $worksheet->write( 3, 2, '', $format12 );
1029
1030 $worksheet->write( 3, 3, 'Shrink ' x 3, $format16 );
1031 $worksheet->write( 3, 4, 'Indent', $format17 );
1032
1033
1034 $worksheet->write( 5, 0, 'Rotation', $heading );
1035 $worksheet->write( 5, 1, 'Rotate 45', $format13 );
1036 $worksheet->write( 6, 1, 'Rotate -45', $format14 );
1037 $worksheet->write( 7, 1, 'Rotate 270', $format15 );
1038 }
1039
1040
1041 ######################################################################
1042 #
1043 # Demonstrate other miscellaneous features.
1044 #
1045 sub misc {
1046
1047 my $worksheet = $workbook->add_worksheet( 'Miscellaneous' );
1048
1049 $worksheet->set_column( 2, 2, 25 );
1050
1051 my $format01 = $workbook->add_format();
1052 my $format02 = $workbook->add_format();
1053 my $format03 = $workbook->add_format();
1054 my $format04 = $workbook->add_format();
1055 my $format05 = $workbook->add_format();
1056 my $format06 = $workbook->add_format();
1057 my $format07 = $workbook->add_format();
1058
1059 $format01->set_underline( 0x01 );
1060 $format02->set_underline( 0x02 );
1061 $format03->set_underline( 0x21 );
1062 $format04->set_underline( 0x22 );
1063 $format05->set_font_strikeout();
1064 $format06->set_font_outline();
1065 $format07->set_font_shadow();
1066
1067 $worksheet->write( 1, 2, 'Underline 0x01', $format01 );
1068 $worksheet->write( 3, 2, 'Underline 0x02', $format02 );
1069 $worksheet->write( 5, 2, 'Underline 0x21', $format03 );
1070 $worksheet->write( 7, 2, 'Underline 0x22', $format04 );
1071 $worksheet->write( 9, 2, 'Strikeout', $format05 );
1072 $worksheet->write( 11, 2, 'Outline (Macintosh only)', $format06 );
1073 $worksheet->write( 13, 2, 'Shadow (Macintosh only)', $format07 );
1074 }
1075
1076
1077 $workbook->close();
1078
1079 __END__
1080
1081 Download this example:
1082 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/formats.pl>
1083
1084 Example: regions.pl
1085 An example of how to use the Excel::Writer::XLSX module to write a
1086 basic Excel workbook with multiple worksheets.
1087
1088 Source code for this example:
1089
1090 #!/usr/bin/perl -w
1091
1092 ###############################################################################
1093 #
1094 # An example of how to use the Excel::Writer::XLSX module to write a basic
1095 # Excel workbook with multiple worksheets.
1096 #
1097 # reverse ('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
1098 #
1099
1100 use strict;
1101 use Excel::Writer::XLSX;
1102
1103 # Create a new Excel workbook
1104 my $workbook = Excel::Writer::XLSX->new( 'regions.xlsx' );
1105
1106 # Add some worksheets
1107 my $north = $workbook->add_worksheet( "North" );
1108 my $south = $workbook->add_worksheet( "South" );
1109 my $east = $workbook->add_worksheet( "East" );
1110 my $west = $workbook->add_worksheet( "West" );
1111
1112 # Add a Format
1113 my $format = $workbook->add_format();
1114 $format->set_bold();
1115 $format->set_color( 'blue' );
1116
1117 # Add a caption to each worksheet
1118 foreach my $worksheet ( $workbook->sheets() ) {
1119 $worksheet->write( 0, 0, "Sales", $format );
1120 }
1121
1122 # Write some data
1123 $north->write( 0, 1, 200000 );
1124 $south->write( 0, 1, 100000 );
1125 $east->write( 0, 1, 150000 );
1126 $west->write( 0, 1, 100000 );
1127
1128 # Set the active worksheet
1129 $south->activate();
1130
1131 # Set the width of the first column
1132 $south->set_column( 0, 0, 20 );
1133
1134 # Set the active cell
1135 $south->set_selection( 0, 1 );
1136
1137 $workbook->close();
1138
1139 __END__
1140
1141 Download this example:
1142 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/regions.pl>
1143
1144 Example: stats.pl
1145 A simple example of how to use functions with the Excel::Writer::XLSX
1146 module.
1147
1148 Source code for this example:
1149
1150 #!/usr/bin/perl -w
1151
1152 ###############################################################################
1153 #
1154 # A simple example of how to use functions with the Excel::Writer::XLSX
1155 # module.
1156 #
1157 # reverse ('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
1158 #
1159
1160 use strict;
1161 use Excel::Writer::XLSX;
1162
1163 # Create a new workbook and add a worksheet
1164 my $workbook = Excel::Writer::XLSX->new( 'stats.xlsx' );
1165 my $worksheet = $workbook->add_worksheet( 'Test data' );
1166
1167 # Set the column width for columns 1
1168 $worksheet->set_column( 0, 0, 20 );
1169
1170
1171 # Create a format for the headings
1172 my $format = $workbook->add_format();
1173 $format->set_bold();
1174
1175
1176 # Write the sample data
1177 $worksheet->write( 0, 0, 'Sample', $format );
1178 $worksheet->write( 0, 1, 1 );
1179 $worksheet->write( 0, 2, 2 );
1180 $worksheet->write( 0, 3, 3 );
1181 $worksheet->write( 0, 4, 4 );
1182 $worksheet->write( 0, 5, 5 );
1183 $worksheet->write( 0, 6, 6 );
1184 $worksheet->write( 0, 7, 7 );
1185 $worksheet->write( 0, 8, 8 );
1186
1187 $worksheet->write( 1, 0, 'Length', $format );
1188 $worksheet->write( 1, 1, 25.4 );
1189 $worksheet->write( 1, 2, 25.4 );
1190 $worksheet->write( 1, 3, 24.8 );
1191 $worksheet->write( 1, 4, 25.0 );
1192 $worksheet->write( 1, 5, 25.3 );
1193 $worksheet->write( 1, 6, 24.9 );
1194 $worksheet->write( 1, 7, 25.2 );
1195 $worksheet->write( 1, 8, 24.8 );
1196
1197 # Write some statistical functions
1198 $worksheet->write( 4, 0, 'Count', $format );
1199 $worksheet->write( 4, 1, '=COUNT(B1:I1)' );
1200
1201 $worksheet->write( 5, 0, 'Sum', $format );
1202 $worksheet->write( 5, 1, '=SUM(B2:I2)' );
1203
1204 $worksheet->write( 6, 0, 'Average', $format );
1205 $worksheet->write( 6, 1, '=AVERAGE(B2:I2)' );
1206
1207 $worksheet->write( 7, 0, 'Min', $format );
1208 $worksheet->write( 7, 1, '=MIN(B2:I2)' );
1209
1210 $worksheet->write( 8, 0, 'Max', $format );
1211 $worksheet->write( 8, 1, '=MAX(B2:I2)' );
1212
1213 $worksheet->write( 9, 0, 'Standard Deviation', $format );
1214 $worksheet->write( 9, 1, '=STDEV(B2:I2)' );
1215
1216 $worksheet->write( 10, 0, 'Kurtosis', $format );
1217 $worksheet->write( 10, 1, '=KURT(B2:I2)' );
1218
1219 $workbook->close();
1220
1221 __END__
1222
1223 Download this example:
1224 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/stats.pl>
1225
1226 Example: autofilter.pl
1227 An example of how to create autofilters with Excel::Writer::XLSX.
1228
1229 An autofilter is a way of adding drop down lists to the headers of a 2D
1230 range of worksheet data. This allows users to filter the data based on
1231 simple criteria so that some data is shown and some is hidden.
1232
1233 Source code for this example:
1234
1235 #!/usr/bin/perl
1236
1237 ###############################################################################
1238 #
1239 # An example of how to create autofilters with Excel::Writer::XLSX.
1240 #
1241 # An autofilter is a way of adding drop down lists to the headers of a 2D range
1242 # of worksheet data. This allows users to filter the data based on
1243 # simple criteria so that some data is shown and some is hidden.
1244 #
1245 # reverse ('(c)'), September 2007, John McNamara, jmcnamara@cpan.org
1246 #
1247
1248 use strict;
1249 use warnings;
1250 use Excel::Writer::XLSX;
1251
1252 my $workbook = Excel::Writer::XLSX->new( 'autofilter.xlsx' );
1253
1254 my $worksheet1 = $workbook->add_worksheet();
1255 my $worksheet2 = $workbook->add_worksheet();
1256 my $worksheet3 = $workbook->add_worksheet();
1257 my $worksheet4 = $workbook->add_worksheet();
1258 my $worksheet5 = $workbook->add_worksheet();
1259 my $worksheet6 = $workbook->add_worksheet();
1260
1261 my $bold = $workbook->add_format( bold => 1 );
1262
1263
1264 # Extract the data embedded at the end of this file.
1265 my @headings = split ' ', <DATA>;
1266 my @data;
1267 push @data, [split] while <DATA>;
1268
1269
1270 # Set up several sheets with the same data.
1271 for my $worksheet ( $workbook->sheets() ) {
1272 $worksheet->set_column( 'A:D', 12 );
1273 $worksheet->set_row( 0, 20, $bold );
1274 $worksheet->write( 'A1', \@headings );
1275 }
1276
1277
1278 ###############################################################################
1279 #
1280 # Example 1. Autofilter without conditions.
1281 #
1282
1283 $worksheet1->autofilter( 'A1:D51' );
1284 $worksheet1->write( 'A2', [ [@data] ] );
1285
1286
1287 ###############################################################################
1288 #
1289 #
1290 # Example 2. Autofilter with a filter condition in the first column.
1291 #
1292
1293 # The range in this example is the same as above but in row-column notation.
1294 $worksheet2->autofilter( 0, 0, 50, 3 );
1295
1296 # The placeholder "Region" in the filter is ignored and can be any string
1297 # that adds clarity to the expression.
1298 #
1299 $worksheet2->filter_column( 0, 'Region eq East' );
1300
1301 #
1302 # Hide the rows that don't match the filter criteria.
1303 #
1304 my $row = 1;
1305
1306 for my $row_data ( @data ) {
1307 my $region = $row_data->[0];
1308
1309 if ( $region eq 'East' ) {
1310
1311 # Row is visible.
1312 }
1313 else {
1314
1315 # Hide row.
1316 $worksheet2->set_row( $row, undef, undef, 1 );
1317 }
1318
1319 $worksheet2->write( $row++, 0, $row_data );
1320 }
1321
1322
1323 ###############################################################################
1324 #
1325 #
1326 # Example 3. Autofilter with a dual filter condition in one of the columns.
1327 #
1328
1329 $worksheet3->autofilter( 'A1:D51' );
1330
1331 $worksheet3->filter_column( 'A', 'x eq East or x eq South' );
1332
1333 #
1334 # Hide the rows that don't match the filter criteria.
1335 #
1336 $row = 1;
1337
1338 for my $row_data ( @data ) {
1339 my $region = $row_data->[0];
1340
1341 if ( $region eq 'East' or $region eq 'South' ) {
1342
1343 # Row is visible.
1344 }
1345 else {
1346
1347 # Hide row.
1348 $worksheet3->set_row( $row, undef, undef, 1 );
1349 }
1350
1351 $worksheet3->write( $row++, 0, $row_data );
1352 }
1353
1354
1355 ###############################################################################
1356 #
1357 #
1358 # Example 4. Autofilter with filter conditions in two columns.
1359 #
1360
1361 $worksheet4->autofilter( 'A1:D51' );
1362
1363 $worksheet4->filter_column( 'A', 'x eq East' );
1364 $worksheet4->filter_column( 'C', 'x > 3000 and x < 8000' );
1365
1366 #
1367 # Hide the rows that don't match the filter criteria.
1368 #
1369 $row = 1;
1370
1371 for my $row_data ( @data ) {
1372 my $region = $row_data->[0];
1373 my $volume = $row_data->[2];
1374
1375 if ( $region eq 'East'
1376 and $volume > 3000
1377 and $volume < 8000 )
1378 {
1379
1380 # Row is visible.
1381 }
1382 else {
1383
1384 # Hide row.
1385 $worksheet4->set_row( $row, undef, undef, 1 );
1386 }
1387
1388 $worksheet4->write( $row++, 0, $row_data );
1389 }
1390
1391
1392 ###############################################################################
1393 #
1394 #
1395 # Example 5. Autofilter with filter for blanks.
1396 #
1397
1398 # Create a blank cell in our test data.
1399 $data[5]->[0] = '';
1400
1401
1402 $worksheet5->autofilter( 'A1:D51' );
1403 $worksheet5->filter_column( 'A', 'x == Blanks' );
1404
1405 #
1406 # Hide the rows that don't match the filter criteria.
1407 #
1408 $row = 1;
1409
1410 for my $row_data ( @data ) {
1411 my $region = $row_data->[0];
1412
1413 if ( $region eq '' ) {
1414
1415 # Row is visible.
1416 }
1417 else {
1418
1419 # Hide row.
1420 $worksheet5->set_row( $row, undef, undef, 1 );
1421 }
1422
1423 $worksheet5->write( $row++, 0, $row_data );
1424 }
1425
1426
1427 ###############################################################################
1428 #
1429 #
1430 # Example 6. Autofilter with filter for non-blanks.
1431 #
1432
1433
1434 $worksheet6->autofilter( 'A1:D51' );
1435 $worksheet6->filter_column( 'A', 'x == NonBlanks' );
1436
1437 #
1438 # Hide the rows that don't match the filter criteria.
1439 #
1440 $row = 1;
1441
1442 for my $row_data ( @data ) {
1443 my $region = $row_data->[0];
1444
1445 if ( $region ne '' ) {
1446
1447 # Row is visible.
1448 }
1449 else {
1450
1451 # Hide row.
1452 $worksheet6->set_row( $row, undef, undef, 1 );
1453 }
1454
1455 $worksheet6->write( $row++, 0, $row_data );
1456 }
1457
1458 $workbook->close();
1459
1460 __DATA__
1461 Region Item Volume Month
1462 East Apple 9000 July
1463 East Apple 5000 July
1464 South Orange 9000 September
1465 North Apple 2000 November
1466 West Apple 9000 November
1467 South Pear 7000 October
1468 North Pear 9000 August
1469 West Orange 1000 December
1470 West Grape 1000 November
1471 South Pear 10000 April
1472 West Grape 6000 January
1473 South Orange 3000 May
1474 North Apple 3000 December
1475 South Apple 7000 February
1476 West Grape 1000 December
1477 East Grape 8000 February
1478 South Grape 10000 June
1479 West Pear 7000 December
1480 South Apple 2000 October
1481 East Grape 7000 December
1482 North Grape 6000 April
1483 East Pear 8000 February
1484 North Apple 7000 August
1485 North Orange 7000 July
1486 North Apple 6000 June
1487 South Grape 8000 September
1488 West Apple 3000 October
1489 South Orange 10000 November
1490 West Grape 4000 July
1491 North Orange 5000 August
1492 East Orange 1000 November
1493 East Orange 4000 October
1494 North Grape 5000 August
1495 East Apple 1000 December
1496 South Apple 10000 March
1497 East Grape 7000 October
1498 West Grape 1000 September
1499 East Grape 10000 October
1500 South Orange 8000 March
1501 North Apple 4000 July
1502 South Orange 5000 July
1503 West Apple 4000 June
1504 East Apple 5000 April
1505 North Pear 3000 August
1506 East Grape 9000 November
1507 North Orange 8000 October
1508 East Apple 10000 June
1509 South Pear 1000 December
1510 North Grape 10000 July
1511 East Grape 6000 February
1512
1513 Download this example:
1514 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/autofilter.pl>
1515
1516 Example: array_formula.pl
1517 Example of how to use the Excel::Writer::XLSX module to write simple
1518 array formulas.
1519
1520 Source code for this example:
1521
1522 #!/usr/bin/perl
1523
1524 #######################################################################
1525 #
1526 # Example of how to use the Excel::Writer::XLSX module to write simple
1527 # array formulas.
1528 #
1529 # reverse ('(c)'), August 2004, John McNamara, jmcnamara@cpan.org
1530 #
1531
1532 use strict;
1533 use warnings;
1534 use Excel::Writer::XLSX;
1535
1536 # Create a new workbook and add a worksheet
1537 my $workbook = Excel::Writer::XLSX->new( 'array_formula.xlsx' );
1538 my $worksheet = $workbook->add_worksheet();
1539
1540 # Write some test data.
1541 $worksheet->write( 'B1', [ [ 500, 10 ], [ 300, 15 ] ] );
1542 $worksheet->write( 'B5', [ [ 1, 2, 3 ], [ 20234, 21003, 10000 ] ] );
1543
1544 # Write an array formula that returns a single value
1545 $worksheet->write( 'A1', '{=SUM(B1:C1*B2:C2)}' );
1546
1547 # Same as above but more verbose.
1548 $worksheet->write_array_formula( 'A2:A2', '{=SUM(B1:C1*B2:C2)}' );
1549
1550 # Write an array formula that returns a range of values
1551 $worksheet->write_array_formula( 'A5:A7', '{=TREND(C5:C7,B5:B7)}' );
1552
1553 $workbook->close();
1554
1555 __END__
1556
1557 Download this example:
1558 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/array_formula.pl>
1559
1560 Example: cgi.pl
1561 Example of how to use the Excel::Writer::XLSX module to send an Excel
1562 file to a browser in a CGI program.
1563
1564 On Windows the hash-bang line should be something like:
1565
1566 #!C:\Perl\bin\perl.exe
1567
1568 The "Content-Disposition" line will cause a prompt to be generated to
1569 save the file. If you want to stream the file to the browser instead,
1570 comment out that line as shown below.
1571
1572 #!/usr/bin/perl
1573
1574 ###############################################################################
1575 #
1576 # Example of how to use the Excel::Writer::XLSX module to send an Excel
1577 # file to a browser in a CGI program.
1578 #
1579 # On Windows the hash-bang line should be something like:
1580 #
1581 # #!C:\Perl\bin\perl.exe
1582 #
1583 # The "Content-Disposition" line will cause a prompt to be generated to save
1584 # the file. If you want to stream the file to the browser instead, comment out
1585 # that line as shown below.
1586 #
1587 # reverse ('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
1588 #
1589
1590 use strict;
1591 use warnings;
1592 use Excel::Writer::XLSX;
1593
1594 # Set the filename and send the content type
1595 my $filename = "cgitest.xlsx";
1596
1597 print "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n";
1598
1599 # The Content-Disposition will generate a prompt to save the file. If you want
1600 # to stream the file to the browser, comment out the following line.
1601 print "Content-Disposition: attachment; filename=$filename\n";
1602 print "\n";
1603
1604 # Redirect the output to STDOUT. Binmode the filehandle in case it is needed.
1605 binmode STDOUT;
1606
1607 my $workbook = Excel::Writer::XLSX->new( \*STDOUT );
1608 my $worksheet = $workbook->add_worksheet();
1609
1610
1611 # Set the column width for column 1
1612 $worksheet->set_column( 0, 0, 20 );
1613
1614
1615 # Create a format
1616 my $format = $workbook->add_format();
1617 $format->set_bold();
1618 $format->set_size( 15 );
1619 $format->set_color( 'blue' );
1620
1621
1622 # Write to the workbook
1623 $worksheet->write( 0, 0, "Hi Excel!", $format );
1624
1625 $workbook->close();
1626
1627 __END__
1628
1629 Download this example:
1630 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/cgi.pl>
1631
1632 Example: chart_area.pl
1633 A demo of an Area chart in Excel::Writer::XLSX.
1634
1635 Source code for this example:
1636
1637 #!/usr/bin/perl
1638
1639 #######################################################################
1640 #
1641 # A demo of an Area chart in Excel::Writer::XLSX.
1642 #
1643 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
1644 #
1645
1646 use strict;
1647 use warnings;
1648 use Excel::Writer::XLSX;
1649
1650 my $workbook = Excel::Writer::XLSX->new( 'chart_area.xlsx' );
1651 my $worksheet = $workbook->add_worksheet();
1652 my $bold = $workbook->add_format( bold => 1 );
1653
1654 # Add the worksheet data that the charts will refer to.
1655 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
1656 my $data = [
1657 [ 2, 3, 4, 5, 6, 7 ],
1658 [ 40, 40, 50, 30, 25, 50 ],
1659 [ 30, 25, 30, 10, 5, 10 ],
1660
1661 ];
1662
1663 $worksheet->write( 'A1', $headings, $bold );
1664 $worksheet->write( 'A2', $data );
1665
1666 # Create a new chart object. In this case an embedded chart.
1667 my $chart1 = $workbook->add_chart( type => 'area', embedded => 1 );
1668
1669 # Configure the first series.
1670 $chart1->add_series(
1671 name => '=Sheet1!$B$1',
1672 categories => '=Sheet1!$A$2:$A$7',
1673 values => '=Sheet1!$B$2:$B$7',
1674 );
1675
1676 # Configure second series. Note alternative use of array ref to define
1677 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
1678 $chart1->add_series(
1679 name => '=Sheet1!$C$1',
1680 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1681 values => [ 'Sheet1', 1, 6, 2, 2 ],
1682 );
1683
1684 # Add a chart title and some axis labels.
1685 $chart1->set_title ( name => 'Results of sample analysis' );
1686 $chart1->set_x_axis( name => 'Test number' );
1687 $chart1->set_y_axis( name => 'Sample length (mm)' );
1688
1689 # Set an Excel chart style. Blue colors with white outline and shadow.
1690 $chart1->set_style( 11 );
1691
1692 # Insert the chart into the worksheet (with an offset).
1693 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
1694
1695
1696 #
1697 # Create a stacked chart sub-type
1698 #
1699 my $chart2 = $workbook->add_chart(
1700 type => 'area',
1701 embedded => 1,
1702 subtype => 'stacked'
1703 );
1704
1705 # Configure the first series.
1706 $chart2->add_series(
1707 name => '=Sheet1!$B$1',
1708 categories => '=Sheet1!$A$2:$A$7',
1709 values => '=Sheet1!$B$2:$B$7',
1710 );
1711
1712 # Configure second series.
1713 $chart2->add_series(
1714 name => '=Sheet1!$C$1',
1715 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1716 values => [ 'Sheet1', 1, 6, 2, 2 ],
1717 );
1718
1719 # Add a chart title and some axis labels.
1720 $chart2->set_title ( name => 'Stacked Chart' );
1721 $chart2->set_x_axis( name => 'Test number' );
1722 $chart2->set_y_axis( name => 'Sample length (mm)' );
1723
1724 # Set an Excel chart style. Blue colors with white outline and shadow.
1725 $chart2->set_style( 12 );
1726
1727 # Insert the chart into the worksheet (with an offset).
1728 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
1729
1730
1731 #
1732 # Create a percent stacked chart sub-type
1733 #
1734 my $chart3 = $workbook->add_chart(
1735 type => 'area',
1736 embedded => 1,
1737 subtype => 'percent_stacked'
1738 );
1739
1740 # Configure the first series.
1741 $chart3->add_series(
1742 name => '=Sheet1!$B$1',
1743 categories => '=Sheet1!$A$2:$A$7',
1744 values => '=Sheet1!$B$2:$B$7',
1745 );
1746
1747 # Configure second series.
1748 $chart3->add_series(
1749 name => '=Sheet1!$C$1',
1750 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1751 values => [ 'Sheet1', 1, 6, 2, 2 ],
1752 );
1753
1754 # Add a chart title and some axis labels.
1755 $chart3->set_title ( name => 'Percent Stacked Chart' );
1756 $chart3->set_x_axis( name => 'Test number' );
1757 $chart3->set_y_axis( name => 'Sample length (mm)' );
1758
1759 # Set an Excel chart style. Blue colors with white outline and shadow.
1760 $chart3->set_style( 13 );
1761
1762 # Insert the chart into the worksheet (with an offset).
1763 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
1764
1765 $workbook->close();
1766
1767 __END__
1768
1769 Download this example:
1770 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_area.pl>
1771
1772 Example: chart_bar.pl
1773 A demo of an Bar chart in Excel::Writer::XLSX.
1774
1775 Source code for this example:
1776
1777 #!/usr/bin/perl
1778
1779 #######################################################################
1780 #
1781 # A demo of an Bar chart in Excel::Writer::XLSX.
1782 #
1783 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
1784 #
1785
1786 use strict;
1787 use warnings;
1788 use Excel::Writer::XLSX;
1789
1790 my $workbook = Excel::Writer::XLSX->new( 'chart_bar.xlsx' );
1791 my $worksheet = $workbook->add_worksheet();
1792 my $bold = $workbook->add_format( bold => 1 );
1793
1794 # Add the worksheet data that the charts will refer to.
1795 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
1796 my $data = [
1797 [ 2, 3, 4, 5, 6, 7 ],
1798 [ 10, 40, 50, 20, 10, 50 ],
1799 [ 30, 60, 70, 50, 40, 30 ],
1800
1801 ];
1802
1803 $worksheet->write( 'A1', $headings, $bold );
1804 $worksheet->write( 'A2', $data );
1805
1806 # Create a new chart object. In this case an embedded chart.
1807 my $chart1 = $workbook->add_chart( type => 'bar', embedded => 1 );
1808
1809 # Configure the first series.
1810 $chart1->add_series(
1811 name => '=Sheet1!$B$1',
1812 categories => '=Sheet1!$A$2:$A$7',
1813 values => '=Sheet1!$B$2:$B$7',
1814 );
1815
1816 # Configure second series. Note alternative use of array ref to define
1817 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
1818 $chart1->add_series(
1819 name => '=Sheet1!$C$1',
1820 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1821 values => [ 'Sheet1', 1, 6, 2, 2 ],
1822 );
1823
1824 # Add a chart title and some axis labels.
1825 $chart1->set_title ( name => 'Results of sample analysis' );
1826 $chart1->set_x_axis( name => 'Test number' );
1827 $chart1->set_y_axis( name => 'Sample length (mm)' );
1828
1829 # Set an Excel chart style. Blue colors with white outline and shadow.
1830 $chart1->set_style( 11 );
1831
1832 # Insert the chart into the worksheet (with an offset).
1833 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
1834
1835
1836 #
1837 # Create a stacked chart sub-type
1838 #
1839 my $chart2 = $workbook->add_chart(
1840 type => 'bar',
1841 embedded => 1,
1842 subtype => 'stacked'
1843 );
1844
1845 # Configure the first series.
1846 $chart2->add_series(
1847 name => '=Sheet1!$B$1',
1848 categories => '=Sheet1!$A$2:$A$7',
1849 values => '=Sheet1!$B$2:$B$7',
1850 );
1851
1852 # Configure second series.
1853 $chart2->add_series(
1854 name => '=Sheet1!$C$1',
1855 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1856 values => [ 'Sheet1', 1, 6, 2, 2 ],
1857 );
1858
1859 # Add a chart title and some axis labels.
1860 $chart2->set_title ( name => 'Stacked Chart' );
1861 $chart2->set_x_axis( name => 'Test number' );
1862 $chart2->set_y_axis( name => 'Sample length (mm)' );
1863
1864 # Set an Excel chart style. Blue colors with white outline and shadow.
1865 $chart2->set_style( 12 );
1866
1867 # Insert the chart into the worksheet (with an offset).
1868 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
1869
1870
1871 #
1872 # Create a percent stacked chart sub-type
1873 #
1874 my $chart3 = $workbook->add_chart(
1875 type => 'bar',
1876 embedded => 1,
1877 subtype => 'percent_stacked'
1878 );
1879
1880 # Configure the first series.
1881 $chart3->add_series(
1882 name => '=Sheet1!$B$1',
1883 categories => '=Sheet1!$A$2:$A$7',
1884 values => '=Sheet1!$B$2:$B$7',
1885 );
1886
1887 # Configure second series.
1888 $chart3->add_series(
1889 name => '=Sheet1!$C$1',
1890 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1891 values => [ 'Sheet1', 1, 6, 2, 2 ],
1892 );
1893
1894 # Add a chart title and some axis labels.
1895 $chart3->set_title ( name => 'Percent Stacked Chart' );
1896 $chart3->set_x_axis( name => 'Test number' );
1897 $chart3->set_y_axis( name => 'Sample length (mm)' );
1898
1899 # Set an Excel chart style. Blue colors with white outline and shadow.
1900 $chart3->set_style( 13 );
1901
1902 # Insert the chart into the worksheet (with an offset).
1903 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
1904
1905 $workbook->close();
1906
1907 __END__
1908
1909 Download this example:
1910 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_bar.pl>
1911
1912 Example: chart_column.pl
1913 A demo of a Column chart in Excel::Writer::XLSX.
1914
1915 Source code for this example:
1916
1917 #!/usr/bin/perl
1918
1919 #######################################################################
1920 #
1921 # A demo of a Column chart in Excel::Writer::XLSX.
1922 #
1923 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
1924 #
1925
1926 use strict;
1927 use warnings;
1928 use Excel::Writer::XLSX;
1929
1930 my $workbook = Excel::Writer::XLSX->new( 'chart_column.xlsx' );
1931 my $worksheet = $workbook->add_worksheet();
1932 my $bold = $workbook->add_format( bold => 1 );
1933
1934 # Add the worksheet data that the charts will refer to.
1935 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
1936 my $data = [
1937 [ 2, 3, 4, 5, 6, 7 ],
1938 [ 10, 40, 50, 20, 10, 50 ],
1939 [ 30, 60, 70, 50, 40, 30 ],
1940
1941 ];
1942
1943 $worksheet->write( 'A1', $headings, $bold );
1944 $worksheet->write( 'A2', $data );
1945
1946 # Create a new chart object. In this case an embedded chart.
1947 my $chart1 = $workbook->add_chart( type => 'column', embedded => 1 );
1948
1949 # Configure the first series.
1950 $chart1->add_series(
1951 name => '=Sheet1!$B$1',
1952 categories => '=Sheet1!$A$2:$A$7',
1953 values => '=Sheet1!$B$2:$B$7',
1954 );
1955
1956 # Configure second series. Note alternative use of array ref to define
1957 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
1958 $chart1->add_series(
1959 name => '=Sheet1!$C$1',
1960 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1961 values => [ 'Sheet1', 1, 6, 2, 2 ],
1962 );
1963
1964 # Add a chart title and some axis labels.
1965 $chart1->set_title ( name => 'Results of sample analysis' );
1966 $chart1->set_x_axis( name => 'Test number' );
1967 $chart1->set_y_axis( name => 'Sample length (mm)' );
1968
1969 # Set an Excel chart style. Blue colors with white outline and shadow.
1970 $chart1->set_style( 11 );
1971
1972 # Insert the chart into the worksheet (with an offset).
1973 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
1974
1975
1976 #
1977 # Create a stacked chart sub-type
1978 #
1979 my $chart2 = $workbook->add_chart(
1980 type => 'column',
1981 embedded => 1,
1982 subtype => 'stacked'
1983 );
1984
1985 # Configure the first series.
1986 $chart2->add_series(
1987 name => '=Sheet1!$B$1',
1988 categories => '=Sheet1!$A$2:$A$7',
1989 values => '=Sheet1!$B$2:$B$7',
1990 );
1991
1992 # Configure second series.
1993 $chart2->add_series(
1994 name => '=Sheet1!$C$1',
1995 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1996 values => [ 'Sheet1', 1, 6, 2, 2 ],
1997 );
1998
1999 # Add a chart title and some axis labels.
2000 $chart2->set_title ( name => 'Stacked Chart' );
2001 $chart2->set_x_axis( name => 'Test number' );
2002 $chart2->set_y_axis( name => 'Sample length (mm)' );
2003
2004 # Set an Excel chart style. Blue colors with white outline and shadow.
2005 $chart2->set_style( 12 );
2006
2007 # Insert the chart into the worksheet (with an offset).
2008 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
2009
2010
2011 #
2012 # Create a percent stacked chart sub-type
2013 #
2014 my $chart3 = $workbook->add_chart(
2015 type => 'column',
2016 embedded => 1,
2017 subtype => 'percent_stacked'
2018 );
2019
2020 # Configure the first series.
2021 $chart3->add_series(
2022 name => '=Sheet1!$B$1',
2023 categories => '=Sheet1!$A$2:$A$7',
2024 values => '=Sheet1!$B$2:$B$7',
2025 );
2026
2027 # Configure second series.
2028 $chart3->add_series(
2029 name => '=Sheet1!$C$1',
2030 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2031 values => [ 'Sheet1', 1, 6, 2, 2 ],
2032 );
2033
2034 # Add a chart title and some axis labels.
2035 $chart3->set_title ( name => 'Percent Stacked Chart' );
2036 $chart3->set_x_axis( name => 'Test number' );
2037 $chart3->set_y_axis( name => 'Sample length (mm)' );
2038
2039 # Set an Excel chart style. Blue colors with white outline and shadow.
2040 $chart3->set_style( 13 );
2041
2042 # Insert the chart into the worksheet (with an offset).
2043 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
2044
2045 $workbook->close();
2046
2047 __END__
2048
2049 Download this example:
2050 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_column.pl>
2051
2052 Example: chart_line.pl
2053 A demo of a Line chart in Excel::Writer::XLSX.
2054
2055 Source code for this example:
2056
2057 #!/usr/bin/perl
2058
2059 #######################################################################
2060 #
2061 # A demo of a Line chart in Excel::Writer::XLSX.
2062 #
2063 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
2064 #
2065
2066 use strict;
2067 use warnings;
2068 use Excel::Writer::XLSX;
2069
2070 my $workbook = Excel::Writer::XLSX->new( 'chart_line.xlsx' );
2071 my $worksheet = $workbook->add_worksheet();
2072 my $bold = $workbook->add_format( bold => 1 );
2073
2074 # Add the worksheet data that the charts will refer to.
2075 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
2076 my $data = [
2077 [ 2, 3, 4, 5, 6, 7 ],
2078 [ 10, 40, 50, 20, 10, 50 ],
2079 [ 30, 60, 70, 50, 40, 30 ],
2080
2081 ];
2082
2083 $worksheet->write( 'A1', $headings, $bold );
2084 $worksheet->write( 'A2', $data );
2085
2086 # Create a new chart object. In this case an embedded chart.
2087 my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
2088
2089 # Configure the first series.
2090 $chart->add_series(
2091 name => '=Sheet1!$B$1',
2092 categories => '=Sheet1!$A$2:$A$7',
2093 values => '=Sheet1!$B$2:$B$7',
2094 );
2095
2096 # Configure second series. Note alternative use of array ref to define
2097 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2098 $chart->add_series(
2099 name => '=Sheet1!$C$1',
2100 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2101 values => [ 'Sheet1', 1, 6, 2, 2 ],
2102 );
2103
2104 # Add a chart title and some axis labels.
2105 $chart->set_title ( name => 'Results of sample analysis' );
2106 $chart->set_x_axis( name => 'Test number' );
2107 $chart->set_y_axis( name => 'Sample length (mm)' );
2108
2109 # Set an Excel chart style. Colors with white outline and shadow.
2110 $chart->set_style( 10 );
2111
2112 # Insert the chart into the worksheet (with an offset).
2113 $worksheet->insert_chart( 'D2', $chart, { x_offset => 25, y_offset => 10 } );
2114
2115 $workbook->close();
2116
2117 __END__
2118
2119 Download this example:
2120 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_line.pl>
2121
2122 Example: chart_pie.pl
2123 A demo of a Pie chart in Excel::Writer::XLSX.
2124
2125 The demo also shows how to set segment colours. It is possible to
2126 define chart colors for most types of Excel::Writer::XLSX charts via
2127 the add_series() method. However, Pie and Doughtnut charts are a
2128 special case since each segment is represented as a point so it is
2129 necessary to assign formatting to each point in the series.
2130
2131 Source code for this example:
2132
2133 #!/usr/bin/perl
2134
2135 #######################################################################
2136 #
2137 # A demo of a Pie chart in Excel::Writer::XLSX.
2138 #
2139 # The demo also shows how to set segment colours. It is possible to define
2140 # chart colors for most types of Excel::Writer::XLSX charts via the
2141 # add_series() method. However, Pie and Doughtnut charts are a special case
2142 # since each segment is represented as a point so it is necessary to assign
2143 # formatting to each point in the series.
2144 #
2145 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
2146 #
2147
2148 use strict;
2149 use warnings;
2150 use Excel::Writer::XLSX;
2151
2152 my $workbook = Excel::Writer::XLSX->new( 'chart_pie.xlsx' );
2153 my $worksheet = $workbook->add_worksheet();
2154 my $bold = $workbook->add_format( bold => 1 );
2155
2156 # Add the worksheet data that the charts will refer to.
2157 my $headings = [ 'Category', 'Values' ];
2158 my $data = [
2159 [ 'Apple', 'Cherry', 'Pecan' ],
2160 [ 60, 30, 10 ],
2161 ];
2162
2163 $worksheet->write( 'A1', $headings, $bold );
2164 $worksheet->write( 'A2', $data );
2165
2166 # Create a new chart object. In this case an embedded chart.
2167 my $chart1 = $workbook->add_chart( type => 'pie', embedded => 1 );
2168
2169 # Configure the series. Note the use of the array ref to define ranges:
2170 # [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2171 # See below for an alternative syntax.
2172 $chart1->add_series(
2173 name => 'Pie sales data',
2174 categories => [ 'Sheet1', 1, 3, 0, 0 ],
2175 values => [ 'Sheet1', 1, 3, 1, 1 ],
2176 );
2177
2178 # Add a title.
2179 $chart1->set_title( name => 'Popular Pie Types' );
2180
2181 # Set an Excel chart style. Colors with white outline and shadow.
2182 $chart1->set_style( 10 );
2183
2184 # Insert the chart into the worksheet (with an offset).
2185 $worksheet->insert_chart( 'C2', $chart1, { x_offset => 25, y_offset => 10 } );
2186
2187
2188 #
2189 # Create a Pie chart with user defined segment colors.
2190 #
2191
2192 # Create an example Pie chart like above.
2193 my $chart2 = $workbook->add_chart( type => 'pie', embedded => 1 );
2194
2195 # Configure the series and add user defined segment colours.
2196 $chart2->add_series(
2197 name => 'Pie sales data',
2198 categories => '=Sheet1!$A$2:$A$4',
2199 values => '=Sheet1!$B$2:$B$4',
2200 points => [
2201 { fill => { color => '#5ABA10' } },
2202 { fill => { color => '#FE110E' } },
2203 { fill => { color => '#CA5C05' } },
2204 ],
2205 );
2206
2207 # Add a title.
2208 $chart2->set_title( name => 'Pie Chart with user defined colors' );
2209
2210
2211 # Insert the chart into the worksheet (with an offset).
2212 $worksheet->insert_chart( 'C18', $chart2, { x_offset => 25, y_offset => 10 } );
2213
2214
2215 #
2216 # Create a Pie chart with rotation of the segments.
2217 #
2218
2219 # Create an example Pie chart like above.
2220 my $chart3 = $workbook->add_chart( type => 'pie', embedded => 1 );
2221
2222 # Configure the series.
2223 $chart3->add_series(
2224 name => 'Pie sales data',
2225 categories => '=Sheet1!$A$2:$A$4',
2226 values => '=Sheet1!$B$2:$B$4',
2227 );
2228
2229 # Add a title.
2230 $chart3->set_title( name => 'Pie Chart with segment rotation' );
2231
2232 # Change the angle/rotation of the first segment.
2233 $chart3->set_rotation(90);
2234
2235 # Insert the chart into the worksheet (with an offset).
2236 $worksheet->insert_chart( 'C34', $chart3, { x_offset => 25, y_offset => 10 } );
2237
2238 $workbook->close();
2239
2240 __END__
2241
2242 Download this example:
2243 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_pie.pl>
2244
2245 Example: chart_doughnut.pl
2246 A demo of a Doughnut chart in Excel::Writer::XLSX.
2247
2248 The demo also shows how to set segment colours. It is possible to
2249 define chart colors for most types of Excel::Writer::XLSX charts via
2250 the add_series() method. However, Pie and Doughtnut charts are a
2251 special case since each segment is represented as a point so it is
2252 necessary to assign formatting to each point in the series.
2253
2254 Source code for this example:
2255
2256 #!/usr/bin/perl
2257
2258 #######################################################################
2259 #
2260 # A demo of a Doughnut chart in Excel::Writer::XLSX.
2261 #
2262 # The demo also shows how to set segment colours. It is possible to define
2263 # chart colors for most types of Excel::Writer::XLSX charts via the
2264 # add_series() method. However, Pie and Doughtnut charts are a special case
2265 # since each segment is represented as a point so it is necessary to assign
2266 # formatting to each point in the series.
2267 #
2268 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
2269 #
2270
2271 use strict;
2272 use warnings;
2273 use Excel::Writer::XLSX;
2274
2275 my $workbook = Excel::Writer::XLSX->new( 'chart_doughnut.xlsx' );
2276 my $worksheet = $workbook->add_worksheet();
2277 my $bold = $workbook->add_format( bold => 1 );
2278
2279 # Add the worksheet data that the charts will refer to.
2280 my $headings = [ 'Category', 'Values' ];
2281 my $data = [
2282 [ 'Glazed', 'Chocolate', 'Cream' ],
2283 [ 50, 35, 15 ],
2284 ];
2285
2286 $worksheet->write( 'A1', $headings, $bold );
2287 $worksheet->write( 'A2', $data );
2288
2289 # Create a new chart object. In this case an embedded chart.
2290 my $chart1 = $workbook->add_chart( type => 'doughnut', embedded => 1 );
2291
2292 # Configure the series. Note the use of the array ref to define ranges:
2293 # [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2294 # See below for an alternative syntax.
2295 $chart1->add_series(
2296 name => 'Doughnut sales data',
2297 categories => [ 'Sheet1', 1, 3, 0, 0 ],
2298 values => [ 'Sheet1', 1, 3, 1, 1 ],
2299 );
2300
2301 # Add a title.
2302 $chart1->set_title( name => 'Popular Doughnut Types' );
2303
2304 # Set an Excel chart style. Colors with white outline and shadow.
2305 $chart1->set_style( 10 );
2306
2307 # Insert the chart into the worksheet (with an offset).
2308 $worksheet->insert_chart( 'C2', $chart1, { x_offset => 25, y_offset => 10 } );
2309
2310
2311 #
2312 # Create a Doughnut chart with user defined segment colors.
2313 #
2314
2315 # Create an example Doughnut chart like above.
2316 my $chart2 = $workbook->add_chart( type => 'doughnut', embedded => 1 );
2317
2318 # Configure the series and add user defined segment colours.
2319 $chart2->add_series(
2320 name => 'Doughnut sales data',
2321 categories => '=Sheet1!$A$2:$A$4',
2322 values => '=Sheet1!$B$2:$B$4',
2323 points => [
2324 { fill => { color => '#FA58D0' } },
2325 { fill => { color => '#61210B' } },
2326 { fill => { color => '#F5F6CE' } },
2327 ],
2328 );
2329
2330 # Add a title.
2331 $chart2->set_title( name => 'Doughnut Chart with user defined colors' );
2332
2333
2334 # Insert the chart into the worksheet (with an offset).
2335 $worksheet->insert_chart( 'C18', $chart2, { x_offset => 25, y_offset => 10 } );
2336
2337
2338 #
2339 # Create a Doughnut chart with rotation of the segments.
2340 #
2341
2342 # Create an example Doughnut chart like above.
2343 my $chart3 = $workbook->add_chart( type => 'doughnut', embedded => 1 );
2344
2345 # Configure the series.
2346 $chart3->add_series(
2347 name => 'Doughnut sales data',
2348 categories => '=Sheet1!$A$2:$A$4',
2349 values => '=Sheet1!$B$2:$B$4',
2350 );
2351
2352 # Add a title.
2353 $chart3->set_title( name => 'Doughnut Chart with segment rotation' );
2354
2355 # Change the angle/rotation of the first segment.
2356 $chart3->set_rotation(90);
2357
2358 # Insert the chart into the worksheet (with an offset).
2359 $worksheet->insert_chart( 'C34', $chart3, { x_offset => 25, y_offset => 10 } );
2360
2361
2362 #
2363 # Create a Doughnut chart with user defined hole size.
2364 #
2365
2366 # Create an example Doughnut chart like above.
2367 my $chart4 = $workbook->add_chart( type => 'doughnut', embedded => 1 );
2368
2369 # Configure the series.
2370 $chart4->add_series(
2371 name => 'Doughnut sales data',
2372 categories => '=Sheet1!$A$2:$A$4',
2373 values => '=Sheet1!$B$2:$B$4',
2374 );
2375
2376 # Add a title.
2377 $chart4->set_title( name => 'Doughnut Chart with user defined hole size' );
2378
2379 # Change the hole size.
2380 $chart4->set_hole_size(33);
2381
2382 # Insert the chart into the worksheet (with an offset).
2383 $worksheet->insert_chart( 'C50', $chart4, { x_offset => 25, y_offset => 10 } );
2384
2385 $workbook->close();
2386
2387 __END__
2388
2389 Download this example:
2390 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_doughnut.pl>
2391
2392 Example: chart_radar.pl
2393 A demo of an Radar chart in Excel::Writer::XLSX.
2394
2395 Source code for this example:
2396
2397 #!/usr/bin/perl
2398
2399 #######################################################################
2400 #
2401 # A demo of an Radar chart in Excel::Writer::XLSX.
2402 #
2403 # reverse ('(c)'), October 2012, John McNamara, jmcnamara@cpan.org
2404 #
2405
2406 use strict;
2407 use warnings;
2408 use Excel::Writer::XLSX;
2409
2410 my $workbook = Excel::Writer::XLSX->new( 'chart_radar.xlsx' );
2411 my $worksheet = $workbook->add_worksheet();
2412 my $bold = $workbook->add_format( bold => 1 );
2413
2414 # Add the worksheet data that the charts will refer to.
2415 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
2416 my $data = [
2417 [ 2, 3, 4, 5, 6, 7 ],
2418 [ 30, 60, 70, 50, 40, 30 ],
2419 [ 25, 40, 50, 30, 50, 40 ],
2420
2421 ];
2422
2423 $worksheet->write( 'A1', $headings, $bold );
2424 $worksheet->write( 'A2', $data );
2425
2426 # Create a new chart object. In this case an embedded chart.
2427 my $chart1 = $workbook->add_chart( type => 'radar', embedded => 1 );
2428
2429 # Configure the first series.
2430 $chart1->add_series(
2431 name => '=Sheet1!$B$1',
2432 categories => '=Sheet1!$A$2:$A$7',
2433 values => '=Sheet1!$B$2:$B$7',
2434 );
2435
2436 # Configure second series. Note alternative use of array ref to define
2437 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2438 $chart1->add_series(
2439 name => '=Sheet1!$C$1',
2440 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2441 values => [ 'Sheet1', 1, 6, 2, 2 ],
2442 );
2443
2444 # Add a chart title and some axis labels.
2445 $chart1->set_title ( name => 'Results of sample analysis' );
2446 $chart1->set_x_axis( name => 'Test number' );
2447 $chart1->set_y_axis( name => 'Sample length (mm)' );
2448
2449 # Set an Excel chart style. Blue colors with white outline and shadow.
2450 $chart1->set_style( 11 );
2451
2452 # Insert the chart into the worksheet (with an offset).
2453 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
2454
2455
2456 #
2457 # Create a with_markers chart sub-type
2458 #
2459 my $chart2 = $workbook->add_chart(
2460 type => 'radar',
2461 embedded => 1,
2462 subtype => 'with_markers'
2463 );
2464
2465 # Configure the first series.
2466 $chart2->add_series(
2467 name => '=Sheet1!$B$1',
2468 categories => '=Sheet1!$A$2:$A$7',
2469 values => '=Sheet1!$B$2:$B$7',
2470 );
2471
2472 # Configure second series.
2473 $chart2->add_series(
2474 name => '=Sheet1!$C$1',
2475 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2476 values => [ 'Sheet1', 1, 6, 2, 2 ],
2477 );
2478
2479 # Add a chart title and some axis labels.
2480 $chart2->set_title ( name => 'Stacked Chart' );
2481 $chart2->set_x_axis( name => 'Test number' );
2482 $chart2->set_y_axis( name => 'Sample length (mm)' );
2483
2484 # Set an Excel chart style. Blue colors with white outline and shadow.
2485 $chart2->set_style( 12 );
2486
2487 # Insert the chart into the worksheet (with an offset).
2488 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
2489
2490
2491 #
2492 # Create a filled chart sub-type
2493 #
2494 my $chart3 = $workbook->add_chart(
2495 type => 'radar',
2496 embedded => 1,
2497 subtype => 'filled'
2498 );
2499
2500 # Configure the first series.
2501 $chart3->add_series(
2502 name => '=Sheet1!$B$1',
2503 categories => '=Sheet1!$A$2:$A$7',
2504 values => '=Sheet1!$B$2:$B$7',
2505 );
2506
2507 # Configure second series.
2508 $chart3->add_series(
2509 name => '=Sheet1!$C$1',
2510 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2511 values => [ 'Sheet1', 1, 6, 2, 2 ],
2512 );
2513
2514 # Add a chart title and some axis labels.
2515 $chart3->set_title ( name => 'Percent Stacked Chart' );
2516 $chart3->set_x_axis( name => 'Test number' );
2517 $chart3->set_y_axis( name => 'Sample length (mm)' );
2518
2519 # Set an Excel chart style. Blue colors with white outline and shadow.
2520 $chart3->set_style( 13 );
2521
2522 # Insert the chart into the worksheet (with an offset).
2523 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
2524
2525 $workbook->close();
2526
2527 __END__
2528
2529 Download this example:
2530 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_radar.pl>
2531
2532 Example: chart_scatter.pl
2533 A demo of a Scatter chart in Excel::Writer::XLSX. Other subtypes are
2534 also supported such as markers_only (the default),
2535 straight_with_markers, straight, smooth_with_markers and smooth. See
2536 the main documentation for more details.
2537
2538 Source code for this example:
2539
2540 #!/usr/bin/perl
2541
2542 #######################################################################
2543 #
2544 # A demo of a Scatter chart in Excel::Writer::XLSX. Other subtypes are
2545 # also supported such as markers_only (the default), straight_with_markers,
2546 # straight, smooth_with_markers and smooth. See the main documentation for
2547 # more details.
2548 #
2549 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
2550 #
2551
2552 use strict;
2553 use warnings;
2554 use Excel::Writer::XLSX;
2555
2556 my $workbook = Excel::Writer::XLSX->new( 'chart_scatter.xlsx' );
2557 my $worksheet = $workbook->add_worksheet();
2558 my $bold = $workbook->add_format( bold => 1 );
2559
2560 # Add the worksheet data that the charts will refer to.
2561 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
2562 my $data = [
2563 [ 2, 3, 4, 5, 6, 7 ],
2564 [ 10, 40, 50, 20, 10, 50 ],
2565 [ 30, 60, 70, 50, 40, 30 ],
2566
2567 ];
2568
2569 $worksheet->write( 'A1', $headings, $bold );
2570 $worksheet->write( 'A2', $data );
2571
2572 # Create a new chart object. In this case an embedded chart.
2573 my $chart1 = $workbook->add_chart( type => 'scatter', embedded => 1 );
2574
2575 # Configure the first series.
2576 $chart1->add_series(
2577 name => '=Sheet1!$B$1',
2578 categories => '=Sheet1!$A$2:$A$7',
2579 values => '=Sheet1!$B$2:$B$7',
2580 );
2581
2582 # Configure second series. Note alternative use of array ref to define
2583 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2584 $chart1->add_series(
2585 name => '=Sheet1!$C$1',
2586 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2587 values => [ 'Sheet1', 1, 6, 2, 2 ],
2588 );
2589
2590 # Add a chart title and some axis labels.
2591 $chart1->set_title ( name => 'Results of sample analysis' );
2592 $chart1->set_x_axis( name => 'Test number' );
2593 $chart1->set_y_axis( name => 'Sample length (mm)' );
2594
2595 # Set an Excel chart style. Blue colors with white outline and shadow.
2596 $chart1->set_style( 11 );
2597
2598 # Insert the chart into the worksheet (with an offset).
2599 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
2600
2601
2602 #
2603 # Create a scatter chart sub-type with straight lines and markers.
2604 #
2605 my $chart2 = $workbook->add_chart(
2606 type => 'scatter',
2607 embedded => 1,
2608 subtype => 'straight_with_markers'
2609 );
2610
2611 # Configure the first series.
2612 $chart2->add_series(
2613 name => '=Sheet1!$B$1',
2614 categories => '=Sheet1!$A$2:$A$7',
2615 values => '=Sheet1!$B$2:$B$7',
2616 );
2617
2618 # Configure second series.
2619 $chart2->add_series(
2620 name => '=Sheet1!$C$1',
2621 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2622 values => [ 'Sheet1', 1, 6, 2, 2 ],
2623 );
2624
2625 # Add a chart title and some axis labels.
2626 $chart2->set_title ( name => 'Straight line with markers' );
2627 $chart2->set_x_axis( name => 'Test number' );
2628 $chart2->set_y_axis( name => 'Sample length (mm)' );
2629
2630 # Set an Excel chart style. Blue colors with white outline and shadow.
2631 $chart2->set_style( 12 );
2632
2633 # Insert the chart into the worksheet (with an offset).
2634 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
2635
2636
2637 #
2638 # Create a scatter chart sub-type with straight lines and no markers.
2639 #
2640 my $chart3 = $workbook->add_chart(
2641 type => 'scatter',
2642 embedded => 1,
2643 subtype => 'straight'
2644 );
2645
2646 # Configure the first series.
2647 $chart3->add_series(
2648 name => '=Sheet1!$B$1',
2649 categories => '=Sheet1!$A$2:$A$7',
2650 values => '=Sheet1!$B$2:$B$7',
2651 );
2652
2653 # Configure second series.
2654 $chart3->add_series(
2655 name => '=Sheet1!$C$1',
2656 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2657 values => [ 'Sheet1', 1, 6, 2, 2 ],
2658 );
2659
2660 # Add a chart title and some axis labels.
2661 $chart3->set_title ( name => 'Straight line' );
2662 $chart3->set_x_axis( name => 'Test number' );
2663 $chart3->set_y_axis( name => 'Sample length (mm)' );
2664
2665 # Set an Excel chart style. Blue colors with white outline and shadow.
2666 $chart3->set_style( 13 );
2667
2668 # Insert the chart into the worksheet (with an offset).
2669 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
2670
2671
2672 #
2673 # Create a scatter chart sub-type with smooth lines and markers.
2674 #
2675 my $chart4 = $workbook->add_chart(
2676 type => 'scatter',
2677 embedded => 1,
2678 subtype => 'smooth_with_markers'
2679 );
2680
2681 # Configure the first series.
2682 $chart4->add_series(
2683 name => '=Sheet1!$B$1',
2684 categories => '=Sheet1!$A$2:$A$7',
2685 values => '=Sheet1!$B$2:$B$7',
2686 );
2687
2688 # Configure second series.
2689 $chart4->add_series(
2690 name => '=Sheet1!$C$1',
2691 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2692 values => [ 'Sheet1', 1, 6, 2, 2 ],
2693 );
2694
2695 # Add a chart title and some axis labels.
2696 $chart4->set_title ( name => 'Smooth line with markers' );
2697 $chart4->set_x_axis( name => 'Test number' );
2698 $chart4->set_y_axis( name => 'Sample length (mm)' );
2699
2700 # Set an Excel chart style. Blue colors with white outline and shadow.
2701 $chart4->set_style( 14 );
2702
2703 # Insert the chart into the worksheet (with an offset).
2704 $worksheet->insert_chart( 'D51', $chart4, { x_offset => 25, y_offset => 10 } );
2705
2706
2707 #
2708 # Create a scatter chart sub-type with smooth lines and no markers.
2709 #
2710 my $chart5 = $workbook->add_chart(
2711 type => 'scatter',
2712 embedded => 1,
2713 subtype => 'smooth'
2714 );
2715
2716 # Configure the first series.
2717 $chart5->add_series(
2718 name => '=Sheet1!$B$1',
2719 categories => '=Sheet1!$A$2:$A$7',
2720 values => '=Sheet1!$B$2:$B$7',
2721 );
2722
2723 # Configure second series.
2724 $chart5->add_series(
2725 name => '=Sheet1!$C$1',
2726 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2727 values => [ 'Sheet1', 1, 6, 2, 2 ],
2728 );
2729
2730 # Add a chart title and some axis labels.
2731 $chart5->set_title ( name => 'Smooth line' );
2732 $chart5->set_x_axis( name => 'Test number' );
2733 $chart5->set_y_axis( name => 'Sample length (mm)' );
2734
2735 # Set an Excel chart style. Blue colors with white outline and shadow.
2736 $chart5->set_style( 15 );
2737
2738 # Insert the chart into the worksheet (with an offset).
2739 $worksheet->insert_chart( 'D66', $chart5, { x_offset => 25, y_offset => 10 } );
2740
2741
2742 $workbook->close();
2743
2744 __END__
2745
2746 Download this example:
2747 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_scatter.pl>
2748
2749 Example: chart_secondary_axis.pl
2750 A demo of a Line chart with a secondary axis in Excel::Writer::XLSX.
2751
2752 Source code for this example:
2753
2754 #!/usr/bin/perl
2755
2756 #######################################################################
2757 #
2758 # A demo of a Line chart with a secondary axis in Excel::Writer::XLSX.
2759 #
2760 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
2761 #
2762
2763 use strict;
2764 use warnings;
2765 use Excel::Writer::XLSX;
2766
2767 my $workbook = Excel::Writer::XLSX->new( 'chart_secondary_axis.xlsx' );
2768 my $worksheet = $workbook->add_worksheet();
2769 my $bold = $workbook->add_format( bold => 1 );
2770
2771 # Add the worksheet data that the charts will refer to.
2772 my $headings = [ 'Aliens', 'Humans', ];
2773 my $data = [
2774 [ 2, 3, 4, 5, 6, 7 ],
2775 [ 10, 40, 50, 20, 10, 50 ],
2776
2777 ];
2778
2779
2780 $worksheet->write( 'A1', $headings, $bold );
2781 $worksheet->write( 'A2', $data );
2782
2783 # Create a new chart object. In this case an embedded chart.
2784 my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
2785
2786 # Configure a series with a secondary axis
2787 $chart->add_series(
2788 name => '=Sheet1!$A$1',
2789 values => '=Sheet1!$A$2:$A$7',
2790 y2_axis => 1,
2791 );
2792
2793 $chart->add_series(
2794 name => '=Sheet1!$B$1',
2795 values => '=Sheet1!$B$2:$B$7',
2796 );
2797
2798 $chart->set_legend( position => 'right' );
2799
2800 # Add a chart title and some axis labels.
2801 $chart->set_title( name => 'Survey results' );
2802 $chart->set_x_axis( name => 'Days', );
2803 $chart->set_y_axis( name => 'Population', major_gridlines => { visible => 0 } );
2804 $chart->set_y2_axis( name => 'Laser wounds' );
2805
2806 # Insert the chart into the worksheet (with an offset).
2807 $worksheet->insert_chart( 'D2', $chart, { x_offset => 25, y_offset => 10 } );
2808
2809 $workbook->close();
2810
2811 __END__
2812
2813 Download this example:
2814 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_secondary_axis.pl>
2815
2816 Example: chart_combined.pl
2817 An example of a Combined chart in Excel::Writer::XLSX.
2818
2819 #!/usr/bin/perl
2820
2821 #######################################################################
2822 #
2823 # An example of a Combined chart in Excel::Writer::XLSX.
2824 #
2825 # reverse ('(c)'), March 2015, John McNamara, jmcnamara@cpan.org
2826 #
2827
2828 use strict;
2829 use warnings;
2830 use Excel::Writer::XLSX;
2831
2832 my $workbook = Excel::Writer::XLSX->new( 'chart_combined.xlsx' );
2833 my $worksheet = $workbook->add_worksheet();
2834 my $bold = $workbook->add_format( bold => 1 );
2835
2836 # Add the worksheet data that the charts will refer to.
2837 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
2838 my $data = [
2839 [ 2, 3, 4, 5, 6, 7 ],
2840 [ 10, 40, 50, 20, 10, 50 ],
2841 [ 30, 60, 70, 50, 40, 30 ],
2842
2843 ];
2844
2845 $worksheet->write( 'A1', $headings, $bold );
2846 $worksheet->write( 'A2', $data );
2847
2848 #
2849 # In the first example we will create a combined column and line chart.
2850 # They will share the same X and Y axes.
2851 #
2852
2853 # Create a new column chart. This will use this as the primary chart.
2854 my $column_chart1 = $workbook->add_chart( type => 'column', embedded => 1 );
2855
2856 # Configure the data series for the primary chart.
2857 $column_chart1->add_series(
2858 name => '=Sheet1!$B$1',
2859 categories => '=Sheet1!$A$2:$A$7',
2860 values => '=Sheet1!$B$2:$B$7',
2861 );
2862
2863 # Create a new column chart. This will use this as the secondary chart.
2864 my $line_chart1 = $workbook->add_chart( type => 'line', embedded => 1 );
2865
2866 # Configure the data series for the secondary chart.
2867 $line_chart1->add_series(
2868 name => '=Sheet1!$C$1',
2869 categories => '=Sheet1!$A$2:$A$7',
2870 values => '=Sheet1!$C$2:$C$7',
2871 );
2872
2873 # Combine the charts.
2874 $column_chart1->combine( $line_chart1 );
2875
2876 # Add a chart title and some axis labels. Note, this is done via the
2877 # primary chart.
2878 $column_chart1->set_title( name => 'Combined chart - same Y axis' );
2879 $column_chart1->set_x_axis( name => 'Test number' );
2880 $column_chart1->set_y_axis( name => 'Sample length (mm)' );
2881
2882
2883 # Insert the chart into the worksheet
2884 $worksheet->insert_chart( 'E2', $column_chart1 );
2885
2886 #
2887 # In the second example we will create a similar combined column and line
2888 # chart except that the secondary chart will have a secondary Y axis.
2889 #
2890
2891 # Create a new column chart. This will use this as the primary chart.
2892 my $column_chart2 = $workbook->add_chart( type => 'column', embedded => 1 );
2893
2894 # Configure the data series for the primary chart.
2895 $column_chart2->add_series(
2896 name => '=Sheet1!$B$1',
2897 categories => '=Sheet1!$A$2:$A$7',
2898 values => '=Sheet1!$B$2:$B$7',
2899 );
2900
2901 # Create a new column chart. This will use this as the secondary chart.
2902 my $line_chart2 = $workbook->add_chart( type => 'line', embedded => 1 );
2903
2904 # Configure the data series for the secondary chart. We also set a
2905 # secondary Y axis via (y2_axis). This is the only difference between
2906 # this and the first example, apart from the axis label below.
2907 $line_chart2->add_series(
2908 name => '=Sheet1!$C$1',
2909 categories => '=Sheet1!$A$2:$A$7',
2910 values => '=Sheet1!$C$2:$C$7',
2911 y2_axis => 1,
2912 );
2913
2914 # Combine the charts.
2915 $column_chart2->combine( $line_chart2 );
2916
2917 # Add a chart title and some axis labels.
2918 $column_chart2->set_title( name => 'Combine chart - secondary Y axis' );
2919 $column_chart2->set_x_axis( name => 'Test number' );
2920 $column_chart2->set_y_axis( name => 'Sample length (mm)' );
2921
2922 # Note: the y2 properites are on the secondary chart.
2923 $line_chart2->set_y2_axis( name => 'Target length (mm)' );
2924
2925
2926 # Insert the chart into the worksheet
2927 $worksheet->insert_chart( 'E18', $column_chart2 );
2928
2929 $workbook->close();
2930
2931 __END__
2932
2933 Download this example:
2934 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_combined.pl>
2935
2936 Example: chart_pareto.pl
2937 A demo of a Pareto chart in Excel::Writer::XLSX.
2938
2939 #!/usr/bin/perl
2940
2941 #######################################################################
2942 #
2943 # A demo of a Pareto chart in Excel::Writer::XLSX.
2944 #
2945 # reverse ('(c)'), March 2015, John McNamara, jmcnamara@cpan.org
2946 #
2947
2948 use strict;
2949 use warnings;
2950 use Excel::Writer::XLSX;
2951
2952 my $workbook = Excel::Writer::XLSX->new( 'chart_pareto.xlsx' );
2953 my $worksheet = $workbook->add_worksheet();
2954
2955 # Formats used in the workbook.
2956 my $bold = $workbook->add_format( bold => 1 );
2957 my $percent_format = $workbook->add_format( num_format => '0.0%' );
2958
2959
2960 # Widen the columns for visibility.
2961 $worksheet->set_column( 'A:A', 15 );
2962 $worksheet->set_column( 'B:C', 10 );
2963
2964 # Add the worksheet data that the charts will refer to.
2965 my $headings = [ 'Reason', 'Number', 'Percentage' ];
2966
2967 my $reasons = [
2968 'Traffic', 'Child care', 'Public Transport', 'Weather',
2969 'Overslept', 'Emergency',
2970 ];
2971
2972 my $numbers = [ 60, 40, 20, 15, 10, 5 ];
2973 my $percents = [ 0.44, 0.667, 0.8, 0.9, 0.967, 1 ];
2974
2975 $worksheet->write_row( 'A1', $headings, $bold );
2976 $worksheet->write_col( 'A2', $reasons );
2977 $worksheet->write_col( 'B2', $numbers );
2978 $worksheet->write_col( 'C2', $percents, $percent_format );
2979
2980
2981 # Create a new column chart. This will be the primary chart.
2982 my $column_chart = $workbook->add_chart( type => 'column', embedded => 1 );
2983
2984 # Add a series.
2985 $column_chart->add_series(
2986 categories => '=Sheet1!$A$2:$A$7',
2987 values => '=Sheet1!$B$2:$B$7',
2988 );
2989
2990 # Add a chart title.
2991 $column_chart->set_title( name => 'Reasons for lateness' );
2992
2993 # Turn off the chart legend.
2994 $column_chart->set_legend( position => 'none' );
2995
2996 # Set the title and scale of the Y axes. Note, the secondary axis is set from
2997 # the primary chart.
2998 $column_chart->set_y_axis(
2999 name => 'Respondents (number)',
3000 min => 0,
3001 max => 120
3002 );
3003 $column_chart->set_y2_axis( max => 1 );
3004
3005 # Create a new line chart. This will be the secondary chart.
3006 my $line_chart = $workbook->add_chart( type => 'line', embedded => 1 );
3007
3008 # Add a series, on the secondary axis.
3009 $line_chart->add_series(
3010 categories => '=Sheet1!$A$2:$A$7',
3011 values => '=Sheet1!$C$2:$C$7',
3012 marker => { type => 'automatic' },
3013 y2_axis => 1,
3014 );
3015
3016
3017 # Combine the charts.
3018 $column_chart->combine( $line_chart );
3019
3020 # Insert the chart into the worksheet.
3021 $worksheet->insert_chart( 'F2', $column_chart );
3022
3023 $workbook->close();
3024
3025 __END__
3026
3027 Download this example:
3028 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_pareto.pl>
3029
3030 Example: chart_stock.pl
3031 A demo of a Stock chart in Excel::Writer::XLSX.
3032
3033 Source code for this example:
3034
3035 #!/usr/bin/perl
3036
3037 #######################################################################
3038 #
3039 # A demo of a Stock chart in Excel::Writer::XLSX.
3040 #
3041 # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
3042 #
3043
3044 use strict;
3045 use warnings;
3046 use Excel::Writer::XLSX;
3047 use Excel::Writer::XLSX;
3048
3049 my $workbook = Excel::Writer::XLSX->new( 'chart_stock.xlsx' );
3050 my $worksheet = $workbook->add_worksheet();
3051 my $bold = $workbook->add_format( bold => 1 );
3052 my $date_format = $workbook->add_format( num_format => 'dd/mm/yyyy' );
3053 my $chart = $workbook->add_chart( type => 'stock', embedded => 1 );
3054
3055
3056 # Add the worksheet data that the charts will refer to.
3057 my $headings = [ 'Date', 'High', 'Low', 'Close' ];
3058 my $data = [
3059
3060 [ '2007-01-01T', '2007-01-02T', '2007-01-03T', '2007-01-04T', '2007-01-05T' ],
3061 [ 27.2, 25.03, 19.05, 20.34, 18.5 ],
3062 [ 23.49, 19.55, 15.12, 17.84, 16.34 ],
3063 [ 25.45, 23.05, 17.32, 20.45, 17.34 ],
3064
3065 ];
3066
3067 $worksheet->write( 'A1', $headings, $bold );
3068
3069 for my $row ( 0 .. 4 ) {
3070 $worksheet->write_date_time( $row+1, 0, $data->[0]->[$row], $date_format );
3071 $worksheet->write( $row+1, 1, $data->[1]->[$row] );
3072 $worksheet->write( $row+1, 2, $data->[2]->[$row] );
3073 $worksheet->write( $row+1, 3, $data->[3]->[$row] );
3074
3075 }
3076
3077 $worksheet->set_column( 'A:D', 11 );
3078
3079 # Add a series for each of the High-Low-Close columns.
3080 $chart->add_series(
3081 categories => '=Sheet1!$A$2:$A$6',
3082 values => '=Sheet1!$B$2:$B$6',
3083 );
3084
3085 $chart->add_series(
3086 categories => '=Sheet1!$A$2:$A$6',
3087 values => '=Sheet1!$C$2:$C$6',
3088 );
3089
3090 $chart->add_series(
3091 categories => '=Sheet1!$A$2:$A$6',
3092 values => '=Sheet1!$D$2:$D$6',
3093 );
3094
3095 # Add a chart title and some axis labels.
3096 $chart->set_title ( name => 'High-Low-Close', );
3097 $chart->set_x_axis( name => 'Date', );
3098 $chart->set_y_axis( name => 'Share price', );
3099
3100
3101 $worksheet->insert_chart( 'E9', $chart );
3102
3103 $workbook->close();
3104
3105 __END__
3106
3107 Download this example:
3108 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_stock.pl>
3109
3110 Example: chart_data_table.pl
3111 A demo of an Column chart with a data table on the X-axis using
3112 Excel::Writer::XLSX.
3113
3114 Source code for this example:
3115
3116 #!/usr/bin/perl
3117
3118 #######################################################################
3119 #
3120 # A demo of an Column chart with a data table on the X-axis using
3121 # Excel::Writer::XLSX.
3122 #
3123 # reverse ('(c)'), December 2012, John McNamara, jmcnamara@cpan.org
3124 #
3125
3126 use strict;
3127 use warnings;
3128 use Excel::Writer::XLSX;
3129
3130 my $workbook = Excel::Writer::XLSX->new( 'chart_data_table.xlsx' );
3131 my $worksheet = $workbook->add_worksheet();
3132 my $bold = $workbook->add_format( bold => 1 );
3133
3134 # Add the worksheet data that the charts will refer to.
3135 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
3136 my $data = [
3137 [ 2, 3, 4, 5, 6, 7 ],
3138 [ 10, 40, 50, 20, 10, 50 ],
3139 [ 30, 60, 70, 50, 40, 30 ],
3140
3141 ];
3142
3143 $worksheet->write( 'A1', $headings, $bold );
3144 $worksheet->write( 'A2', $data );
3145
3146 # Create a column chart with a data table.
3147 my $chart1 = $workbook->add_chart( type => 'column', embedded => 1 );
3148
3149 # Configure the first series.
3150 $chart1->add_series(
3151 name => '=Sheet1!$B$1',
3152 categories => '=Sheet1!$A$2:$A$7',
3153 values => '=Sheet1!$B$2:$B$7',
3154 );
3155
3156 # Configure second series. Note alternative use of array ref to define
3157 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
3158 $chart1->add_series(
3159 name => '=Sheet1!$C$1',
3160 categories => [ 'Sheet1', 1, 6, 0, 0 ],
3161 values => [ 'Sheet1', 1, 6, 2, 2 ],
3162 );
3163
3164 # Add a chart title and some axis labels.
3165 $chart1->set_title( name => 'Chart with Data Table' );
3166 $chart1->set_x_axis( name => 'Test number' );
3167 $chart1->set_y_axis( name => 'Sample length (mm)' );
3168
3169 # Set a default data table on the X-Axis.
3170 $chart1->set_table();
3171
3172 # Insert the chart into the worksheet (with an offset).
3173 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
3174
3175
3176 #
3177 # Create a second chart.
3178 #
3179 my $chart2 = $workbook->add_chart( type => 'column', embedded => 1 );
3180
3181 # Configure the first series.
3182 $chart2->add_series(
3183 name => '=Sheet1!$B$1',
3184 categories => '=Sheet1!$A$2:$A$7',
3185 values => '=Sheet1!$B$2:$B$7',
3186 );
3187
3188 # Configure second series.
3189 $chart2->add_series(
3190 name => '=Sheet1!$C$1',
3191 categories => [ 'Sheet1', 1, 6, 0, 0 ],
3192 values => [ 'Sheet1', 1, 6, 2, 2 ],
3193 );
3194
3195 # Add a chart title and some axis labels.
3196 $chart2->set_title( name => 'Data Table with legend keys' );
3197 $chart2->set_x_axis( name => 'Test number' );
3198 $chart2->set_y_axis( name => 'Sample length (mm)' );
3199
3200 # Set a data table on the X-Axis with the legend keys showm.
3201 $chart2->set_table( show_keys => 1 );
3202
3203 # Hide the chart legend since the keys are show on the data table.
3204 $chart2->set_legend( position => 'none' );
3205
3206 # Insert the chart into the worksheet (with an offset).
3207 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
3208
3209 $workbook->close();
3210
3211 __END__
3212
3213 Download this example:
3214 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_data_table.pl>
3215
3216 Example: chart_data_tools.pl
3217 A demo of an various Excel chart data tools that are available via an
3218 Excel::Writer::XLSX chart.
3219
3220 These include, Trendlines, Data Labels, Error Bars, Drop Lines, High-
3221 Low Lines and Up-Down Bars.
3222
3223 Source code for this example:
3224
3225 #!/usr/bin/perl
3226
3227 #######################################################################
3228 #
3229 # A demo of an various Excel chart data tools that are available via
3230 # an Excel::Writer::XLSX chart.
3231 #
3232 # These include, Trendlines, Data Labels, Error Bars, Drop Lines,
3233 # High-Low Lines and Up-Down Bars.
3234 #
3235 # reverse ('(c)'), December 2012, John McNamara, jmcnamara@cpan.org
3236 #
3237
3238 use strict;
3239 use warnings;
3240 use Excel::Writer::XLSX;
3241
3242 my $workbook = Excel::Writer::XLSX->new( 'chart_data_tools.xlsx' );
3243 my $worksheet = $workbook->add_worksheet();
3244 my $bold = $workbook->add_format( bold => 1 );
3245
3246 # Add the worksheet data that the charts will refer to.
3247 my $headings = [ 'Number', 'Data 1', 'Data 2' ];
3248 my $data = [
3249 [ 2, 3, 4, 5, 6, 7 ],
3250 [ 10, 40, 50, 20, 10, 50 ],
3251 [ 30, 60, 70, 50, 40, 30 ],
3252
3253 ];
3254
3255 $worksheet->write( 'A1', $headings, $bold );
3256 $worksheet->write( 'A2', $data );
3257
3258
3259 #######################################################################
3260 #
3261 # Trendline example.
3262 #
3263
3264 # Create a Line chart.
3265 my $chart1 = $workbook->add_chart( type => 'line', embedded => 1 );
3266
3267 # Configure the first series with a polynomial trendline.
3268 $chart1->add_series(
3269 categories => '=Sheet1!$A$2:$A$7',
3270 values => '=Sheet1!$B$2:$B$7',
3271 trendline => {
3272 type => 'polynomial',
3273 order => 3,
3274 },
3275 );
3276
3277 # Configure the second series with a moving average trendline.
3278 $chart1->add_series(
3279 categories => '=Sheet1!$A$2:$A$7',
3280 values => '=Sheet1!$C$2:$C$7',
3281 trendline => { type => 'linear' },
3282 );
3283
3284 # Add a chart title. and some axis labels.
3285 $chart1->set_title( name => 'Chart with Trendlines' );
3286
3287 # Insert the chart into the worksheet (with an offset).
3288 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
3289
3290
3291 #######################################################################
3292 #
3293 # Data Labels and Markers example.
3294 #
3295
3296 # Create a Line chart.
3297 my $chart2 = $workbook->add_chart( type => 'line', embedded => 1 );
3298
3299 # Configure the first series.
3300 $chart2->add_series(
3301 categories => '=Sheet1!$A$2:$A$7',
3302 values => '=Sheet1!$B$2:$B$7',
3303 data_labels => { value => 1 },
3304 marker => { type => 'automatic' },
3305 );
3306
3307 # Configure the second series.
3308 $chart2->add_series(
3309 categories => '=Sheet1!$A$2:$A$7',
3310 values => '=Sheet1!$C$2:$C$7',
3311 );
3312
3313 # Add a chart title. and some axis labels.
3314 $chart2->set_title( name => 'Chart with Data Labels and Markers' );
3315
3316 # Insert the chart into the worksheet (with an offset).
3317 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
3318
3319
3320 #######################################################################
3321 #
3322 # Error Bars example.
3323 #
3324
3325 # Create a Line chart.
3326 my $chart3 = $workbook->add_chart( type => 'line', embedded => 1 );
3327
3328 # Configure the first series.
3329 $chart3->add_series(
3330 categories => '=Sheet1!$A$2:$A$7',
3331 values => '=Sheet1!$B$2:$B$7',
3332 y_error_bars => { type => 'standard_error' },
3333 );
3334
3335 # Configure the second series.
3336 $chart3->add_series(
3337 categories => '=Sheet1!$A$2:$A$7',
3338 values => '=Sheet1!$C$2:$C$7',
3339 );
3340
3341 # Add a chart title. and some axis labels.
3342 $chart3->set_title( name => 'Chart with Error Bars' );
3343
3344 # Insert the chart into the worksheet (with an offset).
3345 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
3346
3347
3348 #######################################################################
3349 #
3350 # Up-Down Bars example.
3351 #
3352
3353 # Create a Line chart.
3354 my $chart4 = $workbook->add_chart( type => 'line', embedded => 1 );
3355
3356 # Add the Up-Down Bars.
3357 $chart4->set_up_down_bars();
3358
3359 # Configure the first series.
3360 $chart4->add_series(
3361 categories => '=Sheet1!$A$2:$A$7',
3362 values => '=Sheet1!$B$2:$B$7',
3363 );
3364
3365 # Configure the second series.
3366 $chart4->add_series(
3367 categories => '=Sheet1!$A$2:$A$7',
3368 values => '=Sheet1!$C$2:$C$7',
3369 );
3370
3371 # Add a chart title. and some axis labels.
3372 $chart4->set_title( name => 'Chart with Up-Down Bars' );
3373
3374 # Insert the chart into the worksheet (with an offset).
3375 $worksheet->insert_chart( 'D50', $chart4, { x_offset => 25, y_offset => 10 } );
3376
3377
3378 #######################################################################
3379 #
3380 # High-Low Lines example.
3381 #
3382
3383 # Create a Line chart.
3384 my $chart5 = $workbook->add_chart( type => 'line', embedded => 1 );
3385
3386 # Add the High-Low lines.
3387 $chart5->set_high_low_lines();
3388
3389 # Configure the first series.
3390 $chart5->add_series(
3391 categories => '=Sheet1!$A$2:$A$7',
3392 values => '=Sheet1!$B$2:$B$7',
3393 );
3394
3395 # Configure the second series.
3396 $chart5->add_series(
3397 categories => '=Sheet1!$A$2:$A$7',
3398 values => '=Sheet1!$C$2:$C$7',
3399 );
3400
3401 # Add a chart title. and some axis labels.
3402 $chart5->set_title( name => 'Chart with High-Low Lines' );
3403
3404 # Insert the chart into the worksheet (with an offset).
3405 $worksheet->insert_chart( 'D66', $chart5, { x_offset => 25, y_offset => 10 } );
3406
3407
3408 #######################################################################
3409 #
3410 # Drop Lines example.
3411 #
3412
3413 # Create a Line chart.
3414 my $chart6 = $workbook->add_chart( type => 'line', embedded => 1 );
3415
3416 # Add Drop Lines.
3417 $chart6->set_drop_lines();
3418
3419 # Configure the first series.
3420 $chart6->add_series(
3421 categories => '=Sheet1!$A$2:$A$7',
3422 values => '=Sheet1!$B$2:$B$7',
3423 );
3424
3425 # Configure the second series.
3426 $chart6->add_series(
3427 categories => '=Sheet1!$A$2:$A$7',
3428 values => '=Sheet1!$C$2:$C$7',
3429 );
3430
3431 # Add a chart title. and some axis labels.
3432 $chart6->set_title( name => 'Chart with Drop Lines' );
3433
3434 # Insert the chart into the worksheet (with an offset).
3435 $worksheet->insert_chart( 'D82', $chart6, { x_offset => 25, y_offset => 10 } );
3436
3437 $workbook->close();
3438
3439 __END__
3440
3441 Download this example:
3442 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_data_tools.pl>
3443
3444 Example: chart_clustered.pl
3445 A demo of a clustered category chart in Excel::Writer::XLSX.
3446
3447 #!/usr/bin/perl
3448
3449 #######################################################################
3450 #
3451 # A demo of a clustered category chart in Excel::Writer::XLSX.
3452 #
3453 # reverse ('(c)'), March 2015, John McNamara, jmcnamara@cpan.org
3454 #
3455
3456 use strict;
3457 use warnings;
3458 use Excel::Writer::XLSX;
3459
3460 my $workbook = Excel::Writer::XLSX->new( 'chart_clustered.xlsx' );
3461 my $worksheet = $workbook->add_worksheet();
3462 my $bold = $workbook->add_format( bold => 1 );
3463
3464 # Add the worksheet data that the charts will refer to.
3465 my $headings = [ 'Types', 'Sub Type', 'Value 1', 'Value 2', 'Value 3' ];
3466 my $data = [
3467 [ 'Type 1', 'Sub Type A', 5000, 8000, 6000 ],
3468 [ '', 'Sub Type B', 2000, 3000, 4000 ],
3469 [ '', 'Sub Type C', 250, 1000, 2000 ],
3470 [ 'Type 2', 'Sub Type D', 6000, 6000, 6500 ],
3471 [ '', 'Sub Type E', 500, 300, 200 ],
3472 ];
3473
3474 $worksheet->write( 'A1', $headings, $bold );
3475 $worksheet->write_col( 'A2', $data );
3476
3477 # Create a new chart object. In this case an embedded chart.
3478 my $chart = $workbook->add_chart( type => 'column', embedded => 1 );
3479
3480 # Configure the series. Note, that the categories are 2D ranges (from column A
3481 # to column B). This creates the clusters. The series are shown as formula
3482 # strings for clarity but you can also use the array syntax. See the docs.
3483 $chart->add_series(
3484 name => '=Sheet1!$C$1',
3485 categories => '=Sheet1!$A$2:$B$6',
3486 values => '=Sheet1!$C$2:$C$6',
3487 );
3488
3489 $chart->add_series(
3490 name => '=Sheet1!$D$1',
3491 categories => '=Sheet1!$A$2:$B$6',
3492 values => '=Sheet1!$D$2:$D$6',
3493 );
3494
3495 $chart->add_series(
3496 name => '=Sheet1!$E$1',
3497 categories => '=Sheet1!$A$2:$B$6',
3498 values => '=Sheet1!$E$2:$E$6',
3499 );
3500
3501 # Set the Excel chart style.
3502 $chart->set_style( 37 );
3503
3504 # Turn off the legend.
3505 $chart->set_legend( position => 'none' );
3506
3507 # Insert the chart into the worksheet.
3508 $worksheet->insert_chart( 'G3', $chart );
3509
3510 $workbook->close();
3511
3512 __END__
3513
3514 Download this example:
3515 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_clustered.pl>
3516
3517 Example: chart_styles.pl
3518 An example showing all 48 default chart styles available in Excel 2007
3519 using Excel::Writer::XLSX.. Note, these styles are not the same as the
3520 styles available in Excel 2013.
3521
3522 #!/usr/bin/perl
3523
3524 #######################################################################
3525 #
3526 # An example showing all 48 default chart styles available in Excel 2007
3527 # using Excel::Writer::XLSX.. Note, these styles are not the same as the
3528 # styles available in Excel 2013.
3529 #
3530 # reverse ('(c)'), March 2015, John McNamara, jmcnamara@cpan.org
3531 #
3532
3533 use strict;
3534 use warnings;
3535 use Excel::Writer::XLSX;
3536
3537 my $workbook = Excel::Writer::XLSX->new( 'chart_styles.xlsx' );
3538
3539 # Show the styles for all of these chart types.
3540 my @chart_types = ( 'column', 'area', 'line', 'pie' );
3541
3542
3543 for my $chart_type ( @chart_types ) {
3544
3545 # Add a worksheet for each chart type.
3546 my $worksheet = $workbook->add_worksheet( ucfirst( $chart_type ) );
3547 $worksheet->set_zoom( 30 );
3548 my $style_number = 1;
3549
3550 # Create 48 charts, each with a different style.
3551 for ( my $row_num = 0 ; $row_num < 90 ; $row_num += 15 ) {
3552 for ( my $col_num = 0 ; $col_num < 64 ; $col_num += 8 ) {
3553
3554 my $chart = $workbook->add_chart(
3555 type => $chart_type,
3556 embedded => 1
3557 );
3558
3559 $chart->add_series( values => '=Data!$A$1:$A$6' );
3560 $chart->set_title( name => 'Style ' . $style_number );
3561 $chart->set_legend( none => 1 );
3562 $chart->set_style( $style_number );
3563
3564 $worksheet->insert_chart( $row_num, $col_num, $chart );
3565 $style_number++;
3566 }
3567 }
3568 }
3569
3570 # Create a worksheet with data for the charts.
3571 my $data = [ 10, 40, 50, 20, 10, 50 ];
3572 my $data_worksheet = $workbook->add_worksheet( 'Data' );
3573 $data_worksheet->write_col( 'A1', $data );
3574 $data_worksheet->hide();
3575
3576 $workbook->close();
3577
3578 __END__
3579
3580 Download this example:
3581 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_styles.pl>
3582
3583 Example: chart_gauge.pl
3584 A demo of an Gauge Chart in Excel::Writer::XLSX.
3585
3586 A Gauge Chart isn't a native chart type in Excel. It is constructed by
3587 combining a doughnut chart and a pie chart and by using some non-filled
3588 elements. This example follows the following online example of how to
3589 create a Gauge Chart in Excel:
3590 https://www.excel-easy.com/examples/gauge-chart.html
3591
3592 #!/usr/bin/perl
3593
3594 #######################################################################
3595 #
3596 # A demo of an Gauge Chart in Excel::Writer::XLSX.
3597 #
3598 # A Gauge Chart isn't a native chart type in Excel. It is constructed by
3599 # combining a doughnut chart and a pie chart and by using some non-filled
3600 # elements. This example follows the following online example of how to create
3601 # a Gauge Chart in Excel: https://www.excel-easy.com/examples/gauge-chart.html
3602 #
3603 # reverse ('(c)'), May 2019, John McNamara, jmcnamara@cpan.org
3604 #
3605
3606 use strict;
3607 use warnings;
3608 use Excel::Writer::XLSX;
3609
3610 my $workbook = Excel::Writer::XLSX->new( 'chart_gauge.xlsx' );
3611 my $worksheet = $workbook->add_worksheet();
3612
3613 my $chart_doughnut = $workbook->add_chart( type => 'doughnut', embedded => 1 );
3614 my $chart_pie = $workbook->add_chart( type => 'pie', embedded => 1 );
3615
3616 # Add some data for the Doughnut and Pie charts. This is set up so the
3617 # gauge goes from 0-100. It is initially set at 75%.
3618 $worksheet->write_col( 'H2', ['Donut', 25, 50, 25, 100] );
3619 $worksheet->write_col( 'I2', ['Pie', 75, 1, '=200-I4-I3'] );
3620
3621 # Configure the doughnut chart as the background for the gauge.
3622 $chart_doughnut->add_series(
3623 name => '=Sheet1!$H$2',
3624 values => '=Sheet1!$H$3:$H$6',
3625 points => [
3626 { fill => { color => 'green' } },
3627 { fill => { color => 'yellow' } },
3628 { fill => { color => 'red' } },
3629 { fill => { none => 1 } },
3630 ],
3631 );
3632
3633 # Rotate chart so the gauge parts are above the horizontal.
3634 $chart_doughnut->set_rotation( 270 );
3635
3636 # Turn off the chart legend.
3637 $chart_doughnut->set_legend( none => 1 );
3638
3639 # Turn off the chart fill and border.
3640 $chart_doughnut->set_chartarea(
3641 border => { none => 1 },
3642 fill => { none => 1 },
3643 );
3644
3645 # Configure the pie chart as the needle for the gauge.
3646 $chart_pie->add_series(
3647 name => '=Sheet1!$I$2',
3648 values => '=Sheet1!$I$3:$I$6',
3649 points => [
3650 { fill => { none => 1 } },
3651 { fill => { color => 'black' } },
3652 { fill => { none => 1 } },
3653 ],
3654 );
3655
3656 # Rotate the pie chart/needle to align with the doughnut/gauge.
3657 $chart_pie->set_rotation( 270 );
3658
3659 # Combine the pie and doughnut charts.
3660 $chart_doughnut->combine($chart_pie);
3661
3662 # Insert the chart into the worksheet.
3663 $worksheet->insert_chart( 'A1', $chart_doughnut );
3664
3665 $workbook->close();
3666
3667 Download this example:
3668 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/chart_gauge.pl>
3669
3670 Example: colors.pl
3671 Demonstrates Excel::Writer::XLSX's named colours and the Excel colour
3672 palette.
3673
3674 The set_custom_color() Worksheet method can be used to override one of
3675 the built-in palette values with a more suitable colour. See the main
3676 docs.
3677
3678 Source code for this example:
3679
3680 #!/usr/bin/perl -w
3681
3682 ################################################################################
3683 #
3684 # Demonstrates Excel::Writer::XLSX's named colours and the Excel colour
3685 # palette.
3686 #
3687 # The set_custom_color() Worksheet method can be used to override one of the
3688 # built-in palette values with a more suitable colour. See the main docs.
3689 #
3690 # reverse ('(c)'), March 2002, John McNamara, jmcnamara@cpan.org
3691 #
3692
3693 use strict;
3694 use Excel::Writer::XLSX;
3695
3696 my $workbook = Excel::Writer::XLSX->new( 'colors.xlsx' );
3697
3698 # Some common formats
3699 my $center = $workbook->add_format( align => 'center' );
3700 my $heading = $workbook->add_format( align => 'center', bold => 1 );
3701
3702
3703 ######################################################################
3704 #
3705 # Demonstrate the named colors.
3706 #
3707
3708 my %colors = (
3709 0x08, 'black',
3710 0x0C, 'blue',
3711 0x10, 'brown',
3712 0x0F, 'cyan',
3713 0x17, 'gray',
3714 0x11, 'green',
3715 0x0B, 'lime',
3716 0x0E, 'magenta',
3717 0x12, 'navy',
3718 0x35, 'orange',
3719 0x21, 'pink',
3720 0x14, 'purple',
3721 0x0A, 'red',
3722 0x16, 'silver',
3723 0x09, 'white',
3724 0x0D, 'yellow',
3725
3726 );
3727
3728 my $worksheet1 = $workbook->add_worksheet( 'Named colors' );
3729
3730 $worksheet1->set_column( 0, 3, 15 );
3731
3732 $worksheet1->write( 0, 0, "Index", $heading );
3733 $worksheet1->write( 0, 1, "Index", $heading );
3734 $worksheet1->write( 0, 2, "Name", $heading );
3735 $worksheet1->write( 0, 3, "Color", $heading );
3736
3737 my $i = 1;
3738
3739 while ( my ( $index, $color ) = each %colors ) {
3740 my $format = $workbook->add_format(
3741 fg_color => $color,
3742 pattern => 1,
3743 border => 1
3744 );
3745
3746 $worksheet1->write( $i + 1, 0, $index, $center );
3747 $worksheet1->write( $i + 1, 1, sprintf( "0x%02X", $index ), $center );
3748 $worksheet1->write( $i + 1, 2, $color, $center );
3749 $worksheet1->write( $i + 1, 3, '', $format );
3750 $i++;
3751 }
3752
3753
3754 ######################################################################
3755 #
3756 # Demonstrate the standard Excel colors in the range 8..63.
3757 #
3758
3759 my $worksheet2 = $workbook->add_worksheet( 'Standard colors' );
3760
3761 $worksheet2->set_column( 0, 3, 15 );
3762
3763 $worksheet2->write( 0, 0, "Index", $heading );
3764 $worksheet2->write( 0, 1, "Index", $heading );
3765 $worksheet2->write( 0, 2, "Color", $heading );
3766 $worksheet2->write( 0, 3, "Name", $heading );
3767
3768 for my $i ( 8 .. 63 ) {
3769 my $format = $workbook->add_format(
3770 fg_color => $i,
3771 pattern => 1,
3772 border => 1
3773 );
3774
3775 $worksheet2->write( ( $i - 7 ), 0, $i, $center );
3776 $worksheet2->write( ( $i - 7 ), 1, sprintf( "0x%02X", $i ), $center );
3777 $worksheet2->write( ( $i - 7 ), 2, '', $format );
3778
3779 # Add the color names
3780 if ( exists $colors{$i} ) {
3781 $worksheet2->write( ( $i - 7 ), 3, $colors{$i}, $center );
3782
3783 }
3784 }
3785
3786
3787 ######################################################################
3788 #
3789 # Demonstrate the Html colors.
3790 #
3791
3792
3793
3794 %colors = (
3795 '#000000', 'black',
3796 '#0000FF', 'blue',
3797 '#800000', 'brown',
3798 '#00FFFF', 'cyan',
3799 '#808080', 'gray',
3800 '#008000', 'green',
3801 '#00FF00', 'lime',
3802 '#FF00FF', 'magenta',
3803 '#000080', 'navy',
3804 '#FF6600', 'orange',
3805 '#FF00FF', 'pink',
3806 '#800080', 'purple',
3807 '#FF0000', 'red',
3808 '#C0C0C0', 'silver',
3809 '#FFFFFF', 'white',
3810 '#FFFF00', 'yellow',
3811 );
3812
3813 my $worksheet3 = $workbook->add_worksheet( 'Html colors' );
3814
3815 $worksheet3->set_column( 0, 3, 15 );
3816
3817 $worksheet3->write( 0, 0, "Html", $heading );
3818 $worksheet3->write( 0, 1, "Name", $heading );
3819 $worksheet3->write( 0, 2, "Color", $heading );
3820
3821 $i = 1;
3822
3823 while ( my ( $html_color, $color ) = each %colors ) {
3824 my $format = $workbook->add_format(
3825 fg_color => $html_color,
3826 pattern => 1,
3827 border => 1
3828 );
3829
3830 $worksheet3->write( $i + 1, 1, $html_color, $center );
3831 $worksheet3->write( $i + 1, 2, $color, $center );
3832 $worksheet3->write( $i + 1, 3, '', $format );
3833 $i++;
3834 }
3835
3836 $workbook->close();
3837
3838 __END__
3839
3840 Download this example:
3841 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/colors.pl>
3842
3843 Example: comments1.pl
3844 This example demonstrates writing cell comments.
3845
3846 A cell comment is indicated in Excel by a small red triangle in the
3847 upper right-hand corner of the cell.
3848
3849 For more advanced comment options see comments2.pl.
3850
3851 Source code for this example:
3852
3853 #!/usr/bin/perl
3854
3855 ###############################################################################
3856 #
3857 # This example demonstrates writing cell comments.
3858 #
3859 # A cell comment is indicated in Excel by a small red triangle in the upper
3860 # right-hand corner of the cell.
3861 #
3862 # For more advanced comment options see comments2.pl.
3863 #
3864 # reverse ('(c)'), November 2005, John McNamara, jmcnamara@cpan.org
3865 #
3866
3867 use strict;
3868 use warnings;
3869 use Excel::Writer::XLSX;
3870
3871 my $workbook = Excel::Writer::XLSX->new( 'comments1.xlsx' );
3872 my $worksheet = $workbook->add_worksheet();
3873
3874
3875 $worksheet->write( 'A1', 'Hello' );
3876 $worksheet->write_comment( 'A1', 'This is a comment' );
3877
3878 $workbook->close();
3879
3880 __END__
3881
3882 Download this example:
3883 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/comments1.pl>
3884
3885 Example: comments2.pl
3886 This example demonstrates writing cell comments.
3887
3888 A cell comment is indicated in Excel by a small red triangle in the
3889 upper right-hand corner of the cell.
3890
3891 Each of the worksheets demonstrates different features of cell
3892 comments.
3893
3894 Source code for this example:
3895
3896 #!/usr/bin/perl
3897
3898 ###############################################################################
3899 #
3900 # This example demonstrates writing cell comments.
3901 #
3902 # A cell comment is indicated in Excel by a small red triangle in the upper
3903 # right-hand corner of the cell.
3904 #
3905 # Each of the worksheets demonstrates different features of cell comments.
3906 #
3907 # reverse ('(c)'), November 2005, John McNamara, jmcnamara@cpan.org
3908 #
3909
3910 use strict;
3911 use warnings;
3912 use Excel::Writer::XLSX;
3913
3914 my $workbook = Excel::Writer::XLSX->new( 'comments2.xlsx' );
3915 my $text_wrap = $workbook->add_format( text_wrap => 1, valign => 'top' );
3916 my $worksheet1 = $workbook->add_worksheet();
3917 my $worksheet2 = $workbook->add_worksheet();
3918 my $worksheet3 = $workbook->add_worksheet();
3919 my $worksheet4 = $workbook->add_worksheet();
3920 my $worksheet5 = $workbook->add_worksheet();
3921 my $worksheet6 = $workbook->add_worksheet();
3922 my $worksheet7 = $workbook->add_worksheet();
3923 my $worksheet8 = $workbook->add_worksheet();
3924
3925
3926 # Variables that we will use in each example.
3927 my $cell_text = '';
3928 my $comment = '';
3929
3930
3931 ###############################################################################
3932 #
3933 # Example 1. Demonstrates a simple cell comments without formatting.
3934 # comments.
3935 #
3936
3937 # Set up some formatting.
3938 $worksheet1->set_column( 'C:C', 25 );
3939 $worksheet1->set_row( 2, 50 );
3940 $worksheet1->set_row( 5, 50 );
3941
3942
3943 # Simple ascii string.
3944 $cell_text = 'Hold the mouse over this cell to see the comment.';
3945
3946 $comment = 'This is a comment.';
3947
3948 $worksheet1->write( 'C3', $cell_text, $text_wrap );
3949 $worksheet1->write_comment( 'C3', $comment );
3950
3951 $cell_text = 'This is a UTF-8 string.';
3952 $comment = chr 0x263a;
3953
3954 $worksheet1->write( 'C6', $cell_text, $text_wrap );
3955 $worksheet1->write_comment( 'C6', $comment );
3956
3957
3958
3959 ###############################################################################
3960 #
3961 # Example 2. Demonstrates visible and hidden comments.
3962 #
3963
3964 # Set up some formatting.
3965 $worksheet2->set_column( 'C:C', 25 );
3966 $worksheet2->set_row( 2, 50 );
3967 $worksheet2->set_row( 5, 50 );
3968
3969
3970 $cell_text = 'This cell comment is visible.';
3971
3972 $comment = 'Hello.';
3973
3974 $worksheet2->write( 'C3', $cell_text, $text_wrap );
3975 $worksheet2->write_comment( 'C3', $comment, visible => 1 );
3976
3977
3978 $cell_text = "This cell comment isn't visible (the default).";
3979
3980 $comment = 'Hello.';
3981
3982 $worksheet2->write( 'C6', $cell_text, $text_wrap );
3983 $worksheet2->write_comment( 'C6', $comment );
3984
3985
3986 ###############################################################################
3987 #
3988 # Example 3. Demonstrates visible and hidden comments set at the worksheet
3989 # level.
3990 #
3991
3992 # Set up some formatting.
3993 $worksheet3->set_column( 'C:C', 25 );
3994 $worksheet3->set_row( 2, 50 );
3995 $worksheet3->set_row( 5, 50 );
3996 $worksheet3->set_row( 8, 50 );
3997
3998 # Make all comments on the worksheet visible.
3999 $worksheet3->show_comments();
4000
4001 $cell_text = 'This cell comment is visible, explicitly.';
4002
4003 $comment = 'Hello.';
4004
4005 $worksheet3->write( 'C3', $cell_text, $text_wrap );
4006 $worksheet3->write_comment( 'C3', $comment, visible => 1 );
4007
4008
4009 $cell_text =
4010 'This cell comment is also visible because ' . 'we used show_comments().';
4011
4012 $comment = 'Hello.';
4013
4014 $worksheet3->write( 'C6', $cell_text, $text_wrap );
4015 $worksheet3->write_comment( 'C6', $comment );
4016
4017
4018 $cell_text = 'However, we can still override it locally.';
4019
4020 $comment = 'Hello.';
4021
4022 $worksheet3->write( 'C9', $cell_text, $text_wrap );
4023 $worksheet3->write_comment( 'C9', $comment, visible => 0 );
4024
4025
4026 ###############################################################################
4027 #
4028 # Example 4. Demonstrates changes to the comment box dimensions.
4029 #
4030
4031 # Set up some formatting.
4032 $worksheet4->set_column( 'C:C', 25 );
4033 $worksheet4->set_row( 2, 50 );
4034 $worksheet4->set_row( 5, 50 );
4035 $worksheet4->set_row( 8, 50 );
4036 $worksheet4->set_row( 15, 50 );
4037
4038 $worksheet4->show_comments();
4039
4040 $cell_text = 'This cell comment is default size.';
4041
4042 $comment = 'Hello.';
4043
4044 $worksheet4->write( 'C3', $cell_text, $text_wrap );
4045 $worksheet4->write_comment( 'C3', $comment );
4046
4047
4048 $cell_text = 'This cell comment is twice as wide.';
4049
4050 $comment = 'Hello.';
4051
4052 $worksheet4->write( 'C6', $cell_text, $text_wrap );
4053 $worksheet4->write_comment( 'C6', $comment, x_scale => 2 );
4054
4055
4056 $cell_text = 'This cell comment is twice as high.';
4057
4058 $comment = 'Hello.';
4059
4060 $worksheet4->write( 'C9', $cell_text, $text_wrap );
4061 $worksheet4->write_comment( 'C9', $comment, y_scale => 2 );
4062
4063
4064 $cell_text = 'This cell comment is scaled in both directions.';
4065
4066 $comment = 'Hello.';
4067
4068 $worksheet4->write( 'C16', $cell_text, $text_wrap );
4069 $worksheet4->write_comment( 'C16', $comment, x_scale => 1.2, y_scale => 0.8 );
4070
4071
4072 $cell_text = 'This cell comment has width and height specified in pixels.';
4073
4074 $comment = 'Hello.';
4075
4076 $worksheet4->write( 'C19', $cell_text, $text_wrap );
4077 $worksheet4->write_comment( 'C19', $comment, width => 200, height => 20 );
4078
4079
4080 ###############################################################################
4081 #
4082 # Example 5. Demonstrates changes to the cell comment position.
4083 #
4084
4085 $worksheet5->set_column( 'C:C', 25 );
4086 $worksheet5->set_row( 2, 50 );
4087 $worksheet5->set_row( 5, 50 );
4088 $worksheet5->set_row( 8, 50 );
4089 $worksheet5->set_row( 11, 50 );
4090
4091 $worksheet5->show_comments();
4092
4093 $cell_text = 'This cell comment is in the default position.';
4094
4095 $comment = 'Hello.';
4096
4097 $worksheet5->write( 'C3', $cell_text, $text_wrap );
4098 $worksheet5->write_comment( 'C3', $comment );
4099
4100
4101 $cell_text = 'This cell comment has been moved to another cell.';
4102
4103 $comment = 'Hello.';
4104
4105 $worksheet5->write( 'C6', $cell_text, $text_wrap );
4106 $worksheet5->write_comment( 'C6', $comment, start_cell => 'E4' );
4107
4108
4109 $cell_text = 'This cell comment has been moved to another cell.';
4110
4111 $comment = 'Hello.';
4112
4113 $worksheet5->write( 'C9', $cell_text, $text_wrap );
4114 $worksheet5->write_comment( 'C9', $comment, start_row => 8, start_col => 4 );
4115
4116
4117 $cell_text = 'This cell comment has been shifted within its default cell.';
4118
4119 $comment = 'Hello.';
4120
4121 $worksheet5->write( 'C12', $cell_text, $text_wrap );
4122 $worksheet5->write_comment( 'C12', $comment, x_offset => 30, y_offset => 12 );
4123
4124
4125 ###############################################################################
4126 #
4127 # Example 6. Demonstrates changes to the comment background colour.
4128 #
4129
4130 $worksheet6->set_column( 'C:C', 25 );
4131 $worksheet6->set_row( 2, 50 );
4132 $worksheet6->set_row( 5, 50 );
4133 $worksheet6->set_row( 8, 50 );
4134
4135 $worksheet6->show_comments();
4136
4137 $cell_text = 'This cell comment has a different colour.';
4138
4139 $comment = 'Hello.';
4140
4141 $worksheet6->write( 'C3', $cell_text, $text_wrap );
4142 $worksheet6->write_comment( 'C3', $comment, color => 'green' );
4143
4144
4145 $cell_text = 'This cell comment has the default colour.';
4146
4147 $comment = 'Hello.';
4148
4149 $worksheet6->write( 'C6', $cell_text, $text_wrap );
4150 $worksheet6->write_comment( 'C6', $comment );
4151
4152
4153 $cell_text = 'This cell comment has a different colour.';
4154
4155 $comment = 'Hello.';
4156
4157 $worksheet6->write( 'C9', $cell_text, $text_wrap );
4158 $worksheet6->write_comment( 'C9', $comment, color => '#FF6600' );
4159
4160
4161 ###############################################################################
4162 #
4163 # Example 7. Demonstrates how to set the cell comment author.
4164 #
4165
4166 $worksheet7->set_column( 'C:C', 30 );
4167 $worksheet7->set_row( 2, 50 );
4168 $worksheet7->set_row( 5, 50 );
4169 $worksheet7->set_row( 8, 50 );
4170
4171 my $author = '';
4172 my $cell = 'C3';
4173
4174 $cell_text = "Move the mouse over this cell and you will see 'Cell commented "
4175 . "by $author' (blank) in the status bar at the bottom";
4176
4177 $comment = 'Hello.';
4178
4179 $worksheet7->write( $cell, $cell_text, $text_wrap );
4180 $worksheet7->write_comment( $cell, $comment );
4181
4182
4183 $author = 'Perl';
4184 $cell = 'C6';
4185 $cell_text = "Move the mouse over this cell and you will see 'Cell commented "
4186 . "by $author' in the status bar at the bottom";
4187
4188 $comment = 'Hello.';
4189
4190 $worksheet7->write( $cell, $cell_text, $text_wrap );
4191 $worksheet7->write_comment( $cell, $comment, author => $author );
4192
4193
4194 $author = chr 0x20AC;
4195 $cell = 'C9';
4196 $cell_text = "Move the mouse over this cell and you will see 'Cell commented "
4197 . "by $author' in the status bar at the bottom";
4198 $comment = 'Hello.';
4199
4200 $worksheet7->write( $cell, $cell_text, $text_wrap );
4201 $worksheet7->write_comment( $cell, $comment, author => $author );
4202
4203
4204
4205
4206 ###############################################################################
4207 #
4208 # Example 8. Demonstrates the need to explicitly set the row height.
4209 #
4210
4211 # Set up some formatting.
4212 $worksheet8->set_column( 'C:C', 25 );
4213 $worksheet8->set_row( 2, 80 );
4214
4215 $worksheet8->show_comments();
4216
4217
4218 $cell_text =
4219 'The height of this row has been adjusted explicitly using '
4220 . 'set_row(). The size of the comment box is adjusted '
4221 . 'accordingly by Excel::Writer::XLSX.';
4222
4223 $comment = 'Hello.';
4224
4225 $worksheet8->write( 'C3', $cell_text, $text_wrap );
4226 $worksheet8->write_comment( 'C3', $comment );
4227
4228
4229 $cell_text =
4230 'The height of this row has been adjusted by Excel due to the '
4231 . 'text wrap property being set. Unfortunately this means that '
4232 . 'the height of the row is unknown to Excel::Writer::XLSX at '
4233 . "run time and thus the comment box is stretched as well.\n\n"
4234 . 'Use set_row() to specify the row height explicitly to avoid '
4235 . 'this problem.';
4236
4237 $comment = 'Hello.';
4238
4239 $worksheet8->write( 'C6', $cell_text, $text_wrap );
4240 $worksheet8->write_comment( 'C6', $comment );
4241
4242 $workbook->close();
4243
4244 __END__
4245
4246 Download this example:
4247 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/comments2.pl>
4248
4249 Example: conditional_format.pl
4250 Example of how to add conditional formatting to an Excel::Writer::XLSX
4251 file.
4252
4253 Conditional formatting allows you to apply a format to a cell or a
4254 range of cells based on certain criteria.
4255
4256 Source code for this example:
4257
4258 #!/usr/bin/perl
4259
4260 ###############################################################################
4261 #
4262 # Example of how to add conditional formatting to an Excel::Writer::XLSX file.
4263 #
4264 # Conditional formatting allows you to apply a format to a cell or a range of
4265 # cells based on certain criteria.
4266 #
4267 # reverse ('(c)'), October 2011, John McNamara, jmcnamara@cpan.org
4268 #
4269
4270 use strict;
4271 use warnings;
4272 use Excel::Writer::XLSX;
4273
4274 my $workbook = Excel::Writer::XLSX->new( 'conditional_format.xlsx' );
4275 my $worksheet1 = $workbook->add_worksheet();
4276 my $worksheet2 = $workbook->add_worksheet();
4277 my $worksheet3 = $workbook->add_worksheet();
4278 my $worksheet4 = $workbook->add_worksheet();
4279 my $worksheet5 = $workbook->add_worksheet();
4280 my $worksheet6 = $workbook->add_worksheet();
4281 my $worksheet7 = $workbook->add_worksheet();
4282 my $worksheet8 = $workbook->add_worksheet();
4283 my $worksheet9 = $workbook->add_worksheet();
4284
4285
4286 # Light red fill with dark red text.
4287 my $format1 = $workbook->add_format(
4288 bg_color => '#FFC7CE',
4289 color => '#9C0006',
4290
4291 );
4292
4293 # Green fill with dark green text.
4294 my $format2 = $workbook->add_format(
4295 bg_color => '#C6EFCE',
4296 color => '#006100',
4297
4298 );
4299
4300 # Some sample data to run the conditional formatting against.
4301 my $data = [
4302 [ 34, 72, 38, 30, 75, 48, 75, 66, 84, 86 ],
4303 [ 6, 24, 1, 84, 54, 62, 60, 3, 26, 59 ],
4304 [ 28, 79, 97, 13, 85, 93, 93, 22, 5, 14 ],
4305 [ 27, 71, 40, 17, 18, 79, 90, 93, 29, 47 ],
4306 [ 88, 25, 33, 23, 67, 1, 59, 79, 47, 36 ],
4307 [ 24, 100, 20, 88, 29, 33, 38, 54, 54, 88 ],
4308 [ 6, 57, 88, 28, 10, 26, 37, 7, 41, 48 ],
4309 [ 52, 78, 1, 96, 26, 45, 47, 33, 96, 36 ],
4310 [ 60, 54, 81, 66, 81, 90, 80, 93, 12, 55 ],
4311 [ 70, 5, 46, 14, 71, 19, 66, 36, 41, 21 ],
4312 ];
4313
4314
4315 ###############################################################################
4316 #
4317 # Example 1.
4318 #
4319 my $caption = 'Cells with values >= 50 are in light red. '
4320 . 'Values < 50 are in light green.';
4321
4322 # Write the data.
4323 $worksheet1->write( 'A1', $caption );
4324 $worksheet1->write_col( 'B3', $data );
4325
4326 # Write a conditional format over a range.
4327 $worksheet1->conditional_formatting( 'B3:K12',
4328 {
4329 type => 'cell',
4330 criteria => '>=',
4331 value => 50,
4332 format => $format1,
4333 }
4334 );
4335
4336 # Write another conditional format over the same range.
4337 $worksheet1->conditional_formatting( 'B3:K12',
4338 {
4339 type => 'cell',
4340 criteria => '<',
4341 value => 50,
4342 format => $format2,
4343 }
4344 );
4345
4346
4347 ###############################################################################
4348 #
4349 # Example 2.
4350 #
4351 $caption = 'Values between 30 and 70 are in light red. '
4352 . 'Values outside that range are in light green.';
4353
4354 $worksheet2->write( 'A1', $caption );
4355 $worksheet2->write_col( 'B3', $data );
4356
4357 $worksheet2->conditional_formatting( 'B3:K12',
4358 {
4359 type => 'cell',
4360 criteria => 'between',
4361 minimum => 30,
4362 maximum => 70,
4363 format => $format1,
4364 }
4365 );
4366
4367 $worksheet2->conditional_formatting( 'B3:K12',
4368 {
4369 type => 'cell',
4370 criteria => 'not between',
4371 minimum => 30,
4372 maximum => 70,
4373 format => $format2,
4374 }
4375 );
4376
4377
4378 ###############################################################################
4379 #
4380 # Example 3.
4381 #
4382 $caption = 'Duplicate values are in light red. '
4383 . 'Unique values are in light green.';
4384
4385 $worksheet3->write( 'A1', $caption );
4386 $worksheet3->write_col( 'B3', $data );
4387
4388 $worksheet3->conditional_formatting( 'B3:K12',
4389 {
4390 type => 'duplicate',
4391 format => $format1,
4392 }
4393 );
4394
4395 $worksheet3->conditional_formatting( 'B3:K12',
4396 {
4397 type => 'unique',
4398 format => $format2,
4399 }
4400 );
4401
4402
4403 ###############################################################################
4404 #
4405 # Example 4.
4406 #
4407 $caption = 'Above average values are in light red. '
4408 . 'Below average values are in light green.';
4409
4410 $worksheet4->write( 'A1', $caption );
4411 $worksheet4->write_col( 'B3', $data );
4412
4413 $worksheet4->conditional_formatting( 'B3:K12',
4414 {
4415 type => 'average',
4416 criteria => 'above',
4417 format => $format1,
4418 }
4419 );
4420
4421 $worksheet4->conditional_formatting( 'B3:K12',
4422 {
4423 type => 'average',
4424 criteria => 'below',
4425 format => $format2,
4426 }
4427 );
4428
4429
4430 ###############################################################################
4431 #
4432 # Example 5.
4433 #
4434 $caption = 'Top 10 values are in light red. '
4435 . 'Bottom 10 values are in light green.';
4436
4437 $worksheet5->write( 'A1', $caption );
4438 $worksheet5->write_col( 'B3', $data );
4439
4440 $worksheet5->conditional_formatting( 'B3:K12',
4441 {
4442 type => 'top',
4443 value => '10',
4444 format => $format1,
4445 }
4446 );
4447
4448 $worksheet5->conditional_formatting( 'B3:K12',
4449 {
4450 type => 'bottom',
4451 value => '10',
4452 format => $format2,
4453 }
4454 );
4455
4456
4457 ###############################################################################
4458 #
4459 # Example 6.
4460 #
4461 $caption = 'Cells with values >= 50 are in light red. '
4462 . 'Values < 50 are in light green. Non-contiguous ranges.';
4463
4464 # Write the data.
4465 $worksheet6->write( 'A1', $caption );
4466 $worksheet6->write_col( 'B3', $data );
4467
4468 # Write a conditional format over a range.
4469 $worksheet6->conditional_formatting( 'B3:K6,B9:K12',
4470 {
4471 type => 'cell',
4472 criteria => '>=',
4473 value => 50,
4474 format => $format1,
4475 }
4476 );
4477
4478 # Write another conditional format over the same range.
4479 $worksheet6->conditional_formatting( 'B3:K6,B9:K12',
4480 {
4481 type => 'cell',
4482 criteria => '<',
4483 value => 50,
4484 format => $format2,
4485 }
4486 );
4487
4488
4489 ###############################################################################
4490 #
4491 # Example 7.
4492 #
4493 $caption = 'Examples of color scales with default and user colors.';
4494
4495 $data = [ 1 .. 12 ];
4496
4497 $worksheet7->write( 'A1', $caption );
4498
4499 $worksheet7->write ( 'B2', "2 Color Scale" );
4500 $worksheet7->write_col( 'B3', $data );
4501
4502 $worksheet7->write ( 'D2', "2 Color Scale + user colors" );
4503 $worksheet7->write_col( 'D3', $data );
4504
4505 $worksheet7->write ( 'G2', "3 Color Scale" );
4506 $worksheet7->write_col( 'G3', $data );
4507
4508 $worksheet7->write ( 'I2', "3 Color Scale + user colors" );
4509 $worksheet7->write_col( 'I3', $data );
4510
4511
4512 $worksheet7->conditional_formatting( 'B3:B14',
4513 {
4514 type => '2_color_scale',
4515 }
4516 );
4517
4518 $worksheet7->conditional_formatting( 'D3:D14',
4519 {
4520 type => '3_color_scale',
4521 }
4522 );
4523
4524 $worksheet7->conditional_formatting( 'G3:G14',
4525 {
4526 type => '2_color_scale',
4527 min_color => "#FF0000",
4528 max_color => "#00FF00",
4529
4530 }
4531 );
4532
4533 $worksheet7->conditional_formatting( 'I3:I14',
4534 {
4535 type => '3_color_scale',
4536 min_color => "#C5D9F1",
4537 mid_color => "#8DB4E3",
4538 max_color => "#538ED5",
4539 }
4540 );
4541
4542
4543 ###############################################################################
4544 #
4545 # Example 8.
4546 #
4547 $caption = 'Examples of data bars.';
4548
4549 $data = [ 1 .. 12 ];
4550
4551 $worksheet8->write( 'A1', $caption );
4552
4553 $worksheet8->write ( 'B2', "Default data bars" );
4554 $worksheet8->write_col( 'B3', $data );
4555
4556 $worksheet8->write ( 'D2', "Bars only" );
4557 $worksheet8->write_col( 'D3', $data );
4558
4559 $worksheet8->write ( 'F2', "With user color" );
4560 $worksheet8->write_col( 'F3', $data );
4561
4562 $worksheet8->write ( 'H2', "Solid bars" );
4563 $worksheet8->write_col( 'H3', $data );
4564
4565 $worksheet8->write ( 'J2', "Right to left" );
4566 $worksheet8->write_col( 'J3', $data );
4567
4568 $data = [-1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0];
4569
4570 $worksheet8->write ( 'L2', "Excel 2010 style" );
4571 $worksheet8->write_col( 'L3', $data );
4572
4573 $worksheet8->write ( 'N2', "Negative same as positive" );
4574 $worksheet8->write_col( 'N3', $data );
4575
4576
4577 $worksheet8->conditional_formatting( 'B3:B14',
4578 {
4579 type => 'data_bar'
4580 }
4581 );
4582
4583 $worksheet8->conditional_formatting( 'D3:D14',
4584 {
4585 type => 'data_bar',
4586 bar_only => 1
4587 }
4588 );
4589
4590 $worksheet8->conditional_formatting( 'F3:F14',
4591 {
4592 type => 'data_bar',
4593 bar_color => '#63C384'
4594 }
4595 );
4596
4597 $worksheet8->conditional_formatting( 'H3:H14',
4598 {
4599 type => 'data_bar',
4600 bar_solid => 1
4601 }
4602 );
4603
4604 $worksheet8->conditional_formatting( 'J3:J14',
4605 {
4606 type => 'data_bar',
4607 bar_direction => 'right'
4608 }
4609 );
4610
4611 $worksheet8->conditional_formatting( 'L3:L14',
4612 {
4613 type => 'data_bar',
4614 data_bar_2010 => 1
4615 }
4616 );
4617
4618 $worksheet8->conditional_formatting( 'N3:N14',
4619 {
4620 type => 'data_bar',
4621 bar_negative_color_same => 1,
4622 bar_negative_border_color_same => 1
4623 }
4624 );
4625
4626
4627 ###############################################################################
4628 #
4629 # Example 9.
4630 #
4631 $caption = 'Examples of conditional formats with icon sets.';
4632
4633 $data = [
4634 [ 1, 2, 3 ],
4635 [ 1, 2, 3 ],
4636 [ 1, 2, 3 ],
4637 [ 1, 2, 3 ],
4638 [ 1, 2, 3, 4 ],
4639 [ 1, 2, 3, 4, 5 ],
4640 [ 1, 2, 3, 4, 5 ],
4641 ];
4642
4643 $worksheet9->write( 'A1', $caption );
4644 $worksheet9->write_col( 'B3', $data );
4645
4646 $worksheet9->conditional_formatting( 'B3:D3',
4647 {
4648 type => 'icon_set',
4649 icon_style => '3_traffic_lights',
4650 }
4651 );
4652
4653 $worksheet9->conditional_formatting( 'B4:D4',
4654 {
4655 type => 'icon_set',
4656 icon_style => '3_traffic_lights',
4657 reverse_icons => 1,
4658 }
4659 );
4660
4661 $worksheet9->conditional_formatting( 'B5:D5',
4662 {
4663 type => 'icon_set',
4664 icon_style => '3_traffic_lights',
4665 icons_only => 1,
4666 }
4667 );
4668
4669 $worksheet9->conditional_formatting( 'B6:D6',
4670 {
4671 type => 'icon_set',
4672 icon_style => '3_arrows',
4673 }
4674 );
4675
4676 $worksheet9->conditional_formatting( 'B7:E8',
4677 {
4678 type => 'icon_set',
4679 icon_style => '4_arrows',
4680 }
4681 );
4682
4683 $worksheet9->conditional_formatting( 'B8:F8',
4684 {
4685 type => 'icon_set',
4686 icon_style => '5_arrows',
4687 }
4688 );
4689
4690
4691 $worksheet9->conditional_formatting( 'B9:F9',
4692 {
4693 type => 'icon_set',
4694 icon_style => '5_ratings',
4695 }
4696 );
4697
4698 $workbook->close();
4699
4700 __END__
4701
4702 Download this example:
4703 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/conditional_format.pl>
4704
4705 Example: data_validate.pl
4706 Example of how to add data validation and dropdown lists to an
4707 Excel::Writer::XLSX file.
4708
4709 Data validation is a feature of Excel which allows you to restrict the
4710 data that a user enters in a cell and to display help and warning
4711 messages. It also allows you to restrict input to values in a drop down
4712 list.
4713
4714 Source code for this example:
4715
4716 #!/usr/bin/perl
4717
4718 ###############################################################################
4719 #
4720 # Example of how to add data validation and dropdown lists to an
4721 # Excel::Writer::XLSX file.
4722 #
4723 # Data validation is a feature of Excel which allows you to restrict the data
4724 # that a user enters in a cell and to display help and warning messages. It
4725 # also allows you to restrict input to values in a drop down list.
4726 #
4727 # reverse ('(c)'), August 2008, John McNamara, jmcnamara@cpan.org
4728 #
4729
4730 use strict;
4731 use warnings;
4732 use Excel::Writer::XLSX;
4733
4734 my $workbook = Excel::Writer::XLSX->new( 'data_validate.xlsx' );
4735 my $worksheet = $workbook->add_worksheet();
4736
4737 # Add a format for the header cells.
4738 my $header_format = $workbook->add_format(
4739 border => 1,
4740 bg_color => '#C6EFCE',
4741 bold => 1,
4742 text_wrap => 1,
4743 valign => 'vcenter',
4744 indent => 1,
4745 );
4746
4747 # Set up layout of the worksheet.
4748 $worksheet->set_column( 'A:A', 68 );
4749 $worksheet->set_column( 'B:B', 15 );
4750 $worksheet->set_column( 'D:D', 15 );
4751 $worksheet->set_row( 0, 36 );
4752 $worksheet->set_selection( 'B3' );
4753
4754
4755 # Write the header cells and some data that will be used in the examples.
4756 my $row = 0;
4757 my $txt;
4758 my $heading1 = 'Some examples of data validation in Excel::Writer::XLSX';
4759 my $heading2 = 'Enter values in this column';
4760 my $heading3 = 'Sample Data';
4761
4762 $worksheet->write( 'A1', $heading1, $header_format );
4763 $worksheet->write( 'B1', $heading2, $header_format );
4764 $worksheet->write( 'D1', $heading3, $header_format );
4765
4766 $worksheet->write( 'D3', [ 'Integers', 1, 10 ] );
4767 $worksheet->write( 'D4', [ 'List data', 'open', 'high', 'close' ] );
4768 $worksheet->write( 'D5', [ 'Formula', '=AND(F5=50,G5=60)', 50, 60 ] );
4769
4770
4771 #
4772 # Example 1. Limiting input to an integer in a fixed range.
4773 #
4774 $txt = 'Enter an integer between 1 and 10';
4775 $row += 2;
4776
4777 $worksheet->write( $row, 0, $txt );
4778 $worksheet->data_validation(
4779 $row, 1,
4780 {
4781 validate => 'integer',
4782 criteria => 'between',
4783 minimum => 1,
4784 maximum => 10,
4785 }
4786 );
4787
4788
4789 #
4790 # Example 2. Limiting input to an integer outside a fixed range.
4791 #
4792 $txt = 'Enter an integer that is not between 1 and 10 (using cell references)';
4793 $row += 2;
4794
4795 $worksheet->write( $row, 0, $txt );
4796 $worksheet->data_validation(
4797 $row, 1,
4798 {
4799 validate => 'integer',
4800 criteria => 'not between',
4801 minimum => '=E3',
4802 maximum => '=F3',
4803 }
4804 );
4805
4806
4807 #
4808 # Example 3. Limiting input to an integer greater than a fixed value.
4809 #
4810 $txt = 'Enter an integer greater than 0';
4811 $row += 2;
4812
4813 $worksheet->write( $row, 0, $txt );
4814 $worksheet->data_validation(
4815 $row, 1,
4816 {
4817 validate => 'integer',
4818 criteria => '>',
4819 value => 0,
4820 }
4821 );
4822
4823
4824 #
4825 # Example 4. Limiting input to an integer less than a fixed value.
4826 #
4827 $txt = 'Enter an integer less than 10';
4828 $row += 2;
4829
4830 $worksheet->write( $row, 0, $txt );
4831 $worksheet->data_validation(
4832 $row, 1,
4833 {
4834 validate => 'integer',
4835 criteria => '<',
4836 value => 10,
4837 }
4838 );
4839
4840
4841 #
4842 # Example 5. Limiting input to a decimal in a fixed range.
4843 #
4844 $txt = 'Enter a decimal between 0.1 and 0.5';
4845 $row += 2;
4846
4847 $worksheet->write( $row, 0, $txt );
4848 $worksheet->data_validation(
4849 $row, 1,
4850 {
4851 validate => 'decimal',
4852 criteria => 'between',
4853 minimum => 0.1,
4854 maximum => 0.5,
4855 }
4856 );
4857
4858
4859 #
4860 # Example 6. Limiting input to a value in a dropdown list.
4861 #
4862 $txt = 'Select a value from a drop down list';
4863 $row += 2;
4864
4865 $worksheet->write( $row, 0, $txt );
4866 $worksheet->data_validation(
4867 $row, 1,
4868 {
4869 validate => 'list',
4870 source => [ 'open', 'high', 'close' ],
4871 }
4872 );
4873
4874
4875 #
4876 # Example 6. Limiting input to a value in a dropdown list.
4877 #
4878 $txt = 'Select a value from a drop down list (using a cell range)';
4879 $row += 2;
4880
4881 $worksheet->write( $row, 0, $txt );
4882 $worksheet->data_validation(
4883 $row, 1,
4884 {
4885 validate => 'list',
4886 source => '=$E$4:$G$4',
4887 }
4888 );
4889
4890
4891 #
4892 # Example 7. Limiting input to a date in a fixed range.
4893 #
4894 $txt = 'Enter a date between 1/1/2008 and 12/12/2008';
4895 $row += 2;
4896
4897 $worksheet->write( $row, 0, $txt );
4898 $worksheet->data_validation(
4899 $row, 1,
4900 {
4901 validate => 'date',
4902 criteria => 'between',
4903 minimum => '2008-01-01T',
4904 maximum => '2008-12-12T',
4905 }
4906 );
4907
4908
4909 #
4910 # Example 8. Limiting input to a time in a fixed range.
4911 #
4912 $txt = 'Enter a time between 6:00 and 12:00';
4913 $row += 2;
4914
4915 $worksheet->write( $row, 0, $txt );
4916 $worksheet->data_validation(
4917 $row, 1,
4918 {
4919 validate => 'time',
4920 criteria => 'between',
4921 minimum => 'T06:00',
4922 maximum => 'T12:00',
4923 }
4924 );
4925
4926
4927 #
4928 # Example 9. Limiting input to a string greater than a fixed length.
4929 #
4930 $txt = 'Enter a string longer than 3 characters';
4931 $row += 2;
4932
4933 $worksheet->write( $row, 0, $txt );
4934 $worksheet->data_validation(
4935 $row, 1,
4936 {
4937 validate => 'length',
4938 criteria => '>',
4939 value => 3,
4940 }
4941 );
4942
4943
4944 #
4945 # Example 10. Limiting input based on a formula.
4946 #
4947 $txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"';
4948 $row += 2;
4949
4950 $worksheet->write( $row, 0, $txt );
4951 $worksheet->data_validation(
4952 $row, 1,
4953 {
4954 validate => 'custom',
4955 value => '=AND(F5=50,G5=60)',
4956 }
4957 );
4958
4959
4960 #
4961 # Example 11. Displaying and modify data validation messages.
4962 #
4963 $txt = 'Displays a message when you select the cell';
4964 $row += 2;
4965
4966 $worksheet->write( $row, 0, $txt );
4967 $worksheet->data_validation(
4968 $row, 1,
4969 {
4970 validate => 'integer',
4971 criteria => 'between',
4972 minimum => 1,
4973 maximum => 100,
4974 input_title => 'Enter an integer:',
4975 input_message => 'between 1 and 100',
4976 }
4977 );
4978
4979
4980 #
4981 # Example 12. Displaying and modify data validation messages.
4982 #
4983 $txt = 'Display a custom error message when integer isn\'t between 1 and 100';
4984 $row += 2;
4985
4986 $worksheet->write( $row, 0, $txt );
4987 $worksheet->data_validation(
4988 $row, 1,
4989 {
4990 validate => 'integer',
4991 criteria => 'between',
4992 minimum => 1,
4993 maximum => 100,
4994 input_title => 'Enter an integer:',
4995 input_message => 'between 1 and 100',
4996 error_title => 'Input value is not valid!',
4997 error_message => 'It should be an integer between 1 and 100',
4998 }
4999 );
5000
5001
5002 #
5003 # Example 13. Displaying and modify data validation messages.
5004 #
5005 $txt =
5006 'Display a custom information message when integer isn\'t between 1 and 100';
5007 $row += 2;
5008
5009 $worksheet->write( $row, 0, $txt );
5010 $worksheet->data_validation(
5011 $row, 1,
5012 {
5013 validate => 'integer',
5014 criteria => 'between',
5015 minimum => 1,
5016 maximum => 100,
5017 input_title => 'Enter an integer:',
5018 input_message => 'between 1 and 100',
5019 error_title => 'Input value is not valid!',
5020 error_message => 'It should be an integer between 1 and 100',
5021 error_type => 'information',
5022 }
5023 );
5024
5025 $workbook->close();
5026
5027 __END__
5028
5029 Download this example:
5030 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/data_validate.pl>
5031
5032 Example: date_time.pl
5033 Excel::Writer::XLSX example of writing dates and times using the
5034 write_date_time() Worksheet method.
5035
5036 Source code for this example:
5037
5038 #!/usr/bin/perl
5039
5040 ###############################################################################
5041 #
5042 # Excel::Writer::XLSX example of writing dates and times using the
5043 # write_date_time() Worksheet method.
5044 #
5045 # reverse ('(c)'), August 2004, John McNamara, jmcnamara@cpan.org
5046 #
5047
5048 use strict;
5049 use warnings;
5050 use Excel::Writer::XLSX;
5051
5052
5053 # Create a new workbook and add a worksheet
5054 my $workbook = Excel::Writer::XLSX->new( 'date_time.xlsx' );
5055 my $worksheet = $workbook->add_worksheet();
5056 my $bold = $workbook->add_format( bold => 1 );
5057
5058
5059 # Expand the first columns so that the date is visible.
5060 $worksheet->set_column( "A:B", 30 );
5061
5062
5063 # Write the column headers
5064 $worksheet->write( 'A1', 'Formatted date', $bold );
5065 $worksheet->write( 'B1', 'Format', $bold );
5066
5067
5068 # Examples date and time formats. In the output file compare how changing
5069 # the format codes change the appearance of the date.
5070 #
5071 my @date_formats = (
5072 'dd/mm/yy',
5073 'mm/dd/yy',
5074 '',
5075 'd mm yy',
5076 'dd mm yy',
5077 '',
5078 'dd m yy',
5079 'dd mm yy',
5080 'dd mmm yy',
5081 'dd mmmm yy',
5082 '',
5083 'dd mm y',
5084 'dd mm yyy',
5085 'dd mm yyyy',
5086 '',
5087 'd mmmm yyyy',
5088 '',
5089 'dd/mm/yy',
5090 'dd/mm/yy hh:mm',
5091 'dd/mm/yy hh:mm:ss',
5092 'dd/mm/yy hh:mm:ss.000',
5093 '',
5094 'hh:mm',
5095 'hh:mm:ss',
5096 'hh:mm:ss.000',
5097 );
5098
5099
5100 # Write the same date and time using each of the above formats. The empty
5101 # string formats create a blank line to make the example clearer.
5102 #
5103 my $row = 0;
5104 for my $date_format ( @date_formats ) {
5105 $row++;
5106 next if $date_format eq '';
5107
5108 # Create a format for the date or time.
5109 my $format = $workbook->add_format(
5110 num_format => $date_format,
5111 align => 'left'
5112 );
5113
5114 # Write the same date using different formats.
5115 $worksheet->write_date_time( $row, 0, '2004-08-01T12:30:45.123', $format );
5116 $worksheet->write( $row, 1, $date_format );
5117 }
5118
5119
5120 # The following is an example of an invalid date. It is written as a string
5121 # instead of a number. This is also Excel's default behaviour.
5122 #
5123 $row += 2;
5124 $worksheet->write_date_time( $row, 0, '2004-13-01T12:30:45.123' );
5125 $worksheet->write( $row, 1, 'Invalid date. Written as string.', $bold );
5126
5127 $workbook->close();
5128
5129 __END__
5130
5131 Download this example:
5132 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/date_time.pl>
5133
5134 Example: defined_name.pl
5135 Example of how to create defined names in an Excel::Writer::XLSX file.
5136
5137 This method is used to define a user friendly name to represent a
5138 value, a single cell or a range of cells in a workbook.
5139
5140 Source code for this example:
5141
5142 #!/usr/bin/perl
5143
5144 ##############################################################################
5145 #
5146 # Example of how to create defined names in an Excel::Writer::XLSX file.
5147 #
5148 # This method is used to define a user friendly name to represent a value,
5149 # a single cell or a range of cells in a workbook.
5150 #
5151 # reverse ('(c)'), September 2008, John McNamara, jmcnamara@cpan.org
5152 #
5153
5154 use strict;
5155 use warnings;
5156 use Excel::Writer::XLSX;
5157
5158 my $workbook = Excel::Writer::XLSX->new( 'defined_name.xlsx' );
5159 my $worksheet1 = $workbook->add_worksheet();
5160 my $worksheet2 = $workbook->add_worksheet();
5161
5162 # Define some global/workbook names.
5163 $workbook->define_name( 'Exchange_rate', '=0.96' );
5164 $workbook->define_name( 'Sales', '=Sheet1!$G$1:$H$10' );
5165
5166 # Define a local/worksheet name.
5167 $workbook->define_name( 'Sheet2!Sales', '=Sheet2!$G$1:$G$10' );
5168
5169 # Write some text in the file and one of the defined names in a formula.
5170 for my $worksheet ( $workbook->sheets() ) {
5171 $worksheet->set_column( 'A:A', 45 );
5172 $worksheet->write( 'A1', 'This worksheet contains some defined names.' );
5173 $worksheet->write( 'A2', 'See Formulas -> Name Manager above.' );
5174 $worksheet->write( 'A3', 'Example formula in cell B3 ->' );
5175
5176 $worksheet->write( 'B3', '=Exchange_rate' );
5177 }
5178
5179 $workbook->close();
5180
5181 __END__
5182
5183 Download this example:
5184 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/defined_name.pl>
5185
5186 Example: diag_border.pl
5187 A simple formatting example that demonstrates how to add a diagonal
5188 cell border with Excel::Writer::XLSX
5189
5190 Source code for this example:
5191
5192 #!/usr/bin/perl -w
5193
5194 ##############################################################################
5195 #
5196 # A simple formatting example that demonstrates how to add a diagonal cell
5197 # border with Excel::Writer::XLSX
5198 #
5199 # reverse ('(c)'), May 2004, John McNamara, jmcnamara@cpan.org
5200 #
5201
5202 use strict;
5203 use Excel::Writer::XLSX;
5204
5205
5206 my $workbook = Excel::Writer::XLSX->new( 'diag_border.xlsx' );
5207 my $worksheet = $workbook->add_worksheet();
5208
5209
5210 my $format1 = $workbook->add_format( diag_type => 1 );
5211
5212 my $format2 = $workbook->add_format( diag_type => 2 );
5213
5214 my $format3 = $workbook->add_format( diag_type => 3 );
5215
5216 my $format4 = $workbook->add_format(
5217 diag_type => 3,
5218 diag_border => 7,
5219 diag_color => 'red',
5220 );
5221
5222
5223 $worksheet->write( 'B3', 'Text', $format1 );
5224 $worksheet->write( 'B6', 'Text', $format2 );
5225 $worksheet->write( 'B9', 'Text', $format3 );
5226 $worksheet->write( 'B12', 'Text', $format4 );
5227
5228 $workbook->close();
5229
5230 __END__
5231
5232 Download this example:
5233 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/diag_border.pl>
5234
5235 Example: filehandle.pl
5236 Example of using Excel::Writer::XLSX to write Excel files to different
5237 filehandles.
5238
5239 Source code for this example:
5240
5241 #!/usr/bin/perl
5242
5243 ###############################################################################
5244 #
5245 # Example of using Excel::Writer::XLSX to write Excel files to different
5246 # filehandles.
5247 #
5248 # reverse ('(c)'), April 2003, John McNamara, jmcnamara@cpan.org
5249 #
5250
5251 use strict;
5252 use warnings;
5253 use Excel::Writer::XLSX;
5254 use IO::Scalar;
5255
5256
5257 ###############################################################################
5258 #
5259 # Example 1. This demonstrates the standard way of creating an Excel file by
5260 # specifying a file name.
5261 #
5262
5263 my $workbook1 = Excel::Writer::XLSX->new( 'fh_01.xlsx' );
5264 my $worksheet1 = $workbook1->add_worksheet();
5265
5266 $worksheet1->write( 0, 0, 'Hi Excel 1' );
5267
5268 $workbook1->close();
5269
5270
5271 ###############################################################################
5272 #
5273 # Example 2. Write an Excel file to an existing filehandle.
5274 #
5275
5276 open TEST, '>', 'fh_02.xlsx' or die "Couldn't open file: $!";
5277 binmode TEST; # Always do this regardless of whether the platform requires it.
5278
5279 my $workbook2 = Excel::Writer::XLSX->new( \*TEST );
5280 my $worksheet2 = $workbook2->add_worksheet();
5281
5282 $worksheet2->write( 0, 0, 'Hi Excel 2' );
5283
5284 $workbook2->close();
5285
5286 ###############################################################################
5287 #
5288 # Example 3. Write an Excel file to an existing OO style filehandle.
5289 #
5290
5291 my $fh = FileHandle->new( '> fh_03.xlsx' ) or die "Couldn't open file: $!";
5292
5293 binmode( $fh );
5294
5295 my $workbook3 = Excel::Writer::XLSX->new( $fh );
5296 my $worksheet3 = $workbook3->add_worksheet();
5297
5298 $worksheet3->write( 0, 0, 'Hi Excel 3' );
5299
5300 $workbook3->close();
5301
5302
5303 ###############################################################################
5304 #
5305 # Example 4. Write an Excel file to a string via IO::Scalar. Please refer to
5306 # the IO::Scalar documentation for further details.
5307 #
5308
5309 my $xlsx_str;
5310
5311 tie *XLSX, 'IO::Scalar', \$xlsx_str;
5312
5313 my $workbook4 = Excel::Writer::XLSX->new( \*XLSX );
5314 my $worksheet4 = $workbook4->add_worksheet();
5315
5316 $worksheet4->write( 0, 0, 'Hi Excel 4' );
5317 $workbook4->close(); # This is required before we use the scalar
5318
5319
5320 # The Excel file is now in $xlsx_str. As a demonstration, print it to a file.
5321 open TMP, '>', 'fh_04.xlsx' or die "Couldn't open file: $!";
5322 binmode TMP;
5323 print TMP $xlsx_str;
5324 close TMP;
5325
5326
5327 ###############################################################################
5328 #
5329 # Example 5. Write an Excel file to a string via IO::Scalar's newer interface.
5330 # Please refer to the IO::Scalar documentation for further details.
5331 #
5332 my $xlsx_str2;
5333
5334 my $fh5 = IO::Scalar->new( \$xlsx_str2 );
5335
5336 my $workbook5 = Excel::Writer::XLSX->new( $fh5 );
5337 my $worksheet5 = $workbook5->add_worksheet();
5338
5339 $worksheet5->write( 0, 0, 'Hi Excel 5' );
5340 $workbook5->close(); # This is required before we use the scalar
5341
5342 # The Excel file is now in $xlsx_str. As a demonstration, print it to a file.
5343 open TMP, '>', 'fh_05.xlsx' or die "Couldn't open file: $!";
5344 binmode TMP;
5345 print TMP $xlsx_str2;
5346 close TMP;
5347
5348 __END__
5349
5350 Download this example:
5351 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/filehandle.pl>
5352
5353 Example: headers.pl
5354 This program shows several examples of how to set up headers and
5355 footers with Excel::Writer::XLSX.
5356
5357 The control characters used in the header/footer strings are:
5358
5359 Control Category Description
5360 ======= ======== ===========
5361 &L Justification Left
5362 &C Center
5363 &R Right
5364
5365 &P Information Page number
5366 &N Total number of pages
5367 &D Date
5368 &T Time
5369 &F File name
5370 &A Worksheet name
5371
5372 &fontsize Font Font size
5373 &"font,style" Font name and style
5374 &U Single underline
5375 &E Double underline
5376 &S Strikethrough
5377 &X Superscript
5378 &Y Subscript
5379
5380 &[Picture] Images Image placeholder
5381 &G Same as &[Picture]
5382
5383 && Miscellaneous Literal ampersand &
5384
5385 See the main Excel::Writer::XLSX documentation for more information.
5386
5387 Source code for this example:
5388
5389 #!/usr/bin/perl
5390
5391 ######################################################################
5392 #
5393 # This program shows several examples of how to set up headers and
5394 # footers with Excel::Writer::XLSX.
5395 #
5396 # The control characters used in the header/footer strings are:
5397 #
5398 # Control Category Description
5399 # ======= ======== ===========
5400 # &L Justification Left
5401 # &C Center
5402 # &R Right
5403 #
5404 # &P Information Page number
5405 # &N Total number of pages
5406 # &D Date
5407 # &T Time
5408 # &F File name
5409 # &A Worksheet name
5410 #
5411 # &fontsize Font Font size
5412 # &"font,style" Font name and style
5413 # &U Single underline
5414 # &E Double underline
5415 # &S Strikethrough
5416 # &X Superscript
5417 # &Y Subscript
5418 #
5419 # &[Picture] Images Image placeholder
5420 # &G Same as &[Picture]
5421 #
5422 # && Miscellaneous Literal ampersand &
5423 #
5424 # See the main Excel::Writer::XLSX documentation for more information.
5425 #
5426 # reverse ('(c)'), March 2002, John McNamara, jmcnamara@cpan.org
5427 #
5428
5429
5430 use strict;
5431 use warnings;
5432 use Excel::Writer::XLSX;
5433
5434 my $workbook = Excel::Writer::XLSX->new( 'headers.xlsx' );
5435 my $preview = 'Select Print Preview to see the header and footer';
5436
5437
5438 ######################################################################
5439 #
5440 # A simple example to start
5441 #
5442 my $worksheet1 = $workbook->add_worksheet( 'Simple' );
5443 my $header1 = '&CHere is some centred text.';
5444 my $footer1 = '&LHere is some left aligned text.';
5445
5446 $worksheet1->set_header( $header1 );
5447 $worksheet1->set_footer( $footer1 );
5448
5449 $worksheet1->set_column( 'A:A', 50 );
5450 $worksheet1->write( 'A1', $preview );
5451
5452
5453 ######################################################################
5454 #
5455 # A simple example to start
5456 #
5457 my $worksheet2 = $workbook->add_worksheet( 'Image' );
5458 my $header2 = '&L&[Picture]';
5459
5460 # Adjust the page top margin to allow space for the header image.
5461 $worksheet2->set_margin_top(1.75);
5462
5463 $worksheet2->set_header( $header2, 0.3, {image_left => 'republic.png'});
5464
5465 $worksheet2->set_column( 'A:A', 50 );
5466 $worksheet2->write( 'A1', $preview );
5467
5468
5469 ######################################################################
5470 #
5471 # This is an example of some of the header/footer variables.
5472 #
5473 my $worksheet3 = $workbook->add_worksheet( 'Variables' );
5474 my $header3 = '&LPage &P of &N' . '&CFilename: &F' . '&RSheetname: &A';
5475 my $footer3 = '&LCurrent date: &D' . '&RCurrent time: &T';
5476
5477 $worksheet3->set_header( $header3 );
5478 $worksheet3->set_footer( $footer3 );
5479
5480 $worksheet3->set_column( 'A:A', 50 );
5481 $worksheet3->write( 'A1', $preview );
5482 $worksheet3->write( 'A21', 'Next sheet' );
5483 $worksheet3->set_h_pagebreaks( 20 );
5484
5485
5486 ######################################################################
5487 #
5488 # This example shows how to use more than one font
5489 #
5490 my $worksheet4 = $workbook->add_worksheet( 'Mixed fonts' );
5491 my $header4 = q(&C&"Courier New,Bold"Hello &"Arial,Italic"World);
5492 my $footer4 = q(&C&"Symbol"e&"Arial" = mc&X2);
5493
5494 $worksheet4->set_header( $header4 );
5495 $worksheet4->set_footer( $footer4 );
5496
5497 $worksheet4->set_column( 'A:A', 50 );
5498 $worksheet4->write( 'A1', $preview );
5499
5500
5501 ######################################################################
5502 #
5503 # Example of line wrapping
5504 #
5505 my $worksheet5 = $workbook->add_worksheet( 'Word wrap' );
5506 my $header5 = "&CHeading 1\nHeading 2";
5507
5508 $worksheet5->set_header( $header5 );
5509
5510 $worksheet5->set_column( 'A:A', 50 );
5511 $worksheet5->write( 'A1', $preview );
5512
5513
5514 ######################################################################
5515 #
5516 # Example of inserting a literal ampersand &
5517 #
5518 my $worksheet6 = $workbook->add_worksheet( 'Ampersand' );
5519 my $header6 = '&CCuriouser && Curiouser - Attorneys at Law';
5520
5521 $worksheet6->set_header( $header6 );
5522
5523 $worksheet6->set_column( 'A:A', 50 );
5524 $worksheet6->write( 'A1', $preview );
5525
5526 $workbook->close();
5527
5528 __END__
5529
5530 Download this example:
5531 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/headers.pl>
5532
5533 Example: hide_row_col.pl
5534 Example of how to hide rows and columns in Excel::Writer::XLSX. In
5535 order to hide rows without setting each one, (of approximately 1
5536 million rows), Excel uses an optimisation to hide all rows that don't
5537 have data.
5538
5539 Source code for this example:
5540
5541 #!/usr/bin/perl
5542
5543 ###############################################################################
5544 #
5545 # Example of how to hide rows and columns in Excel::Writer::XLSX. In order to
5546 # hide rows without setting each one, (of approximately 1 million rows),
5547 # Excel uses an optimisation to hide all rows that don't have data.
5548 #
5549 # reverse ('(c)'), December 2012, John McNamara, jmcnamara@cpan.org
5550 #
5551
5552 use strict;
5553 use warnings;
5554 use Excel::Writer::XLSX;
5555
5556 my $workbook = Excel::Writer::XLSX->new( 'hide_row_col.xlsx' );
5557 my $worksheet = $workbook->add_worksheet();
5558
5559
5560 # Write some data.
5561 $worksheet->write( 'D1', 'Some hidden columns.' );
5562 $worksheet->write( 'A8', 'Some hidden rows.' );
5563
5564 # Hide all rows without data.
5565 $worksheet->set_default_row( undef, 1 );
5566
5567 # Set emptys row that we do want to display. All other will be hidden.
5568 for my $row (1 .. 6) {
5569 $worksheet->set_row( $row, 15 );
5570 }
5571
5572 # Hide a range of columns.
5573 $worksheet->set_column( 'G:XFD', undef, undef, 1);
5574
5575 $workbook->close();
5576
5577 __END__
5578
5579 Download this example:
5580 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/hide_row_col.pl>
5581
5582 Example: hide_sheet.pl
5583 Example of how to hide a worksheet with Excel::Writer::XLSX.
5584
5585 Source code for this example:
5586
5587 #!/usr/bin/perl
5588
5589 #######################################################################
5590 #
5591 # Example of how to hide a worksheet with Excel::Writer::XLSX.
5592 #
5593 # reverse ('(c)'), April 2005, John McNamara, jmcnamara@cpan.org
5594 #
5595
5596 use strict;
5597 use warnings;
5598 use Excel::Writer::XLSX;
5599
5600 my $workbook = Excel::Writer::XLSX->new( 'hide_sheet.xlsx' );
5601 my $worksheet1 = $workbook->add_worksheet();
5602 my $worksheet2 = $workbook->add_worksheet();
5603 my $worksheet3 = $workbook->add_worksheet();
5604
5605 $worksheet1->set_column( 'A:A', 30 );
5606 $worksheet2->set_column( 'A:A', 30 );
5607 $worksheet3->set_column( 'A:A', 30 );
5608
5609 # Sheet2 won't be visible until it is unhidden in Excel.
5610 $worksheet2->hide();
5611
5612 $worksheet1->write( 0, 0, 'Sheet2 is hidden' );
5613 $worksheet2->write( 0, 0, "Now it's my turn to find you." );
5614 $worksheet3->write( 0, 0, 'Sheet2 is hidden' );
5615
5616 $workbook->close();
5617
5618 __END__
5619
5620 Download this example:
5621 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/hide_sheet.pl>
5622
5623 Example: hyperlink1.pl
5624 Example of how to use the Excel::Writer::XLSX module to write
5625 hyperlinks
5626
5627 See also hyperlink2.pl for worksheet URL examples.
5628
5629 Source code for this example:
5630
5631 #!/usr/bin/perl
5632
5633 ###############################################################################
5634 #
5635 # Example of how to use the Excel::Writer::XLSX module to write hyperlinks
5636 #
5637 # See also hyperlink2.pl for worksheet URL examples.
5638 #
5639 # reverse ('(c)'), May 2004, John McNamara, jmcnamara@cpan.org
5640 #
5641
5642 use strict;
5643 use warnings;
5644 use Excel::Writer::XLSX;
5645
5646 # Create a new workbook and add a worksheet
5647 my $workbook = Excel::Writer::XLSX->new( 'hyperlink.xlsx' );
5648
5649
5650 my $worksheet = $workbook->add_worksheet( 'Hyperlinks' );
5651
5652 # Format the first column
5653 $worksheet->set_column( 'A:A', 30 );
5654 $worksheet->set_selection( 'B1' );
5655
5656 # Add a user defined hyperlink format.
5657 my $red_format = $workbook->add_format(
5658 color => 'red',
5659 bold => 1,
5660 underline => 1,
5661 size => 12,
5662 );
5663
5664 # Add an alternate description string to the URL.
5665 my $str = 'Perl home.';
5666
5667 # Add a "tool tip" to the URL.
5668 my $tip = 'Get the latest Perl news here.';
5669
5670
5671 # Write some hyperlinks. Unspecified or undefined format paraamters will be
5672 # replace with the defuault Excel hyperlink style.
5673 $worksheet->write( 'A1', 'http://www.perl.com/' );
5674 $worksheet->write( 'A3', 'http://www.perl.com/', undef, $str );
5675 $worksheet->write( 'A5', 'http://www.perl.com/', undef, $str, $tip );
5676 $worksheet->write( 'A7', 'http://www.perl.com/', $red_format );
5677 $worksheet->write( 'A9', 'mailto:jmcnamara@cpan.org', undef, 'Mail me' );
5678
5679 # Write a URL that isn't a hyperlink
5680 $worksheet->write_string( 'A11', 'http://www.perl.com/' );
5681
5682 $workbook->close();
5683
5684 __END__
5685
5686 Download this example:
5687 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/hyperlink1.pl>
5688
5689 Example: hyperlink2.pl
5690 Example of how to use the Excel::Writer::XLSX module to write internal
5691 and external hyperlinks.
5692
5693 If you wish to run this program and follow the hyperlinks you should
5694 create the following directory structure:
5695
5696 C:\ -- Temp --+-- Europe
5697 |
5698 \-- Asia
5699
5700 See also hyperlink1.pl for web URL examples.
5701
5702 #!/usr/bin/perl
5703
5704 ###############################################################################
5705 #
5706 # Example of how to use the Excel::Writer::XLSX module to write internal and
5707 # external hyperlinks.
5708 #
5709 # If you wish to run this program and follow the hyperlinks you should create
5710 # the following directory structure:
5711 #
5712 # C:\ -- Temp --+-- Europe
5713 # |
5714 # \-- Asia
5715 #
5716 #
5717 # See also hyperlink1.pl for web URL examples.
5718 #
5719 # reverse ('(c)'), February 2002, John McNamara, jmcnamara@cpan.org
5720 #
5721
5722
5723 use strict;
5724 use warnings;
5725 use Excel::Writer::XLSX;
5726
5727 # Create three workbooks:
5728 # C:\Temp\Europe\Ireland.xlsx
5729 # C:\Temp\Europe\Italy.xlsx
5730 # C:\Temp\Asia\China.xlsx
5731 #
5732
5733 my $ireland = Excel::Writer::XLSX->new( 'C:\Temp\Europe\Ireland.xlsx' );
5734
5735 my $ire_links = $ireland->add_worksheet( 'Links' );
5736 my $ire_sales = $ireland->add_worksheet( 'Sales' );
5737 my $ire_data = $ireland->add_worksheet( 'Product Data' );
5738 my $ire_url_format = $ireland->get_default_url_format();
5739
5740
5741 my $italy = Excel::Writer::XLSX->new( 'C:\Temp\Europe\Italy.xlsx' );
5742
5743 my $ita_links = $italy->add_worksheet( 'Links' );
5744 my $ita_sales = $italy->add_worksheet( 'Sales' );
5745 my $ita_data = $italy->add_worksheet( 'Product Data' );
5746 my $ita_url_format = $italy->get_default_url_format();
5747
5748
5749 my $china = Excel::Writer::XLSX->new( 'C:\Temp\Asia\China.xlsx' );
5750
5751 my $cha_links = $china->add_worksheet( 'Links' );
5752 my $cha_sales = $china->add_worksheet( 'Sales' );
5753 my $cha_data = $china->add_worksheet( 'Product Data' );
5754 my $cha_url_format = $china->get_default_url_format();
5755
5756
5757 # Add an alternative format
5758 my $format = $ireland->add_format( color => 'green', bold => 1 );
5759 $ire_links->set_column( 'A:B', 25 );
5760
5761
5762 ###############################################################################
5763 #
5764 # Examples of internal links
5765 #
5766 $ire_links->write( 'A1', 'Internal links', $format );
5767
5768 # Internal link
5769 $ire_links->write_url( 'A2', 'internal:Sales!A2', $ire_url_format );
5770
5771 # Internal link to a range
5772 $ire_links->write_url( 'A3', 'internal:Sales!A3:D3', $ire_url_format );
5773
5774 # Internal link with an alternative string
5775 $ire_links->write_url( 'A4', 'internal:Sales!A4', $ire_url_format, 'Link' );
5776
5777 # Internal link with an alternative format
5778 $ire_links->write_url( 'A5', 'internal:Sales!A5', $format );
5779
5780 # Internal link with an alternative string and format
5781 $ire_links->write_url( 'A6', 'internal:Sales!A6', $ire_url_format, 'Link' );
5782
5783 # Internal link (spaces in worksheet name)
5784 $ire_links->write_url( 'A7', q{internal:'Product Data'!A7}, $ire_url_format );
5785
5786
5787 ###############################################################################
5788 #
5789 # Examples of external links
5790 #
5791 $ire_links->write( 'B1', 'External links', $format );
5792
5793 # External link to a local file
5794 $ire_links->write_url( 'B2', 'external:Italy.xlsx', $ire_url_format );
5795
5796 # External link to a local file with worksheet
5797 $ire_links->write_url( 'B3', 'external:Italy.xlsx#Sales!B3', $ire_url_format );
5798
5799 # External link to a local file with worksheet and alternative string
5800 $ire_links->write_url( 'B4', 'external:Italy.xlsx#Sales!B4', $ire_url_format, 'Link' );
5801
5802 # External link to a local file with worksheet and format
5803 $ire_links->write_url( 'B5', 'external:Italy.xlsx#Sales!B5', $format );
5804
5805 # External link to a remote file, absolute path
5806 $ire_links->write_url( 'B6', 'external:C:/Temp/Asia/China.xlsx', $ire_url_format );
5807
5808 # External link to a remote file, relative path
5809 $ire_links->write_url( 'B7', 'external:../Asia/China.xlsx', $ire_url_format );
5810
5811 # External link to a remote file with worksheet
5812 $ire_links->write_url( 'B8', 'external:C:/Temp/Asia/China.xlsx#Sales!B8', $ire_url_format );
5813
5814 # External link to a remote file with worksheet (with spaces in the name)
5815 $ire_links->write_url( 'B9', q{external:C:/Temp/Asia/China.xlsx#'Product Data'!B9}, $ire_url_format );
5816
5817
5818 ###############################################################################
5819 #
5820 # Some utility links to return to the main sheet
5821 #
5822 $ire_sales->write_url( 'A2', 'internal:Links!A2', $ire_url_format, 'Back' );
5823 $ire_sales->write_url( 'A3', 'internal:Links!A3', $ire_url_format, 'Back' );
5824 $ire_sales->write_url( 'A4', 'internal:Links!A4', $ire_url_format, 'Back' );
5825 $ire_sales->write_url( 'A5', 'internal:Links!A5', $ire_url_format, 'Back' );
5826 $ire_sales->write_url( 'A6', 'internal:Links!A6', $ire_url_format, 'Back' );
5827 $ire_data->write_url ( 'A7', 'internal:Links!A7', $ire_url_format, 'Back' );
5828
5829 $ita_links->write_url( 'A1', 'external:Ireland.xlsx#Links!B2', $ita_url_format, 'Back' );
5830 $ita_sales->write_url( 'B3', 'external:Ireland.xlsx#Links!B3', $ita_url_format, 'Back' );
5831 $ita_sales->write_url( 'B4', 'external:Ireland.xlsx#Links!B4', $ita_url_format, 'Back' );
5832 $ita_sales->write_url( 'B5', 'external:Ireland.xlsx#Links!B5', $ita_url_format, 'Back' );
5833 $cha_links->write_url( 'A1', 'external:C:/Temp/Europe/Ireland.xlsx#Links!B6', $cha_url_format, 'Back' );
5834 $cha_sales->write_url( 'B8', 'external:C:/Temp/Europe/Ireland.xlsx#Links!B8', $cha_url_format, 'Back' );
5835 $cha_data->write_url ( 'B9', 'external:C:/Temp/Europe/Ireland.xlsx#Links!B9', $cha_url_format, 'Back' );
5836
5837 $ireland->close();
5838 $italy->close();
5839 $china->close();
5840
5841 __END__
5842
5843 Download this example:
5844 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/hyperlink2.pl>
5845
5846 Example: indent.pl
5847 A simple formatting example using Excel::Writer::XLSX.
5848
5849 This program demonstrates the indentation cell format.
5850
5851 Source code for this example:
5852
5853 #!/usr/bin/perl -w
5854
5855 ##############################################################################
5856 #
5857 # A simple formatting example using Excel::Writer::XLSX.
5858 #
5859 # This program demonstrates the indentation cell format.
5860 #
5861 # reverse ('(c)'), May 2004, John McNamara, jmcnamara@cpan.org
5862 #
5863
5864
5865 use strict;
5866 use Excel::Writer::XLSX;
5867
5868 my $workbook = Excel::Writer::XLSX->new( 'indent.xlsx' );
5869
5870 my $worksheet = $workbook->add_worksheet();
5871 my $indent1 = $workbook->add_format( indent => 1 );
5872 my $indent2 = $workbook->add_format( indent => 2 );
5873
5874 $worksheet->set_column( 'A:A', 40 );
5875
5876
5877 $worksheet->write( 'A1', "This text is indented 1 level", $indent1 );
5878 $worksheet->write( 'A2', "This text is indented 2 levels", $indent2 );
5879
5880 $workbook->close();
5881
5882 __END__
5883
5884 Download this example:
5885 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/indent.pl>
5886
5887 Example: macros.pl
5888 An example of adding macros to an Excel::Writer::XLSX file using a VBA
5889 project file extracted from an existing Excel xlsm file.
5890
5891 The "extract_vba" utility supplied with Excel::Writer::XLSX can be used
5892 to extract the vbaProject.bin file.
5893
5894 An embedded macro is connected to a form button on the worksheet.
5895
5896 Source code for this example:
5897
5898 #!/usr/bin/perl
5899
5900 #######################################################################
5901 #
5902 # An example of adding macros to an Excel::Writer::XLSX file using
5903 # a VBA project file extracted from an existing Excel xlsm file.
5904 #
5905 # The C<extract_vba> utility supplied with Excel::Writer::XLSX can be
5906 # used to extract the vbaProject.bin file.
5907 #
5908 # An embedded macro is connected to a form button on the worksheet.
5909 #
5910 # reverse('(c)'), November 2012, John McNamara, jmcnamara@cpan.org
5911 #
5912
5913 use strict;
5914 use warnings;
5915 use Excel::Writer::XLSX;
5916
5917 # Note the file extension should be .xlsm.
5918 my $workbook = Excel::Writer::XLSX->new( 'macros.xlsm' );
5919 my $worksheet = $workbook->add_worksheet();
5920
5921 $worksheet->set_column( 'A:A', 30 );
5922
5923 # Add the VBA project binary.
5924 $workbook->add_vba_project( './vbaProject.bin' );
5925
5926 # Show text for the end user.
5927 $worksheet->write( 'A3', 'Press the button to say hello.' );
5928
5929 # Add a button tied to a macro in the VBA project.
5930 $worksheet->insert_button(
5931 'B3',
5932 {
5933 macro => 'say_hello',
5934 caption => 'Press Me',
5935 width => 80,
5936 height => 30
5937 }
5938 );
5939
5940 $workbook->close();
5941
5942 __END__
5943
5944 Download this example:
5945 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/macros.pl>
5946
5947 Example: merge1.pl
5948 Simple example of merging cells using the Excel::Writer::XLSX module.
5949
5950 This example merges three cells using the "Centre Across Selection"
5951 alignment which was the Excel 5 method of achieving a merge. For a more
5952 modern approach use the merge_range() worksheet method instead. See
5953 the merge3.pl - merge6.pl programs.
5954
5955 Source code for this example:
5956
5957 #!/usr/bin/perl
5958
5959 ###############################################################################
5960 #
5961 # Simple example of merging cells using the Excel::Writer::XLSX module.
5962 #
5963 # This example merges three cells using the "Centre Across Selection"
5964 # alignment which was the Excel 5 method of achieving a merge. For a more
5965 # modern approach use the merge_range() worksheet method instead.
5966 # See the merge3.pl - merge6.pl programs.
5967 #
5968 # reverse ('(c)'), August 2002, John McNamara, jmcnamara@cpan.org
5969 #
5970
5971 use strict;
5972 use warnings;
5973 use Excel::Writer::XLSX;
5974
5975 # Create a new workbook and add a worksheet
5976 my $workbook = Excel::Writer::XLSX->new( 'merge1.xlsx' );
5977 my $worksheet = $workbook->add_worksheet();
5978
5979
5980 # Increase the cell size of the merged cells to highlight the formatting.
5981 $worksheet->set_column( 'B:D', 20 );
5982 $worksheet->set_row( 2, 30 );
5983
5984
5985 # Create a merge format
5986 my $format = $workbook->add_format( center_across => 1 );
5987
5988
5989 # Only one cell should contain text, the others should be blank.
5990 $worksheet->write( 2, 1, "Center across selection", $format );
5991 $worksheet->write_blank( 2, 2, $format );
5992 $worksheet->write_blank( 2, 3, $format );
5993
5994 $workbook->close();
5995
5996 __END__
5997
5998 Download this example:
5999 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/merge1.pl>
6000
6001 Example: merge2.pl
6002 Simple example of merging cells using the Excel::Writer::XLSX module
6003
6004 This example merges three cells using the "Centre Across Selection"
6005 alignment which was the Excel 5 method of achieving a merge. For a more
6006 modern approach use the merge_range() worksheet method instead. See
6007 the merge3.pl - merge6.pl programs.
6008
6009 Source code for this example:
6010
6011 #!/usr/bin/perl
6012
6013 ###############################################################################
6014 #
6015 # Simple example of merging cells using the Excel::Writer::XLSX module
6016 #
6017 # This example merges three cells using the "Centre Across Selection"
6018 # alignment which was the Excel 5 method of achieving a merge. For a more
6019 # modern approach use the merge_range() worksheet method instead.
6020 # See the merge3.pl - merge6.pl programs.
6021 #
6022 # reverse ('(c)'), August 2002, John McNamara, jmcnamara@cpan.org
6023 #
6024
6025 use strict;
6026 use warnings;
6027 use Excel::Writer::XLSX;
6028
6029 # Create a new workbook and add a worksheet
6030 my $workbook = Excel::Writer::XLSX->new( 'merge2.xlsx' );
6031 my $worksheet = $workbook->add_worksheet();
6032
6033
6034 # Increase the cell size of the merged cells to highlight the formatting.
6035 $worksheet->set_column( 1, 2, 30 );
6036 $worksheet->set_row( 2, 40 );
6037
6038
6039 # Create a merged format
6040 my $format = $workbook->add_format(
6041 center_across => 1,
6042 bold => 1,
6043 size => 15,
6044 pattern => 1,
6045 border => 6,
6046 color => 'white',
6047 fg_color => 'green',
6048 border_color => 'yellow',
6049 align => 'vcenter',
6050 );
6051
6052
6053 # Only one cell should contain text, the others should be blank.
6054 $worksheet->write( 2, 1, "Center across selection", $format );
6055 $worksheet->write_blank( 2, 2, $format );
6056
6057 $workbook->close();
6058
6059 __END__
6060
6061 Download this example:
6062 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/merge2.pl>
6063
6064 Example: merge3.pl
6065 Example of how to use Excel::Writer::XLSX to write a hyperlink in a
6066 merged cell.
6067
6068 Source code for this example:
6069
6070 #!/usr/bin/perl
6071
6072 ###############################################################################
6073 #
6074 # Example of how to use Excel::Writer::XLSX to write a hyperlink in a
6075 # merged cell.
6076 #
6077 # reverse ('(c)'), September 2002, John McNamara, jmcnamara@cpan.org
6078 #
6079
6080 use strict;
6081 use warnings;
6082 use Excel::Writer::XLSX;
6083
6084 # Create a new workbook and add a worksheet
6085 my $workbook = Excel::Writer::XLSX->new( 'merge3.xlsx' );
6086 my $worksheet = $workbook->add_worksheet();
6087
6088
6089 # Increase the cell size of the merged cells to highlight the formatting.
6090 $worksheet->set_row( $_, 30 ) for ( 3, 6, 7 );
6091 $worksheet->set_column( 'B:D', 20 );
6092
6093
6094 ###############################################################################
6095 #
6096 # Example: Merge cells containing a hyperlink using merge_range().
6097 #
6098 my $format = $workbook->add_format(
6099 border => 1,
6100 underline => 1,
6101 color => 'blue',
6102 align => 'center',
6103 valign => 'vcenter',
6104 );
6105
6106 # Merge 3 cells
6107 $worksheet->merge_range( 'B4:D4', 'http://www.perl.com', $format );
6108
6109
6110 # Merge 3 cells over two rows
6111 $worksheet->merge_range( 'B7:D8', 'http://www.perl.com', $format );
6112
6113
6114 $workbook->close();
6115
6116 __END__
6117
6118 Download this example:
6119 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/merge3.pl>
6120
6121 Example: merge4.pl
6122 Example of how to use the Excel::Writer::XLSX merge_range() workbook
6123 method with complex formatting.
6124
6125 Source code for this example:
6126
6127 #!/usr/bin/perl
6128
6129 ###############################################################################
6130 #
6131 # Example of how to use the Excel::Writer::XLSX merge_range() workbook
6132 # method with complex formatting.
6133 #
6134 # reverse ('(c)'), September 2002, John McNamara, jmcnamara@cpan.org
6135 #
6136
6137 use strict;
6138 use warnings;
6139 use Excel::Writer::XLSX;
6140
6141 # Create a new workbook and add a worksheet
6142 my $workbook = Excel::Writer::XLSX->new( 'merge4.xlsx' );
6143 my $worksheet = $workbook->add_worksheet();
6144
6145
6146 # Increase the cell size of the merged cells to highlight the formatting.
6147 $worksheet->set_row( $_, 30 ) for ( 1 .. 11 );
6148 $worksheet->set_column( 'B:D', 20 );
6149
6150
6151 ###############################################################################
6152 #
6153 # Example 1: Text centered vertically and horizontally
6154 #
6155 my $format1 = $workbook->add_format(
6156 border => 6,
6157 bold => 1,
6158 color => 'red',
6159 valign => 'vcenter',
6160 align => 'center',
6161 );
6162
6163
6164 $worksheet->merge_range( 'B2:D3', 'Vertical and horizontal', $format1 );
6165
6166
6167 ###############################################################################
6168 #
6169 # Example 2: Text aligned to the top and left
6170 #
6171 my $format2 = $workbook->add_format(
6172 border => 6,
6173 bold => 1,
6174 color => 'red',
6175 valign => 'top',
6176 align => 'left',
6177 );
6178
6179
6180 $worksheet->merge_range( 'B5:D6', 'Aligned to the top and left', $format2 );
6181
6182
6183 ###############################################################################
6184 #
6185 # Example 3: Text aligned to the bottom and right
6186 #
6187 my $format3 = $workbook->add_format(
6188 border => 6,
6189 bold => 1,
6190 color => 'red',
6191 valign => 'bottom',
6192 align => 'right',
6193 );
6194
6195
6196 $worksheet->merge_range( 'B8:D9', 'Aligned to the bottom and right', $format3 );
6197
6198
6199 ###############################################################################
6200 #
6201 # Example 4: Text justified (i.e. wrapped) in the cell
6202 #
6203 my $format4 = $workbook->add_format(
6204 border => 6,
6205 bold => 1,
6206 color => 'red',
6207 valign => 'top',
6208 align => 'justify',
6209 );
6210
6211
6212 $worksheet->merge_range( 'B11:D12', 'Justified: ' . 'so on and ' x 18,
6213 $format4 );
6214
6215 $workbook->close();
6216
6217 __END__
6218
6219 Download this example:
6220 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/merge4.pl>
6221
6222 Example: merge5.pl
6223 Example of how to use the Excel::Writer::XLSX merge_cells() workbook
6224 method with complex formatting and rotation.
6225
6226 Source code for this example:
6227
6228 #!/usr/bin/perl
6229
6230 ###############################################################################
6231 #
6232 # Example of how to use the Excel::Writer::XLSX merge_cells() workbook
6233 # method with complex formatting and rotation.
6234 #
6235 #
6236 # reverse ('(c)'), September 2002, John McNamara, jmcnamara@cpan.org
6237 #
6238
6239 use strict;
6240 use warnings;
6241 use Excel::Writer::XLSX;
6242
6243 # Create a new workbook and add a worksheet
6244 my $workbook = Excel::Writer::XLSX->new( 'merge5.xlsx' );
6245 my $worksheet = $workbook->add_worksheet();
6246
6247
6248 # Increase the cell size of the merged cells to highlight the formatting.
6249 $worksheet->set_row( $_, 36 ) for ( 3 .. 8 );
6250 $worksheet->set_column( $_, $_, 15 ) for ( 1, 3, 5 );
6251
6252
6253 ###############################################################################
6254 #
6255 # Rotation 1, letters run from top to bottom
6256 #
6257 my $format1 = $workbook->add_format(
6258 border => 6,
6259 bold => 1,
6260 color => 'red',
6261 valign => 'vcentre',
6262 align => 'centre',
6263 rotation => 270,
6264 );
6265
6266
6267 $worksheet->merge_range( 'B4:B9', 'Rotation 270', $format1 );
6268
6269
6270 ###############################################################################
6271 #
6272 # Rotation 2, 90° anticlockwise
6273 #
6274 my $format2 = $workbook->add_format(
6275 border => 6,
6276 bold => 1,
6277 color => 'red',
6278 valign => 'vcentre',
6279 align => 'centre',
6280 rotation => 90,
6281 );
6282
6283
6284 $worksheet->merge_range( 'D4:D9', 'Rotation 90°', $format2 );
6285
6286
6287 ###############################################################################
6288 #
6289 # Rotation 3, 90° clockwise
6290 #
6291 my $format3 = $workbook->add_format(
6292 border => 6,
6293 bold => 1,
6294 color => 'red',
6295 valign => 'vcentre',
6296 align => 'centre',
6297 rotation => -90,
6298 );
6299
6300
6301 $worksheet->merge_range( 'F4:F9', 'Rotation -90°', $format3 );
6302
6303 $workbook->close();
6304
6305 __END__
6306
6307 Download this example:
6308 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/merge5.pl>
6309
6310 Example: merge6.pl
6311 Example of how to use the Excel::Writer::XLSX merge_cells() workbook
6312 method with Unicode strings.
6313
6314 Source code for this example:
6315
6316 #!/usr/bin/perl
6317
6318 ###############################################################################
6319 #
6320 # Example of how to use the Excel::Writer::XLSX merge_cells() workbook
6321 # method with Unicode strings.
6322 #
6323 #
6324 # reverse ('(c)'), December 2005, John McNamara, jmcnamara@cpan.org
6325 #
6326
6327 use strict;
6328 use warnings;
6329 use Excel::Writer::XLSX;
6330
6331 # Create a new workbook and add a worksheet
6332 my $workbook = Excel::Writer::XLSX->new( 'merge6.xlsx' );
6333 my $worksheet = $workbook->add_worksheet();
6334
6335
6336 # Increase the cell size of the merged cells to highlight the formatting.
6337 $worksheet->set_row( $_, 36 ) for 2 .. 9;
6338 $worksheet->set_column( 'B:D', 25 );
6339
6340
6341 # Format for the merged cells.
6342 my $format = $workbook->add_format(
6343 border => 6,
6344 bold => 1,
6345 color => 'red',
6346 size => 20,
6347 valign => 'vcentre',
6348 align => 'left',
6349 indent => 1,
6350 );
6351
6352
6353 ###############################################################################
6354 #
6355 # Write an Ascii string.
6356 #
6357 $worksheet->merge_range( 'B3:D4', 'ASCII: A simple string', $format );
6358
6359
6360 ###############################################################################
6361 #
6362 # Write a UTF-8 Unicode string.
6363 #
6364 my $smiley = chr 0x263a;
6365 $worksheet->merge_range( 'B6:D7', "UTF-8: A Unicode smiley $smiley", $format );
6366
6367 $workbook->close();
6368
6369 __END__
6370
6371 Download this example:
6372 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/merge6.pl>
6373
6374 Example: mod_perl1.pl
6375 Example of how to use the Excel::Writer::XLSX module to send an Excel
6376 file to a browser using mod_perl 1 and Apache
6377
6378 This module ties *XLSX directly to Apache, and with the correct
6379 content-disposition/types it will prompt the user to save the file, or
6380 open it at this location.
6381
6382 This script is a modification of the Excel::Writer::XLSX cgi.pl
6383 example.
6384
6385 Change the name of this file to Cgi.pm. Change the package location to
6386 wherever you locate this package. In the example below it is located
6387 in the Excel::Writer::XLSX directory.
6388
6389 Your httpd.conf entry for this module, should you choose to use it as a
6390 stand alone app, should look similar to the following:
6391
6392 <Location /spreadsheet-test>
6393 SetHandler perl-script
6394 PerlHandler Excel::Writer::XLSX::Cgi
6395 PerlSendHeader On
6396 </Location>
6397
6398 The PerlHandler name above and the package name below *have* to match.
6399
6400 ###############################################################################
6401 #
6402 # Example of how to use the Excel::Writer::XLSX module to send an Excel
6403 # file to a browser using mod_perl 1 and Apache
6404 #
6405 # This module ties *XLSX directly to Apache, and with the correct
6406 # content-disposition/types it will prompt the user to save
6407 # the file, or open it at this location.
6408 #
6409 # This script is a modification of the Excel::Writer::XLSX cgi.pl example.
6410 #
6411 # Change the name of this file to Cgi.pm.
6412 # Change the package location to wherever you locate this package.
6413 # In the example below it is located in the Excel::Writer::XLSX directory.
6414 #
6415 # Your httpd.conf entry for this module, should you choose to use it
6416 # as a stand alone app, should look similar to the following:
6417 #
6418 # <Location /spreadsheet-test>
6419 # SetHandler perl-script
6420 # PerlHandler Excel::Writer::XLSX::Cgi
6421 # PerlSendHeader On
6422 # </Location>
6423 #
6424 # The PerlHandler name above and the package name below *have* to match.
6425
6426 # Apr 2001, Thomas Sullivan, webmaster@860.org
6427 # Feb 2001, John McNamara, jmcnamara@cpan.org
6428
6429 package Excel::Writer::XLSX::Cgi;
6430
6431 ##########################################
6432 # Pragma Definitions
6433 ##########################################
6434 use strict;
6435
6436 ##########################################
6437 # Required Modules
6438 ##########################################
6439 use Apache::Constants qw(:common);
6440 use Apache::Request;
6441 use Apache::URI; # This may not be needed
6442 use Excel::Writer::XLSX;
6443
6444 ##########################################
6445 # Main App Body
6446 ##########################################
6447 sub handler {
6448
6449 # New apache object
6450 # Should you decide to use it.
6451 my $r = Apache::Request->new( shift );
6452
6453 # Set the filename and send the content type
6454 # This will appear when they save the spreadsheet
6455 my $filename = "cgitest.xlsx";
6456
6457 ####################################################
6458 ## Send the content type headers
6459 ####################################################
6460 print "Content-disposition: attachment;filename=$filename\n";
6461 print "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n\n";
6462
6463 ####################################################
6464 # Tie a filehandle to Apache's STDOUT.
6465 # Create a new workbook and add a worksheet.
6466 ####################################################
6467 tie *XLSX => 'Apache';
6468 binmode( *XLSX );
6469
6470 my $workbook = Excel::Writer::XLSX->new( \*XLSX );
6471 my $worksheet = $workbook->add_worksheet();
6472
6473
6474 # Set the column width for column 1
6475 $worksheet->set_column( 0, 0, 20 );
6476
6477
6478 # Create a format
6479 my $format = $workbook->add_format();
6480 $format->set_bold();
6481 $format->set_size( 15 );
6482 $format->set_color( 'blue' );
6483
6484
6485 # Write to the workbook
6486 $worksheet->write( 0, 0, "Hi Excel!", $format );
6487
6488 # You must close the workbook for Content-disposition
6489 $workbook->close();
6490 }
6491
6492 1;
6493
6494 Download this example:
6495 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/mod_perl1.pl>
6496
6497 Example: mod_perl2.pl
6498 Example of how to use the Excel::Writer::XLSX module to send an Excel
6499 file to a browser using mod_perl 2 and Apache.
6500
6501 This module ties *XLSX directly to Apache, and with the correct
6502 content-disposition/types it will prompt the user to save the file, or
6503 open it at this location.
6504
6505 This script is a modification of the Excel::Writer::XLSX cgi.pl
6506 example.
6507
6508 Change the name of this file to MP2Test.pm. Change the package
6509 location to wherever you locate this package. In the example below it
6510 is located in the Excel::Writer::XLSX directory.
6511
6512 Your httpd.conf entry for this module, should you choose to use it as a
6513 stand alone app, should look similar to the following:
6514
6515 PerlModule Apache2::RequestRec
6516 PerlModule APR::Table
6517 PerlModule Apache2::RequestIO
6518
6519 <Location /spreadsheet-test>
6520 SetHandler perl-script
6521 PerlResponseHandler Excel::Writer::XLSX::MP2Test
6522 </Location>
6523
6524 The PerlResponseHandler must match the package name below.
6525
6526 ###############################################################################
6527 #
6528 # Example of how to use the Excel::Writer::XLSX module to send an Excel
6529 # file to a browser using mod_perl 2 and Apache.
6530 #
6531 # This module ties *XLSX directly to Apache, and with the correct
6532 # content-disposition/types it will prompt the user to save
6533 # the file, or open it at this location.
6534 #
6535 # This script is a modification of the Excel::Writer::XLSX cgi.pl example.
6536 #
6537 # Change the name of this file to MP2Test.pm.
6538 # Change the package location to wherever you locate this package.
6539 # In the example below it is located in the Excel::Writer::XLSX directory.
6540 #
6541 # Your httpd.conf entry for this module, should you choose to use it
6542 # as a stand alone app, should look similar to the following:
6543 #
6544 # PerlModule Apache2::RequestRec
6545 # PerlModule APR::Table
6546 # PerlModule Apache2::RequestIO
6547 #
6548 # <Location /spreadsheet-test>
6549 # SetHandler perl-script
6550 # PerlResponseHandler Excel::Writer::XLSX::MP2Test
6551 # </Location>
6552 #
6553 # The PerlResponseHandler must match the package name below.
6554
6555 # Jun 2004, Matisse Enzer, matisse@matisse.net (mod_perl 2 version)
6556 # Apr 2001, Thomas Sullivan, webmaster@860.org
6557 # Feb 2001, John McNamara, jmcnamara@cpan.org
6558
6559 package Excel::Writer::XLSX::MP2Test;
6560
6561 ##########################################
6562 # Pragma Definitions
6563 ##########################################
6564 use strict;
6565
6566 ##########################################
6567 # Required Modules
6568 ##########################################
6569 use Apache2::Const -compile => qw( :common );
6570 use Excel::Writer::XLSX;
6571
6572 ##########################################
6573 # Main App Body
6574 ##########################################
6575 sub handler {
6576 my ( $r ) = @_; # Apache request object is passed to handler in mod_perl 2
6577
6578 # Set the filename and send the content type
6579 # This will appear when they save the spreadsheet
6580 my $filename = "mod_perl2_test.xlsx";
6581
6582 ####################################################
6583 ## Send the content type headers the mod_perl 2 way
6584 ####################################################
6585 $r->headers_out->{'Content-Disposition'} = "attachment;filename=$filename";
6586 $r->content_type( 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
6587
6588 ####################################################
6589 # Tie a filehandle to Apache's STDOUT.
6590 # Create a new workbook and add a worksheet.
6591 ####################################################
6592 tie *XLSX => $r; # The mod_perl 2 way. Tie to the Apache::RequestRec object
6593 binmode( *XLSX );
6594
6595 my $workbook = Excel::Writer::XLSX->new( \*XLSX );
6596 my $worksheet = $workbook->add_worksheet();
6597
6598
6599 # Set the column width for column 1
6600 $worksheet->set_column( 0, 0, 20 );
6601
6602
6603 # Create a format
6604 my $format = $workbook->add_format();
6605 $format->set_bold();
6606 $format->set_size( 15 );
6607 $format->set_color( 'blue' );
6608
6609
6610 # Write to the workbook
6611 $worksheet->write( 0, 0, 'Hi Excel! from ' . $r->hostname, $format );
6612
6613 # You must close the workbook for Content-disposition
6614 $workbook->close();
6615 return Apache2::Const::OK;
6616 }
6617
6618 1;
6619
6620 Download this example:
6621 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/mod_perl2.pl>
6622
6623 Example: outline.pl
6624 Example of how use Excel::Writer::XLSX to generate Excel outlines and
6625 grouping.
6626
6627 Excel allows you to group rows or columns so that they can be hidden or
6628 displayed with a single mouse click. This feature is referred to as
6629 outlines.
6630
6631 Outlines can reduce complex data down to a few salient sub-totals or
6632 summaries.
6633
6634 This feature is best viewed in Excel but the following is an ASCII
6635 representation of what a worksheet with three outlines might look like.
6636 Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
6637 level 1. The lines at the left hand side are called outline level bars.
6638
6639 ------------------------------------------
6640 1 2 3 | | A | B | C | D | ...
6641 ------------------------------------------
6642 _ | 1 | A | | | | ...
6643 | _ | 2 | B | | | | ...
6644 | | | 3 | (C) | | | | ...
6645 | | | 4 | (D) | | | | ...
6646 | - | 5 | E | | | | ...
6647 | _ | 6 | F | | | | ...
6648 | | | 7 | (G) | | | | ...
6649 | | | 8 | (H) | | | | ...
6650 | - | 9 | I | | | | ...
6651 - | . | ... | ... | ... | ... | ...
6652
6653 Clicking the minus sign on each of the level 2 outlines will collapse
6654 and hide the data as shown in the next figure. The minus sign changes
6655 to a plus sign to indicate that the data in the outline is hidden.
6656
6657 ------------------------------------------
6658 1 2 3 | | A | B | C | D | ...
6659 ------------------------------------------
6660 _ | 1 | A | | | | ...
6661 | | 2 | B | | | | ...
6662 | + | 5 | E | | | | ...
6663 | | 6 | F | | | | ...
6664 | + | 9 | I | | | | ...
6665 - | . | ... | ... | ... | ... | ...
6666
6667 Clicking on the minus sign on the level 1 outline will collapse the
6668 remaining rows as follows:
6669
6670 ------------------------------------------
6671 1 2 3 | | A | B | C | D | ...
6672 ------------------------------------------
6673 | 1 | A | | | | ...
6674 + | . | ... | ... | ... | ... | ...
6675
6676 See the main Excel::Writer::XLSX documentation for more information.
6677
6678 Source code for this example:
6679
6680 #!/usr/bin/perl
6681
6682 ###############################################################################
6683 #
6684 # Example of how use Excel::Writer::XLSX to generate Excel outlines and
6685 # grouping.
6686 #
6687 #
6688 # Excel allows you to group rows or columns so that they can be hidden or
6689 # displayed with a single mouse click. This feature is referred to as outlines.
6690 #
6691 # Outlines can reduce complex data down to a few salient sub-totals or
6692 # summaries.
6693 #
6694 # This feature is best viewed in Excel but the following is an ASCII
6695 # representation of what a worksheet with three outlines might look like.
6696 # Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
6697 # level 1. The lines at the left hand side are called outline level bars.
6698 #
6699 #
6700 # ------------------------------------------
6701 # 1 2 3 | | A | B | C | D | ...
6702 # ------------------------------------------
6703 # _ | 1 | A | | | | ...
6704 # | _ | 2 | B | | | | ...
6705 # | | | 3 | (C) | | | | ...
6706 # | | | 4 | (D) | | | | ...
6707 # | - | 5 | E | | | | ...
6708 # | _ | 6 | F | | | | ...
6709 # | | | 7 | (G) | | | | ...
6710 # | | | 8 | (H) | | | | ...
6711 # | - | 9 | I | | | | ...
6712 # - | . | ... | ... | ... | ... | ...
6713 #
6714 #
6715 # Clicking the minus sign on each of the level 2 outlines will collapse and
6716 # hide the data as shown in the next figure. The minus sign changes to a plus
6717 # sign to indicate that the data in the outline is hidden.
6718 #
6719 # ------------------------------------------
6720 # 1 2 3 | | A | B | C | D | ...
6721 # ------------------------------------------
6722 # _ | 1 | A | | | | ...
6723 # | | 2 | B | | | | ...
6724 # | + | 5 | E | | | | ...
6725 # | | 6 | F | | | | ...
6726 # | + | 9 | I | | | | ...
6727 # - | . | ... | ... | ... | ... | ...
6728 #
6729 #
6730 # Clicking on the minus sign on the level 1 outline will collapse the remaining
6731 # rows as follows:
6732 #
6733 # ------------------------------------------
6734 # 1 2 3 | | A | B | C | D | ...
6735 # ------------------------------------------
6736 # | 1 | A | | | | ...
6737 # + | . | ... | ... | ... | ... | ...
6738 #
6739 # See the main Excel::Writer::XLSX documentation for more information.
6740 #
6741 # reverse ('(c)'), April 2003, John McNamara, jmcnamara@cpan.org
6742 #
6743
6744
6745 use strict;
6746 use warnings;
6747 use Excel::Writer::XLSX;
6748
6749 # Create a new workbook and add some worksheets
6750 my $workbook = Excel::Writer::XLSX->new( 'outline.xlsx' );
6751 my $worksheet1 = $workbook->add_worksheet( 'Outlined Rows' );
6752 my $worksheet2 = $workbook->add_worksheet( 'Collapsed Rows' );
6753 my $worksheet3 = $workbook->add_worksheet( 'Outline Columns' );
6754 my $worksheet4 = $workbook->add_worksheet( 'Outline levels' );
6755
6756 # Add a general format
6757 my $bold = $workbook->add_format( bold => 1 );
6758
6759
6760 ###############################################################################
6761 #
6762 # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
6763 # functions so that it looks like the type of automatic outlines that are
6764 # generated when you use the Excel Data->SubTotals menu item.
6765 #
6766
6767
6768 # For outlines the important parameters are $hidden and $level. Rows with the
6769 # same $level are grouped together. The group will be collapsed if $hidden is
6770 # non-zero. $height and $XF are assigned default values if they are undef.
6771 #
6772 # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
6773 #
6774 $worksheet1->set_row( 1, undef, undef, 0, 2 );
6775 $worksheet1->set_row( 2, undef, undef, 0, 2 );
6776 $worksheet1->set_row( 3, undef, undef, 0, 2 );
6777 $worksheet1->set_row( 4, undef, undef, 0, 2 );
6778 $worksheet1->set_row( 5, undef, undef, 0, 1 );
6779
6780 $worksheet1->set_row( 6, undef, undef, 0, 2 );
6781 $worksheet1->set_row( 7, undef, undef, 0, 2 );
6782 $worksheet1->set_row( 8, undef, undef, 0, 2 );
6783 $worksheet1->set_row( 9, undef, undef, 0, 2 );
6784 $worksheet1->set_row( 10, undef, undef, 0, 1 );
6785
6786
6787 # Add a column format for clarity
6788 $worksheet1->set_column( 'A:A', 20 );
6789
6790 # Add the data, labels and formulas
6791 $worksheet1->write( 'A1', 'Region', $bold );
6792 $worksheet1->write( 'A2', 'North' );
6793 $worksheet1->write( 'A3', 'North' );
6794 $worksheet1->write( 'A4', 'North' );
6795 $worksheet1->write( 'A5', 'North' );
6796 $worksheet1->write( 'A6', 'North Total', $bold );
6797
6798 $worksheet1->write( 'B1', 'Sales', $bold );
6799 $worksheet1->write( 'B2', 1000 );
6800 $worksheet1->write( 'B3', 1200 );
6801 $worksheet1->write( 'B4', 900 );
6802 $worksheet1->write( 'B5', 1200 );
6803 $worksheet1->write( 'B6', '=SUBTOTAL(9,B2:B5)', $bold );
6804
6805 $worksheet1->write( 'A7', 'South' );
6806 $worksheet1->write( 'A8', 'South' );
6807 $worksheet1->write( 'A9', 'South' );
6808 $worksheet1->write( 'A10', 'South' );
6809 $worksheet1->write( 'A11', 'South Total', $bold );
6810
6811 $worksheet1->write( 'B7', 400 );
6812 $worksheet1->write( 'B8', 600 );
6813 $worksheet1->write( 'B9', 500 );
6814 $worksheet1->write( 'B10', 600 );
6815 $worksheet1->write( 'B11', '=SUBTOTAL(9,B7:B10)', $bold );
6816
6817 $worksheet1->write( 'A12', 'Grand Total', $bold );
6818 $worksheet1->write( 'B12', '=SUBTOTAL(9,B2:B10)', $bold );
6819
6820
6821 ###############################################################################
6822 #
6823 # Example 2: Create a worksheet with outlined rows. This is the same as the
6824 # previous example except that the rows are collapsed.
6825 # Note: We need to indicate the row that contains the collapsed symbol '+'
6826 # with the optional parameter, $collapsed.
6827
6828 # The group will be collapsed if $hidden is non-zero.
6829 # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
6830 #
6831 $worksheet2->set_row( 1, undef, undef, 1, 2 );
6832 $worksheet2->set_row( 2, undef, undef, 1, 2 );
6833 $worksheet2->set_row( 3, undef, undef, 1, 2 );
6834 $worksheet2->set_row( 4, undef, undef, 1, 2 );
6835 $worksheet2->set_row( 5, undef, undef, 1, 1 );
6836
6837 $worksheet2->set_row( 6, undef, undef, 1, 2 );
6838 $worksheet2->set_row( 7, undef, undef, 1, 2 );
6839 $worksheet2->set_row( 8, undef, undef, 1, 2 );
6840 $worksheet2->set_row( 9, undef, undef, 1, 2 );
6841 $worksheet2->set_row( 10, undef, undef, 1, 1 );
6842 $worksheet2->set_row( 11, undef, undef, 0, 0, 1 );
6843
6844
6845 # Add a column format for clarity
6846 $worksheet2->set_column( 'A:A', 20 );
6847
6848 # Add the data, labels and formulas
6849 $worksheet2->write( 'A1', 'Region', $bold );
6850 $worksheet2->write( 'A2', 'North' );
6851 $worksheet2->write( 'A3', 'North' );
6852 $worksheet2->write( 'A4', 'North' );
6853 $worksheet2->write( 'A5', 'North' );
6854 $worksheet2->write( 'A6', 'North Total', $bold );
6855
6856 $worksheet2->write( 'B1', 'Sales', $bold );
6857 $worksheet2->write( 'B2', 1000 );
6858 $worksheet2->write( 'B3', 1200 );
6859 $worksheet2->write( 'B4', 900 );
6860 $worksheet2->write( 'B5', 1200 );
6861 $worksheet2->write( 'B6', '=SUBTOTAL(9,B2:B5)', $bold );
6862
6863 $worksheet2->write( 'A7', 'South' );
6864 $worksheet2->write( 'A8', 'South' );
6865 $worksheet2->write( 'A9', 'South' );
6866 $worksheet2->write( 'A10', 'South' );
6867 $worksheet2->write( 'A11', 'South Total', $bold );
6868
6869 $worksheet2->write( 'B7', 400 );
6870 $worksheet2->write( 'B8', 600 );
6871 $worksheet2->write( 'B9', 500 );
6872 $worksheet2->write( 'B10', 600 );
6873 $worksheet2->write( 'B11', '=SUBTOTAL(9,B7:B10)', $bold );
6874
6875 $worksheet2->write( 'A12', 'Grand Total', $bold );
6876 $worksheet2->write( 'B12', '=SUBTOTAL(9,B2:B10)', $bold );
6877
6878
6879 ###############################################################################
6880 #
6881 # Example 3: Create a worksheet with outlined columns.
6882 #
6883 my $data = [
6884 [ 'Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ' Total' ],
6885 [ 'North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)' ],
6886 [ 'South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)' ],
6887 [ 'East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)' ],
6888 [ 'West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G5)' ],
6889 ];
6890
6891 # Add bold format to the first row
6892 $worksheet3->set_row( 0, undef, $bold );
6893
6894 # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
6895 $worksheet3->set_column( 'A:A', 10, $bold );
6896 $worksheet3->set_column( 'B:G', 5, undef, 0, 1 );
6897 $worksheet3->set_column( 'H:H', 10 );
6898
6899 # Write the data and a formula
6900 $worksheet3->write_col( 'A1', $data );
6901 $worksheet3->write( 'H6', '=SUM(H2:H5)', $bold );
6902
6903
6904 ###############################################################################
6905 #
6906 # Example 4: Show all possible outline levels.
6907 #
6908 my $levels = [
6909 "Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6",
6910 "Level 7", "Level 6", "Level 5", "Level 4", "Level 3", "Level 2",
6911 "Level 1"
6912 ];
6913
6914
6915 $worksheet4->write_col( 'A1', $levels );
6916
6917 $worksheet4->set_row( 0, undef, undef, undef, 1 );
6918 $worksheet4->set_row( 1, undef, undef, undef, 2 );
6919 $worksheet4->set_row( 2, undef, undef, undef, 3 );
6920 $worksheet4->set_row( 3, undef, undef, undef, 4 );
6921 $worksheet4->set_row( 4, undef, undef, undef, 5 );
6922 $worksheet4->set_row( 5, undef, undef, undef, 6 );
6923 $worksheet4->set_row( 6, undef, undef, undef, 7 );
6924 $worksheet4->set_row( 7, undef, undef, undef, 6 );
6925 $worksheet4->set_row( 8, undef, undef, undef, 5 );
6926 $worksheet4->set_row( 9, undef, undef, undef, 4 );
6927 $worksheet4->set_row( 10, undef, undef, undef, 3 );
6928 $worksheet4->set_row( 11, undef, undef, undef, 2 );
6929 $worksheet4->set_row( 12, undef, undef, undef, 1 );
6930
6931 $workbook->close();
6932
6933 __END__
6934
6935 Download this example:
6936 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/outline.pl>
6937
6938 Example: outline_collapsed.pl
6939 Example of how to use Excel::Writer::XLSX to generate Excel outlines
6940 and grouping.
6941
6942 These examples focus mainly on collapsed outlines. See also the
6943 outlines.pl example program for more general examples.
6944
6945 Source code for this example:
6946
6947 #!/usr/bin/perl
6948
6949 ###############################################################################
6950 #
6951 # Example of how to use Excel::Writer::XLSX to generate Excel outlines and
6952 # grouping.
6953 #
6954 # These examples focus mainly on collapsed outlines. See also the
6955 # outlines.pl example program for more general examples.
6956 #
6957 # reverse ('(c)'), March 2008, John McNamara, jmcnamara@cpan.org
6958 #
6959
6960 use strict;
6961 use warnings;
6962 use Excel::Writer::XLSX;
6963
6964 # Create a new workbook and add some worksheets
6965 my $workbook = Excel::Writer::XLSX->new( 'outline_collapsed.xlsx' );
6966 my $worksheet1 = $workbook->add_worksheet( 'Outlined Rows' );
6967 my $worksheet2 = $workbook->add_worksheet( 'Collapsed Rows 1' );
6968 my $worksheet3 = $workbook->add_worksheet( 'Collapsed Rows 2' );
6969 my $worksheet4 = $workbook->add_worksheet( 'Collapsed Rows 3' );
6970 my $worksheet5 = $workbook->add_worksheet( 'Outline Columns' );
6971 my $worksheet6 = $workbook->add_worksheet( 'Collapsed Columns' );
6972
6973
6974 # Add a general format
6975 my $bold = $workbook->add_format( bold => 1 );
6976
6977
6978 #
6979 # This function will generate the same data and sub-totals on each worksheet.
6980 #
6981 sub create_sub_totals {
6982
6983 my $worksheet = $_[0];
6984
6985 # Add a column format for clarity
6986 $worksheet->set_column( 'A:A', 20 );
6987
6988 # Add the data, labels and formulas
6989 $worksheet->write( 'A1', 'Region', $bold );
6990 $worksheet->write( 'A2', 'North' );
6991 $worksheet->write( 'A3', 'North' );
6992 $worksheet->write( 'A4', 'North' );
6993 $worksheet->write( 'A5', 'North' );
6994 $worksheet->write( 'A6', 'North Total', $bold );
6995
6996 $worksheet->write( 'B1', 'Sales', $bold );
6997 $worksheet->write( 'B2', 1000 );
6998 $worksheet->write( 'B3', 1200 );
6999 $worksheet->write( 'B4', 900 );
7000 $worksheet->write( 'B5', 1200 );
7001 $worksheet->write( 'B6', '=SUBTOTAL(9,B2:B5)', $bold );
7002
7003 $worksheet->write( 'A7', 'South' );
7004 $worksheet->write( 'A8', 'South' );
7005 $worksheet->write( 'A9', 'South' );
7006 $worksheet->write( 'A10', 'South' );
7007 $worksheet->write( 'A11', 'South Total', $bold );
7008
7009 $worksheet->write( 'B7', 400 );
7010 $worksheet->write( 'B8', 600 );
7011 $worksheet->write( 'B9', 500 );
7012 $worksheet->write( 'B10', 600 );
7013 $worksheet->write( 'B11', '=SUBTOTAL(9,B7:B10)', $bold );
7014
7015 $worksheet->write( 'A12', 'Grand Total', $bold );
7016 $worksheet->write( 'B12', '=SUBTOTAL(9,B2:B10)', $bold );
7017
7018 }
7019
7020
7021 ###############################################################################
7022 #
7023 # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
7024 # functions so that it looks like the type of automatic outlines that are
7025 # generated when you use the Excel Data->SubTotals menu item.
7026 #
7027
7028 # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
7029 $worksheet1->set_row( 1, undef, undef, 0, 2 );
7030 $worksheet1->set_row( 2, undef, undef, 0, 2 );
7031 $worksheet1->set_row( 3, undef, undef, 0, 2 );
7032 $worksheet1->set_row( 4, undef, undef, 0, 2 );
7033 $worksheet1->set_row( 5, undef, undef, 0, 1 );
7034
7035 $worksheet1->set_row( 6, undef, undef, 0, 2 );
7036 $worksheet1->set_row( 7, undef, undef, 0, 2 );
7037 $worksheet1->set_row( 8, undef, undef, 0, 2 );
7038 $worksheet1->set_row( 9, undef, undef, 0, 2 );
7039 $worksheet1->set_row( 10, undef, undef, 0, 1 );
7040
7041 # Write the sub-total data that is common to the row examples.
7042 create_sub_totals( $worksheet1 );
7043
7044
7045 ###############################################################################
7046 #
7047 # Example 2: Create a worksheet with collapsed outlined rows.
7048 # This is the same as the example 1 except that the all rows are collapsed.
7049 # Note: We need to indicate the row that contains the collapsed symbol '+' with
7050 # the optional parameter, $collapsed.
7051
7052 $worksheet2->set_row( 1, undef, undef, 1, 2 );
7053 $worksheet2->set_row( 2, undef, undef, 1, 2 );
7054 $worksheet2->set_row( 3, undef, undef, 1, 2 );
7055 $worksheet2->set_row( 4, undef, undef, 1, 2 );
7056 $worksheet2->set_row( 5, undef, undef, 1, 1 );
7057
7058 $worksheet2->set_row( 6, undef, undef, 1, 2 );
7059 $worksheet2->set_row( 7, undef, undef, 1, 2 );
7060 $worksheet2->set_row( 8, undef, undef, 1, 2 );
7061 $worksheet2->set_row( 9, undef, undef, 1, 2 );
7062 $worksheet2->set_row( 10, undef, undef, 1, 1 );
7063
7064 $worksheet2->set_row( 11, undef, undef, 0, 0, 1 );
7065
7066 # Write the sub-total data that is common to the row examples.
7067 create_sub_totals( $worksheet2 );
7068
7069
7070 ###############################################################################
7071 #
7072 # Example 3: Create a worksheet with collapsed outlined rows.
7073 # Same as the example 1 except that the two sub-totals are collapsed.
7074
7075 $worksheet3->set_row( 1, undef, undef, 1, 2 );
7076 $worksheet3->set_row( 2, undef, undef, 1, 2 );
7077 $worksheet3->set_row( 3, undef, undef, 1, 2 );
7078 $worksheet3->set_row( 4, undef, undef, 1, 2 );
7079 $worksheet3->set_row( 5, undef, undef, 0, 1, 1 );
7080
7081 $worksheet3->set_row( 6, undef, undef, 1, 2 );
7082 $worksheet3->set_row( 7, undef, undef, 1, 2 );
7083 $worksheet3->set_row( 8, undef, undef, 1, 2 );
7084 $worksheet3->set_row( 9, undef, undef, 1, 2 );
7085 $worksheet3->set_row( 10, undef, undef, 0, 1, 1 );
7086
7087
7088 # Write the sub-total data that is common to the row examples.
7089 create_sub_totals( $worksheet3 );
7090
7091
7092 ###############################################################################
7093 #
7094 # Example 4: Create a worksheet with outlined rows.
7095 # Same as the example 1 except that the two sub-totals are collapsed.
7096
7097 $worksheet4->set_row( 1, undef, undef, 1, 2 );
7098 $worksheet4->set_row( 2, undef, undef, 1, 2 );
7099 $worksheet4->set_row( 3, undef, undef, 1, 2 );
7100 $worksheet4->set_row( 4, undef, undef, 1, 2 );
7101 $worksheet4->set_row( 5, undef, undef, 1, 1, 1 );
7102
7103 $worksheet4->set_row( 6, undef, undef, 1, 2 );
7104 $worksheet4->set_row( 7, undef, undef, 1, 2 );
7105 $worksheet4->set_row( 8, undef, undef, 1, 2 );
7106 $worksheet4->set_row( 9, undef, undef, 1, 2 );
7107 $worksheet4->set_row( 10, undef, undef, 1, 1, 1 );
7108
7109 $worksheet4->set_row( 11, undef, undef, 0, 0, 1 );
7110
7111 # Write the sub-total data that is common to the row examples.
7112 create_sub_totals( $worksheet4 );
7113
7114
7115 ###############################################################################
7116 #
7117 # Example 5: Create a worksheet with outlined columns.
7118 #
7119 my $data = [
7120 [ 'Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Total' ],
7121 [ 'North', 50, 20, 15, 25, 65, 80,, '=SUM(B2:G2)' ],
7122 [ 'South', 10, 20, 30, 50, 50, 50,, '=SUM(B3:G3)' ],
7123 [ 'East', 45, 75, 50, 15, 75, 100,, '=SUM(B4:G4)' ],
7124 [ 'West', 15, 15, 55, 35, 20, 50,, '=SUM(B5:G6)' ],
7125 ];
7126
7127 # Add bold format to the first row
7128 $worksheet5->set_row( 0, undef, $bold );
7129
7130 # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
7131 $worksheet5->set_column( 'A:A', 10, $bold );
7132 $worksheet5->set_column( 'B:G', 5, undef, 0, 1 );
7133 $worksheet5->set_column( 'H:H', 10 );
7134
7135 # Write the data and a formula
7136 $worksheet5->write_col( 'A1', $data );
7137 $worksheet5->write( 'H6', '=SUM(H2:H5)', $bold );
7138
7139
7140 ###############################################################################
7141 #
7142 # Example 6: Create a worksheet with collapsed outlined columns.
7143 # This is the same as the previous example except collapsed columns.
7144
7145 # Add bold format to the first row
7146 $worksheet6->set_row( 0, undef, $bold );
7147
7148 # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
7149 $worksheet6->set_column( 'A:A', 10, $bold );
7150 $worksheet6->set_column( 'B:G', 5, undef, 1, 1 );
7151 $worksheet6->set_column( 'H:H', 10, undef, 0, 0, 1 );
7152
7153 # Write the data and a formula
7154 $worksheet6->write_col( 'A1', $data );
7155 $worksheet6->write( 'H6', '=SUM(H2:H5)', $bold );
7156
7157 $workbook->close();
7158
7159 __END__
7160
7161 Download this example:
7162 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/outline_collapsed.pl>
7163
7164 Example: panes.pl
7165 Example of using the Excel::Writer::XLSX module to create worksheet
7166 panes.
7167
7168 Source code for this example:
7169
7170 #!/usr/bin/perl
7171
7172 #######################################################################
7173 #
7174 # Example of using the Excel::Writer::XLSX module to create worksheet panes.
7175 #
7176 # reverse ('(c)'), May 2001, John McNamara, jmcnamara@cpan.org
7177 #
7178
7179 use strict;
7180 use warnings;
7181 use Excel::Writer::XLSX;
7182
7183 my $workbook = Excel::Writer::XLSX->new( 'panes.xlsx' );
7184
7185 my $worksheet1 = $workbook->add_worksheet( 'Panes 1' );
7186 my $worksheet2 = $workbook->add_worksheet( 'Panes 2' );
7187 my $worksheet3 = $workbook->add_worksheet( 'Panes 3' );
7188 my $worksheet4 = $workbook->add_worksheet( 'Panes 4' );
7189
7190 # Freeze panes
7191 $worksheet1->freeze_panes( 1, 0 ); # 1 row
7192
7193 $worksheet2->freeze_panes( 0, 1 ); # 1 column
7194 $worksheet3->freeze_panes( 1, 1 ); # 1 row and column
7195
7196 # Split panes.
7197 # The divisions must be specified in terms of row and column dimensions.
7198 # The default row height is 15 and the default column width is 8.43
7199 #
7200 $worksheet4->split_panes( 15, 8.43 ); # 1 row and column
7201
7202
7203 #######################################################################
7204 #
7205 # Set up some formatting and text to highlight the panes
7206 #
7207
7208 my $header = $workbook->add_format(
7209 align => 'center',
7210 valign => 'vcenter',
7211 fg_color => '#C3FFC0',
7212 );
7213
7214 my $center = $workbook->add_format( align => 'center' );
7215
7216
7217 #######################################################################
7218 #
7219 # Sheet 1
7220 #
7221
7222 $worksheet1->set_column( 'A:I', 16 );
7223 $worksheet1->set_row( 0, 20 );
7224 $worksheet1->set_selection( 'C3' );
7225
7226 for my $i ( 0 .. 8 ) {
7227 $worksheet1->write( 0, $i, 'Scroll down', $header );
7228 }
7229
7230 for my $i ( 1 .. 100 ) {
7231 for my $j ( 0 .. 8 ) {
7232 $worksheet1->write( $i, $j, $i + 1, $center );
7233 }
7234 }
7235
7236
7237 #######################################################################
7238 #
7239 # Sheet 2
7240 #
7241
7242 $worksheet2->set_column( 'A:A', 16 );
7243 $worksheet2->set_selection( 'C3' );
7244
7245 for my $i ( 0 .. 49 ) {
7246 $worksheet2->set_row( $i, 15 );
7247 $worksheet2->write( $i, 0, 'Scroll right', $header );
7248 }
7249
7250 for my $i ( 0 .. 49 ) {
7251 for my $j ( 1 .. 25 ) {
7252 $worksheet2->write( $i, $j, $j, $center );
7253 }
7254 }
7255
7256
7257 #######################################################################
7258 #
7259 # Sheet 3
7260 #
7261
7262 $worksheet3->set_column( 'A:Z', 16 );
7263 $worksheet3->set_selection( 'C3' );
7264
7265 $worksheet3->write( 0, 0, '', $header );
7266
7267 for my $i ( 1 .. 25 ) {
7268 $worksheet3->write( 0, $i, 'Scroll down', $header );
7269 }
7270
7271 for my $i ( 1 .. 49 ) {
7272 $worksheet3->write( $i, 0, 'Scroll right', $header );
7273 }
7274
7275 for my $i ( 1 .. 49 ) {
7276 for my $j ( 1 .. 25 ) {
7277 $worksheet3->write( $i, $j, $j, $center );
7278 }
7279 }
7280
7281
7282 #######################################################################
7283 #
7284 # Sheet 4
7285 #
7286
7287 $worksheet4->set_selection( 'C3' );
7288
7289 for my $i ( 1 .. 25 ) {
7290 $worksheet4->write( 0, $i, 'Scroll', $center );
7291 }
7292
7293 for my $i ( 1 .. 49 ) {
7294 $worksheet4->write( $i, 0, 'Scroll', $center );
7295 }
7296
7297 for my $i ( 1 .. 49 ) {
7298 for my $j ( 1 .. 25 ) {
7299 $worksheet4->write( $i, $j, $j, $center );
7300 }
7301 }
7302
7303 $workbook->close();
7304
7305 __END__
7306
7307 Download this example:
7308 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/panes.pl>
7309
7310 Example: properties.pl
7311 An example of adding document properties to a Excel::Writer::XLSX file.
7312
7313 Source code for this example:
7314
7315 #!/usr/bin/perl
7316
7317 ##############################################################################
7318 #
7319 # An example of adding document properties to a Excel::Writer::XLSX file.
7320 #
7321 # reverse ('(c)'), August 2008, John McNamara, jmcnamara@cpan.org
7322 #
7323
7324 use strict;
7325 use warnings;
7326 use Excel::Writer::XLSX;
7327
7328 my $workbook = Excel::Writer::XLSX->new( 'properties.xlsx' );
7329 my $worksheet = $workbook->add_worksheet();
7330
7331
7332 $workbook->set_properties(
7333 title => 'This is an example spreadsheet',
7334 subject => 'With document properties',
7335 author => 'John McNamara',
7336 manager => 'Dr. Heinz Doofenshmirtz',
7337 company => 'of Wolves',
7338 category => 'Example spreadsheets',
7339 keywords => 'Sample, Example, Properties',
7340 comments => 'Created with Perl and Excel::Writer::XLSX',
7341 status => 'Quo',
7342 );
7343
7344
7345 $worksheet->set_column( 'A:A', 70 );
7346 $worksheet->write( 'A1', qq{Select 'Office Button -> Prepare -> Properties' to see the file properties.} );
7347
7348 $workbook->close();
7349
7350 __END__
7351
7352 Download this example:
7353 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/properties.pl>
7354
7355 Example: protection.pl
7356 Example of cell locking and formula hiding in an Excel worksheet via
7357 the Excel::Writer::XLSX module.
7358
7359 Source code for this example:
7360
7361 #!/usr/bin/perl
7362
7363 ########################################################################
7364 #
7365 # Example of cell locking and formula hiding in an Excel worksheet via
7366 # the Excel::Writer::XLSX module.
7367 #
7368 # reverse ('(c)'), August 2001, John McNamara, jmcnamara@cpan.org
7369 #
7370
7371 use strict;
7372 use warnings;
7373 use Excel::Writer::XLSX;
7374
7375 my $workbook = Excel::Writer::XLSX->new( 'protection.xlsx' );
7376 my $worksheet = $workbook->add_worksheet();
7377
7378 # Create some format objects
7379 my $unlocked = $workbook->add_format( locked => 0 );
7380 my $hidden = $workbook->add_format( hidden => 1 );
7381
7382 # Format the columns
7383 $worksheet->set_column( 'A:A', 45 );
7384 $worksheet->set_selection( 'B3' );
7385
7386 # Protect the worksheet
7387 $worksheet->protect();
7388
7389 # Examples of cell locking and hiding.
7390 $worksheet->write( 'A1', 'Cell B1 is locked. It cannot be edited.' );
7391 $worksheet->write_formula( 'B1', '=1+2', undef, 3 ); # Locked by default.
7392
7393 $worksheet->write( 'A2', 'Cell B2 is unlocked. It can be edited.' );
7394 $worksheet->write_formula( 'B2', '=1+2', $unlocked, 3 );
7395
7396 $worksheet->write( 'A3', "Cell B3 is hidden. The formula isn't visible." );
7397 $worksheet->write_formula( 'B3', '=1+2', $hidden, 3 );
7398
7399 $worksheet->write( 'A5', 'Use Menu->Tools->Protection->Unprotect Sheet' );
7400 $worksheet->write( 'A6', 'to remove the worksheet protection.' );
7401
7402 $workbook->close();
7403
7404 __END__
7405
7406 Download this example:
7407 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/protection.pl>
7408
7409 Example: rich_strings.pl
7410 An Excel::Writer::XLSX example showing how to use "rich strings", i.e.,
7411 strings with multiple formatting.
7412
7413 Source code for this example:
7414
7415 #!/usr/bin/perl
7416
7417 #######################################################################
7418 #
7419 # An Excel::Writer::XLSX example showing how to use "rich strings", i.e.,
7420 # strings with multiple formatting.
7421 #
7422 # reverse ('(c)'), February 2011, John McNamara, jmcnamara@cpan.org
7423 #
7424
7425 use strict;
7426 use warnings;
7427 use Excel::Writer::XLSX;
7428
7429 my $workbook = Excel::Writer::XLSX->new( 'rich_strings.xlsx' );
7430 my $worksheet = $workbook->add_worksheet();
7431
7432 $worksheet->set_column( 'A:A', 30 );
7433
7434 # Set some formats to use.
7435 my $bold = $workbook->add_format( bold => 1 );
7436 my $italic = $workbook->add_format( italic => 1 );
7437 my $red = $workbook->add_format( color => 'red' );
7438 my $blue = $workbook->add_format( color => 'blue' );
7439 my $center = $workbook->add_format( align => 'center' );
7440 my $super = $workbook->add_format( font_script => 1 );
7441
7442
7443 # Write some strings with multiple formats.
7444 $worksheet->write_rich_string( 'A1',
7445 'This is ', $bold, 'bold', ' and this is ', $italic, 'italic' );
7446
7447 $worksheet->write_rich_string( 'A3',
7448 'This is ', $red, 'red', ' and this is ', $blue, 'blue' );
7449
7450 $worksheet->write_rich_string( 'A5',
7451 'Some ', $bold, 'bold text', ' centered', $center );
7452
7453 $worksheet->write_rich_string( 'A7',
7454 $italic, 'j = k', $super, '(n-1)', $center );
7455
7456 $workbook->close();
7457
7458 __END__
7459
7460 Download this example:
7461 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/rich_strings.pl>
7462
7463 Example: right_to_left.pl
7464 Example of how to change the default worksheet direction from left-to-
7465 right to right-to-left as required by some eastern verions of Excel.
7466
7467 Source code for this example:
7468
7469 #!/usr/bin/perl
7470
7471 #######################################################################
7472 #
7473 # Example of how to change the default worksheet direction from
7474 # left-to-right to right-to-left as required by some eastern verions
7475 # of Excel.
7476 #
7477 # reverse ('(c)'), January 2006, John McNamara, jmcnamara@cpan.org
7478 #
7479
7480 use strict;
7481 use warnings;
7482 use Excel::Writer::XLSX;
7483
7484 my $workbook = Excel::Writer::XLSX->new( 'right_to_left.xlsx' );
7485 my $worksheet1 = $workbook->add_worksheet();
7486 my $worksheet2 = $workbook->add_worksheet();
7487
7488 $worksheet2->right_to_left();
7489
7490 $worksheet1->write( 0, 0, 'Hello' ); # A1, B1, C1, ...
7491 $worksheet2->write( 0, 0, 'Hello' ); # ..., C1, B1, A1
7492
7493 $workbook->close();
7494
7495 __END__
7496
7497 Download this example:
7498 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/right_to_left.pl>
7499
7500 Example: sales.pl
7501 Example of a sales worksheet to demonstrate several different features.
7502 Also uses functions from the Excel::Writer::XLSX::Utility module.
7503
7504 Source code for this example:
7505
7506 #!/usr/bin/perl -w
7507
7508 ###############################################################################
7509 #
7510 # Example of a sales worksheet to demonstrate several different features.
7511 # Also uses functions from the L<Excel::Writer::XLSX::Utility> module.
7512 #
7513 # reverse ('(c)'), October 2001, John McNamara, jmcnamara@cpan.org
7514 #
7515
7516 use strict;
7517 use Excel::Writer::XLSX;
7518 use Excel::Writer::XLSX::Utility;
7519
7520 # Create a new workbook and add a worksheet
7521 my $workbook = Excel::Writer::XLSX->new( 'sales.xlsx' );
7522 my $worksheet = $workbook->add_worksheet( 'May Sales' );
7523
7524
7525 # Set up some formats
7526 my %heading = (
7527 bold => 1,
7528 pattern => 1,
7529 fg_color => '#C3FFC0',
7530 border => 1,
7531 align => 'center',
7532 );
7533
7534 my %total = (
7535 bold => 1,
7536 top => 1,
7537 num_format => '$#,##0.00'
7538 );
7539
7540 my $heading = $workbook->add_format( %heading );
7541 my $total_format = $workbook->add_format( %total );
7542 my $price_format = $workbook->add_format( num_format => '$#,##0.00' );
7543 my $date_format = $workbook->add_format( num_format => 'mmm d yyy' );
7544
7545
7546 # Write the main headings
7547 $worksheet->freeze_panes( 1 ); # Freeze the first row
7548 $worksheet->write( 'A1', 'Item', $heading );
7549 $worksheet->write( 'B1', 'Quantity', $heading );
7550 $worksheet->write( 'C1', 'Price', $heading );
7551 $worksheet->write( 'D1', 'Total', $heading );
7552 $worksheet->write( 'E1', 'Date', $heading );
7553
7554 # Set the column widths
7555 $worksheet->set_column( 'A:A', 25 );
7556 $worksheet->set_column( 'B:B', 10 );
7557 $worksheet->set_column( 'C:E', 16 );
7558
7559
7560 # Extract the sales data from the __DATA__ section at the end of the file.
7561 # In reality this information would probably come from a database
7562 my @sales;
7563
7564 foreach my $line ( <DATA> ) {
7565 chomp $line;
7566 next if $line eq '';
7567
7568 # Simple-minded processing of CSV data. Refer to the Text::CSV_XS
7569 # and Text::xSV modules for a more complete CSV handling.
7570 my @items = split /,/, $line;
7571 push @sales, \@items;
7572 }
7573
7574
7575 # Write out the items from each row
7576 my $row = 1;
7577 foreach my $sale ( @sales ) {
7578
7579 $worksheet->write( $row, 0, @$sale[0] );
7580 $worksheet->write( $row, 1, @$sale[1] );
7581 $worksheet->write( $row, 2, @$sale[2], $price_format );
7582
7583 # Create a formula like '=B2*C2'
7584 my $formula =
7585 '=' . xl_rowcol_to_cell( $row, 1 ) . "*" . xl_rowcol_to_cell( $row, 2 );
7586
7587 $worksheet->write( $row, 3, $formula, $price_format );
7588
7589 # Parse the date
7590 my $date = xl_decode_date_US( @$sale[3] );
7591 $worksheet->write( $row, 4, $date, $date_format );
7592 $row++;
7593 }
7594
7595 # Create a formula to sum the totals, like '=SUM(D2:D6)'
7596 my $total = '=SUM(D2:' . xl_rowcol_to_cell( $row - 1, 3 ) . ")";
7597
7598 $worksheet->write( $row, 3, $total, $total_format );
7599
7600 $workbook->close();
7601
7602 __DATA__
7603 586 card,20,125.50,5/12/01
7604 Flat Screen Monitor,1,1300.00,5/12/01
7605 64 MB dimms,45,49.99,5/13/01
7606 15 GB HD,12,300.00,5/13/01
7607 Speakers (pair),5,15.50,5/14/01
7608
7609 Download this example:
7610 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/sales.pl>
7611
7612 Example: shape1.pl
7613 A simple example of how to use the Excel::Writer::XLSX module to add
7614 shapes to an Excel xlsx file.
7615
7616 Source code for this example:
7617
7618 #!/usr/bin/perl
7619
7620 #######################################################################
7621 #
7622 # A simple example of how to use the Excel::Writer::XLSX module to
7623 # add shapes to an Excel xlsx file.
7624 #
7625 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
7626 #
7627
7628 use strict;
7629 use warnings;
7630 use Excel::Writer::XLSX;
7631
7632 my $workbook = Excel::Writer::XLSX->new( 'shape1.xlsx' );
7633 my $worksheet = $workbook->add_worksheet();
7634
7635 # Add a circle, with centered text.
7636 my $ellipse = $workbook->add_shape(
7637 type => 'ellipse',
7638 text => "Hello\nWorld",
7639 width => 60,
7640 height => 60
7641 );
7642
7643 $worksheet->insert_shape( 'A1', $ellipse, 50, 50 );
7644
7645 # Add a plus sign.
7646 my $plus = $workbook->add_shape( type => 'plus', width => 20, height => 20 );
7647 $worksheet->insert_shape( 'D8', $plus );
7648
7649 $workbook->close();
7650
7651 __END__
7652
7653 Download this example:
7654 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape1.pl>
7655
7656 Example: shape2.pl
7657 A simple example of how to use the Excel::Writer::XLSX module to modify
7658 shape properties in an Excel xlsx file.
7659
7660 Source code for this example:
7661
7662 #!/usr/bin/perl
7663
7664 #######################################################################
7665 #
7666 # A simple example of how to use the Excel::Writer::XLSX module to
7667 # modify shape properties in an Excel xlsx file.
7668 #
7669 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
7670 #
7671
7672 use strict;
7673 use warnings;
7674 use Excel::Writer::XLSX;
7675
7676 my $workbook = Excel::Writer::XLSX->new( 'shape2.xlsx' );
7677 my $worksheet = $workbook->add_worksheet();
7678
7679 $worksheet->hide_gridlines( 2 );
7680
7681 my $plain = $workbook->add_shape(
7682 type => 'smileyFace',
7683 text => "Plain",
7684 width => 100,
7685 height => 100,
7686 );
7687
7688 my $bbformat = $workbook->add_format(
7689 color => 'red',
7690 font => 'Lucida Calligraphy',
7691 );
7692
7693 $bbformat->set_bold();
7694 $bbformat->set_underline();
7695 $bbformat->set_italic();
7696
7697 my $decor = $workbook->add_shape(
7698 type => 'smileyFace',
7699 text => "Decorated",
7700 rotation => 45,
7701 width => 200,
7702 height => 100,
7703 format => $bbformat,
7704 line_type => 'sysDot',
7705 line_weight => 3,
7706 fill => 'FFFF00',
7707 line => '3366FF',
7708 );
7709
7710 $worksheet->insert_shape( 'A1', $plain, 50, 50 );
7711 $worksheet->insert_shape( 'A1', $decor, 250, 50 );
7712
7713 $workbook->close();
7714
7715 __END__
7716
7717 Download this example:
7718 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape2.pl>
7719
7720 Example: shape3.pl
7721 A simple example of how to use the Excel::Writer::XLSX module to scale
7722 shapes in an Excel xlsx file.
7723
7724 Source code for this example:
7725
7726 #!/usr/bin/perl
7727
7728 #######################################################################
7729 #
7730 # A simple example of how to use the Excel::Writer::XLSX module to
7731 # scale shapes in an Excel xlsx file.
7732 #
7733 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
7734 #
7735
7736 use strict;
7737 use warnings;
7738 use Excel::Writer::XLSX;
7739
7740 my $workbook = Excel::Writer::XLSX->new( 'shape3.xlsx' );
7741 my $worksheet = $workbook->add_worksheet();
7742
7743 my $normal = $workbook->add_shape(
7744 name => 'chip',
7745 type => 'diamond',
7746 text => "Normal",
7747 width => 100,
7748 height => 100,
7749 );
7750
7751 $worksheet->insert_shape( 'A1', $normal, 50, 50 );
7752 $normal->set_text( 'Scaled 3w x 2h' );
7753 $normal->set_name( 'Hope' );
7754 $worksheet->insert_shape( 'A1', $normal, 250, 50, 3, 2 );
7755
7756 $workbook->close();
7757
7758 __END__
7759
7760 Download this example:
7761 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape3.pl>
7762
7763 Example: shape4.pl
7764 A simple example of how to use the Excel::Writer::XLSX module to
7765 demonstrate stenciling in an Excel xlsx file.
7766
7767 Source code for this example:
7768
7769 #!/usr/bin/perl
7770
7771 #######################################################################
7772 #
7773 # A simple example of how to use the Excel::Writer::XLSX module to
7774 # demonstrate stenciling in an Excel xlsx file.
7775 #
7776 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
7777 #
7778
7779 use strict;
7780 use warnings;
7781 use Excel::Writer::XLSX;
7782
7783 my $workbook = Excel::Writer::XLSX->new( 'shape4.xlsx' );
7784 my $worksheet = $workbook->add_worksheet();
7785
7786 $worksheet->hide_gridlines( 2 );
7787
7788 my $type = 'rect';
7789 my $shape = $workbook->add_shape(
7790 type => $type,
7791 width => 90,
7792 height => 90,
7793 );
7794
7795 for my $n ( 1 .. 10 ) {
7796
7797 # Change the last 5 rectangles to stars. Previously inserted shapes stay
7798 # as rectangles.
7799 $type = 'star5' if $n == 6;
7800 $shape->set_type( $type );
7801 $shape->set_text( "$type $n" );
7802 $worksheet->insert_shape( 'A1', $shape, $n * 100, 50 );
7803 }
7804
7805
7806 my $stencil = $workbook->add_shape(
7807 stencil => 1, # The default.
7808 width => 90,
7809 height => 90,
7810 text => 'started as a box',
7811 );
7812 $worksheet->insert_shape( 'A1', $stencil, 100, 150 );
7813
7814 $stencil->set_stencil( 0 );
7815 $worksheet->insert_shape( 'A1', $stencil, 200, 150 );
7816 $worksheet->insert_shape( 'A1', $stencil, 300, 150 );
7817
7818 # Ooops! Changed my mind. Change the rectangle to an ellipse (circle),
7819 # for the last two shapes.
7820 $stencil->set_type( 'ellipse' );
7821 $stencil->set_text( 'Now its a circle' );
7822
7823 $workbook->close();
7824
7825 __END__
7826
7827 Download this example:
7828 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape4.pl>
7829
7830 Example: shape5.pl
7831 A simple example of how to use the Excel::Writer::XLSX module to add
7832 shapes (objects and top/bottom connectors) to an Excel xlsx file.
7833
7834 Source code for this example:
7835
7836 #!/usr/bin/perl
7837
7838 #######################################################################
7839 #
7840 # A simple example of how to use the Excel::Writer::XLSX module to
7841 # add shapes (objects and top/bottom connectors) to an Excel xlsx file.
7842 #
7843 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
7844 #
7845
7846 use strict;
7847 use warnings;
7848 use Excel::Writer::XLSX;
7849
7850 my $workbook = Excel::Writer::XLSX->new( 'shape5.xlsx' );
7851 my $worksheet = $workbook->add_worksheet();
7852
7853 my $s1 = $workbook->add_shape( type => 'ellipse', width => 60, height => 60 );
7854 $worksheet->insert_shape( 'A1', $s1, 50, 50 );
7855
7856 my $s2 = $workbook->add_shape( type => 'plus', width => 20, height => 20 );
7857 $worksheet->insert_shape( 'A1', $s2, 250, 200 );
7858
7859 # Create a connector to link the two shapes.
7860 my $cxn_shape = $workbook->add_shape( type => 'bentConnector3' );
7861
7862 # Link the start of the connector to the right side.
7863 $cxn_shape->set_start( $s1->get_id() );
7864 $cxn_shape->set_start_index( 4 ); # 4th connection pt, clockwise from top(0).
7865 $cxn_shape->set_start_side( 'b' ); # r)ight or b)ottom.
7866
7867 # Link the end of the connector to the left side.
7868 $cxn_shape->set_end( $s2->get_id() );
7869 $cxn_shape->set_end_index( 0 ); # clockwise from top(0).
7870 $cxn_shape->set_end_side( 't' ); # t)top.
7871
7872 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
7873
7874 $workbook->close();
7875
7876 __END__
7877
7878 Download this example:
7879 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape5.pl>
7880
7881 Example: shape6.pl
7882 A simple example of how to use the Excel::Writer::XLSX module to add
7883 shapes (objects and right/left connectors) to an Excel xlsx file.
7884
7885 Source code for this example:
7886
7887 #!/usr/bin/perl
7888
7889 #######################################################################
7890 #
7891 # A simple example of how to use the Excel::Writer::XLSX module to
7892 # add shapes (objects and right/left connectors) to an Excel xlsx file.
7893 #
7894 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
7895 #
7896
7897 use strict;
7898 use warnings;
7899 use Excel::Writer::XLSX;
7900
7901 my $workbook = Excel::Writer::XLSX->new( 'shape6.xlsx' );
7902 my $worksheet = $workbook->add_worksheet();
7903
7904 my $s1 = $workbook->add_shape( type => 'chevron', width => 60, height => 60 );
7905 $worksheet->insert_shape( 'A1', $s1, 50, 50 );
7906
7907 my $s2 = $workbook->add_shape( type => 'pentagon', width => 20, height => 20 );
7908 $worksheet->insert_shape( 'A1', $s2, 250, 200 );
7909
7910 # Create a connector to link the two shapes.
7911 my $cxn_shape = $workbook->add_shape( type => 'curvedConnector3' );
7912
7913 # Link the start of the connector to the right side.
7914 $cxn_shape->set_start( $s1->get_id() );
7915 $cxn_shape->set_start_index( 2 ); # 2nd connection pt, clockwise from top(0).
7916 $cxn_shape->set_start_side( 'r' ); # r)ight or b)ottom.
7917
7918 # Link the end of the connector to the left side.
7919 $cxn_shape->set_end( $s2->get_id() );
7920 $cxn_shape->set_end_index( 4 ); # 4th connection pt, clockwise from top(0).
7921 $cxn_shape->set_end_side( 'l' ); # l)eft or t)op.
7922
7923 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
7924
7925 $workbook->close();
7926
7927 __END__
7928
7929 Download this example:
7930 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape6.pl>
7931
7932 Example: shape7.pl
7933 A simple example of how to use the Excel::Writer::XLSX module to add
7934 shapes and one-to-many connectors to an Excel xlsx file.
7935
7936 Source code for this example:
7937
7938 #!/usr/bin/perl
7939
7940 #######################################################################
7941 #
7942 # A simple example of how to use the Excel::Writer::XLSX module to
7943 # add shapes and one-to-many connectors to an Excel xlsx file.
7944 #
7945 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
7946 #
7947
7948 use strict;
7949 use warnings;
7950 use Excel::Writer::XLSX;
7951
7952 my $workbook = Excel::Writer::XLSX->new( 'shape7.xlsx' );
7953 my $worksheet = $workbook->add_worksheet();
7954
7955 # Add a circle, with centered text. c is for circle, not center.
7956 my $cw = 60;
7957 my $ch = 60;
7958 my $cx = 210;
7959 my $cy = 190;
7960
7961 my $ellipse = $workbook->add_shape(
7962 type => 'ellipse',
7963 id => 2,
7964 text => "Hello\nWorld",
7965 width => $cw,
7966 height => $ch
7967 );
7968 $worksheet->insert_shape( 'A1', $ellipse, $cx, $cy );
7969
7970 # Add a plus sign at 4 different positions around the circle.
7971 my $pw = 20;
7972 my $ph = 20;
7973 my $px = 120;
7974 my $py = 250;
7975 my $plus =
7976 $workbook->add_shape( type => 'plus', id => 3, width => $pw, height => $ph );
7977 my $p1 = $worksheet->insert_shape( 'A1', $plus, 350, 350 );
7978 my $p2 = $worksheet->insert_shape( 'A1', $plus, 150, 350 );
7979 my $p3 = $worksheet->insert_shape( 'A1', $plus, 350, 150 );
7980 $plus->set_adjustments( 35 ); # change shape of plus symbol.
7981 my $p4 = $worksheet->insert_shape( 'A1', $plus, 150, 150 );
7982
7983 my $cxn_shape = $workbook->add_shape( type => 'bentConnector3', fill => 0 );
7984
7985 $cxn_shape->set_start( $ellipse->get_id() );
7986 $cxn_shape->set_start_index( 4 ); # 4nd connection pt, clockwise from top(0).
7987 $cxn_shape->set_start_side( 'b' ); # r)ight or b)ottom.
7988
7989 $cxn_shape->set_end( $p1->get_id() );
7990 $cxn_shape->set_end_index( 0 );
7991 $cxn_shape->set_end_side( 't' );
7992 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
7993
7994 $cxn_shape->set_end( $p2->get_id() );
7995 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
7996
7997 $cxn_shape->set_end( $p3->get_id() );
7998 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
7999
8000 $cxn_shape->set_end( $p4->get_id() );
8001 $cxn_shape->set_adjustments( -50, 45, 120 );
8002 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8003
8004 $workbook->close();
8005
8006 __END__
8007
8008 Download this example:
8009 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape7.pl>
8010
8011 Example: shape8.pl
8012 A simple example of how to use the Excel::Writer::XLSX module to add
8013 shapes and one-to-many connectors to an Excel xlsx file.
8014
8015 Source code for this example:
8016
8017 #!/usr/bin/perl
8018
8019 #######################################################################
8020 #
8021 # A simple example of how to use the Excel::Writer::XLSX module to
8022 # add shapes and one-to-many connectors to an Excel xlsx file.
8023 #
8024 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
8025 #
8026
8027 use strict;
8028 use warnings;
8029 use Excel::Writer::XLSX;
8030
8031 my $workbook = Excel::Writer::XLSX->new( 'shape8.xlsx' );
8032 my $worksheet = $workbook->add_worksheet();
8033
8034 # Add a circle, with centered text. c is for circle, not center.
8035 my $cw = 60;
8036 my $ch = 60;
8037 my $cx = 210;
8038 my $cy = 190;
8039
8040 my $ellipse = $workbook->add_shape(
8041 type => 'ellipse',
8042 id => 2,
8043 text => "Hello\nWorld",
8044 width => $cw,
8045 height => $ch
8046 );
8047 $worksheet->insert_shape( 'A1', $ellipse, $cx, $cy );
8048
8049 # Add a plus sign at 4 different positions around the circle.
8050 my $pw = 20;
8051 my $ph = 20;
8052 my $px = 120;
8053 my $py = 250;
8054 my $plus =
8055 $workbook->add_shape( type => 'plus', id => 3, width => $pw, height => $ph );
8056 my $p1 = $worksheet->insert_shape( 'A1', $plus, 350, 150 ); # 2:00
8057 my $p2 = $worksheet->insert_shape( 'A1', $plus, 350, 350 ); # 4:00
8058 my $p3 = $worksheet->insert_shape( 'A1', $plus, 150, 350 ); # 8:00
8059 my $p4 = $worksheet->insert_shape( 'A1', $plus, 150, 150 ); # 10:00
8060
8061 my $cxn_shape = $workbook->add_shape( type => 'bentConnector3', fill => 0 );
8062
8063 $cxn_shape->set_start( $ellipse->get_id() );
8064 $cxn_shape->set_start_index( 2 ); # 2nd connection pt, clockwise from top(0).
8065 $cxn_shape->set_start_side( 'r' ); # r)ight or b)ottom.
8066
8067 $cxn_shape->set_end( $p1->get_id() );
8068 $cxn_shape->set_end_index( 3 ); # 3rd connection point on plus, right side
8069 $cxn_shape->set_end_side( 'l' );
8070 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8071
8072 $cxn_shape->set_end( $p2->get_id() );
8073 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8074
8075 $cxn_shape->set_end( $p3->get_id() );
8076 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8077
8078 $cxn_shape->set_end( $p4->get_id() );
8079 $cxn_shape->set_adjustments( -50, 45, 120 );
8080 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8081
8082 $workbook->close();
8083
8084 __END__
8085
8086 Download this example:
8087 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape8.pl>
8088
8089 Example: shape_all.pl
8090 A simple example of how to use the Excel::Writer::XLSX module to add
8091 all shapes (as currently implemented) to an Excel xlsx file.
8092
8093 The list at the end consists of all the shape types defined as
8094 ST_ShapeType in ECMA-376, Office Open XML File Formats Part 4.
8095
8096 The grouping by worksheet name is for illustration only. It isn't part
8097 of the ECMA-376 standard.
8098
8099 Source code for this example:
8100
8101 #!/usr/bin/perl
8102
8103 #######################################################################
8104 #
8105 # A simple example of how to use the Excel::Writer::XLSX module to
8106 # add all shapes (as currently implemented) to an Excel xlsx file.
8107 #
8108 # The list at the end consists of all the shape types defined as
8109 # ST_ShapeType in ECMA-376, Office Open XML File Formats Part 4.
8110 #
8111 # The grouping by worksheet name is for illustration only. It isn't
8112 # part of the ECMA-376 standard.
8113 #
8114 # reverse ('(c)'), May 2012, John McNamara, jmcnamara@cpan.org
8115 #
8116
8117 use strict;
8118 use warnings;
8119 use Excel::Writer::XLSX;
8120
8121 my $workbook = Excel::Writer::XLSX->new( 'shape_all.xlsx' );
8122
8123 my ( $worksheet, $last_sheet, $shape, $r ) = ( 0, '', '', undef, 0 );
8124
8125 while ( <DATA> ) {
8126 chomp;
8127 next unless m/^\w/; # Skip blank lines and comments.
8128
8129 my ( $sheet, $name ) = split( /\t/, $_ );
8130 if ( $last_sheet ne $sheet ) {
8131 $worksheet = $workbook->add_worksheet( $sheet );
8132 $r = 2;
8133 }
8134 $last_sheet = $sheet;
8135 $shape = $workbook->add_shape(
8136 type => $name,
8137 text => $name,
8138 width => 90,
8139 height => 90
8140 );
8141
8142 # Connectors can not have labels, so write the connector name in the cell
8143 # to the left.
8144 $worksheet->write( $r, 0, $name ) if $sheet eq 'Connector';
8145 $worksheet->insert_shape( $r, 2, $shape, 0, 0 );
8146 $r += 5;
8147 }
8148
8149 $workbook->close();
8150
8151 __END__
8152 Action actionButtonBackPrevious
8153 Action actionButtonBeginning
8154 Action actionButtonBlank
8155 Action actionButtonDocument
8156 Action actionButtonEnd
8157 Action actionButtonForwardNext
8158 Action actionButtonHelp
8159 Action actionButtonHome
8160 Action actionButtonInformation
8161 Action actionButtonMovie
8162 Action actionButtonReturn
8163 Action actionButtonSound
8164 Arrow bentArrow
8165 Arrow bentUpArrow
8166 Arrow circularArrow
8167 Arrow curvedDownArrow
8168 Arrow curvedLeftArrow
8169 Arrow curvedRightArrow
8170 Arrow curvedUpArrow
8171 Arrow downArrow
8172 Arrow leftArrow
8173 Arrow leftCircularArrow
8174 Arrow leftRightArrow
8175 Arrow leftRightCircularArrow
8176 Arrow leftRightUpArrow
8177 Arrow leftUpArrow
8178 Arrow notchedRightArrow
8179 Arrow quadArrow
8180 Arrow rightArrow
8181 Arrow stripedRightArrow
8182 Arrow swooshArrow
8183 Arrow upArrow
8184 Arrow upDownArrow
8185 Arrow uturnArrow
8186 Basic blockArc
8187 Basic can
8188 Basic chevron
8189 Basic cube
8190 Basic decagon
8191 Basic diamond
8192 Basic dodecagon
8193 Basic donut
8194 Basic ellipse
8195 Basic funnel
8196 Basic gear6
8197 Basic gear9
8198 Basic heart
8199 Basic heptagon
8200 Basic hexagon
8201 Basic homePlate
8202 Basic lightningBolt
8203 Basic line
8204 Basic lineInv
8205 Basic moon
8206 Basic nonIsoscelesTrapezoid
8207 Basic noSmoking
8208 Basic octagon
8209 Basic parallelogram
8210 Basic pentagon
8211 Basic pie
8212 Basic pieWedge
8213 Basic plaque
8214 Basic rect
8215 Basic round1Rect
8216 Basic round2DiagRect
8217 Basic round2SameRect
8218 Basic roundRect
8219 Basic rtTriangle
8220 Basic smileyFace
8221 Basic snip1Rect
8222 Basic snip2DiagRect
8223 Basic snip2SameRect
8224 Basic snipRoundRect
8225 Basic star10
8226 Basic star12
8227 Basic star16
8228 Basic star24
8229 Basic star32
8230 Basic star4
8231 Basic star5
8232 Basic star6
8233 Basic star7
8234 Basic star8
8235 Basic sun
8236 Basic teardrop
8237 Basic trapezoid
8238 Basic triangle
8239 Callout accentBorderCallout1
8240 Callout accentBorderCallout2
8241 Callout accentBorderCallout3
8242 Callout accentCallout1
8243 Callout accentCallout2
8244 Callout accentCallout3
8245 Callout borderCallout1
8246 Callout borderCallout2
8247 Callout borderCallout3
8248 Callout callout1
8249 Callout callout2
8250 Callout callout3
8251 Callout cloudCallout
8252 Callout downArrowCallout
8253 Callout leftArrowCallout
8254 Callout leftRightArrowCallout
8255 Callout quadArrowCallout
8256 Callout rightArrowCallout
8257 Callout upArrowCallout
8258 Callout upDownArrowCallout
8259 Callout wedgeEllipseCallout
8260 Callout wedgeRectCallout
8261 Callout wedgeRoundRectCallout
8262 Chart chartPlus
8263 Chart chartStar
8264 Chart chartX
8265 Connector bentConnector2
8266 Connector bentConnector3
8267 Connector bentConnector4
8268 Connector bentConnector5
8269 Connector curvedConnector2
8270 Connector curvedConnector3
8271 Connector curvedConnector4
8272 Connector curvedConnector5
8273 Connector straightConnector1
8274 FlowChart flowChartAlternateProcess
8275 FlowChart flowChartCollate
8276 FlowChart flowChartConnector
8277 FlowChart flowChartDecision
8278 FlowChart flowChartDelay
8279 FlowChart flowChartDisplay
8280 FlowChart flowChartDocument
8281 FlowChart flowChartExtract
8282 FlowChart flowChartInputOutput
8283 FlowChart flowChartInternalStorage
8284 FlowChart flowChartMagneticDisk
8285 FlowChart flowChartMagneticDrum
8286 FlowChart flowChartMagneticTape
8287 FlowChart flowChartManualInput
8288 FlowChart flowChartManualOperation
8289 FlowChart flowChartMerge
8290 FlowChart flowChartMultidocument
8291 FlowChart flowChartOfflineStorage
8292 FlowChart flowChartOffpageConnector
8293 FlowChart flowChartOnlineStorage
8294 FlowChart flowChartOr
8295 FlowChart flowChartPredefinedProcess
8296 FlowChart flowChartPreparation
8297 FlowChart flowChartProcess
8298 FlowChart flowChartPunchedCard
8299 FlowChart flowChartPunchedTape
8300 FlowChart flowChartSort
8301 FlowChart flowChartSummingJunction
8302 FlowChart flowChartTerminator
8303 Math mathDivide
8304 Math mathEqual
8305 Math mathMinus
8306 Math mathMultiply
8307 Math mathNotEqual
8308 Math mathPlus
8309 Star_Banner arc
8310 Star_Banner bevel
8311 Star_Banner bracePair
8312 Star_Banner bracketPair
8313 Star_Banner chord
8314 Star_Banner cloud
8315 Star_Banner corner
8316 Star_Banner diagStripe
8317 Star_Banner doubleWave
8318 Star_Banner ellipseRibbon
8319 Star_Banner ellipseRibbon2
8320 Star_Banner foldedCorner
8321 Star_Banner frame
8322 Star_Banner halfFrame
8323 Star_Banner horizontalScroll
8324 Star_Banner irregularSeal1
8325 Star_Banner irregularSeal2
8326 Star_Banner leftBrace
8327 Star_Banner leftBracket
8328 Star_Banner leftRightRibbon
8329 Star_Banner plus
8330 Star_Banner ribbon
8331 Star_Banner ribbon2
8332 Star_Banner rightBrace
8333 Star_Banner rightBracket
8334 Star_Banner verticalScroll
8335 Star_Banner wave
8336 Tabs cornerTabs
8337 Tabs plaqueTabs
8338 Tabs squareTabs
8339
8340 Download this example:
8341 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/shape_all.pl>
8342
8343 Example: sparklines1.pl
8344 Example of how to add sparklines to an Excel::Writer::XLSX file.
8345
8346 Sparklines are small charts that fit in a single cell and are used to
8347 show trends in data. See sparklines2.pl for examples of more complex
8348 sparkline formatting.
8349
8350 Source code for this example:
8351
8352 #!/usr/bin/perl
8353
8354 ###############################################################################
8355 #
8356 # Example of how to add sparklines to an Excel::Writer::XLSX file.
8357 #
8358 # Sparklines are small charts that fit in a single cell and are
8359 # used to show trends in data. See sparklines2.pl for examples
8360 # of more complex sparkline formatting.
8361 #
8362 # reverse ('(c)'), November 2011, John McNamara, jmcnamara@cpan.org
8363 #
8364
8365 use strict;
8366 use warnings;
8367 use Excel::Writer::XLSX;
8368
8369 my $workbook = Excel::Writer::XLSX->new( 'sparklines1.xlsx' );
8370 my $worksheet = $workbook->add_worksheet();
8371
8372 # Some sample data to plot.
8373 my $data = [
8374
8375 [ -2, 2, 3, -1, 0 ],
8376 [ 30, 20, 33, 20, 15 ],
8377 [ 1, -1, -1, 1, -1 ],
8378
8379 ];
8380
8381 # Write the sample data to the worksheet.
8382 $worksheet->write_col( 'A1', $data );
8383
8384
8385 # Add a line sparkline (the default) with markers.
8386 $worksheet->add_sparkline(
8387 {
8388 location => 'F1',
8389 range => 'Sheet1!A1:E1',
8390 markers => 1,
8391 }
8392 );
8393
8394 # Add a column sparkline with non-default style.
8395 $worksheet->add_sparkline(
8396 {
8397 location => 'F2',
8398 range => 'Sheet1!A2:E2',
8399 type => 'column',
8400 style => 12,
8401 }
8402 );
8403
8404 # Add a win/loss sparkline with negative values highlighted.
8405 $worksheet->add_sparkline(
8406 {
8407 location => 'F3',
8408 range => 'Sheet1!A3:E3',
8409 type => 'win_loss',
8410 negative_points => 1,
8411 }
8412 );
8413
8414 $workbook->close();
8415
8416 __END__
8417
8418 Download this example:
8419 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/sparklines1.pl>
8420
8421 Example: sparklines2.pl
8422 Example of how to add sparklines to an Excel::Writer::XLSX file.
8423
8424 Sparklines are small charts that fit in a single cell and are used to
8425 show trends in data. This example shows the majority of options that
8426 can be applied to sparklines.
8427
8428 Source code for this example:
8429
8430 #!/usr/bin/perl
8431
8432 ###############################################################################
8433 #
8434 # Example of how to add sparklines to an Excel::Writer::XLSX file.
8435 #
8436 # Sparklines are small charts that fit in a single cell and are
8437 # used to show trends in data. This example shows the majority of
8438 # options that can be applied to sparklines.
8439 #
8440 # reverse ('(c)'), November 2011, John McNamara, jmcnamara@cpan.org
8441 #
8442
8443 use strict;
8444 use warnings;
8445 use Excel::Writer::XLSX;
8446
8447 my $workbook = Excel::Writer::XLSX->new( 'sparklines2.xlsx' );
8448 my $worksheet1 = $workbook->add_worksheet();
8449 my $worksheet2 = $workbook->add_worksheet();
8450 my $bold = $workbook->add_format( bold => 1 );
8451 my $str;
8452 my $row = 1;
8453
8454 # Set the columns widths to make the output clearer.
8455 $worksheet1->set_column( 'A:A', 14 );
8456 $worksheet1->set_column( 'B:B', 50 );
8457 $worksheet1->set_zoom( 150 );
8458
8459 # Headings.
8460 $worksheet1->write( 'A1', 'Sparkline', $bold );
8461 $worksheet1->write( 'B1', 'Description', $bold );
8462
8463
8464 ###############################################################################
8465 #
8466 $str = 'A default "line" sparkline.';
8467
8468 $worksheet1->add_sparkline(
8469 {
8470 location => 'A2',
8471 range => 'Sheet2!A1:J1',
8472 }
8473 );
8474
8475 $worksheet1->write( $row++, 1, $str );
8476
8477
8478 ###############################################################################
8479 #
8480 $str = 'A default "column" sparkline.';
8481
8482 $worksheet1->add_sparkline(
8483 {
8484 location => 'A3',
8485 range => 'Sheet2!A2:J2',
8486 type => 'column',
8487 }
8488 );
8489
8490 $worksheet1->write( $row++, 1, $str );
8491
8492
8493 ###############################################################################
8494 #
8495 $str = 'A default "win/loss" sparkline.';
8496
8497 $worksheet1->add_sparkline(
8498 {
8499 location => 'A4',
8500 range => 'Sheet2!A3:J3',
8501 type => 'win_loss',
8502 }
8503 );
8504
8505 $worksheet1->write( $row++, 1, $str );
8506 $row++;
8507
8508
8509 ###############################################################################
8510 #
8511 $str = 'Line with markers.';
8512
8513 $worksheet1->add_sparkline(
8514 {
8515 location => 'A6',
8516 range => 'Sheet2!A1:J1',
8517 markers => 1,
8518 }
8519 );
8520
8521 $worksheet1->write( $row++, 1, $str );
8522
8523
8524 ###############################################################################
8525 #
8526 $str = 'Line with high and low points.';
8527
8528 $worksheet1->add_sparkline(
8529 {
8530 location => 'A7',
8531 range => 'Sheet2!A1:J1',
8532 high_point => 1,
8533 low_point => 1,
8534 }
8535 );
8536
8537 $worksheet1->write( $row++, 1, $str );
8538
8539
8540 ###############################################################################
8541 #
8542 $str = 'Line with first and last point markers.';
8543
8544 $worksheet1->add_sparkline(
8545 {
8546 location => 'A8',
8547 range => 'Sheet2!A1:J1',
8548 first_point => 1,
8549 last_point => 1,
8550 }
8551 );
8552
8553 $worksheet1->write( $row++, 1, $str );
8554
8555
8556 ###############################################################################
8557 #
8558 $str = 'Line with negative point markers.';
8559
8560 $worksheet1->add_sparkline(
8561 {
8562 location => 'A9',
8563 range => 'Sheet2!A1:J1',
8564 negative_points => 1,
8565 }
8566 );
8567
8568 $worksheet1->write( $row++, 1, $str );
8569
8570
8571 ###############################################################################
8572 #
8573 $str = 'Line with axis.';
8574
8575 $worksheet1->add_sparkline(
8576 {
8577 location => 'A10',
8578 range => 'Sheet2!A1:J1',
8579 axis => 1,
8580 }
8581 );
8582
8583 $worksheet1->write( $row++, 1, $str );
8584 $row++;
8585
8586
8587 ###############################################################################
8588 #
8589 $str = 'Column with default style (1).';
8590
8591 $worksheet1->add_sparkline(
8592 {
8593 location => 'A12',
8594 range => 'Sheet2!A2:J2',
8595 type => 'column',
8596 }
8597 );
8598
8599 $worksheet1->write( $row++, 1, $str );
8600
8601
8602 ###############################################################################
8603 #
8604 $str = 'Column with style 2.';
8605
8606 $worksheet1->add_sparkline(
8607 {
8608 location => 'A13',
8609 range => 'Sheet2!A2:J2',
8610 type => 'column',
8611 style => 2,
8612 }
8613 );
8614
8615 $worksheet1->write( $row++, 1, $str );
8616
8617
8618 ###############################################################################
8619 #
8620 $str = 'Column with style 3.';
8621
8622 $worksheet1->add_sparkline(
8623 {
8624 location => 'A14',
8625 range => 'Sheet2!A2:J2',
8626 type => 'column',
8627 style => 3,
8628 }
8629 );
8630
8631 $worksheet1->write( $row++, 1, $str );
8632
8633
8634 ###############################################################################
8635 #
8636 $str = 'Column with style 4.';
8637
8638 $worksheet1->add_sparkline(
8639 {
8640 location => 'A15',
8641 range => 'Sheet2!A2:J2',
8642 type => 'column',
8643 style => 4,
8644 }
8645 );
8646
8647 $worksheet1->write( $row++, 1, $str );
8648
8649
8650 ###############################################################################
8651 #
8652 $str = 'Column with style 5.';
8653
8654 $worksheet1->add_sparkline(
8655 {
8656 location => 'A16',
8657 range => 'Sheet2!A2:J2',
8658 type => 'column',
8659 style => 5,
8660 }
8661 );
8662
8663 $worksheet1->write( $row++, 1, $str );
8664
8665
8666 ###############################################################################
8667 #
8668 $str = 'Column with style 6.';
8669
8670 $worksheet1->add_sparkline(
8671 {
8672 location => 'A17',
8673 range => 'Sheet2!A2:J2',
8674 type => 'column',
8675 style => 6,
8676 }
8677 );
8678
8679 $worksheet1->write( $row++, 1, $str );
8680
8681
8682 ###############################################################################
8683 #
8684 $str = 'Column with a user defined colour.';
8685
8686 $worksheet1->add_sparkline(
8687 {
8688 location => 'A18',
8689 range => 'Sheet2!A2:J2',
8690 type => 'column',
8691 series_color => '#E965E0',
8692 }
8693 );
8694
8695 $worksheet1->write( $row++, 1, $str );
8696 $row++;
8697
8698
8699 ###############################################################################
8700 #
8701 $str = 'A win/loss sparkline.';
8702
8703 $worksheet1->add_sparkline(
8704 {
8705 location => 'A20',
8706 range => 'Sheet2!A3:J3',
8707 type => 'win_loss',
8708 }
8709 );
8710
8711 $worksheet1->write( $row++, 1, $str );
8712
8713
8714 ###############################################################################
8715 #
8716 $str = 'A win/loss sparkline with negative points highlighted.';
8717
8718 $worksheet1->add_sparkline(
8719 {
8720 location => 'A21',
8721 range => 'Sheet2!A3:J3',
8722 type => 'win_loss',
8723 negative_points => 1,
8724 }
8725 );
8726
8727 $worksheet1->write( $row++, 1, $str );
8728 $row++;
8729
8730
8731 ###############################################################################
8732 #
8733 $str = 'A left to right column (the default).';
8734
8735 $worksheet1->add_sparkline(
8736 {
8737 location => 'A23',
8738 range => 'Sheet2!A4:J4',
8739 type => 'column',
8740 style => 20,
8741 }
8742 );
8743
8744 $worksheet1->write( $row++, 1, $str );
8745
8746
8747 ###############################################################################
8748 #
8749 $str = 'A right to left column.';
8750
8751 $worksheet1->add_sparkline(
8752 {
8753 location => 'A24',
8754 range => 'Sheet2!A4:J4',
8755 type => 'column',
8756 style => 20,
8757 reverse => 1,
8758 }
8759 );
8760
8761 $worksheet1->write( $row++, 1, $str );
8762
8763
8764 ###############################################################################
8765 #
8766 $str = 'Sparkline and text in one cell.';
8767
8768 $worksheet1->add_sparkline(
8769 {
8770 location => 'A25',
8771 range => 'Sheet2!A4:J4',
8772 type => 'column',
8773 style => 20,
8774 }
8775 );
8776
8777 $worksheet1->write( $row, 0, 'Growth' );
8778 $worksheet1->write( $row++, 1, $str );
8779 $row++;
8780
8781
8782 ###############################################################################
8783 #
8784 $str = 'A grouped sparkline. Changes are applied to all three.';
8785
8786 $worksheet1->add_sparkline(
8787 {
8788 location => [ 'A27', 'A28', 'A29' ],
8789 range => [ 'Sheet2!A5:J5', 'Sheet2!A6:J6', 'Sheet2!A7:J7' ],
8790 markers => 1,
8791 }
8792 );
8793
8794 $worksheet1->write( $row++, 1, $str );
8795
8796
8797
8798
8799 ###############################################################################
8800 #
8801 # Create a second worksheet with data to plot.
8802 #
8803
8804 $worksheet2->set_column( 'A:J', 11 );
8805
8806 my $data = [
8807
8808 # Simple line data.
8809 [ -2, 2, 3, -1, 0, -2, 3, 2, 1, 0 ],
8810
8811 # Simple column data.
8812 [ 30, 20, 33, 20, 15, 5, 5, 15, 10, 15 ],
8813
8814 # Simple win/loss data.
8815 [ 1, 1, -1, -1, 1, -1, 1, 1, 1, -1 ],
8816
8817 # Unbalanced histogram.
8818 [ 5, 6, 7, 10, 15, 20, 30, 50, 70, 100 ],
8819
8820 # Data for the grouped sparkline example.
8821 [ -2, 2, 3, -1, 0, -2, 3, 2, 1, 0 ],
8822 [ 3, -1, 0, -2, 3, 2, 1, 0, 2, 1 ],
8823 [ 0, -2, 3, 2, 1, 0, 1, 2, 3, 1 ],
8824
8825
8826 ];
8827
8828 # Write the sample data to the worksheet.
8829 $worksheet2->write_col( 'A1', $data );
8830
8831 $workbook->close();
8832
8833 __END__
8834
8835 Download this example:
8836 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/sparklines2.pl>
8837
8838 Example: stats_ext.pl
8839 Example of formatting using the Excel::Writer::XLSX module
8840
8841 This is a simple example of how to use functions that reference cells
8842 in other worksheets within the same workbook.
8843
8844 Source code for this example:
8845
8846 #!/usr/bin/perl -w
8847
8848 ###############################################################################
8849 #
8850 # Example of formatting using the Excel::Writer::XLSX module
8851 #
8852 # This is a simple example of how to use functions that reference cells in
8853 # other worksheets within the same workbook.
8854 #
8855 # reverse ('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
8856 #
8857
8858 use strict;
8859 use Excel::Writer::XLSX;
8860
8861 # Create a new workbook and add a worksheet
8862 my $workbook = Excel::Writer::XLSX->new( 'stats_ext.xlsx' );
8863 my $worksheet1 = $workbook->add_worksheet( 'Test results' );
8864 my $worksheet2 = $workbook->add_worksheet( 'Data' );
8865
8866 # Set the column width for columns 1
8867 $worksheet1->set_column( 'A:A', 20 );
8868
8869
8870 # Create a format for the headings
8871 my $heading = $workbook->add_format();
8872 $heading->set_bold();
8873
8874 # Create a numerical format
8875 my $numformat = $workbook->add_format();
8876 $numformat->set_num_format( '0.00' );
8877
8878
8879 # Write some statistical functions
8880 $worksheet1->write( 'A1', 'Count', $heading );
8881 $worksheet1->write( 'B1', '=COUNT(Data!B2:B9)' );
8882
8883 $worksheet1->write( 'A2', 'Sum', $heading );
8884 $worksheet1->write( 'B2', '=SUM(Data!B2:B9)' );
8885
8886 $worksheet1->write( 'A3', 'Average', $heading );
8887 $worksheet1->write( 'B3', '=AVERAGE(Data!B2:B9)' );
8888
8889 $worksheet1->write( 'A4', 'Min', $heading );
8890 $worksheet1->write( 'B4', '=MIN(Data!B2:B9)' );
8891
8892 $worksheet1->write( 'A5', 'Max', $heading );
8893 $worksheet1->write( 'B5', '=MAX(Data!B2:B9)' );
8894
8895 $worksheet1->write( 'A6', 'Standard Deviation', $heading );
8896 $worksheet1->write( 'B6', '=STDEV(Data!B2:B9)' );
8897
8898 $worksheet1->write( 'A7', 'Kurtosis', $heading );
8899 $worksheet1->write( 'B7', '=KURT(Data!B2:B9)' );
8900
8901
8902 # Write the sample data
8903 $worksheet2->write( 'A1', 'Sample', $heading );
8904 $worksheet2->write( 'A2', 1 );
8905 $worksheet2->write( 'A3', 2 );
8906 $worksheet2->write( 'A4', 3 );
8907 $worksheet2->write( 'A5', 4 );
8908 $worksheet2->write( 'A6', 5 );
8909 $worksheet2->write( 'A7', 6 );
8910 $worksheet2->write( 'A8', 7 );
8911 $worksheet2->write( 'A9', 8 );
8912
8913 $worksheet2->write( 'B1', 'Length', $heading );
8914 $worksheet2->write( 'B2', 25.4, $numformat );
8915 $worksheet2->write( 'B3', 25.4, $numformat );
8916 $worksheet2->write( 'B4', 24.8, $numformat );
8917 $worksheet2->write( 'B5', 25.0, $numformat );
8918 $worksheet2->write( 'B6', 25.3, $numformat );
8919 $worksheet2->write( 'B7', 24.9, $numformat );
8920 $worksheet2->write( 'B8', 25.2, $numformat );
8921 $worksheet2->write( 'B9', 24.8, $numformat );
8922
8923 $workbook->close();
8924
8925 __END__
8926
8927 Download this example:
8928 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/stats_ext.pl>
8929
8930 Example: stocks.pl
8931 Example of formatting using the Excel::Writer::XLSX module
8932
8933 This example shows how to use a conditional numerical format with
8934 colours to indicate if a share price has gone up or down.
8935
8936 Source code for this example:
8937
8938 #!/usr/bin/perl -w
8939
8940 ###############################################################################
8941 #
8942 # Example of formatting using the Excel::Writer::XLSX module
8943 #
8944 # This example shows how to use a conditional numerical format
8945 # with colours to indicate if a share price has gone up or down.
8946 #
8947 # reverse ('(c)'), March 2001, John McNamara, jmcnamara@cpan.org
8948 #
8949
8950 use strict;
8951 use Excel::Writer::XLSX;
8952
8953 # Create a new workbook and add a worksheet
8954 my $workbook = Excel::Writer::XLSX->new( 'stocks.xlsx' );
8955 my $worksheet = $workbook->add_worksheet();
8956
8957 # Set the column width for columns 1, 2, 3 and 4
8958 $worksheet->set_column( 0, 3, 15 );
8959
8960
8961 # Create a format for the column headings
8962 my $header = $workbook->add_format();
8963 $header->set_bold();
8964 $header->set_size( 12 );
8965 $header->set_color( 'blue' );
8966
8967
8968 # Create a format for the stock price
8969 my $f_price = $workbook->add_format();
8970 $f_price->set_align( 'left' );
8971 $f_price->set_num_format( '$0.00' );
8972
8973
8974 # Create a format for the stock volume
8975 my $f_volume = $workbook->add_format();
8976 $f_volume->set_align( 'left' );
8977 $f_volume->set_num_format( '#,##0' );
8978
8979
8980 # Create a format for the price change. This is an example of a conditional
8981 # format. The number is formatted as a percentage. If it is positive it is
8982 # formatted in green, if it is negative it is formatted in red and if it is
8983 # zero it is formatted as the default font colour (in this case black).
8984 # Note: the [Green] format produces an unappealing lime green. Try
8985 # [Color 10] instead for a dark green.
8986 #
8987 my $f_change = $workbook->add_format();
8988 $f_change->set_align( 'left' );
8989 $f_change->set_num_format( '[Green]0.0%;[Red]-0.0%;0.0%' );
8990
8991
8992 # Write out the data
8993 $worksheet->write( 0, 0, 'Company', $header );
8994 $worksheet->write( 0, 1, 'Price', $header );
8995 $worksheet->write( 0, 2, 'Volume', $header );
8996 $worksheet->write( 0, 3, 'Change', $header );
8997
8998 $worksheet->write( 1, 0, 'Damage Inc.' );
8999 $worksheet->write( 1, 1, 30.25, $f_price ); # $30.25
9000 $worksheet->write( 1, 2, 1234567, $f_volume ); # 1,234,567
9001 $worksheet->write( 1, 3, 0.085, $f_change ); # 8.5% in green
9002
9003 $worksheet->write( 2, 0, 'Dump Corp.' );
9004 $worksheet->write( 2, 1, 1.56, $f_price ); # $1.56
9005 $worksheet->write( 2, 2, 7564, $f_volume ); # 7,564
9006 $worksheet->write( 2, 3, -0.015, $f_change ); # -1.5% in red
9007
9008 $worksheet->write( 3, 0, 'Rev Ltd.' );
9009 $worksheet->write( 3, 1, 0.13, $f_price ); # $0.13
9010 $worksheet->write( 3, 2, 321, $f_volume ); # 321
9011 $worksheet->write( 3, 3, 0, $f_change ); # 0 in the font color (black)
9012
9013
9014 $workbook->close();
9015
9016 __END__
9017
9018 Download this example:
9019 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/stocks.pl>
9020
9021 Example: tab_colors.pl
9022 Example of how to set Excel worksheet tab colours.
9023
9024 Source code for this example:
9025
9026 #!/usr/bin/perl
9027
9028 #######################################################################
9029 #
9030 # Example of how to set Excel worksheet tab colours.
9031 #
9032 # reverse ('(c)'), May 2006, John McNamara, jmcnamara@cpan.org
9033 #
9034
9035 use strict;
9036 use warnings;
9037 use Excel::Writer::XLSX;
9038
9039
9040 my $workbook = Excel::Writer::XLSX->new( 'tab_colors.xlsx' );
9041
9042 my $worksheet1 = $workbook->add_worksheet();
9043 my $worksheet2 = $workbook->add_worksheet();
9044 my $worksheet3 = $workbook->add_worksheet();
9045 my $worksheet4 = $workbook->add_worksheet();
9046
9047 # Worksheet1 will have the default tab colour.
9048 $worksheet2->set_tab_color( 'red' );
9049 $worksheet3->set_tab_color( 'green' );
9050 $worksheet4->set_tab_color( '#FF6600'); # Orange
9051
9052 $workbook->close();
9053
9054 __END__
9055
9056 Download this example:
9057 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/tab_colors.pl>
9058
9059 Example: tables.pl
9060 Example of how to add tables to an Excel::Writer::XLSX worksheet.
9061
9062 Tables in Excel are used to group rows and columns of data into a
9063 single structure that can be referenced in a formula or formatted
9064 collectively.
9065
9066 Source code for this example:
9067
9068 #!/usr/bin/perl
9069
9070 ###############################################################################
9071 #
9072 # Example of how to add tables to an Excel::Writer::XLSX worksheet.
9073 #
9074 # Tables in Excel are used to group rows and columns of data into a single
9075 # structure that can be referenced in a formula or formatted collectively.
9076 #
9077 # reverse ('(c)'), September 2012, John McNamara, jmcnamara@cpan.org
9078 #
9079
9080 use strict;
9081 use warnings;
9082 use Excel::Writer::XLSX;
9083
9084 my $workbook = Excel::Writer::XLSX->new( 'tables.xlsx' );
9085 my $worksheet1 = $workbook->add_worksheet();
9086 my $worksheet2 = $workbook->add_worksheet();
9087 my $worksheet3 = $workbook->add_worksheet();
9088 my $worksheet4 = $workbook->add_worksheet();
9089 my $worksheet5 = $workbook->add_worksheet();
9090 my $worksheet6 = $workbook->add_worksheet();
9091 my $worksheet7 = $workbook->add_worksheet();
9092 my $worksheet8 = $workbook->add_worksheet();
9093 my $worksheet9 = $workbook->add_worksheet();
9094 my $worksheet10 = $workbook->add_worksheet();
9095 my $worksheet11 = $workbook->add_worksheet();
9096 my $worksheet12 = $workbook->add_worksheet();
9097 my $worksheet13 = $workbook->add_worksheet();
9098
9099 my $currency_format = $workbook->add_format( num_format => '$#,##0' );
9100
9101
9102 # Some sample data for the table.
9103 my $data = [
9104 [ 'Apples', 10000, 5000, 8000, 6000 ],
9105 [ 'Pears', 2000, 3000, 4000, 5000 ],
9106 [ 'Bananas', 6000, 6000, 6500, 6000 ],
9107 [ 'Oranges', 500, 300, 200, 700 ],
9108
9109 ];
9110
9111
9112 ###############################################################################
9113 #
9114 # Example 1.
9115 #
9116 my $caption = 'Default table with no data.';
9117
9118 # Set the columns widths.
9119 $worksheet1->set_column( 'B:G', 12 );
9120
9121 # Write the caption.
9122 $worksheet1->write( 'B1', $caption );
9123
9124 # Add a table to the worksheet.
9125 $worksheet1->add_table( 'B3:F7' );
9126
9127
9128 ###############################################################################
9129 #
9130 # Example 2.
9131 #
9132 $caption = 'Default table with data.';
9133
9134 # Set the columns widths.
9135 $worksheet2->set_column( 'B:G', 12 );
9136
9137 # Write the caption.
9138 $worksheet2->write( 'B1', $caption );
9139
9140 # Add a table to the worksheet.
9141 $worksheet2->add_table( 'B3:F7', { data => $data } );
9142
9143
9144 ###############################################################################
9145 #
9146 # Example 3.
9147 #
9148 $caption = 'Table without default autofilter.';
9149
9150 # Set the columns widths.
9151 $worksheet3->set_column( 'B:G', 12 );
9152
9153 # Write the caption.
9154 $worksheet3->write( 'B1', $caption );
9155
9156 # Add a table to the worksheet.
9157 $worksheet3->add_table( 'B3:F7', { autofilter => 0 } );
9158
9159 # Table data can also be written separately, as an array or individual cells.
9160 $worksheet3->write_col( 'B4', $data );
9161
9162
9163 ###############################################################################
9164 #
9165 # Example 4.
9166 #
9167 $caption = 'Table without default header row.';
9168
9169 # Set the columns widths.
9170 $worksheet4->set_column( 'B:G', 12 );
9171
9172 # Write the caption.
9173 $worksheet4->write( 'B1', $caption );
9174
9175 # Add a table to the worksheet.
9176 $worksheet4->add_table( 'B4:F7', { header_row => 0 } );
9177
9178 # Table data can also be written separately, as an array or individual cells.
9179 $worksheet4->write_col( 'B4', $data );
9180
9181
9182 ###############################################################################
9183 #
9184 # Example 5.
9185 #
9186 $caption = 'Default table with "First Column" and "Last Column" options.';
9187
9188 # Set the columns widths.
9189 $worksheet5->set_column( 'B:G', 12 );
9190
9191 # Write the caption.
9192 $worksheet5->write( 'B1', $caption );
9193
9194 # Add a table to the worksheet.
9195 $worksheet5->add_table( 'B3:F7', { first_column => 1, last_column => 1 } );
9196
9197 # Table data can also be written separately, as an array or individual cells.
9198 $worksheet5->write_col( 'B4', $data );
9199
9200
9201 ###############################################################################
9202 #
9203 # Example 6.
9204 #
9205 $caption = 'Table with banded columns but without default banded rows.';
9206
9207 # Set the columns widths.
9208 $worksheet6->set_column( 'B:G', 12 );
9209
9210 # Write the caption.
9211 $worksheet6->write( 'B1', $caption );
9212
9213 # Add a table to the worksheet.
9214 $worksheet6->add_table( 'B3:F7', { banded_rows => 0, banded_columns => 1 } );
9215
9216 # Table data can also be written separately, as an array or individual cells.
9217 $worksheet6->write_col( 'B4', $data );
9218
9219
9220 ###############################################################################
9221 #
9222 # Example 7.
9223 #
9224 $caption = 'Table with user defined column headers';
9225
9226 # Set the columns widths.
9227 $worksheet7->set_column( 'B:G', 12 );
9228
9229 # Write the caption.
9230 $worksheet7->write( 'B1', $caption );
9231
9232 # Add a table to the worksheet.
9233 $worksheet7->add_table(
9234 'B3:F7',
9235 {
9236 data => $data,
9237 columns => [
9238 { header => 'Product' },
9239 { header => 'Quarter 1' },
9240 { header => 'Quarter 2' },
9241 { header => 'Quarter 3' },
9242 { header => 'Quarter 4' },
9243 ]
9244 }
9245 );
9246
9247
9248 ###############################################################################
9249 #
9250 # Example 8.
9251 #
9252 $caption = 'Table with user defined column headers';
9253
9254 # Set the columns widths.
9255 $worksheet8->set_column( 'B:G', 12 );
9256
9257 # Write the caption.
9258 $worksheet8->write( 'B1', $caption );
9259
9260 # Add a table to the worksheet.
9261 $worksheet8->add_table(
9262 'B3:G7',
9263 {
9264 data => $data,
9265 columns => [
9266 { header => 'Product' },
9267 { header => 'Quarter 1' },
9268 { header => 'Quarter 2' },
9269 { header => 'Quarter 3' },
9270 { header => 'Quarter 4' },
9271 {
9272 header => 'Year',
9273 formula => '=SUM(Table8[@[Quarter 1]:[Quarter 4]])'
9274 },
9275 ]
9276 }
9277 );
9278
9279
9280 ###############################################################################
9281 #
9282 # Example 9.
9283 #
9284 $caption = 'Table with totals row (but no caption or totals).';
9285
9286 # Set the columns widths.
9287 $worksheet9->set_column( 'B:G', 12 );
9288
9289 # Write the caption.
9290 $worksheet9->write( 'B1', $caption );
9291
9292 # Add a table to the worksheet.
9293 $worksheet9->add_table(
9294 'B3:G8',
9295 {
9296 data => $data,
9297 total_row => 1,
9298 columns => [
9299 { header => 'Product' },
9300 { header => 'Quarter 1' },
9301 { header => 'Quarter 2' },
9302 { header => 'Quarter 3' },
9303 { header => 'Quarter 4' },
9304 {
9305 header => 'Year',
9306 formula => '=SUM(Table9[@[Quarter 1]:[Quarter 4]])'
9307 },
9308 ]
9309 }
9310 );
9311
9312
9313 ###############################################################################
9314 #
9315 # Example 10.
9316 #
9317 $caption = 'Table with totals row with user captions and functions.';
9318
9319 # Set the columns widths.
9320 $worksheet10->set_column( 'B:G', 12 );
9321
9322 # Write the caption.
9323 $worksheet10->write( 'B1', $caption );
9324
9325 # Add a table to the worksheet.
9326 $worksheet10->add_table(
9327 'B3:G8',
9328 {
9329 data => $data,
9330 total_row => 1,
9331 columns => [
9332 { header => 'Product', total_string => 'Totals' },
9333 { header => 'Quarter 1', total_function => 'sum' },
9334 { header => 'Quarter 2', total_function => 'sum' },
9335 { header => 'Quarter 3', total_function => 'sum' },
9336 { header => 'Quarter 4', total_function => 'sum' },
9337 {
9338 header => 'Year',
9339 formula => '=SUM(Table10[@[Quarter 1]:[Quarter 4]])',
9340 total_function => 'sum'
9341 },
9342 ]
9343 }
9344 );
9345
9346
9347 ###############################################################################
9348 #
9349 # Example 11.
9350 #
9351 $caption = 'Table with alternative Excel style.';
9352
9353 # Set the columns widths.
9354 $worksheet11->set_column( 'B:G', 12 );
9355
9356 # Write the caption.
9357 $worksheet11->write( 'B1', $caption );
9358
9359 # Add a table to the worksheet.
9360 $worksheet11->add_table(
9361 'B3:G8',
9362 {
9363 data => $data,
9364 style => 'Table Style Light 11',
9365 total_row => 1,
9366 columns => [
9367 { header => 'Product', total_string => 'Totals' },
9368 { header => 'Quarter 1', total_function => 'sum' },
9369 { header => 'Quarter 2', total_function => 'sum' },
9370 { header => 'Quarter 3', total_function => 'sum' },
9371 { header => 'Quarter 4', total_function => 'sum' },
9372 {
9373 header => 'Year',
9374 formula => '=SUM(Table11[@[Quarter 1]:[Quarter 4]])',
9375 total_function => 'sum'
9376 },
9377 ]
9378 }
9379 );
9380
9381
9382 ###############################################################################
9383 #
9384 # Example 12.
9385 #
9386 $caption = 'Table with no Excel style.';
9387
9388 # Set the columns widths.
9389 $worksheet12->set_column( 'B:G', 12 );
9390
9391 # Write the caption.
9392 $worksheet12->write( 'B1', $caption );
9393
9394 # Add a table to the worksheet.
9395 $worksheet12->add_table(
9396 'B3:G8',
9397 {
9398 data => $data,
9399 style => 'None',
9400 total_row => 1,
9401 columns => [
9402 { header => 'Product', total_string => 'Totals' },
9403 { header => 'Quarter 1', total_function => 'sum' },
9404 { header => 'Quarter 2', total_function => 'sum' },
9405 { header => 'Quarter 3', total_function => 'sum' },
9406 { header => 'Quarter 4', total_function => 'sum' },
9407 {
9408 header => 'Year',
9409 formula => '=SUM(Table12[@[Quarter 1]:[Quarter 4]])',
9410 total_function => 'sum'
9411 },
9412 ]
9413 }
9414 );
9415
9416
9417 ###############################################################################
9418 #
9419 # Example 13.
9420 #
9421 $caption = 'Table with column formats.';
9422
9423 # Set the columns widths.
9424 $worksheet13->set_column( 'B:G', 12 );
9425
9426 # Write the caption.
9427 $worksheet13->write( 'B1', $caption );
9428
9429 # Add a table to the worksheet.
9430 $worksheet13->add_table(
9431 'B3:G8',
9432 {
9433 data => $data,
9434 total_row => 1,
9435 columns => [
9436 { header => 'Product', total_string => 'Totals' },
9437 {
9438 header => 'Quarter 1',
9439 total_function => 'sum',
9440 format => $currency_format,
9441 },
9442 {
9443 header => 'Quarter 2',
9444 total_function => 'sum',
9445 format => $currency_format,
9446 },
9447 {
9448 header => 'Quarter 3',
9449 total_function => 'sum',
9450 format => $currency_format,
9451 },
9452 {
9453 header => 'Quarter 4',
9454 total_function => 'sum',
9455 format => $currency_format,
9456 },
9457 {
9458 header => 'Year',
9459 formula => '=SUM(Table13[@[Quarter 1]:[Quarter 4]])',
9460 total_function => 'sum',
9461 format => $currency_format,
9462 },
9463 ]
9464 }
9465 );
9466
9467
9468 $workbook->close();
9469
9470 __END__
9471
9472 Download this example:
9473 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/tables.pl>
9474
9475 Example: write_handler1.pl
9476 Example of how to add a user defined data handler to the
9477 Excel::Writer::XLSX write() method.
9478
9479 The following example shows how to add a handler for a 7 digit ID
9480 number.
9481
9482 Source code for this example:
9483
9484 #!/usr/bin/perl -w
9485
9486 ###############################################################################
9487 #
9488 # Example of how to add a user defined data handler to the
9489 # Excel::Writer::XLSX write() method.
9490 #
9491 # The following example shows how to add a handler for a 7 digit ID number.
9492 #
9493 #
9494 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
9495 #
9496
9497 use strict;
9498 use Excel::Writer::XLSX;
9499
9500
9501 my $workbook = Excel::Writer::XLSX->new( 'write_handler1.xlsx' );
9502 my $worksheet = $workbook->add_worksheet();
9503
9504
9505 ###############################################################################
9506 #
9507 # Add a handler for 7 digit id numbers. This is useful when you want a string
9508 # such as 0000001 written as a string instead of a number and thus preserve
9509 # the leading zeroes.
9510 #
9511 # Note: you can get the same effect using the keep_leading_zeros() method but
9512 # this serves as a simple example.
9513 #
9514 $worksheet->add_write_handler( qr[^\d{7}$], \&write_my_id );
9515
9516
9517 ###############################################################################
9518 #
9519 # The following function processes the data when a match is found.
9520 #
9521 sub write_my_id {
9522
9523 my $worksheet = shift;
9524
9525 return $worksheet->write_string( @_ );
9526 }
9527
9528
9529 # This format maintains the cell as text even if it is edited.
9530 my $id_format = $workbook->add_format( num_format => '@' );
9531
9532
9533 # Write some numbers in the user defined format
9534 $worksheet->write( 'A1', '0000000', $id_format );
9535 $worksheet->write( 'A2', '0000001', $id_format );
9536 $worksheet->write( 'A3', '0004000', $id_format );
9537 $worksheet->write( 'A4', '1234567', $id_format );
9538
9539 # Write some numbers that don't match the defined format
9540 $worksheet->write( 'A6', '000000', $id_format );
9541 $worksheet->write( 'A7', '000001', $id_format );
9542 $worksheet->write( 'A8', '004000', $id_format );
9543 $worksheet->write( 'A9', '123456', $id_format );
9544
9545 $workbook->close();
9546
9547 __END__
9548
9549 Download this example:
9550 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/write_handler1.pl>
9551
9552 Example: write_handler2.pl
9553 Example of how to add a user defined data handler to the
9554 Excel::Writer::XLSX write() method.
9555
9556 The following example shows how to add a handler for a 7 digit ID
9557 number. It adds an additional constraint to the write_handler1.pl in
9558 that it only filters data that isn't in the third column.
9559
9560 Source code for this example:
9561
9562 #!/usr/bin/perl -w
9563
9564 ###############################################################################
9565 #
9566 # Example of how to add a user defined data handler to the
9567 # Excel::Writer::XLSX write() method.
9568 #
9569 # The following example shows how to add a handler for a 7 digit ID number.
9570 # It adds an additional constraint to the write_handler1.pl in that it only
9571 # filters data that isn't in the third column.
9572 #
9573 #
9574 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
9575 #
9576
9577 use strict;
9578 use Excel::Writer::XLSX;
9579
9580
9581 my $workbook = Excel::Writer::XLSX->new( 'write_handler2.xlsx' );
9582 my $worksheet = $workbook->add_worksheet();
9583
9584
9585 ###############################################################################
9586 #
9587 # Add a handler for 7 digit id numbers. This is useful when you want a string
9588 # such as 0000001 written as a string instead of a number and thus preserve
9589 # the leading zeroes.
9590 #
9591 # Note: you can get the same effect using the keep_leading_zeros() method but
9592 # this serves as a simple example.
9593 #
9594 $worksheet->add_write_handler( qr[^\d{7}$], \&write_my_id );
9595
9596
9597 ###############################################################################
9598 #
9599 # The following function processes the data when a match is found. The handler
9600 # is set up so that it only filters data if it is in the third column.
9601 #
9602 sub write_my_id {
9603
9604 my $worksheet = shift;
9605 my $col = $_[1];
9606
9607 # col is zero based
9608 if ( $col != 2 ) {
9609 return $worksheet->write_string( @_ );
9610 }
9611 else {
9612
9613 # Reject the match and return control to write()
9614 return undef;
9615 }
9616
9617 }
9618
9619
9620 # This format maintains the cell as text even if it is edited.
9621 my $id_format = $workbook->add_format( num_format => '@' );
9622
9623
9624 # Write some numbers in the user defined format
9625 $worksheet->write( 'A1', '0000000', $id_format );
9626 $worksheet->write( 'B1', '0000001', $id_format );
9627 $worksheet->write( 'C1', '0000002', $id_format );
9628 $worksheet->write( 'D1', '0000003', $id_format );
9629
9630 $workbook->close();
9631
9632 __END__
9633
9634 Download this example:
9635 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/write_handler2.pl>
9636
9637 Example: write_handler3.pl
9638 Example of how to add a user defined data handler to the
9639 Excel::Writer::XLSX write() method.
9640
9641 The following example shows how to add a handler for dates in a
9642 specific format.
9643
9644 See write_handler4.pl for a more rigorous example with error handling.
9645
9646 Source code for this example:
9647
9648 #!/usr/bin/perl -w
9649
9650 ###############################################################################
9651 #
9652 # Example of how to add a user defined data handler to the
9653 # Excel::Writer::XLSX write() method.
9654 #
9655 # The following example shows how to add a handler for dates in a specific
9656 # format.
9657 #
9658 # See write_handler4.pl for a more rigorous example with error handling.
9659 #
9660 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
9661 #
9662
9663 use strict;
9664 use Excel::Writer::XLSX;
9665
9666
9667 my $workbook = Excel::Writer::XLSX->new( 'write_handler3.xlsx' );
9668 my $worksheet = $workbook->add_worksheet();
9669 my $date_format = $workbook->add_format( num_format => 'dd/mm/yy' );
9670
9671
9672 ###############################################################################
9673 #
9674 # Add a handler to match dates in the following format: d/m/yyyy
9675 #
9676 # The day and month can be single or double digits.
9677 #
9678 $worksheet->add_write_handler( qr[^\d{1,2}/\d{1,2}/\d{4}$], \&write_my_date );
9679
9680
9681 ###############################################################################
9682 #
9683 # The following function processes the data when a match is found.
9684 # See write_handler4.pl for a more rigorous example with error handling.
9685 #
9686 sub write_my_date {
9687
9688 my $worksheet = shift;
9689 my @args = @_;
9690
9691 my $token = $args[2];
9692 $token =~ qr[^(\d{1,2})/(\d{1,2})/(\d{4})$];
9693
9694 # Change to the date format required by write_date_time().
9695 my $date = sprintf "%4d-%02d-%02dT", $3, $2, $1;
9696
9697 $args[2] = $date;
9698
9699 return $worksheet->write_date_time( @args );
9700 }
9701
9702
9703 # Write some dates in the user defined format
9704 $worksheet->write( 'A1', '22/12/2004', $date_format );
9705 $worksheet->write( 'A2', '1/1/1995', $date_format );
9706 $worksheet->write( 'A3', '01/01/1995', $date_format );
9707
9708 $workbook->close();
9709
9710 __END__
9711
9712 Download this example:
9713 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/write_handler3.pl>
9714
9715 Example: write_handler4.pl
9716 Example of how to add a user defined data handler to the
9717 Excel::Writer::XLSX write() method.
9718
9719 The following example shows how to add a handler for dates in a
9720 specific format.
9721
9722 This is a more rigorous version of write_handler3.pl.
9723
9724 Source code for this example:
9725
9726 #!/usr/bin/perl -w
9727
9728 ###############################################################################
9729 #
9730 # Example of how to add a user defined data handler to the
9731 # Excel::Writer::XLSX write() method.
9732 #
9733 # The following example shows how to add a handler for dates in a specific
9734 # format.
9735 #
9736 # This is a more rigorous version of write_handler3.pl.
9737 #
9738 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
9739 #
9740
9741 use strict;
9742 use Excel::Writer::XLSX;
9743
9744
9745 my $workbook = Excel::Writer::XLSX->new( 'write_handler4.xlsx' );
9746 my $worksheet = $workbook->add_worksheet();
9747 my $date_format = $workbook->add_format( num_format => 'dd/mm/yy' );
9748
9749
9750 ###############################################################################
9751 #
9752 # Add a handler to match dates in the following formats: d/m/yy, d/m/yyyy
9753 #
9754 # The day and month can be single or double digits and the year can be 2 or 4
9755 # digits.
9756 #
9757 $worksheet->add_write_handler( qr[^\d{1,2}/\d{1,2}/\d{2,4}$], \&write_my_date );
9758
9759
9760 ###############################################################################
9761 #
9762 # The following function processes the data when a match is found.
9763 #
9764 sub write_my_date {
9765
9766 my $worksheet = shift;
9767 my @args = @_;
9768
9769 my $token = $args[2];
9770
9771 if ( $token =~ qr[^(\d{1,2})/(\d{1,2})/(\d{2,4})$] ) {
9772
9773 my $day = $1;
9774 my $mon = $2;
9775 my $year = $3;
9776
9777 # Use a window for 2 digit dates. This will keep some ragged Perl
9778 # programmer employed in thirty years time. :-)
9779 if ( length $year == 2 ) {
9780 if ( $year < 50 ) {
9781 $year += 2000;
9782 }
9783 else {
9784 $year += 1900;
9785 }
9786 }
9787
9788 my $date = sprintf "%4d-%02d-%02dT", $year, $mon, $day;
9789
9790 # Convert the ISO ISO8601 style string to an Excel date
9791 $date = $worksheet->convert_date_time( $date );
9792
9793 if ( defined $date ) {
9794
9795 # Date was valid
9796 $args[2] = $date;
9797 return $worksheet->write_number( @args );
9798 }
9799 else {
9800
9801 # Not a valid date therefore write as a string
9802 return $worksheet->write_string( @args );
9803 }
9804 }
9805 else {
9806
9807 # Shouldn't happen if the same match is used in the re and sub.
9808 return undef;
9809 }
9810 }
9811
9812
9813 # Write some dates in the user defined format
9814 $worksheet->write( 'A1', '22/12/2004', $date_format );
9815 $worksheet->write( 'A2', '22/12/04', $date_format );
9816 $worksheet->write( 'A3', '2/12/04', $date_format );
9817 $worksheet->write( 'A4', '2/5/04', $date_format );
9818 $worksheet->write( 'A5', '2/5/95', $date_format );
9819 $worksheet->write( 'A6', '2/5/1995', $date_format );
9820
9821 # Some erroneous dates
9822 $worksheet->write( 'A8', '2/5/1895', $date_format ); # Date out of Excel range
9823 $worksheet->write( 'A9', '29/2/2003', $date_format ); # Invalid leap day
9824 $worksheet->write( 'A10', '50/50/50', $date_format ); # Matches but isn't a date
9825
9826 $workbook->close();
9827
9828 __END__
9829
9830 Download this example:
9831 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/write_handler4.pl>
9832
9833 Example: write_to_scalar.pl
9834 An example of writing an Excel::Writer::XLSX file to a perl scalar.
9835
9836 #!/usr/bin/perl
9837
9838 ##############################################################################
9839 #
9840 # An example of writing an Excel::Writer::XLSX file to a perl scalar.
9841 #
9842 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
9843 #
9844
9845 use strict;
9846 use warnings;
9847 use Excel::Writer::XLSX;
9848
9849 # Use a scalar as a filehandle.
9850 open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
9851
9852
9853 # Spreadsheet::WriteExce accepts filehandle as well as file names.
9854 my $workbook = Excel::Writer::XLSX->new( $fh );
9855 my $worksheet = $workbook->add_worksheet();
9856
9857 $worksheet->write( 0, 0, 'Hi Excel!' );
9858
9859 $workbook->close();
9860
9861
9862 # The Excel file in now in $str. Remember to binmode() the output
9863 # filehandle before printing it.
9864 open my $out_fh, '>', 'write_to_scalar.xlsx'
9865 or die "Failed to open out filehandle: $!";
9866
9867 binmode $out_fh;
9868 print $out_fh $str;
9869 close $out_fh;
9870
9871 __END__
9872
9873 Download this example:
9874 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/write_to_scalar.pl>
9875
9876 Example: unicode_2022_jp.pl
9877 A simple example of converting some Unicode text to an Excel file using
9878 Excel::Writer::XLSX.
9879
9880 This example generates some Japanese from a file with ISO-2022-JP
9881 encoded text.
9882
9883 Source code for this example:
9884
9885 #!/usr/bin/perl
9886
9887 ##############################################################################
9888 #
9889 # A simple example of converting some Unicode text to an Excel file using
9890 # Excel::Writer::XLSX.
9891 #
9892 # This example generates some Japanese from a file with ISO-2022-JP
9893 # encoded text.
9894 #
9895 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
9896 #
9897
9898 use strict;
9899 use warnings;
9900 use Excel::Writer::XLSX;
9901
9902
9903 my $workbook = Excel::Writer::XLSX->new( 'unicode_2022_jp.xlsx' );
9904
9905 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
9906
9907 my $worksheet = $workbook->add_worksheet();
9908 $worksheet->set_column( 'A:A', 50 );
9909
9910
9911 my $file = 'unicode_2022_jp.txt';
9912
9913 open FH, '<:encoding(iso-2022-jp)', $file or die "Couldn't open $file: $!\n";
9914
9915 my $row = 0;
9916
9917 while ( <FH> ) {
9918 next if /^#/; # Ignore the comments in the sample file.
9919 chomp;
9920 $worksheet->write( $row++, 0, $_ );
9921 }
9922
9923 $workbook->close();
9924
9925 __END__
9926
9927 Download this example:
9928 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_2022_jp.pl>
9929
9930 Example: unicode_8859_11.pl
9931 A simple example of converting some Unicode text to an Excel file using
9932 Excel::Writer::XLSX.
9933
9934 This example generates some Thai from a file with ISO-8859-11 encoded
9935 text.
9936
9937 Source code for this example:
9938
9939 #!/usr/bin/perl
9940
9941 ##############################################################################
9942 #
9943 # A simple example of converting some Unicode text to an Excel file using
9944 # Excel::Writer::XLSX.
9945 #
9946 # This example generates some Thai from a file with ISO-8859-11 encoded text.
9947 #
9948 #
9949 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
9950 #
9951
9952 use strict;
9953 use warnings;
9954 use Excel::Writer::XLSX;
9955
9956
9957 my $workbook = Excel::Writer::XLSX->new( 'unicode_8859_11.xlsx' );
9958
9959 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
9960
9961 my $worksheet = $workbook->add_worksheet();
9962 $worksheet->set_column( 'A:A', 50 );
9963
9964
9965 my $file = 'unicode_8859_11.txt';
9966
9967 open FH, '<:encoding(iso-8859-11)', $file or die "Couldn't open $file: $!\n";
9968
9969 my $row = 0;
9970
9971 while ( <FH> ) {
9972 next if /^#/; # Ignore the comments in the sample file.
9973 chomp;
9974 $worksheet->write( $row++, 0, $_ );
9975 }
9976
9977 $workbook->close();
9978
9979 __END__
9980
9981 Download this example:
9982 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_8859_11.pl>
9983
9984 Example: unicode_8859_7.pl
9985 A simple example of converting some Unicode text to an Excel file using
9986 Excel::Writer::XLSX.
9987
9988 This example generates some Greek from a file with ISO-8859-7 encoded
9989 text.
9990
9991 Source code for this example:
9992
9993 #!/usr/bin/perl
9994
9995 ##############################################################################
9996 #
9997 # A simple example of converting some Unicode text to an Excel file using
9998 # Excel::Writer::XLSX.
9999 #
10000 # This example generates some Greek from a file with ISO-8859-7 encoded text.
10001 #
10002 #
10003 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
10004 #
10005
10006 use strict;
10007 use warnings;
10008 use Excel::Writer::XLSX;
10009
10010
10011 my $workbook = Excel::Writer::XLSX->new( 'unicode_8859_7.xlsx' );
10012
10013 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10014
10015 my $worksheet = $workbook->add_worksheet();
10016 $worksheet->set_column( 'A:A', 50 );
10017
10018
10019 my $file = 'unicode_8859_7.txt';
10020
10021 open FH, '<:encoding(iso-8859-7)', $file or die "Couldn't open $file: $!\n";
10022
10023 my $row = 0;
10024
10025 while ( <FH> ) {
10026 next if /^#/; # Ignore the comments in the sample file.
10027 chomp;
10028 $worksheet->write( $row++, 0, $_ );
10029 }
10030
10031 $workbook->close();
10032
10033 __END__
10034
10035 Download this example:
10036 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_8859_7.pl>
10037
10038 Example: unicode_big5.pl
10039 A simple example of converting some Unicode text to an Excel file using
10040 Excel::Writer::XLSX.
10041
10042 This example generates some Chinese from a file with BIG5 encoded text.
10043
10044 Source code for this example:
10045
10046 #!/usr/bin/perl
10047
10048 ##############################################################################
10049 #
10050 # A simple example of converting some Unicode text to an Excel file using
10051 # Excel::Writer::XLSX.
10052 #
10053 # This example generates some Chinese from a file with BIG5 encoded text.
10054 #
10055 #
10056 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
10057 #
10058
10059 use strict;
10060 use warnings;
10061 use Excel::Writer::XLSX;
10062
10063
10064 my $workbook = Excel::Writer::XLSX->new( 'unicode_big5.xlsx' );
10065
10066 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10067
10068 my $worksheet = $workbook->add_worksheet();
10069 $worksheet->set_column( 'A:A', 80 );
10070
10071
10072 my $file = 'unicode_big5.txt';
10073
10074 open FH, '<:encoding(big5)', $file or die "Couldn't open $file: $!\n";
10075
10076 my $row = 0;
10077
10078 while ( <FH> ) {
10079 next if /^#/; # Ignore the comments in the sample file.
10080 chomp;
10081 $worksheet->write( $row++, 0, $_ );
10082 }
10083
10084 $workbook->close();
10085
10086 __END__
10087
10088 Download this example:
10089 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_big5.pl>
10090
10091 Example: unicode_cp1251.pl
10092 A simple example of converting some Unicode text to an Excel file using
10093 Excel::Writer::XLSX.
10094
10095 This example generates some Russian from a file with CP1251 encoded
10096 text.
10097
10098 Source code for this example:
10099
10100 #!/usr/bin/perl
10101
10102 ##############################################################################
10103 #
10104 # A simple example of converting some Unicode text to an Excel file using
10105 # Excel::Writer::XLSX.
10106 #
10107 # This example generates some Russian from a file with CP1251 encoded text.
10108 #
10109 #
10110 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
10111 #
10112
10113 use strict;
10114 use warnings;
10115 use Excel::Writer::XLSX;
10116
10117
10118 my $workbook = Excel::Writer::XLSX->new( 'unicode_cp1251.xlsx' );
10119
10120 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10121
10122 my $worksheet = $workbook->add_worksheet();
10123 $worksheet->set_column( 'A:A', 50 );
10124
10125
10126 my $file = 'unicode_cp1251.txt';
10127
10128 open FH, '<:encoding(cp1251)', $file or die "Couldn't open $file: $!\n";
10129
10130 my $row = 0;
10131
10132 while ( <FH> ) {
10133 next if /^#/; # Ignore the comments in the sample file.
10134 chomp;
10135 $worksheet->write( $row++, 0, $_ );
10136 }
10137
10138 $workbook->close();
10139
10140 __END__
10141
10142 Download this example:
10143 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_cp1251.pl>
10144
10145 Example: unicode_cp1256.pl
10146 A simple example of converting some Unicode text to an Excel file using
10147 Excel::Writer::XLSX.
10148
10149 This example generates some Arabic text from a CP-1256 encoded file.
10150
10151 Source code for this example:
10152
10153 #!/usr/bin/perl
10154
10155 ##############################################################################
10156 #
10157 # A simple example of converting some Unicode text to an Excel file using
10158 # Excel::Writer::XLSX.
10159 #
10160 # This example generates some Arabic text from a CP-1256 encoded file.
10161 #
10162 #
10163 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
10164 #
10165
10166 use strict;
10167 use warnings;
10168 use Excel::Writer::XLSX;
10169
10170
10171 my $workbook = Excel::Writer::XLSX->new( 'unicode_cp1256.xlsx' );
10172
10173 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10174
10175 my $worksheet = $workbook->add_worksheet();
10176 $worksheet->set_column( 'A:A', 50 );
10177
10178
10179 my $file = 'unicode_cp1256.txt';
10180
10181 open FH, '<:encoding(cp1256)', $file or die "Couldn't open $file: $!\n";
10182
10183 my $row = 0;
10184
10185 while ( <FH> ) {
10186 next if /^#/; # Ignore the comments in the sample file.
10187 chomp;
10188 $worksheet->write( $row++, 0, $_ );
10189 }
10190
10191 $workbook->close();
10192
10193 __END__
10194
10195 Download this example:
10196 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_cp1256.pl>
10197
10198 Example: unicode_cyrillic.pl
10199 A simple example of writing some Russian cyrillic text using
10200 Excel::Writer::XLSX.
10201
10202 Source code for this example:
10203
10204 #!/usr/bin/perl
10205
10206 ##############################################################################
10207 #
10208 # A simple example of writing some Russian cyrillic text using
10209 # Excel::Writer::XLSX.
10210 #
10211 #
10212 #
10213 #
10214 # reverse ('(c)'), March 2005, John McNamara, jmcnamara@cpan.org
10215 #
10216
10217 use strict;
10218 use warnings;
10219 use Excel::Writer::XLSX;
10220
10221
10222 # In this example we generate utf8 strings from character data but in a
10223 # real application we would expect them to come from an external source.
10224 #
10225
10226
10227 # Create a Russian worksheet name in utf8.
10228 my $sheet = pack "U*", 0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438,
10229 0x0446, 0x0430;
10230
10231
10232 # Create a Russian string.
10233 my $str = pack "U*", 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441,
10234 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C,
10235 0x0438, 0x0440, 0x0021;
10236
10237
10238 my $workbook = Excel::Writer::XLSX->new( 'unicode_cyrillic.xlsx' );
10239
10240 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10241
10242 my $worksheet = $workbook->add_worksheet( $sheet . '1' );
10243
10244 $worksheet->set_column( 'A:A', 18 );
10245 $worksheet->write( 'A1', $str );
10246
10247 $workbook->close();
10248
10249 __END__
10250
10251 Download this example:
10252 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_cyrillic.pl>
10253
10254 Example: unicode_koi8r.pl
10255 A simple example of converting some Unicode text to an Excel file using
10256 Excel::Writer::XLSX.
10257
10258 This example generates some Russian from a file with KOI8-R encoded
10259 text.
10260
10261 Source code for this example:
10262
10263 #!/usr/bin/perl
10264
10265 ##############################################################################
10266 #
10267 # A simple example of converting some Unicode text to an Excel file using
10268 # Excel::Writer::XLSX.
10269 #
10270 # This example generates some Russian from a file with KOI8-R encoded text.
10271 #
10272 #
10273 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
10274 #
10275
10276 use strict;
10277 use warnings;
10278 use Excel::Writer::XLSX;
10279
10280
10281 my $workbook = Excel::Writer::XLSX->new( 'unicode_koi8r.xlsx' );
10282
10283 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10284
10285 my $worksheet = $workbook->add_worksheet();
10286 $worksheet->set_column( 'A:A', 50 );
10287
10288
10289 my $file = 'unicode_koi8r.txt';
10290
10291 open FH, '<:encoding(koi8-r)', $file or die "Couldn't open $file: $!\n";
10292
10293 my $row = 0;
10294
10295 while ( <FH> ) {
10296 next if /^#/; # Ignore the comments in the sample file.
10297 chomp;
10298 $worksheet->write( $row++, 0, $_ );
10299 }
10300
10301 $workbook->close();
10302
10303 __END__
10304
10305 Download this example:
10306 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_koi8r.pl>
10307
10308 Example: unicode_polish_utf8.pl
10309 A simple example of converting some Unicode text to an Excel file using
10310 Excel::Writer::XLSX.
10311
10312 This example generates some Polish from a file with UTF8 encoded text.
10313
10314 Source code for this example:
10315
10316 #!/usr/bin/perl
10317
10318 ##############################################################################
10319 #
10320 # A simple example of converting some Unicode text to an Excel file using
10321 # Excel::Writer::XLSX.
10322 #
10323 # This example generates some Polish from a file with UTF8 encoded text.
10324 #
10325 #
10326 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
10327 #
10328
10329 use strict;
10330 use warnings;
10331 use Excel::Writer::XLSX;
10332
10333
10334 my $workbook = Excel::Writer::XLSX->new( 'unicode_polish_utf8.xlsx' );
10335
10336 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10337
10338 my $worksheet = $workbook->add_worksheet();
10339 $worksheet->set_column( 'A:A', 50 );
10340
10341
10342 my $file = 'unicode_polish_utf8.txt';
10343
10344 open FH, '<:encoding(utf8)', $file or die "Couldn't open $file: $!\n";
10345
10346 my $row = 0;
10347
10348 while ( <FH> ) {
10349 next if /^#/; # Ignore the comments in the sample file.
10350 chomp;
10351 $worksheet->write( $row++, 0, $_ );
10352 }
10353
10354 $workbook->close();
10355
10356 __END__
10357
10358 Download this example:
10359 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_polish_utf8.pl>
10360
10361 Example: unicode_shift_jis.pl
10362 A simple example of converting some Unicode text to an Excel file using
10363 Excel::Writer::XLSX.
10364
10365 This example generates some Japenese text from a file with Shift-JIS
10366 encoded text.
10367
10368 Source code for this example:
10369
10370 #!/usr/bin/perl
10371
10372 ##############################################################################
10373 #
10374 # A simple example of converting some Unicode text to an Excel file using
10375 # Excel::Writer::XLSX.
10376 #
10377 # This example generates some Japenese text from a file with Shift-JIS
10378 # encoded text.
10379 #
10380 # reverse ('(c)'), September 2004, John McNamara, jmcnamara@cpan.org
10381 #
10382
10383 use strict;
10384 use warnings;
10385 use Excel::Writer::XLSX;
10386
10387
10388 my $workbook = Excel::Writer::XLSX->new( 'unicode_shift_jis.xlsx' );
10389
10390 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10391
10392 my $worksheet = $workbook->add_worksheet();
10393 $worksheet->set_column( 'A:A', 50 );
10394
10395
10396 my $file = 'unicode_shift_jis.txt';
10397
10398 open FH, '<:encoding(shiftjis)', $file or die "Couldn't open $file: $!\n";
10399
10400 my $row = 0;
10401
10402 while ( <FH> ) {
10403 next if /^#/; # Ignore the comments in the sample file.
10404 chomp;
10405 $worksheet->write( $row++, 0, $_ );
10406 }
10407
10408 $workbook->close();
10409
10410 __END__
10411
10412 Download this example:
10413 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.03/examples/unicode_shift_jis.pl>
10414
10416 John McNamara jmcnamara@cpan.org
10417
10418 Contributed examples contain the original author's name.
10419
10421 Copyright MM-MMXVI, John McNamara.
10422
10423 All Rights Reserved. This module is free software. It may be used,
10424 redistributed and/or modified under the same terms as Perl itself.
10425
10426
10427
10428perl v5.30.1 2020-01-30 Excel::Writer::XLSX::Examples(3)