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 91 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 doughnut 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_data_labels.pl" A demo of standard and custom chart
74 data labels.
75
76 • "Example: chart_clustered.pl" A demo of a chart with a clustered
77 axis.
78
79 • "Example: chart_styles.pl" A demo of the available chart styles.
80
81 • "Example: chart_gauge.pl" A demo of a gauge style chart.
82
83 • "Example: colors.pl" A demo of the colour palette and named
84 colours.
85
86 • "Example: comments1.pl" Add comments to worksheet cells.
87
88 • "Example: comments2.pl" Add comments with advanced options.
89
90 • "Example: conditional_format.pl" Add conditional formats to a range
91 of cells.
92
93 • "Example: data_validate.pl" An example of data validation and
94 dropdown lists.
95
96 • "Example: date_time.pl" Write dates and times with
97 write_date_time().
98
99 • "Example: defined_name.pl" Example of how to create defined names.
100
101 • "Example: diag_border.pl" A simple example of diagonal cell
102 borders.
103
104 • "Example: filehandle.pl" Examples of working with filehandles.
105
106 • "Example: headers.pl" Examples of worksheet headers and footers.
107
108 • "Example: hide_row_col.pl" Example of hiding rows and columns.
109
110 • "Example: hide_sheet.pl" Simple example of hiding a worksheet.
111
112 • "Example: hyperlink1.pl" Shows how to create web hyperlinks.
113
114 • "Example: hyperlink2.pl" Examples of internal and external
115 hyperlinks.
116
117 • "Example: indent.pl" An example of cell indentation.
118
119 • "Example: ignore_errors.pl" An example of turning off worksheet
120 cells errors/warnings.
121
122 • "Example: macros.pl" An example of adding macros from an existing
123 file.
124
125 • "Example: merge1.pl" A simple example of cell merging.
126
127 • "Example: merge2.pl" A simple example of cell merging with
128 formatting.
129
130 • "Example: merge3.pl" Add hyperlinks to merged cells.
131
132 • "Example: merge4.pl" An advanced example of merging with
133 formatting.
134
135 • "Example: merge5.pl" An advanced example of merging with
136 formatting.
137
138 • "Example: merge6.pl" An example of merging with Unicode strings.
139
140 • "Example: mod_perl1.pl" A simple mod_perl 1 program.
141
142 • "Example: mod_perl2.pl" A simple mod_perl 2 program.
143
144 • "Example: outline.pl" An example of outlines and grouping.
145
146 • "Example: outline_collapsed.pl" An example of collapsed outlines.
147
148 • "Example: panes.pl" An example of how to create panes.
149
150 • "Example: properties.pl" Add document properties to a workbook.
151
152 • "Example: protection.pl" Example of cell locking and formula
153 hiding.
154
155 • "Example: rich_strings.pl" Example of strings with multiple
156 formats.
157
158 • "Example: right_to_left.pl" Change default sheet direction to right
159 to left.
160
161 • "Example: sales.pl" An example of a simple sales spreadsheet.
162
163 • "Example: shape1.pl" Insert shapes in worksheet.
164
165 • "Example: shape2.pl" Insert shapes in worksheet. With properties.
166
167 • "Example: shape3.pl" Insert shapes in worksheet. Scaled.
168
169 • "Example: shape4.pl" Insert shapes in worksheet. With modification.
170
171 • "Example: shape5.pl" Insert shapes in worksheet. With connections.
172
173 • "Example: shape6.pl" Insert shapes in worksheet. With connections.
174
175 • "Example: shape7.pl" Insert shapes in worksheet. One to many
176 connections.
177
178 • "Example: shape8.pl" Insert shapes in worksheet. One to many
179 connections.
180
181 • "Example: shape_all.pl" Demo of all the available shape and
182 connector types.
183
184 • "Example: sparklines1.pl" Simple sparklines demo.
185
186 • "Example: sparklines2.pl" Sparklines demo showing formatting
187 options.
188
189 • "Example: stats_ext.pl" Same as stats.pl with external references.
190
191 • "Example: stocks.pl" Demonstrates conditional formatting.
192
193 • "Example: background.pl" Example of how to set the background image
194 for a worksheet.
195
196 • "Example: tab_colors.pl" Example of how to set worksheet tab
197 colours.
198
199 • "Example: tables.pl" Add Excel tables to a worksheet.
200
201 • "Example: write_handler1.pl" Example of extending the write()
202 method. Step 1.
203
204 • "Example: write_handler2.pl" Example of extending the write()
205 method. Step 2.
206
207 • "Example: write_handler3.pl" Example of extending the write()
208 method. Step 3.
209
210 • "Example: write_handler4.pl" Example of extending the write()
211 method. Step 4.
212
213 • "Example: write_to_scalar.pl" Example of writing an Excel file to a
214 Perl scalar.
215
216 • "Example: unicode_2022_jp.pl" Japanese: ISO-2022-JP.
217
218 • "Example: unicode_8859_11.pl" Thai: ISO-8859_11.
219
220 • "Example: unicode_8859_7.pl" Greek: ISO-8859_7.
221
222 • "Example: unicode_big5.pl" Chinese: BIG5.
223
224 • "Example: unicode_cp1251.pl" Russian: CP1251.
225
226 • "Example: unicode_cp1256.pl" Arabic: CP1256.
227
228 • "Example: unicode_cyrillic.pl" Russian: Cyrillic.
229
230 • "Example: unicode_koi8r.pl" Russian: KOI8-R.
231
232 • "Example: unicode_polish_utf8.pl" Polish : UTF8.
233
234 • "Example: unicode_shift_jis.pl" Japanese: Shift JIS.
235
236 Example: a_simple.pl
237 A simple example of how to use the Excel::Writer::XLSX module to write
238 text and numbers to an Excel xlsx file.
239
240 Source code for this example:
241
242 #!/usr/bin/perl -w
243
244 #######################################################################
245 #
246 # A simple example of how to use the Excel::Writer::XLSX module to
247 # write text and numbers to an Excel xlsx file.
248 #
249 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
250 #
251
252 use strict;
253 use Excel::Writer::XLSX;
254
255 # Create a new workbook called simple.xls and add a worksheet
256 my $workbook = Excel::Writer::XLSX->new( 'a_simple.xlsx' );
257 my $worksheet = $workbook->add_worksheet();
258
259 # The general syntax is write($row, $column, $token). Note that row and
260 # column are zero indexed
261 #
262
263 # Write some text
264 $worksheet->write( 0, 0, "Hi Excel!" );
265
266
267 # Write some numbers
268 $worksheet->write( 2, 0, 3 ); # Writes 3
269 $worksheet->write( 3, 0, 3.00000 ); # Writes 3
270 $worksheet->write( 4, 0, 3.00001 ); # Writes 3.00001
271 $worksheet->write( 5, 0, 3.14159 ); # TeX revision no.?
272
273
274 # Write some formulas
275 $worksheet->write( 7, 0, '=A3 + A6' );
276 $worksheet->write( 8, 0, '=IF(A5>3,"Yes", "No")' );
277
278
279 # Write a hyperlink
280 $worksheet->write( 10, 0, 'http://www.perl.com/' );
281
282 $workbook->close();
283
284 __END__
285
286 Download this example:
287 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/a_simple.pl>
288
289 Example: bug_report.pl
290 A template for submitting a bug report.
291
292 Run this program and read the output from the command line.
293
294 #!/usr/bin/perl -w
295
296 ###############################################################################
297 #
298 # A template for submitting a bug report.
299 #
300 # Run this program and read the output from the command line.
301 #
302 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
303 #
304
305
306 use strict;
307
308 print << 'HINTS_1';
309
310 REPORTING A BUG OR ASKING A QUESTION
311
312 Feel free to report bugs or ask questions. However, to save time
313 consider the following steps first:
314
315 Read the documentation:
316
317 The Excel::Writer::XLSX documentation has been refined in
318 response to user questions. Therefore, if you have a question it is
319 possible that someone else has asked it before you and that it is
320 already addressed in the documentation. Since there is a lot of
321 documentation to get through you should at least read the table of
322 contents and search for keywords that you are interested in.
323
324 Look at the example programs:
325
326 There are over 80 example programs shipped with the standard
327 Excel::Writer::XLSX distribution. Many of these were created
328 in response to user questions. Try to identify an example program
329 that corresponds to your query and adapt it to your needs.
330
331 HINTS_1
332 print "Press enter ..."; <STDIN>;
333
334 print << 'HINTS_2';
335
336 If you submit a bug report here are some pointers.
337
338 1. Put "Excel::Writer::XLSX:" at the beginning of the subject line.
339 This helps to filter genuine messages from spam.
340
341 2. Describe the problems as clearly and as concisely as possible.
342
343 3. Send a sample program. It is often easier to describe a problem in
344 code than in written prose.
345
346 4. The sample program should be as small as possible to demonstrate the
347 problem. Don't copy and past large sections of your program. The
348 program should also be self contained and working.
349
350 A sample bug report is generated below. If you use this format then it
351 will help to analyse your question and respond to it more quickly.
352
353 Please don't send patches without contacting the author first.
354
355
356 HINTS_2
357 print "Press enter ..."; <STDIN>;
358
359
360 print << 'EMAIL';
361
362 =======================================================================
363
364 To: John McNamara <jmcnamara@cpan.org>
365 Subject: Excel::Writer::XLSX: Problem with something.
366
367 Hi John,
368
369 I am using Excel::Writer::XLSX and I have encountered a problem. I
370 want it to do SOMETHING but the module appears to do SOMETHING_ELSE.
371
372 Here is some code that demonstrates the problem.
373
374 #!/usr/bin/perl -w
375
376 use strict;
377 use Excel::Writer::XLSX;
378
379 my $workbook = Excel::Writer::XLSX->new("reload.xls");
380 my $worksheet = $workbook->add_worksheet();
381
382 $worksheet->write(0, 0, "Hi Excel!");
383
384 $workbook->close();
385
386 __END__
387
388 My automatically generated system details are as follows:
389 EMAIL
390
391
392 print "\n Perl version : $]";
393 print "\n OS name : $^O";
394 print "\n Module versions: (not all are required)\n";
395
396
397 my @modules = qw(
398 Excel::Writer::XLSX
399 Spreadsheet::WriteExcel
400 Archive::Zip
401 XML::Writer
402 IO::File
403 File::Temp
404 );
405
406
407 for my $module (@modules) {
408 my $version;
409 eval "require $module";
410
411 if (not $@) {
412 $version = $module->VERSION;
413 $version = '(unknown)' if not defined $version;
414 }
415 else {
416 $version = '(not installed)';
417 }
418
419 printf "%21s%-24s\t%s\n", "", $module, $version;
420 }
421
422
423 print << "BYE";
424 Yours etc.,
425
426 A. Person
427 --
428
429 BYE
430
431 __END__
432
433 Download this example:
434 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/bug_report.pl>
435
436 Example: demo.pl
437 A simple demo of some of the features of Excel::Writer::XLSX.
438
439 This program is used to create the project screenshot for Freshmeat:
440 <http://freshmeat.net/projects/writeexcel/>
441
442 Source code for this example:
443
444 #!/usr/bin/perl -w
445
446 #######################################################################
447 #
448 # A simple demo of some of the features of Excel::Writer::XLSX.
449 #
450 # This program is used to create the project screenshot for Freshmeat:
451 # L<http://freshmeat.net/projects/writeexcel/>
452 #
453 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
454 #
455
456 use strict;
457 use Excel::Writer::XLSX;
458
459 my $workbook = Excel::Writer::XLSX->new( 'demo.xlsx' );
460 my $worksheet = $workbook->add_worksheet( 'Demo' );
461 my $worksheet2 = $workbook->add_worksheet( 'Another sheet' );
462 my $worksheet3 = $workbook->add_worksheet( 'And another' );
463
464 my $bold = $workbook->add_format( bold => 1 );
465
466
467 #######################################################################
468 #
469 # Write a general heading
470 #
471 $worksheet->set_column( 'A:A', 36, $bold );
472 $worksheet->set_column( 'B:B', 20 );
473 $worksheet->set_row( 0, 40 );
474
475 my $heading = $workbook->add_format(
476 bold => 1,
477 color => 'blue',
478 size => 16,
479 merge => 1,
480 align => 'vcenter',
481 );
482
483 my @headings = ( 'Features of Excel::Writer::XLSX', '' );
484 $worksheet->write_row( 'A1', \@headings, $heading );
485
486
487 #######################################################################
488 #
489 # Some text examples
490 #
491 my $text_format = $workbook->add_format(
492 bold => 1,
493 italic => 1,
494 color => 'red',
495 size => 18,
496 font => 'Lucida Calligraphy'
497 );
498
499
500 $worksheet->write( 'A2', "Text" );
501 $worksheet->write( 'B2', "Hello Excel" );
502 $worksheet->write( 'A3', "Formatted text" );
503 $worksheet->write( 'B3', "Hello Excel", $text_format );
504 $worksheet->write( 'A4', "Unicode text" );
505 $worksheet->write( 'B4', "\x{0410} \x{0411} \x{0412} \x{0413} \x{0414}" );
506
507 #######################################################################
508 #
509 # Some numeric examples
510 #
511 my $num1_format = $workbook->add_format( num_format => '$#,##0.00' );
512 my $num2_format = $workbook->add_format( num_format => ' d mmmm yyy' );
513
514
515 $worksheet->write( 'A5', "Numbers" );
516 $worksheet->write( 'B5', 1234.56 );
517 $worksheet->write( 'A6', "Formatted numbers" );
518 $worksheet->write( 'B6', 1234.56, $num1_format );
519 $worksheet->write( 'A7', "Formatted numbers" );
520 $worksheet->write( 'B7', 37257, $num2_format );
521
522
523 #######################################################################
524 #
525 # Formulae
526 #
527 $worksheet->set_selection( 'B8' );
528 $worksheet->write( 'A8', 'Formulas and functions, "=SIN(PI()/4)"' );
529 $worksheet->write( 'B8', '=SIN(PI()/4)' );
530
531
532 #######################################################################
533 #
534 # Hyperlinks
535 #
536 $worksheet->write( 'A9', "Hyperlinks" );
537 $worksheet->write( 'B9', 'http://www.perl.com/' );
538
539
540 #######################################################################
541 #
542 # Images
543 #
544 $worksheet->write( 'A10', "Images" );
545 $worksheet->insert_image( 'B10', 'republic.png',
546 { x_offset => 16, y_offset => 8 } );
547
548
549 #######################################################################
550 #
551 # Misc
552 #
553 $worksheet->write( 'A18', "Page/printer setup" );
554 $worksheet->write( 'A19', "Multiple worksheets" );
555
556 $workbook->close();
557
558 __END__
559
560 Download this example:
561 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/demo.pl>
562
563 Example: formats.pl
564 Examples of formatting using the Excel::Writer::XLSX module.
565
566 This program demonstrates almost all possible formatting options. It is
567 worth running this program and viewing the output Excel file if you are
568 interested in the various formatting possibilities.
569
570 Source code for this example:
571
572 #!/usr/bin/perl -w
573
574 ###############################################################################
575 #
576 # Examples of formatting using the Excel::Writer::XLSX module.
577 #
578 # This program demonstrates almost all possible formatting options. It is worth
579 # running this program and viewing the output Excel file if you are interested
580 # in the various formatting possibilities.
581 #
582 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
583 #
584
585 use strict;
586 use Excel::Writer::XLSX;
587
588 my $workbook = Excel::Writer::XLSX->new( 'formats.xlsx' );
589
590 # Some common formats
591 my $center = $workbook->add_format( align => 'center' );
592 my $heading = $workbook->add_format( align => 'center', bold => 1 );
593
594 # The named colors
595 my %colors = (
596 0x08, 'black',
597 0x0C, 'blue',
598 0x10, 'brown',
599 0x0F, 'cyan',
600 0x17, 'gray',
601 0x11, 'green',
602 0x0B, 'lime',
603 0x0E, 'magenta',
604 0x12, 'navy',
605 0x35, 'orange',
606 0x21, 'pink',
607 0x14, 'purple',
608 0x0A, 'red',
609 0x16, 'silver',
610 0x09, 'white',
611 0x0D, 'yellow',
612
613 );
614
615 # Call these subroutines to demonstrate different formatting options
616 intro();
617 fonts();
618 named_colors();
619 standard_colors();
620 numeric_formats();
621 borders();
622 patterns();
623 alignment();
624 misc();
625
626 # Note: this is required
627 $workbook->close();
628
629
630 ######################################################################
631 #
632 # Intro.
633 #
634 sub intro {
635
636 my $worksheet = $workbook->add_worksheet( 'Introduction' );
637
638 $worksheet->set_column( 0, 0, 60 );
639
640 my $format = $workbook->add_format();
641 $format->set_bold();
642 $format->set_size( 14 );
643 $format->set_color( 'blue' );
644 $format->set_align( 'center' );
645
646 my $format2 = $workbook->add_format();
647 $format2->set_bold();
648 $format2->set_color( 'blue' );
649
650 my $format3 = $workbook->add_format(
651 color => 'blue',
652 underline => 1,
653 );
654
655 $worksheet->write( 2, 0, 'This workbook demonstrates some of', $format );
656 $worksheet->write( 3, 0, 'the formatting options provided by', $format );
657 $worksheet->write( 4, 0, 'the Excel::Writer::XLSX module.', $format );
658 $worksheet->write( 'A7', 'Sections:', $format2 );
659
660 $worksheet->write( 'A8', "internal:Fonts!A1", 'Fonts', $format3 );
661
662 $worksheet->write( 'A9', "internal:'Named colors'!A1",
663 'Named colors', $format3 );
664
665 $worksheet->write(
666 'A10',
667 "internal:'Standard colors'!A1",
668 'Standard colors', $format3
669 );
670
671 $worksheet->write(
672 'A11',
673 "internal:'Numeric formats'!A1",
674 'Numeric formats', $format3
675 );
676
677 $worksheet->write( 'A12', "internal:Borders!A1", 'Borders', $format3 );
678 $worksheet->write( 'A13', "internal:Patterns!A1", 'Patterns', $format3 );
679 $worksheet->write( 'A14', "internal:Alignment!A1", 'Alignment', $format3 );
680 $worksheet->write( 'A15', "internal:Miscellaneous!A1", 'Miscellaneous',
681 $format3 );
682
683 }
684
685
686 ######################################################################
687 #
688 # Demonstrate the named colors.
689 #
690 sub named_colors {
691
692 my $worksheet = $workbook->add_worksheet( 'Named colors' );
693
694 $worksheet->set_column( 0, 3, 15 );
695
696 $worksheet->write( 0, 0, "Index", $heading );
697 $worksheet->write( 0, 1, "Index", $heading );
698 $worksheet->write( 0, 2, "Name", $heading );
699 $worksheet->write( 0, 3, "Color", $heading );
700
701 my $i = 1;
702
703 while ( my ( $index, $color ) = each %colors ) {
704 my $format = $workbook->add_format(
705 bg_color => $color,
706 pattern => 1,
707 border => 1
708 );
709
710 $worksheet->write( $i + 1, 0, $index, $center );
711 $worksheet->write( $i + 1, 1, sprintf( "0x%02X", $index ), $center );
712 $worksheet->write( $i + 1, 2, $color, $center );
713 $worksheet->write( $i + 1, 3, '', $format );
714 $i++;
715 }
716 }
717
718
719 ######################################################################
720 #
721 # Demonstrate the standard Excel colors in the range 8..63.
722 #
723 sub standard_colors {
724
725 my $worksheet = $workbook->add_worksheet( 'Standard colors' );
726
727 $worksheet->set_column( 0, 3, 15 );
728
729 $worksheet->write( 0, 0, "Index", $heading );
730 $worksheet->write( 0, 1, "Index", $heading );
731 $worksheet->write( 0, 2, "Color", $heading );
732 $worksheet->write( 0, 3, "Name", $heading );
733
734 for my $i ( 8 .. 63 ) {
735 my $format = $workbook->add_format(
736 bg_color => $i,
737 pattern => 1,
738 border => 1
739 );
740
741 $worksheet->write( ( $i - 7 ), 0, $i, $center );
742 $worksheet->write( ( $i - 7 ), 1, sprintf( "0x%02X", $i ), $center );
743 $worksheet->write( ( $i - 7 ), 2, '', $format );
744
745 # Add the color names
746 if ( exists $colors{$i} ) {
747 $worksheet->write( ( $i - 7 ), 3, $colors{$i}, $center );
748
749 }
750 }
751 }
752
753
754 ######################################################################
755 #
756 # Demonstrate the standard numeric formats.
757 #
758 sub numeric_formats {
759
760 my $worksheet = $workbook->add_worksheet( 'Numeric formats' );
761
762 $worksheet->set_column( 0, 4, 15 );
763 $worksheet->set_column( 5, 5, 45 );
764
765 $worksheet->write( 0, 0, "Index", $heading );
766 $worksheet->write( 0, 1, "Index", $heading );
767 $worksheet->write( 0, 2, "Unformatted", $heading );
768 $worksheet->write( 0, 3, "Formatted", $heading );
769 $worksheet->write( 0, 4, "Negative", $heading );
770 $worksheet->write( 0, 5, "Format", $heading );
771
772 #<<<
773 my @formats;
774 push @formats, [ 0x00, 1234.567, 0, 'General' ];
775 push @formats, [ 0x01, 1234.567, 0, '0' ];
776 push @formats, [ 0x02, 1234.567, 0, '0.00' ];
777 push @formats, [ 0x03, 1234.567, 0, '#,##0' ];
778 push @formats, [ 0x04, 1234.567, 0, '#,##0.00' ];
779 push @formats, [ 0x05, 1234.567, -1234.567, '($#,##0_);($#,##0)' ];
780 push @formats, [ 0x06, 1234.567, -1234.567, '($#,##0_);[Red]($#,##0)' ];
781 push @formats, [ 0x07, 1234.567, -1234.567, '($#,##0.00_);($#,##0.00)' ];
782 push @formats, [ 0x08, 1234.567, -1234.567, '($#,##0.00_);[Red]($#,##0.00)' ];
783 push @formats, [ 0x09, 0.567, 0, '0%' ];
784 push @formats, [ 0x0a, 0.567, 0, '0.00%' ];
785 push @formats, [ 0x0b, 1234.567, 0, '0.00E+00' ];
786 push @formats, [ 0x0c, 0.75, 0, '# ?/?' ];
787 push @formats, [ 0x0d, 0.3125, 0, '# ??/??' ];
788 push @formats, [ 0x0e, 36892.521, 0, 'm/d/yy' ];
789 push @formats, [ 0x0f, 36892.521, 0, 'd-mmm-yy' ];
790 push @formats, [ 0x10, 36892.521, 0, 'd-mmm' ];
791 push @formats, [ 0x11, 36892.521, 0, 'mmm-yy' ];
792 push @formats, [ 0x12, 36892.521, 0, 'h:mm AM/PM' ];
793 push @formats, [ 0x13, 36892.521, 0, 'h:mm:ss AM/PM' ];
794 push @formats, [ 0x14, 36892.521, 0, 'h:mm' ];
795 push @formats, [ 0x15, 36892.521, 0, 'h:mm:ss' ];
796 push @formats, [ 0x16, 36892.521, 0, 'm/d/yy h:mm' ];
797 push @formats, [ 0x25, 1234.567, -1234.567, '(#,##0_);(#,##0)' ];
798 push @formats, [ 0x26, 1234.567, -1234.567, '(#,##0_);[Red](#,##0)' ];
799 push @formats, [ 0x27, 1234.567, -1234.567, '(#,##0.00_);(#,##0.00)' ];
800 push @formats, [ 0x28, 1234.567, -1234.567, '(#,##0.00_);[Red](#,##0.00)' ];
801 push @formats, [ 0x29, 1234.567, -1234.567, '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)' ];
802 push @formats, [ 0x2a, 1234.567, -1234.567, '_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)' ];
803 push @formats, [ 0x2b, 1234.567, -1234.567, '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' ];
804 push @formats, [ 0x2c, 1234.567, -1234.567, '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)' ];
805 push @formats, [ 0x2d, 36892.521, 0, 'mm:ss' ];
806 push @formats, [ 0x2e, 3.0153, 0, '[h]:mm:ss' ];
807 push @formats, [ 0x2f, 36892.521, 0, 'mm:ss.0' ];
808 push @formats, [ 0x30, 1234.567, 0, '##0.0E+0' ];
809 push @formats, [ 0x31, 1234.567, 0, '@' ];
810 #>>>
811
812 my $i;
813 foreach my $format ( @formats ) {
814 my $style = $workbook->add_format();
815 $style->set_num_format( $format->[0] );
816
817 $i++;
818 $worksheet->write( $i, 0, $format->[0], $center );
819 $worksheet->write( $i, 1, sprintf( "0x%02X", $format->[0] ), $center );
820 $worksheet->write( $i, 2, $format->[1], $center );
821 $worksheet->write( $i, 3, $format->[1], $style );
822
823 if ( $format->[2] ) {
824 $worksheet->write( $i, 4, $format->[2], $style );
825 }
826
827 $worksheet->write_string( $i, 5, $format->[3] );
828 }
829 }
830
831
832 ######################################################################
833 #
834 # Demonstrate the font options.
835 #
836 sub fonts {
837
838 my $worksheet = $workbook->add_worksheet( 'Fonts' );
839
840 $worksheet->set_column( 0, 0, 30 );
841 $worksheet->set_column( 1, 1, 10 );
842
843 $worksheet->write( 0, 0, "Font name", $heading );
844 $worksheet->write( 0, 1, "Font size", $heading );
845
846 my @fonts;
847 push @fonts, [ 10, 'Arial' ];
848 push @fonts, [ 12, 'Arial' ];
849 push @fonts, [ 14, 'Arial' ];
850 push @fonts, [ 12, 'Arial Black' ];
851 push @fonts, [ 12, 'Arial Narrow' ];
852 push @fonts, [ 12, 'Century Schoolbook' ];
853 push @fonts, [ 12, 'Courier' ];
854 push @fonts, [ 12, 'Courier New' ];
855 push @fonts, [ 12, 'Garamond' ];
856 push @fonts, [ 12, 'Impact' ];
857 push @fonts, [ 12, 'Lucida Handwriting' ];
858 push @fonts, [ 12, 'Times New Roman' ];
859 push @fonts, [ 12, 'Symbol' ];
860 push @fonts, [ 12, 'Wingdings' ];
861 push @fonts, [ 12, 'A font that doesn\'t exist' ];
862
863 my $i;
864 foreach my $font ( @fonts ) {
865 my $format = $workbook->add_format();
866
867 $format->set_size( $font->[0] );
868 $format->set_font( $font->[1] );
869
870 $i++;
871 $worksheet->write( $i, 0, $font->[1], $format );
872 $worksheet->write( $i, 1, $font->[0], $format );
873 }
874
875 }
876
877
878 ######################################################################
879 #
880 # Demonstrate the standard Excel border styles.
881 #
882 sub borders {
883
884 my $worksheet = $workbook->add_worksheet( 'Borders' );
885
886 $worksheet->set_column( 0, 4, 10 );
887 $worksheet->set_column( 5, 5, 40 );
888
889 $worksheet->write( 0, 0, "Index", $heading );
890 $worksheet->write( 0, 1, "Index", $heading );
891 $worksheet->write( 0, 3, "Style", $heading );
892 $worksheet->write( 0, 5, "The style is highlighted in red for ", $heading );
893 $worksheet->write( 1, 5, "emphasis, the default color is black.",
894 $heading );
895
896 for my $i ( 0 .. 13 ) {
897 my $format = $workbook->add_format();
898 $format->set_border( $i );
899 $format->set_border_color( 'red' );
900 $format->set_align( 'center' );
901
902 $worksheet->write( ( 2 * ( $i + 1 ) ), 0, $i, $center );
903 $worksheet->write( ( 2 * ( $i + 1 ) ),
904 1, sprintf( "0x%02X", $i ), $center );
905
906 $worksheet->write( ( 2 * ( $i + 1 ) ), 3, "Border", $format );
907 }
908
909 $worksheet->write( 30, 0, "Diag type", $heading );
910 $worksheet->write( 30, 1, "Index", $heading );
911 $worksheet->write( 30, 3, "Style", $heading );
912 $worksheet->write( 30, 5, "Diagonal Border styles", $heading );
913
914 for my $i ( 1 .. 3 ) {
915 my $format = $workbook->add_format();
916 $format->set_diag_type( $i );
917 $format->set_diag_border( 1 );
918 $format->set_diag_color( 'red' );
919 $format->set_align( 'center' );
920
921 $worksheet->write( ( 2 * ( $i + 15 ) ), 0, $i, $center );
922 $worksheet->write( ( 2 * ( $i + 15 ) ),
923 1, sprintf( "0x%02X", $i ), $center );
924
925 $worksheet->write( ( 2 * ( $i + 15 ) ), 3, "Border", $format );
926 }
927 }
928
929
930 ######################################################################
931 #
932 # Demonstrate the standard Excel cell patterns.
933 #
934 sub patterns {
935
936 my $worksheet = $workbook->add_worksheet( 'Patterns' );
937
938 $worksheet->set_column( 0, 4, 10 );
939 $worksheet->set_column( 5, 5, 50 );
940
941 $worksheet->write( 0, 0, "Index", $heading );
942 $worksheet->write( 0, 1, "Index", $heading );
943 $worksheet->write( 0, 3, "Pattern", $heading );
944
945 $worksheet->write( 0, 5, "The background colour has been set to silver.",
946 $heading );
947 $worksheet->write( 1, 5, "The foreground colour has been set to green.",
948 $heading );
949
950 for my $i ( 0 .. 18 ) {
951 my $format = $workbook->add_format();
952
953 $format->set_pattern( $i );
954 $format->set_bg_color( 'silver' );
955 $format->set_fg_color( 'green' );
956 $format->set_align( 'center' );
957
958 $worksheet->write( ( 2 * ( $i + 1 ) ), 0, $i, $center );
959 $worksheet->write( ( 2 * ( $i + 1 ) ),
960 1, sprintf( "0x%02X", $i ), $center );
961
962 $worksheet->write( ( 2 * ( $i + 1 ) ), 3, "Pattern", $format );
963
964 if ( $i == 1 ) {
965 $worksheet->write( ( 2 * ( $i + 1 ) ),
966 5, "This is solid colour, the most useful pattern.", $heading );
967 }
968 }
969 }
970
971
972 ######################################################################
973 #
974 # Demonstrate the standard Excel cell alignments.
975 #
976 sub alignment {
977
978 my $worksheet = $workbook->add_worksheet( 'Alignment' );
979
980 $worksheet->set_column( 0, 7, 12 );
981 $worksheet->set_row( 0, 40 );
982 $worksheet->set_selection( 7, 0 );
983
984 my $format01 = $workbook->add_format();
985 my $format02 = $workbook->add_format();
986 my $format03 = $workbook->add_format();
987 my $format04 = $workbook->add_format();
988 my $format05 = $workbook->add_format();
989 my $format06 = $workbook->add_format();
990 my $format07 = $workbook->add_format();
991 my $format08 = $workbook->add_format();
992 my $format09 = $workbook->add_format();
993 my $format10 = $workbook->add_format();
994 my $format11 = $workbook->add_format();
995 my $format12 = $workbook->add_format();
996 my $format13 = $workbook->add_format();
997 my $format14 = $workbook->add_format();
998 my $format15 = $workbook->add_format();
999 my $format16 = $workbook->add_format();
1000 my $format17 = $workbook->add_format();
1001
1002 $format02->set_align( 'top' );
1003 $format03->set_align( 'bottom' );
1004 $format04->set_align( 'vcenter' );
1005 $format05->set_align( 'vjustify' );
1006 $format06->set_text_wrap();
1007
1008 $format07->set_align( 'left' );
1009 $format08->set_align( 'right' );
1010 $format09->set_align( 'center' );
1011 $format10->set_align( 'fill' );
1012 $format11->set_align( 'justify' );
1013 $format12->set_merge();
1014
1015 $format13->set_rotation( 45 );
1016 $format14->set_rotation( -45 );
1017 $format15->set_rotation( 270 );
1018
1019 $format16->set_shrink();
1020 $format17->set_indent( 1 );
1021
1022 $worksheet->write( 0, 0, 'Vertical', $heading );
1023 $worksheet->write( 0, 1, 'top', $format02 );
1024 $worksheet->write( 0, 2, 'bottom', $format03 );
1025 $worksheet->write( 0, 3, 'vcenter', $format04 );
1026 $worksheet->write( 0, 4, 'vjustify', $format05 );
1027 $worksheet->write( 0, 5, "text\nwrap", $format06 );
1028
1029 $worksheet->write( 2, 0, 'Horizontal', $heading );
1030 $worksheet->write( 2, 1, 'left', $format07 );
1031 $worksheet->write( 2, 2, 'right', $format08 );
1032 $worksheet->write( 2, 3, 'center', $format09 );
1033 $worksheet->write( 2, 4, 'fill', $format10 );
1034 $worksheet->write( 2, 5, 'justify', $format11 );
1035
1036 $worksheet->write( 3, 1, 'merge', $format12 );
1037 $worksheet->write( 3, 2, '', $format12 );
1038
1039 $worksheet->write( 3, 3, 'Shrink ' x 3, $format16 );
1040 $worksheet->write( 3, 4, 'Indent', $format17 );
1041
1042
1043 $worksheet->write( 5, 0, 'Rotation', $heading );
1044 $worksheet->write( 5, 1, 'Rotate 45', $format13 );
1045 $worksheet->write( 6, 1, 'Rotate -45', $format14 );
1046 $worksheet->write( 7, 1, 'Rotate 270', $format15 );
1047 }
1048
1049
1050 ######################################################################
1051 #
1052 # Demonstrate other miscellaneous features.
1053 #
1054 sub misc {
1055
1056 my $worksheet = $workbook->add_worksheet( 'Miscellaneous' );
1057
1058 $worksheet->set_column( 2, 2, 25 );
1059
1060 my $format01 = $workbook->add_format();
1061 my $format02 = $workbook->add_format();
1062 my $format03 = $workbook->add_format();
1063 my $format04 = $workbook->add_format();
1064 my $format05 = $workbook->add_format();
1065 my $format06 = $workbook->add_format();
1066 my $format07 = $workbook->add_format();
1067
1068 $format01->set_underline( 0x01 );
1069 $format02->set_underline( 0x02 );
1070 $format03->set_underline( 0x21 );
1071 $format04->set_underline( 0x22 );
1072 $format05->set_font_strikeout();
1073 $format06->set_font_outline();
1074 $format07->set_font_shadow();
1075
1076 $worksheet->write( 1, 2, 'Underline 0x01', $format01 );
1077 $worksheet->write( 3, 2, 'Underline 0x02', $format02 );
1078 $worksheet->write( 5, 2, 'Underline 0x21', $format03 );
1079 $worksheet->write( 7, 2, 'Underline 0x22', $format04 );
1080 $worksheet->write( 9, 2, 'Strikeout', $format05 );
1081 $worksheet->write( 11, 2, 'Outline (Macintosh only)', $format06 );
1082 $worksheet->write( 13, 2, 'Shadow (Macintosh only)', $format07 );
1083 }
1084
1085
1086 $workbook->close();
1087
1088 __END__
1089
1090 Download this example:
1091 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/formats.pl>
1092
1093 Example: regions.pl
1094 An example of how to use the Excel::Writer::XLSX module to write a
1095 basic Excel workbook with multiple worksheets.
1096
1097 Source code for this example:
1098
1099 #!/usr/bin/perl -w
1100
1101 ###############################################################################
1102 #
1103 # An example of how to use the Excel::Writer::XLSX module to write a basic
1104 # Excel workbook with multiple worksheets.
1105 #
1106 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
1107 #
1108
1109 use strict;
1110 use Excel::Writer::XLSX;
1111
1112 # Create a new Excel workbook
1113 my $workbook = Excel::Writer::XLSX->new( 'regions.xlsx' );
1114
1115 # Add some worksheets
1116 my $north = $workbook->add_worksheet( "North" );
1117 my $south = $workbook->add_worksheet( "South" );
1118 my $east = $workbook->add_worksheet( "East" );
1119 my $west = $workbook->add_worksheet( "West" );
1120
1121 # Add a Format
1122 my $format = $workbook->add_format();
1123 $format->set_bold();
1124 $format->set_color( 'blue' );
1125
1126 # Add a caption to each worksheet
1127 foreach my $worksheet ( $workbook->sheets() ) {
1128 $worksheet->write( 0, 0, "Sales", $format );
1129 }
1130
1131 # Write some data
1132 $north->write( 0, 1, 200000 );
1133 $south->write( 0, 1, 100000 );
1134 $east->write( 0, 1, 150000 );
1135 $west->write( 0, 1, 100000 );
1136
1137 # Set the active worksheet
1138 $south->activate();
1139
1140 # Set the width of the first column
1141 $south->set_column( 0, 0, 20 );
1142
1143 # Set the active cell
1144 $south->set_selection( 0, 1 );
1145
1146 $workbook->close();
1147
1148 __END__
1149
1150 Download this example:
1151 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/regions.pl>
1152
1153 Example: stats.pl
1154 A simple example of how to use functions with the Excel::Writer::XLSX
1155 module.
1156
1157 Source code for this example:
1158
1159 #!/usr/bin/perl -w
1160
1161 ###############################################################################
1162 #
1163 # A simple example of how to use functions with the Excel::Writer::XLSX
1164 # module.
1165 #
1166 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
1167 #
1168
1169 use strict;
1170 use Excel::Writer::XLSX;
1171
1172 # Create a new workbook and add a worksheet
1173 my $workbook = Excel::Writer::XLSX->new( 'stats.xlsx' );
1174 my $worksheet = $workbook->add_worksheet( 'Test data' );
1175
1176 # Set the column width for columns 1
1177 $worksheet->set_column( 0, 0, 20 );
1178
1179
1180 # Create a format for the headings
1181 my $format = $workbook->add_format();
1182 $format->set_bold();
1183
1184
1185 # Write the sample data
1186 $worksheet->write( 0, 0, 'Sample', $format );
1187 $worksheet->write( 0, 1, 1 );
1188 $worksheet->write( 0, 2, 2 );
1189 $worksheet->write( 0, 3, 3 );
1190 $worksheet->write( 0, 4, 4 );
1191 $worksheet->write( 0, 5, 5 );
1192 $worksheet->write( 0, 6, 6 );
1193 $worksheet->write( 0, 7, 7 );
1194 $worksheet->write( 0, 8, 8 );
1195
1196 $worksheet->write( 1, 0, 'Length', $format );
1197 $worksheet->write( 1, 1, 25.4 );
1198 $worksheet->write( 1, 2, 25.4 );
1199 $worksheet->write( 1, 3, 24.8 );
1200 $worksheet->write( 1, 4, 25.0 );
1201 $worksheet->write( 1, 5, 25.3 );
1202 $worksheet->write( 1, 6, 24.9 );
1203 $worksheet->write( 1, 7, 25.2 );
1204 $worksheet->write( 1, 8, 24.8 );
1205
1206 # Write some statistical functions
1207 $worksheet->write( 4, 0, 'Count', $format );
1208 $worksheet->write( 4, 1, '=COUNT(B1:I1)' );
1209
1210 $worksheet->write( 5, 0, 'Sum', $format );
1211 $worksheet->write( 5, 1, '=SUM(B2:I2)' );
1212
1213 $worksheet->write( 6, 0, 'Average', $format );
1214 $worksheet->write( 6, 1, '=AVERAGE(B2:I2)' );
1215
1216 $worksheet->write( 7, 0, 'Min', $format );
1217 $worksheet->write( 7, 1, '=MIN(B2:I2)' );
1218
1219 $worksheet->write( 8, 0, 'Max', $format );
1220 $worksheet->write( 8, 1, '=MAX(B2:I2)' );
1221
1222 $worksheet->write( 9, 0, 'Standard Deviation', $format );
1223 $worksheet->write( 9, 1, '=STDEV(B2:I2)' );
1224
1225 $worksheet->write( 10, 0, 'Kurtosis', $format );
1226 $worksheet->write( 10, 1, '=KURT(B2:I2)' );
1227
1228 $workbook->close();
1229
1230 __END__
1231
1232 Download this example:
1233 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/stats.pl>
1234
1235 Example: autofilter.pl
1236 An example of how to create autofilters with Excel::Writer::XLSX.
1237
1238 An autofilter is a way of adding drop down lists to the headers of a 2D
1239 range of worksheet data. This allows users to filter the data based on
1240 simple criteria so that some data is shown and some is hidden.
1241
1242 Source code for this example:
1243
1244 #!/usr/bin/perl
1245
1246 ###############################################################################
1247 #
1248 # An example of how to create autofilters with Excel::Writer::XLSX.
1249 #
1250 # An autofilter is a way of adding drop down lists to the headers of a 2D range
1251 # of worksheet data. This allows users to filter the data based on
1252 # simple criteria so that some data is shown and some is hidden.
1253 #
1254 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
1255 #
1256
1257 use strict;
1258 use warnings;
1259 use Excel::Writer::XLSX;
1260
1261 my $workbook = Excel::Writer::XLSX->new( 'autofilter.xlsx' );
1262
1263 my $worksheet1 = $workbook->add_worksheet();
1264 my $worksheet2 = $workbook->add_worksheet();
1265 my $worksheet3 = $workbook->add_worksheet();
1266 my $worksheet4 = $workbook->add_worksheet();
1267 my $worksheet5 = $workbook->add_worksheet();
1268 my $worksheet6 = $workbook->add_worksheet();
1269
1270 my $bold = $workbook->add_format( bold => 1 );
1271
1272
1273 # Extract the data embedded at the end of this file.
1274 my @headings = split ' ', <DATA>;
1275 my @data;
1276 push @data, [split] while <DATA>;
1277
1278
1279 # Set up several sheets with the same data.
1280 for my $worksheet ( $workbook->sheets() ) {
1281 $worksheet->set_column( 'A:D', 12 );
1282 $worksheet->set_row( 0, 20, $bold );
1283 $worksheet->write( 'A1', \@headings );
1284 }
1285
1286
1287 ###############################################################################
1288 #
1289 # Example 1. Autofilter without conditions.
1290 #
1291
1292 $worksheet1->autofilter( 'A1:D51' );
1293 $worksheet1->write( 'A2', [ [@data] ] );
1294
1295
1296 ###############################################################################
1297 #
1298 #
1299 # Example 2. Autofilter with a filter condition in the first column.
1300 #
1301
1302 # The range in this example is the same as above but in row-column notation.
1303 $worksheet2->autofilter( 0, 0, 50, 3 );
1304
1305 # The placeholder "Region" in the filter is ignored and can be any string
1306 # that adds clarity to the expression.
1307 #
1308 $worksheet2->filter_column( 0, 'Region eq East' );
1309
1310 #
1311 # Hide the rows that don't match the filter criteria.
1312 #
1313 my $row = 1;
1314
1315 for my $row_data ( @data ) {
1316 my $region = $row_data->[0];
1317
1318 if ( $region eq 'East' ) {
1319
1320 # Row is visible.
1321 }
1322 else {
1323
1324 # Hide row.
1325 $worksheet2->set_row( $row, undef, undef, 1 );
1326 }
1327
1328 $worksheet2->write( $row++, 0, $row_data );
1329 }
1330
1331
1332 ###############################################################################
1333 #
1334 #
1335 # Example 3. Autofilter with a dual filter condition in one of the columns.
1336 #
1337
1338 $worksheet3->autofilter( 'A1:D51' );
1339
1340 $worksheet3->filter_column( 'A', 'x eq East or x eq South' );
1341
1342 #
1343 # Hide the rows that don't match the filter criteria.
1344 #
1345 $row = 1;
1346
1347 for my $row_data ( @data ) {
1348 my $region = $row_data->[0];
1349
1350 if ( $region eq 'East' or $region eq 'South' ) {
1351
1352 # Row is visible.
1353 }
1354 else {
1355
1356 # Hide row.
1357 $worksheet3->set_row( $row, undef, undef, 1 );
1358 }
1359
1360 $worksheet3->write( $row++, 0, $row_data );
1361 }
1362
1363
1364 ###############################################################################
1365 #
1366 #
1367 # Example 4. Autofilter with filter conditions in two columns.
1368 #
1369
1370 $worksheet4->autofilter( 'A1:D51' );
1371
1372 $worksheet4->filter_column( 'A', 'x eq East' );
1373 $worksheet4->filter_column( 'C', 'x > 3000 and x < 8000' );
1374
1375 #
1376 # Hide the rows that don't match the filter criteria.
1377 #
1378 $row = 1;
1379
1380 for my $row_data ( @data ) {
1381 my $region = $row_data->[0];
1382 my $volume = $row_data->[2];
1383
1384 if ( $region eq 'East'
1385 and $volume > 3000
1386 and $volume < 8000 )
1387 {
1388
1389 # Row is visible.
1390 }
1391 else {
1392
1393 # Hide row.
1394 $worksheet4->set_row( $row, undef, undef, 1 );
1395 }
1396
1397 $worksheet4->write( $row++, 0, $row_data );
1398 }
1399
1400
1401 ###############################################################################
1402 #
1403 #
1404 # Example 5. Autofilter with filter for blanks.
1405 #
1406
1407 # Create a blank cell in our test data.
1408 $data[5]->[0] = '';
1409
1410
1411 $worksheet5->autofilter( 'A1:D51' );
1412 $worksheet5->filter_column( 'A', 'x == Blanks' );
1413
1414 #
1415 # Hide the rows that don't match the filter criteria.
1416 #
1417 $row = 1;
1418
1419 for my $row_data ( @data ) {
1420 my $region = $row_data->[0];
1421
1422 if ( $region eq '' ) {
1423
1424 # Row is visible.
1425 }
1426 else {
1427
1428 # Hide row.
1429 $worksheet5->set_row( $row, undef, undef, 1 );
1430 }
1431
1432 $worksheet5->write( $row++, 0, $row_data );
1433 }
1434
1435
1436 ###############################################################################
1437 #
1438 #
1439 # Example 6. Autofilter with filter for non-blanks.
1440 #
1441
1442
1443 $worksheet6->autofilter( 'A1:D51' );
1444 $worksheet6->filter_column( 'A', 'x == NonBlanks' );
1445
1446 #
1447 # Hide the rows that don't match the filter criteria.
1448 #
1449 $row = 1;
1450
1451 for my $row_data ( @data ) {
1452 my $region = $row_data->[0];
1453
1454 if ( $region ne '' ) {
1455
1456 # Row is visible.
1457 }
1458 else {
1459
1460 # Hide row.
1461 $worksheet6->set_row( $row, undef, undef, 1 );
1462 }
1463
1464 $worksheet6->write( $row++, 0, $row_data );
1465 }
1466
1467 $workbook->close();
1468
1469 __DATA__
1470 Region Item Volume Month
1471 East Apple 9000 July
1472 East Apple 5000 July
1473 South Orange 9000 September
1474 North Apple 2000 November
1475 West Apple 9000 November
1476 South Pear 7000 October
1477 North Pear 9000 August
1478 West Orange 1000 December
1479 West Grape 1000 November
1480 South Pear 10000 April
1481 West Grape 6000 January
1482 South Orange 3000 May
1483 North Apple 3000 December
1484 South Apple 7000 February
1485 West Grape 1000 December
1486 East Grape 8000 February
1487 South Grape 10000 June
1488 West Pear 7000 December
1489 South Apple 2000 October
1490 East Grape 7000 December
1491 North Grape 6000 April
1492 East Pear 8000 February
1493 North Apple 7000 August
1494 North Orange 7000 July
1495 North Apple 6000 June
1496 South Grape 8000 September
1497 West Apple 3000 October
1498 South Orange 10000 November
1499 West Grape 4000 July
1500 North Orange 5000 August
1501 East Orange 1000 November
1502 East Orange 4000 October
1503 North Grape 5000 August
1504 East Apple 1000 December
1505 South Apple 10000 March
1506 East Grape 7000 October
1507 West Grape 1000 September
1508 East Grape 10000 October
1509 South Orange 8000 March
1510 North Apple 4000 July
1511 South Orange 5000 July
1512 West Apple 4000 June
1513 East Apple 5000 April
1514 North Pear 3000 August
1515 East Grape 9000 November
1516 North Orange 8000 October
1517 East Apple 10000 June
1518 South Pear 1000 December
1519 North Grape 10000 July
1520 East Grape 6000 February
1521
1522 Download this example:
1523 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/autofilter.pl>
1524
1525 Example: array_formula.pl
1526 Example of how to use the Excel::Writer::XLSX module to write simple
1527 array formulas.
1528
1529 Source code for this example:
1530
1531 #!/usr/bin/perl
1532
1533 #######################################################################
1534 #
1535 # Example of how to use the Excel::Writer::XLSX module to write simple
1536 # array formulas.
1537 #
1538 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
1539 #
1540
1541 use strict;
1542 use warnings;
1543 use Excel::Writer::XLSX;
1544
1545 # Create a new workbook and add a worksheet
1546 my $workbook = Excel::Writer::XLSX->new( 'array_formula.xlsx' );
1547 my $worksheet = $workbook->add_worksheet();
1548
1549 # Write some test data.
1550 $worksheet->write( 'B1', [ [ 500, 10 ], [ 300, 15 ] ] );
1551 $worksheet->write( 'B5', [ [ 1, 2, 3 ], [ 20234, 21003, 10000 ] ] );
1552
1553 # Write an array formula that returns a single value
1554 $worksheet->write( 'A1', '{=SUM(B1:C1*B2:C2)}' );
1555
1556 # Same as above but more verbose.
1557 $worksheet->write_array_formula( 'A2:A2', '{=SUM(B1:C1*B2:C2)}' );
1558
1559 # Write an array formula that returns a range of values
1560 $worksheet->write_array_formula( 'A5:A7', '{=TREND(C5:C7,B5:B7)}' );
1561
1562 $workbook->close();
1563
1564 __END__
1565
1566 Download this example:
1567 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/array_formula.pl>
1568
1569 Example: cgi.pl
1570 Example of how to use the Excel::Writer::XLSX module to send an Excel
1571 file to a browser in a CGI program.
1572
1573 On Windows the hash-bang line should be something like:
1574
1575 #!C:\Perl\bin\perl.exe
1576
1577 The "Content-Disposition" line will cause a prompt to be generated to
1578 save the file. If you want to stream the file to the browser instead,
1579 comment out that line as shown below.
1580
1581 #!/usr/bin/perl
1582
1583 ###############################################################################
1584 #
1585 # Example of how to use the Excel::Writer::XLSX module to send an Excel
1586 # file to a browser in a CGI program.
1587 #
1588 # On Windows the hash-bang line should be something like:
1589 #
1590 # #!C:\Perl\bin\perl.exe
1591 #
1592 # The "Content-Disposition" line will cause a prompt to be generated to save
1593 # the file. If you want to stream the file to the browser instead, comment out
1594 # that line as shown below.
1595 #
1596 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
1597 #
1598
1599 use strict;
1600 use warnings;
1601 use Excel::Writer::XLSX;
1602
1603 # Set the filename and send the content type
1604 my $filename = "cgitest.xlsx";
1605
1606 print "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n";
1607
1608 # The Content-Disposition will generate a prompt to save the file. If you want
1609 # to stream the file to the browser, comment out the following line.
1610 print "Content-Disposition: attachment; filename=$filename\n";
1611 print "\n";
1612
1613 # Redirect the output to STDOUT. Binmode the filehandle in case it is needed.
1614 binmode STDOUT;
1615
1616 my $workbook = Excel::Writer::XLSX->new( \*STDOUT );
1617 my $worksheet = $workbook->add_worksheet();
1618
1619
1620 # Set the column width for column 1
1621 $worksheet->set_column( 0, 0, 20 );
1622
1623
1624 # Create a format
1625 my $format = $workbook->add_format();
1626 $format->set_bold();
1627 $format->set_size( 15 );
1628 $format->set_color( 'blue' );
1629
1630
1631 # Write to the workbook
1632 $worksheet->write( 0, 0, "Hi Excel!", $format );
1633
1634 $workbook->close();
1635
1636 __END__
1637
1638 Download this example:
1639 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/cgi.pl>
1640
1641 Example: chart_area.pl
1642 A demo of an Area chart in Excel::Writer::XLSX.
1643
1644 Source code for this example:
1645
1646 #!/usr/bin/perl
1647
1648 #######################################################################
1649 #
1650 # A demo of an Area chart in Excel::Writer::XLSX.
1651 #
1652 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
1653 #
1654
1655 use strict;
1656 use warnings;
1657 use Excel::Writer::XLSX;
1658
1659 my $workbook = Excel::Writer::XLSX->new( 'chart_area.xlsx' );
1660 my $worksheet = $workbook->add_worksheet();
1661 my $bold = $workbook->add_format( bold => 1 );
1662
1663 # Add the worksheet data that the charts will refer to.
1664 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
1665 my $data = [
1666 [ 2, 3, 4, 5, 6, 7 ],
1667 [ 40, 40, 50, 30, 25, 50 ],
1668 [ 30, 25, 30, 10, 5, 10 ],
1669
1670 ];
1671
1672 $worksheet->write( 'A1', $headings, $bold );
1673 $worksheet->write( 'A2', $data );
1674
1675 # Create a new chart object. In this case an embedded chart.
1676 my $chart1 = $workbook->add_chart( type => 'area', embedded => 1 );
1677
1678 # Configure the first series.
1679 $chart1->add_series(
1680 name => '=Sheet1!$B$1',
1681 categories => '=Sheet1!$A$2:$A$7',
1682 values => '=Sheet1!$B$2:$B$7',
1683 );
1684
1685 # Configure second series. Note alternative use of array ref to define
1686 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
1687 $chart1->add_series(
1688 name => '=Sheet1!$C$1',
1689 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1690 values => [ 'Sheet1', 1, 6, 2, 2 ],
1691 );
1692
1693 # Add a chart title and some axis labels.
1694 $chart1->set_title ( name => 'Results of sample analysis' );
1695 $chart1->set_x_axis( name => 'Test number' );
1696 $chart1->set_y_axis( name => 'Sample length (mm)' );
1697
1698 # Set an Excel chart style. Blue colors with white outline and shadow.
1699 $chart1->set_style( 11 );
1700
1701 # Insert the chart into the worksheet (with an offset).
1702 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
1703
1704
1705 #
1706 # Create a stacked chart sub-type
1707 #
1708 my $chart2 = $workbook->add_chart(
1709 type => 'area',
1710 embedded => 1,
1711 subtype => 'stacked'
1712 );
1713
1714 # Configure the first series.
1715 $chart2->add_series(
1716 name => '=Sheet1!$B$1',
1717 categories => '=Sheet1!$A$2:$A$7',
1718 values => '=Sheet1!$B$2:$B$7',
1719 );
1720
1721 # Configure second series.
1722 $chart2->add_series(
1723 name => '=Sheet1!$C$1',
1724 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1725 values => [ 'Sheet1', 1, 6, 2, 2 ],
1726 );
1727
1728 # Add a chart title and some axis labels.
1729 $chart2->set_title ( name => 'Stacked Chart' );
1730 $chart2->set_x_axis( name => 'Test number' );
1731 $chart2->set_y_axis( name => 'Sample length (mm)' );
1732
1733 # Set an Excel chart style. Blue colors with white outline and shadow.
1734 $chart2->set_style( 12 );
1735
1736 # Insert the chart into the worksheet (with an offset).
1737 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
1738
1739
1740 #
1741 # Create a percent stacked chart sub-type
1742 #
1743 my $chart3 = $workbook->add_chart(
1744 type => 'area',
1745 embedded => 1,
1746 subtype => 'percent_stacked'
1747 );
1748
1749 # Configure the first series.
1750 $chart3->add_series(
1751 name => '=Sheet1!$B$1',
1752 categories => '=Sheet1!$A$2:$A$7',
1753 values => '=Sheet1!$B$2:$B$7',
1754 );
1755
1756 # Configure second series.
1757 $chart3->add_series(
1758 name => '=Sheet1!$C$1',
1759 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1760 values => [ 'Sheet1', 1, 6, 2, 2 ],
1761 );
1762
1763 # Add a chart title and some axis labels.
1764 $chart3->set_title ( name => 'Percent Stacked Chart' );
1765 $chart3->set_x_axis( name => 'Test number' );
1766 $chart3->set_y_axis( name => 'Sample length (mm)' );
1767
1768 # Set an Excel chart style. Blue colors with white outline and shadow.
1769 $chart3->set_style( 13 );
1770
1771 # Insert the chart into the worksheet (with an offset).
1772 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
1773
1774 $workbook->close();
1775
1776 __END__
1777
1778 Download this example:
1779 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_area.pl>
1780
1781 Example: chart_bar.pl
1782 A demo of an Bar chart in Excel::Writer::XLSX.
1783
1784 Source code for this example:
1785
1786 #!/usr/bin/perl
1787
1788 #######################################################################
1789 #
1790 # A demo of an Bar chart in Excel::Writer::XLSX.
1791 #
1792 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
1793 #
1794
1795 use strict;
1796 use warnings;
1797 use Excel::Writer::XLSX;
1798
1799 my $workbook = Excel::Writer::XLSX->new( 'chart_bar.xlsx' );
1800 my $worksheet = $workbook->add_worksheet();
1801 my $bold = $workbook->add_format( bold => 1 );
1802
1803 # Add the worksheet data that the charts will refer to.
1804 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
1805 my $data = [
1806 [ 2, 3, 4, 5, 6, 7 ],
1807 [ 10, 40, 50, 20, 10, 50 ],
1808 [ 30, 60, 70, 50, 40, 30 ],
1809
1810 ];
1811
1812 $worksheet->write( 'A1', $headings, $bold );
1813 $worksheet->write( 'A2', $data );
1814
1815 # Create a new chart object. In this case an embedded chart.
1816 my $chart1 = $workbook->add_chart( type => 'bar', embedded => 1 );
1817
1818 # Configure the first series.
1819 $chart1->add_series(
1820 name => '=Sheet1!$B$1',
1821 categories => '=Sheet1!$A$2:$A$7',
1822 values => '=Sheet1!$B$2:$B$7',
1823 );
1824
1825 # Configure second series. Note alternative use of array ref to define
1826 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
1827 $chart1->add_series(
1828 name => '=Sheet1!$C$1',
1829 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1830 values => [ 'Sheet1', 1, 6, 2, 2 ],
1831 );
1832
1833 # Add a chart title and some axis labels.
1834 $chart1->set_title ( name => 'Results of sample analysis' );
1835 $chart1->set_x_axis( name => 'Test number' );
1836 $chart1->set_y_axis( name => 'Sample length (mm)' );
1837
1838 # Set an Excel chart style. Blue colors with white outline and shadow.
1839 $chart1->set_style( 11 );
1840
1841 # Insert the chart into the worksheet (with an offset).
1842 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
1843
1844
1845 #
1846 # Create a stacked chart sub-type
1847 #
1848 my $chart2 = $workbook->add_chart(
1849 type => 'bar',
1850 embedded => 1,
1851 subtype => 'stacked'
1852 );
1853
1854 # Configure the first series.
1855 $chart2->add_series(
1856 name => '=Sheet1!$B$1',
1857 categories => '=Sheet1!$A$2:$A$7',
1858 values => '=Sheet1!$B$2:$B$7',
1859 );
1860
1861 # Configure second series.
1862 $chart2->add_series(
1863 name => '=Sheet1!$C$1',
1864 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1865 values => [ 'Sheet1', 1, 6, 2, 2 ],
1866 );
1867
1868 # Add a chart title and some axis labels.
1869 $chart2->set_title ( name => 'Stacked Chart' );
1870 $chart2->set_x_axis( name => 'Test number' );
1871 $chart2->set_y_axis( name => 'Sample length (mm)' );
1872
1873 # Set an Excel chart style. Blue colors with white outline and shadow.
1874 $chart2->set_style( 12 );
1875
1876 # Insert the chart into the worksheet (with an offset).
1877 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
1878
1879
1880 #
1881 # Create a percent stacked chart sub-type
1882 #
1883 my $chart3 = $workbook->add_chart(
1884 type => 'bar',
1885 embedded => 1,
1886 subtype => 'percent_stacked'
1887 );
1888
1889 # Configure the first series.
1890 $chart3->add_series(
1891 name => '=Sheet1!$B$1',
1892 categories => '=Sheet1!$A$2:$A$7',
1893 values => '=Sheet1!$B$2:$B$7',
1894 );
1895
1896 # Configure second series.
1897 $chart3->add_series(
1898 name => '=Sheet1!$C$1',
1899 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1900 values => [ 'Sheet1', 1, 6, 2, 2 ],
1901 );
1902
1903 # Add a chart title and some axis labels.
1904 $chart3->set_title ( name => 'Percent Stacked Chart' );
1905 $chart3->set_x_axis( name => 'Test number' );
1906 $chart3->set_y_axis( name => 'Sample length (mm)' );
1907
1908 # Set an Excel chart style. Blue colors with white outline and shadow.
1909 $chart3->set_style( 13 );
1910
1911 # Insert the chart into the worksheet (with an offset).
1912 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
1913
1914 $workbook->close();
1915
1916 __END__
1917
1918 Download this example:
1919 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_bar.pl>
1920
1921 Example: chart_column.pl
1922 A demo of a Column chart in Excel::Writer::XLSX.
1923
1924 Source code for this example:
1925
1926 #!/usr/bin/perl
1927
1928 #######################################################################
1929 #
1930 # A demo of a Column chart in Excel::Writer::XLSX.
1931 #
1932 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
1933 #
1934
1935 use strict;
1936 use warnings;
1937 use Excel::Writer::XLSX;
1938
1939 my $workbook = Excel::Writer::XLSX->new( 'chart_column.xlsx' );
1940 my $worksheet = $workbook->add_worksheet();
1941 my $bold = $workbook->add_format( bold => 1 );
1942
1943 # Add the worksheet data that the charts will refer to.
1944 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
1945 my $data = [
1946 [ 2, 3, 4, 5, 6, 7 ],
1947 [ 10, 40, 50, 20, 10, 50 ],
1948 [ 30, 60, 70, 50, 40, 30 ],
1949
1950 ];
1951
1952 $worksheet->write( 'A1', $headings, $bold );
1953 $worksheet->write( 'A2', $data );
1954
1955 # Create a new chart object. In this case an embedded chart.
1956 my $chart1 = $workbook->add_chart( type => 'column', embedded => 1 );
1957
1958 # Configure the first series.
1959 $chart1->add_series(
1960 name => '=Sheet1!$B$1',
1961 categories => '=Sheet1!$A$2:$A$7',
1962 values => '=Sheet1!$B$2:$B$7',
1963 );
1964
1965 # Configure second series. Note alternative use of array ref to define
1966 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
1967 $chart1->add_series(
1968 name => '=Sheet1!$C$1',
1969 categories => [ 'Sheet1', 1, 6, 0, 0 ],
1970 values => [ 'Sheet1', 1, 6, 2, 2 ],
1971 );
1972
1973 # Add a chart title and some axis labels.
1974 $chart1->set_title ( name => 'Results of sample analysis' );
1975 $chart1->set_x_axis( name => 'Test number' );
1976 $chart1->set_y_axis( name => 'Sample length (mm)' );
1977
1978 # Set an Excel chart style. Blue colors with white outline and shadow.
1979 $chart1->set_style( 11 );
1980
1981 # Insert the chart into the worksheet (with an offset).
1982 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
1983
1984
1985 #
1986 # Create a stacked chart sub-type
1987 #
1988 my $chart2 = $workbook->add_chart(
1989 type => 'column',
1990 embedded => 1,
1991 subtype => 'stacked'
1992 );
1993
1994 # Configure the first series.
1995 $chart2->add_series(
1996 name => '=Sheet1!$B$1',
1997 categories => '=Sheet1!$A$2:$A$7',
1998 values => '=Sheet1!$B$2:$B$7',
1999 );
2000
2001 # Configure second series.
2002 $chart2->add_series(
2003 name => '=Sheet1!$C$1',
2004 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2005 values => [ 'Sheet1', 1, 6, 2, 2 ],
2006 );
2007
2008 # Add a chart title and some axis labels.
2009 $chart2->set_title ( name => 'Stacked Chart' );
2010 $chart2->set_x_axis( name => 'Test number' );
2011 $chart2->set_y_axis( name => 'Sample length (mm)' );
2012
2013 # Set an Excel chart style. Blue colors with white outline and shadow.
2014 $chart2->set_style( 12 );
2015
2016 # Insert the chart into the worksheet (with an offset).
2017 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
2018
2019
2020 #
2021 # Create a percent stacked chart sub-type
2022 #
2023 my $chart3 = $workbook->add_chart(
2024 type => 'column',
2025 embedded => 1,
2026 subtype => 'percent_stacked'
2027 );
2028
2029 # Configure the first series.
2030 $chart3->add_series(
2031 name => '=Sheet1!$B$1',
2032 categories => '=Sheet1!$A$2:$A$7',
2033 values => '=Sheet1!$B$2:$B$7',
2034 );
2035
2036 # Configure second series.
2037 $chart3->add_series(
2038 name => '=Sheet1!$C$1',
2039 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2040 values => [ 'Sheet1', 1, 6, 2, 2 ],
2041 );
2042
2043 # Add a chart title and some axis labels.
2044 $chart3->set_title ( name => 'Percent Stacked Chart' );
2045 $chart3->set_x_axis( name => 'Test number' );
2046 $chart3->set_y_axis( name => 'Sample length (mm)' );
2047
2048 # Set an Excel chart style. Blue colors with white outline and shadow.
2049 $chart3->set_style( 13 );
2050
2051 # Insert the chart into the worksheet (with an offset).
2052 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
2053
2054 $workbook->close();
2055
2056 __END__
2057
2058 Download this example:
2059 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_column.pl>
2060
2061 Example: chart_line.pl
2062 A demo of a Line chart in Excel::Writer::XLSX.
2063
2064 Source code for this example:
2065
2066 #!/usr/bin/perl
2067
2068 #######################################################################
2069 #
2070 # A demo of a Line chart in Excel::Writer::XLSX.
2071 #
2072 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
2073 #
2074
2075 use strict;
2076 use warnings;
2077 use Excel::Writer::XLSX;
2078
2079 my $workbook = Excel::Writer::XLSX->new( 'chart_line.xlsx' );
2080 my $worksheet = $workbook->add_worksheet();
2081 my $bold = $workbook->add_format( bold => 1 );
2082
2083 # Add the worksheet data that the charts will refer to.
2084 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
2085 my $data = [
2086 [ 2, 3, 4, 5, 6, 7 ],
2087 [ 10, 40, 50, 20, 10, 50 ],
2088 [ 30, 60, 70, 50, 40, 30 ],
2089
2090 ];
2091
2092 $worksheet->write( 'A1', $headings, $bold );
2093 $worksheet->write( 'A2', $data );
2094
2095 # Create a new chart object. In this case an embedded chart.
2096 my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
2097
2098 # Configure the first series.
2099 $chart->add_series(
2100 name => '=Sheet1!$B$1',
2101 categories => '=Sheet1!$A$2:$A$7',
2102 values => '=Sheet1!$B$2:$B$7',
2103 );
2104
2105 # Configure second series. Note alternative use of array ref to define
2106 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2107 $chart->add_series(
2108 name => '=Sheet1!$C$1',
2109 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2110 values => [ 'Sheet1', 1, 6, 2, 2 ],
2111 );
2112
2113 # Add a chart title and some axis labels.
2114 $chart->set_title ( name => 'Results of sample analysis' );
2115 $chart->set_x_axis( name => 'Test number' );
2116 $chart->set_y_axis( name => 'Sample length (mm)' );
2117
2118 # Set an Excel chart style. Colors with white outline and shadow.
2119 $chart->set_style( 10 );
2120
2121 # Insert the chart into the worksheet (with an offset).
2122 $worksheet->insert_chart( 'D2', $chart, { x_offset => 25, y_offset => 10 } );
2123
2124
2125 #
2126 # Create a stacked chart sub-type
2127 #
2128 my $chart2 = $workbook->add_chart(
2129 type => 'line',
2130 embedded => 1,
2131 subtype => 'stacked'
2132 );
2133
2134 # Configure the first series.
2135 $chart2->add_series(
2136 name => '=Sheet1!$B$1',
2137 categories => '=Sheet1!$A$2:$A$7',
2138 values => '=Sheet1!$B$2:$B$7',
2139 );
2140
2141 # Configure second series.
2142 $chart2->add_series(
2143 name => '=Sheet1!$C$1',
2144 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2145 values => [ 'Sheet1', 1, 6, 2, 2 ],
2146 );
2147
2148 # Add a chart title and some axis labels.
2149 $chart2->set_title ( name => 'Stacked Chart' );
2150 $chart2->set_x_axis( name => 'Test number' );
2151 $chart2->set_y_axis( name => 'Sample length (mm)' );
2152
2153 # Set an Excel chart style. Blue colors with white outline and shadow.
2154 $chart2->set_style( 12 );
2155
2156 # Insert the chart into the worksheet (with an offset).
2157 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
2158
2159
2160 #
2161 # Create a percent stacked chart sub-type
2162 #
2163 my $chart3 = $workbook->add_chart(
2164 type => 'line',
2165 embedded => 1,
2166 subtype => 'percent_stacked'
2167 );
2168
2169 # Configure the first series.
2170 $chart3->add_series(
2171 name => '=Sheet1!$B$1',
2172 categories => '=Sheet1!$A$2:$A$7',
2173 values => '=Sheet1!$B$2:$B$7',
2174 );
2175
2176 # Configure second series.
2177 $chart3->add_series(
2178 name => '=Sheet1!$C$1',
2179 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2180 values => [ 'Sheet1', 1, 6, 2, 2 ],
2181 );
2182
2183 # Add a chart title and some axis labels.
2184 $chart3->set_title ( name => 'Percent Stacked Chart' );
2185 $chart3->set_x_axis( name => 'Test number' );
2186 $chart3->set_y_axis( name => 'Sample length (mm)' );
2187
2188 # Set an Excel chart style. Blue colors with white outline and shadow.
2189 $chart3->set_style( 13 );
2190
2191 # Insert the chart into the worksheet (with an offset).
2192 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
2193
2194 $workbook->close();
2195
2196 __END__
2197
2198 Download this example:
2199 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_line.pl>
2200
2201 Example: chart_pie.pl
2202 A demo of a Pie chart in Excel::Writer::XLSX.
2203
2204 The demo also shows how to set segment colours. It is possible to
2205 define chart colors for most types of Excel::Writer::XLSX charts via
2206 the add_series() method. However, Pie and Doughtnut charts are a
2207 special case since each segment is represented as a point so it is
2208 necessary to assign formatting to each point in the series.
2209
2210 Source code for this example:
2211
2212 #!/usr/bin/perl
2213
2214 #######################################################################
2215 #
2216 # A demo of a Pie chart in Excel::Writer::XLSX.
2217 #
2218 # The demo also shows how to set segment colours. It is possible to define
2219 # chart colors for most types of Excel::Writer::XLSX charts via the
2220 # add_series() method. However, Pie and Doughtnut charts are a special case
2221 # since each segment is represented as a point so it is necessary to assign
2222 # formatting to each point in the series.
2223 #
2224 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
2225 #
2226
2227 use strict;
2228 use warnings;
2229 use Excel::Writer::XLSX;
2230
2231 my $workbook = Excel::Writer::XLSX->new( 'chart_pie.xlsx' );
2232 my $worksheet = $workbook->add_worksheet();
2233 my $bold = $workbook->add_format( bold => 1 );
2234
2235 # Add the worksheet data that the charts will refer to.
2236 my $headings = [ 'Category', 'Values' ];
2237 my $data = [
2238 [ 'Apple', 'Cherry', 'Pecan' ],
2239 [ 60, 30, 10 ],
2240 ];
2241
2242 $worksheet->write( 'A1', $headings, $bold );
2243 $worksheet->write( 'A2', $data );
2244
2245 # Create a new chart object. In this case an embedded chart.
2246 my $chart1 = $workbook->add_chart( type => 'pie', embedded => 1 );
2247
2248 # Configure the series. Note the use of the array ref to define ranges:
2249 # [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2250 # See below for an alternative syntax.
2251 $chart1->add_series(
2252 name => 'Pie sales data',
2253 categories => [ 'Sheet1', 1, 3, 0, 0 ],
2254 values => [ 'Sheet1', 1, 3, 1, 1 ],
2255 );
2256
2257 # Add a title.
2258 $chart1->set_title( name => 'Popular Pie Types' );
2259
2260 # Set an Excel chart style. Colors with white outline and shadow.
2261 $chart1->set_style( 10 );
2262
2263 # Insert the chart into the worksheet (with an offset).
2264 $worksheet->insert_chart( 'C2', $chart1, { x_offset => 25, y_offset => 10 } );
2265
2266
2267 #
2268 # Create a Pie chart with user defined segment colors.
2269 #
2270
2271 # Create an example Pie chart like above.
2272 my $chart2 = $workbook->add_chart( type => 'pie', embedded => 1 );
2273
2274 # Configure the series and add user defined segment colours.
2275 $chart2->add_series(
2276 name => 'Pie sales data',
2277 categories => '=Sheet1!$A$2:$A$4',
2278 values => '=Sheet1!$B$2:$B$4',
2279 points => [
2280 { fill => { color => '#5ABA10' } },
2281 { fill => { color => '#FE110E' } },
2282 { fill => { color => '#CA5C05' } },
2283 ],
2284 );
2285
2286 # Add a title.
2287 $chart2->set_title( name => 'Pie Chart with user defined colors' );
2288
2289
2290 # Insert the chart into the worksheet (with an offset).
2291 $worksheet->insert_chart( 'C18', $chart2, { x_offset => 25, y_offset => 10 } );
2292
2293
2294 #
2295 # Create a Pie chart with rotation of the segments.
2296 #
2297
2298 # Create an example Pie chart like above.
2299 my $chart3 = $workbook->add_chart( type => 'pie', embedded => 1 );
2300
2301 # Configure the series.
2302 $chart3->add_series(
2303 name => 'Pie sales data',
2304 categories => '=Sheet1!$A$2:$A$4',
2305 values => '=Sheet1!$B$2:$B$4',
2306 );
2307
2308 # Add a title.
2309 $chart3->set_title( name => 'Pie Chart with segment rotation' );
2310
2311 # Change the angle/rotation of the first segment.
2312 $chart3->set_rotation(90);
2313
2314 # Insert the chart into the worksheet (with an offset).
2315 $worksheet->insert_chart( 'C34', $chart3, { x_offset => 25, y_offset => 10 } );
2316
2317 $workbook->close();
2318
2319 __END__
2320
2321 Download this example:
2322 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_pie.pl>
2323
2324 Example: chart_doughnut.pl
2325 A demo of a Doughnut chart in Excel::Writer::XLSX.
2326
2327 The demo also shows how to set segment colours. It is possible to
2328 define chart colors for most types of Excel::Writer::XLSX charts via
2329 the add_series() method. However, Pie and Doughtnut charts are a
2330 special case since each segment is represented as a point so it is
2331 necessary to assign formatting to each point in the series.
2332
2333 Source code for this example:
2334
2335 #!/usr/bin/perl
2336
2337 #######################################################################
2338 #
2339 # A demo of a Doughnut chart in Excel::Writer::XLSX.
2340 #
2341 # The demo also shows how to set segment colours. It is possible to define
2342 # chart colors for most types of Excel::Writer::XLSX charts via the
2343 # add_series() method. However, Pie and Doughtnut charts are a special case
2344 # since each segment is represented as a point so it is necessary to assign
2345 # formatting to each point in the series.
2346 #
2347 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
2348 #
2349
2350 use strict;
2351 use warnings;
2352 use Excel::Writer::XLSX;
2353
2354 my $workbook = Excel::Writer::XLSX->new( 'chart_doughnut.xlsx' );
2355 my $worksheet = $workbook->add_worksheet();
2356 my $bold = $workbook->add_format( bold => 1 );
2357
2358 # Add the worksheet data that the charts will refer to.
2359 my $headings = [ 'Category', 'Values' ];
2360 my $data = [
2361 [ 'Glazed', 'Chocolate', 'Cream' ],
2362 [ 50, 35, 15 ],
2363 ];
2364
2365 $worksheet->write( 'A1', $headings, $bold );
2366 $worksheet->write( 'A2', $data );
2367
2368 # Create a new chart object. In this case an embedded chart.
2369 my $chart1 = $workbook->add_chart( type => 'doughnut', embedded => 1 );
2370
2371 # Configure the series. Note the use of the array ref to define ranges:
2372 # [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2373 # See below for an alternative syntax.
2374 $chart1->add_series(
2375 name => 'Doughnut sales data',
2376 categories => [ 'Sheet1', 1, 3, 0, 0 ],
2377 values => [ 'Sheet1', 1, 3, 1, 1 ],
2378 );
2379
2380 # Add a title.
2381 $chart1->set_title( name => 'Popular Doughnut Types' );
2382
2383 # Set an Excel chart style. Colors with white outline and shadow.
2384 $chart1->set_style( 10 );
2385
2386 # Insert the chart into the worksheet (with an offset).
2387 $worksheet->insert_chart( 'C2', $chart1, { x_offset => 25, y_offset => 10 } );
2388
2389
2390 #
2391 # Create a Doughnut chart with user defined segment colors.
2392 #
2393
2394 # Create an example Doughnut chart like above.
2395 my $chart2 = $workbook->add_chart( type => 'doughnut', embedded => 1 );
2396
2397 # Configure the series and add user defined segment colours.
2398 $chart2->add_series(
2399 name => 'Doughnut sales data',
2400 categories => '=Sheet1!$A$2:$A$4',
2401 values => '=Sheet1!$B$2:$B$4',
2402 points => [
2403 { fill => { color => '#FA58D0' } },
2404 { fill => { color => '#61210B' } },
2405 { fill => { color => '#F5F6CE' } },
2406 ],
2407 );
2408
2409 # Add a title.
2410 $chart2->set_title( name => 'Doughnut Chart with user defined colors' );
2411
2412
2413 # Insert the chart into the worksheet (with an offset).
2414 $worksheet->insert_chart( 'C18', $chart2, { x_offset => 25, y_offset => 10 } );
2415
2416
2417 #
2418 # Create a Doughnut chart with rotation of the segments.
2419 #
2420
2421 # Create an example Doughnut chart like above.
2422 my $chart3 = $workbook->add_chart( type => 'doughnut', embedded => 1 );
2423
2424 # Configure the series.
2425 $chart3->add_series(
2426 name => 'Doughnut sales data',
2427 categories => '=Sheet1!$A$2:$A$4',
2428 values => '=Sheet1!$B$2:$B$4',
2429 );
2430
2431 # Add a title.
2432 $chart3->set_title( name => 'Doughnut Chart with segment rotation' );
2433
2434 # Change the angle/rotation of the first segment.
2435 $chart3->set_rotation(90);
2436
2437 # Insert the chart into the worksheet (with an offset).
2438 $worksheet->insert_chart( 'C34', $chart3, { x_offset => 25, y_offset => 10 } );
2439
2440
2441 #
2442 # Create a Doughnut chart with user defined hole size.
2443 #
2444
2445 # Create an example Doughnut chart like above.
2446 my $chart4 = $workbook->add_chart( type => 'doughnut', embedded => 1 );
2447
2448 # Configure the series.
2449 $chart4->add_series(
2450 name => 'Doughnut sales data',
2451 categories => '=Sheet1!$A$2:$A$4',
2452 values => '=Sheet1!$B$2:$B$4',
2453 );
2454
2455 # Add a title.
2456 $chart4->set_title( name => 'Doughnut Chart with user defined hole size' );
2457
2458 # Change the hole size.
2459 $chart4->set_hole_size(33);
2460
2461 # Insert the chart into the worksheet (with an offset).
2462 $worksheet->insert_chart( 'C50', $chart4, { x_offset => 25, y_offset => 10 } );
2463
2464 $workbook->close();
2465
2466 __END__
2467
2468 Download this example:
2469 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_doughnut.pl>
2470
2471 Example: chart_radar.pl
2472 A demo of an Radar chart in Excel::Writer::XLSX.
2473
2474 Source code for this example:
2475
2476 #!/usr/bin/perl
2477
2478 #######################################################################
2479 #
2480 # A demo of an Radar chart in Excel::Writer::XLSX.
2481 #
2482 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
2483 #
2484
2485 use strict;
2486 use warnings;
2487 use Excel::Writer::XLSX;
2488
2489 my $workbook = Excel::Writer::XLSX->new( 'chart_radar.xlsx' );
2490 my $worksheet = $workbook->add_worksheet();
2491 my $bold = $workbook->add_format( bold => 1 );
2492
2493 # Add the worksheet data that the charts will refer to.
2494 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
2495 my $data = [
2496 [ 2, 3, 4, 5, 6, 7 ],
2497 [ 30, 60, 70, 50, 40, 30 ],
2498 [ 25, 40, 50, 30, 50, 40 ],
2499
2500 ];
2501
2502 $worksheet->write( 'A1', $headings, $bold );
2503 $worksheet->write( 'A2', $data );
2504
2505 # Create a new chart object. In this case an embedded chart.
2506 my $chart1 = $workbook->add_chart( type => 'radar', embedded => 1 );
2507
2508 # Configure the first series.
2509 $chart1->add_series(
2510 name => '=Sheet1!$B$1',
2511 categories => '=Sheet1!$A$2:$A$7',
2512 values => '=Sheet1!$B$2:$B$7',
2513 );
2514
2515 # Configure second series. Note alternative use of array ref to define
2516 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2517 $chart1->add_series(
2518 name => '=Sheet1!$C$1',
2519 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2520 values => [ 'Sheet1', 1, 6, 2, 2 ],
2521 );
2522
2523 # Add a chart title and some axis labels.
2524 $chart1->set_title ( name => 'Results of sample analysis' );
2525 $chart1->set_x_axis( name => 'Test number' );
2526 $chart1->set_y_axis( name => 'Sample length (mm)' );
2527
2528 # Set an Excel chart style. Blue colors with white outline and shadow.
2529 $chart1->set_style( 11 );
2530
2531 # Insert the chart into the worksheet (with an offset).
2532 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
2533
2534
2535 #
2536 # Create a with_markers chart sub-type
2537 #
2538 my $chart2 = $workbook->add_chart(
2539 type => 'radar',
2540 embedded => 1,
2541 subtype => 'with_markers'
2542 );
2543
2544 # Configure the first series.
2545 $chart2->add_series(
2546 name => '=Sheet1!$B$1',
2547 categories => '=Sheet1!$A$2:$A$7',
2548 values => '=Sheet1!$B$2:$B$7',
2549 );
2550
2551 # Configure second series.
2552 $chart2->add_series(
2553 name => '=Sheet1!$C$1',
2554 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2555 values => [ 'Sheet1', 1, 6, 2, 2 ],
2556 );
2557
2558 # Add a chart title and some axis labels.
2559 $chart2->set_title ( name => 'Stacked Chart' );
2560 $chart2->set_x_axis( name => 'Test number' );
2561 $chart2->set_y_axis( name => 'Sample length (mm)' );
2562
2563 # Set an Excel chart style. Blue colors with white outline and shadow.
2564 $chart2->set_style( 12 );
2565
2566 # Insert the chart into the worksheet (with an offset).
2567 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
2568
2569
2570 #
2571 # Create a filled chart sub-type
2572 #
2573 my $chart3 = $workbook->add_chart(
2574 type => 'radar',
2575 embedded => 1,
2576 subtype => 'filled'
2577 );
2578
2579 # Configure the first series.
2580 $chart3->add_series(
2581 name => '=Sheet1!$B$1',
2582 categories => '=Sheet1!$A$2:$A$7',
2583 values => '=Sheet1!$B$2:$B$7',
2584 );
2585
2586 # Configure second series.
2587 $chart3->add_series(
2588 name => '=Sheet1!$C$1',
2589 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2590 values => [ 'Sheet1', 1, 6, 2, 2 ],
2591 );
2592
2593 # Add a chart title and some axis labels.
2594 $chart3->set_title ( name => 'Percent Stacked Chart' );
2595 $chart3->set_x_axis( name => 'Test number' );
2596 $chart3->set_y_axis( name => 'Sample length (mm)' );
2597
2598 # Set an Excel chart style. Blue colors with white outline and shadow.
2599 $chart3->set_style( 13 );
2600
2601 # Insert the chart into the worksheet (with an offset).
2602 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
2603
2604 $workbook->close();
2605
2606 __END__
2607
2608 Download this example:
2609 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_radar.pl>
2610
2611 Example: chart_scatter.pl
2612 A demo of a Scatter chart in Excel::Writer::XLSX. Other subtypes are
2613 also supported such as markers_only (the default),
2614 straight_with_markers, straight, smooth_with_markers and smooth. See
2615 the main documentation for more details.
2616
2617 Source code for this example:
2618
2619 #!/usr/bin/perl
2620
2621 #######################################################################
2622 #
2623 # A demo of a Scatter chart in Excel::Writer::XLSX. Other subtypes are
2624 # also supported such as markers_only (the default), straight_with_markers,
2625 # straight, smooth_with_markers and smooth. See the main documentation for
2626 # more details.
2627 #
2628 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
2629 #
2630
2631 use strict;
2632 use warnings;
2633 use Excel::Writer::XLSX;
2634
2635 my $workbook = Excel::Writer::XLSX->new( 'chart_scatter.xlsx' );
2636 my $worksheet = $workbook->add_worksheet();
2637 my $bold = $workbook->add_format( bold => 1 );
2638
2639 # Add the worksheet data that the charts will refer to.
2640 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
2641 my $data = [
2642 [ 2, 3, 4, 5, 6, 7 ],
2643 [ 10, 40, 50, 20, 10, 50 ],
2644 [ 30, 60, 70, 50, 40, 30 ],
2645
2646 ];
2647
2648 $worksheet->write( 'A1', $headings, $bold );
2649 $worksheet->write( 'A2', $data );
2650
2651 # Create a new chart object. In this case an embedded chart.
2652 my $chart1 = $workbook->add_chart( type => 'scatter', embedded => 1 );
2653
2654 # Configure the first series.
2655 $chart1->add_series(
2656 name => '=Sheet1!$B$1',
2657 categories => '=Sheet1!$A$2:$A$7',
2658 values => '=Sheet1!$B$2:$B$7',
2659 );
2660
2661 # Configure second series. Note alternative use of array ref to define
2662 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
2663 $chart1->add_series(
2664 name => '=Sheet1!$C$1',
2665 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2666 values => [ 'Sheet1', 1, 6, 2, 2 ],
2667 );
2668
2669 # Add a chart title and some axis labels.
2670 $chart1->set_title ( name => 'Results of sample analysis' );
2671 $chart1->set_x_axis( name => 'Test number' );
2672 $chart1->set_y_axis( name => 'Sample length (mm)' );
2673
2674 # Set an Excel chart style. Blue colors with white outline and shadow.
2675 $chart1->set_style( 11 );
2676
2677 # Insert the chart into the worksheet (with an offset).
2678 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
2679
2680
2681 #
2682 # Create a scatter chart sub-type with straight lines and markers.
2683 #
2684 my $chart2 = $workbook->add_chart(
2685 type => 'scatter',
2686 embedded => 1,
2687 subtype => 'straight_with_markers'
2688 );
2689
2690 # Configure the first series.
2691 $chart2->add_series(
2692 name => '=Sheet1!$B$1',
2693 categories => '=Sheet1!$A$2:$A$7',
2694 values => '=Sheet1!$B$2:$B$7',
2695 );
2696
2697 # Configure second series.
2698 $chart2->add_series(
2699 name => '=Sheet1!$C$1',
2700 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2701 values => [ 'Sheet1', 1, 6, 2, 2 ],
2702 );
2703
2704 # Add a chart title and some axis labels.
2705 $chart2->set_title ( name => 'Straight line with markers' );
2706 $chart2->set_x_axis( name => 'Test number' );
2707 $chart2->set_y_axis( name => 'Sample length (mm)' );
2708
2709 # Set an Excel chart style. Blue colors with white outline and shadow.
2710 $chart2->set_style( 12 );
2711
2712 # Insert the chart into the worksheet (with an offset).
2713 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
2714
2715
2716 #
2717 # Create a scatter chart sub-type with straight lines and no markers.
2718 #
2719 my $chart3 = $workbook->add_chart(
2720 type => 'scatter',
2721 embedded => 1,
2722 subtype => 'straight'
2723 );
2724
2725 # Configure the first series.
2726 $chart3->add_series(
2727 name => '=Sheet1!$B$1',
2728 categories => '=Sheet1!$A$2:$A$7',
2729 values => '=Sheet1!$B$2:$B$7',
2730 );
2731
2732 # Configure second series.
2733 $chart3->add_series(
2734 name => '=Sheet1!$C$1',
2735 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2736 values => [ 'Sheet1', 1, 6, 2, 2 ],
2737 );
2738
2739 # Add a chart title and some axis labels.
2740 $chart3->set_title ( name => 'Straight line' );
2741 $chart3->set_x_axis( name => 'Test number' );
2742 $chart3->set_y_axis( name => 'Sample length (mm)' );
2743
2744 # Set an Excel chart style. Blue colors with white outline and shadow.
2745 $chart3->set_style( 13 );
2746
2747 # Insert the chart into the worksheet (with an offset).
2748 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
2749
2750
2751 #
2752 # Create a scatter chart sub-type with smooth lines and markers.
2753 #
2754 my $chart4 = $workbook->add_chart(
2755 type => 'scatter',
2756 embedded => 1,
2757 subtype => 'smooth_with_markers'
2758 );
2759
2760 # Configure the first series.
2761 $chart4->add_series(
2762 name => '=Sheet1!$B$1',
2763 categories => '=Sheet1!$A$2:$A$7',
2764 values => '=Sheet1!$B$2:$B$7',
2765 );
2766
2767 # Configure second series.
2768 $chart4->add_series(
2769 name => '=Sheet1!$C$1',
2770 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2771 values => [ 'Sheet1', 1, 6, 2, 2 ],
2772 );
2773
2774 # Add a chart title and some axis labels.
2775 $chart4->set_title ( name => 'Smooth line with markers' );
2776 $chart4->set_x_axis( name => 'Test number' );
2777 $chart4->set_y_axis( name => 'Sample length (mm)' );
2778
2779 # Set an Excel chart style. Blue colors with white outline and shadow.
2780 $chart4->set_style( 14 );
2781
2782 # Insert the chart into the worksheet (with an offset).
2783 $worksheet->insert_chart( 'D51', $chart4, { x_offset => 25, y_offset => 10 } );
2784
2785
2786 #
2787 # Create a scatter chart sub-type with smooth lines and no markers.
2788 #
2789 my $chart5 = $workbook->add_chart(
2790 type => 'scatter',
2791 embedded => 1,
2792 subtype => 'smooth'
2793 );
2794
2795 # Configure the first series.
2796 $chart5->add_series(
2797 name => '=Sheet1!$B$1',
2798 categories => '=Sheet1!$A$2:$A$7',
2799 values => '=Sheet1!$B$2:$B$7',
2800 );
2801
2802 # Configure second series.
2803 $chart5->add_series(
2804 name => '=Sheet1!$C$1',
2805 categories => [ 'Sheet1', 1, 6, 0, 0 ],
2806 values => [ 'Sheet1', 1, 6, 2, 2 ],
2807 );
2808
2809 # Add a chart title and some axis labels.
2810 $chart5->set_title ( name => 'Smooth line' );
2811 $chart5->set_x_axis( name => 'Test number' );
2812 $chart5->set_y_axis( name => 'Sample length (mm)' );
2813
2814 # Set an Excel chart style. Blue colors with white outline and shadow.
2815 $chart5->set_style( 15 );
2816
2817 # Insert the chart into the worksheet (with an offset).
2818 $worksheet->insert_chart( 'D66', $chart5, { x_offset => 25, y_offset => 10 } );
2819
2820
2821 $workbook->close();
2822
2823 __END__
2824
2825 Download this example:
2826 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_scatter.pl>
2827
2828 Example: chart_secondary_axis.pl
2829 A demo of a Line chart with a secondary axis in Excel::Writer::XLSX.
2830
2831 Source code for this example:
2832
2833 #!/usr/bin/perl
2834
2835 #######################################################################
2836 #
2837 # A demo of a Line chart with a secondary axis in Excel::Writer::XLSX.
2838 #
2839 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
2840 #
2841
2842 use strict;
2843 use warnings;
2844 use Excel::Writer::XLSX;
2845
2846 my $workbook = Excel::Writer::XLSX->new( 'chart_secondary_axis.xlsx' );
2847 my $worksheet = $workbook->add_worksheet();
2848 my $bold = $workbook->add_format( bold => 1 );
2849
2850 # Add the worksheet data that the charts will refer to.
2851 my $headings = [ 'Aliens', 'Humans', ];
2852 my $data = [
2853 [ 2, 3, 4, 5, 6, 7 ],
2854 [ 10, 40, 50, 20, 10, 50 ],
2855
2856 ];
2857
2858
2859 $worksheet->write( 'A1', $headings, $bold );
2860 $worksheet->write( 'A2', $data );
2861
2862 # Create a new chart object. In this case an embedded chart.
2863 my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
2864
2865 # Configure a series with a secondary axis
2866 $chart->add_series(
2867 name => '=Sheet1!$A$1',
2868 values => '=Sheet1!$A$2:$A$7',
2869 y2_axis => 1,
2870 );
2871
2872 $chart->add_series(
2873 name => '=Sheet1!$B$1',
2874 values => '=Sheet1!$B$2:$B$7',
2875 );
2876
2877 $chart->set_legend( position => 'right' );
2878
2879 # Add a chart title and some axis labels.
2880 $chart->set_title( name => 'Survey results' );
2881 $chart->set_x_axis( name => 'Days', );
2882 $chart->set_y_axis( name => 'Population', major_gridlines => { visible => 0 } );
2883 $chart->set_y2_axis( name => 'Laser wounds' );
2884
2885 # Insert the chart into the worksheet (with an offset).
2886 $worksheet->insert_chart( 'D2', $chart, { x_offset => 25, y_offset => 10 } );
2887
2888 $workbook->close();
2889
2890 __END__
2891
2892 Download this example:
2893 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_secondary_axis.pl>
2894
2895 Example: chart_combined.pl
2896 An example of a Combined chart in Excel::Writer::XLSX.
2897
2898 #!/usr/bin/perl
2899
2900 #######################################################################
2901 #
2902 # An example of a Combined chart in Excel::Writer::XLSX.
2903 #
2904 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
2905 #
2906
2907 use strict;
2908 use warnings;
2909 use Excel::Writer::XLSX;
2910
2911 my $workbook = Excel::Writer::XLSX->new( 'chart_combined.xlsx' );
2912 my $worksheet = $workbook->add_worksheet();
2913 my $bold = $workbook->add_format( bold => 1 );
2914
2915 # Add the worksheet data that the charts will refer to.
2916 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
2917 my $data = [
2918 [ 2, 3, 4, 5, 6, 7 ],
2919 [ 10, 40, 50, 20, 10, 50 ],
2920 [ 30, 60, 70, 50, 40, 30 ],
2921
2922 ];
2923
2924 $worksheet->write( 'A1', $headings, $bold );
2925 $worksheet->write( 'A2', $data );
2926
2927 #
2928 # In the first example we will create a combined column and line chart.
2929 # They will share the same X and Y axes.
2930 #
2931
2932 # Create a new column chart. This will use this as the primary chart.
2933 my $column_chart1 = $workbook->add_chart( type => 'column', embedded => 1 );
2934
2935 # Configure the data series for the primary chart.
2936 $column_chart1->add_series(
2937 name => '=Sheet1!$B$1',
2938 categories => '=Sheet1!$A$2:$A$7',
2939 values => '=Sheet1!$B$2:$B$7',
2940 );
2941
2942 # Create a new column chart. This will use this as the secondary chart.
2943 my $line_chart1 = $workbook->add_chart( type => 'line', embedded => 1 );
2944
2945 # Configure the data series for the secondary chart.
2946 $line_chart1->add_series(
2947 name => '=Sheet1!$C$1',
2948 categories => '=Sheet1!$A$2:$A$7',
2949 values => '=Sheet1!$C$2:$C$7',
2950 );
2951
2952 # Combine the charts.
2953 $column_chart1->combine( $line_chart1 );
2954
2955 # Add a chart title and some axis labels. Note, this is done via the
2956 # primary chart.
2957 $column_chart1->set_title( name => 'Combined chart - same Y axis' );
2958 $column_chart1->set_x_axis( name => 'Test number' );
2959 $column_chart1->set_y_axis( name => 'Sample length (mm)' );
2960
2961
2962 # Insert the chart into the worksheet
2963 $worksheet->insert_chart( 'E2', $column_chart1 );
2964
2965 #
2966 # In the second example we will create a similar combined column and line
2967 # chart except that the secondary chart will have a secondary Y axis.
2968 #
2969
2970 # Create a new column chart. This will use this as the primary chart.
2971 my $column_chart2 = $workbook->add_chart( type => 'column', embedded => 1 );
2972
2973 # Configure the data series for the primary chart.
2974 $column_chart2->add_series(
2975 name => '=Sheet1!$B$1',
2976 categories => '=Sheet1!$A$2:$A$7',
2977 values => '=Sheet1!$B$2:$B$7',
2978 );
2979
2980 # Create a new column chart. This will use this as the secondary chart.
2981 my $line_chart2 = $workbook->add_chart( type => 'line', embedded => 1 );
2982
2983 # Configure the data series for the secondary chart. We also set a
2984 # secondary Y axis via (y2_axis). This is the only difference between
2985 # this and the first example, apart from the axis label below.
2986 $line_chart2->add_series(
2987 name => '=Sheet1!$C$1',
2988 categories => '=Sheet1!$A$2:$A$7',
2989 values => '=Sheet1!$C$2:$C$7',
2990 y2_axis => 1,
2991 );
2992
2993 # Combine the charts.
2994 $column_chart2->combine( $line_chart2 );
2995
2996 # Add a chart title and some axis labels.
2997 $column_chart2->set_title( name => 'Combine chart - secondary Y axis' );
2998 $column_chart2->set_x_axis( name => 'Test number' );
2999 $column_chart2->set_y_axis( name => 'Sample length (mm)' );
3000
3001 # Note: the y2 properites are on the secondary chart.
3002 $line_chart2->set_y2_axis( name => 'Target length (mm)' );
3003
3004
3005 # Insert the chart into the worksheet
3006 $worksheet->insert_chart( 'E18', $column_chart2 );
3007
3008 $workbook->close();
3009
3010 __END__
3011
3012 Download this example:
3013 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_combined.pl>
3014
3015 Example: chart_pareto.pl
3016 A demo of a Pareto chart in Excel::Writer::XLSX.
3017
3018 #!/usr/bin/perl
3019
3020 #######################################################################
3021 #
3022 # A demo of a Pareto chart in Excel::Writer::XLSX.
3023 #
3024 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
3025 #
3026
3027 use strict;
3028 use warnings;
3029 use Excel::Writer::XLSX;
3030
3031 my $workbook = Excel::Writer::XLSX->new( 'chart_pareto.xlsx' );
3032 my $worksheet = $workbook->add_worksheet();
3033
3034 # Formats used in the workbook.
3035 my $bold = $workbook->add_format( bold => 1 );
3036 my $percent_format = $workbook->add_format( num_format => '0.0%' );
3037
3038
3039 # Widen the columns for visibility.
3040 $worksheet->set_column( 'A:A', 15 );
3041 $worksheet->set_column( 'B:C', 10 );
3042
3043 # Add the worksheet data that the charts will refer to.
3044 my $headings = [ 'Reason', 'Number', 'Percentage' ];
3045
3046 my $reasons = [
3047 'Traffic', 'Child care', 'Public Transport', 'Weather',
3048 'Overslept', 'Emergency',
3049 ];
3050
3051 my $numbers = [ 60, 40, 20, 15, 10, 5 ];
3052 my $percents = [ 0.44, 0.667, 0.8, 0.9, 0.967, 1 ];
3053
3054 $worksheet->write_row( 'A1', $headings, $bold );
3055 $worksheet->write_col( 'A2', $reasons );
3056 $worksheet->write_col( 'B2', $numbers );
3057 $worksheet->write_col( 'C2', $percents, $percent_format );
3058
3059
3060 # Create a new column chart. This will be the primary chart.
3061 my $column_chart = $workbook->add_chart( type => 'column', embedded => 1 );
3062
3063 # Add a series.
3064 $column_chart->add_series(
3065 categories => '=Sheet1!$A$2:$A$7',
3066 values => '=Sheet1!$B$2:$B$7',
3067 );
3068
3069 # Add a chart title.
3070 $column_chart->set_title( name => 'Reasons for lateness' );
3071
3072 # Turn off the chart legend.
3073 $column_chart->set_legend( position => 'none' );
3074
3075 # Set the title and scale of the Y axes. Note, the secondary axis is set from
3076 # the primary chart.
3077 $column_chart->set_y_axis(
3078 name => 'Respondents (number)',
3079 min => 0,
3080 max => 120
3081 );
3082 $column_chart->set_y2_axis( max => 1 );
3083
3084 # Create a new line chart. This will be the secondary chart.
3085 my $line_chart = $workbook->add_chart( type => 'line', embedded => 1 );
3086
3087 # Add a series, on the secondary axis.
3088 $line_chart->add_series(
3089 categories => '=Sheet1!$A$2:$A$7',
3090 values => '=Sheet1!$C$2:$C$7',
3091 marker => { type => 'automatic' },
3092 y2_axis => 1,
3093 );
3094
3095
3096 # Combine the charts.
3097 $column_chart->combine( $line_chart );
3098
3099 # Insert the chart into the worksheet.
3100 $worksheet->insert_chart( 'F2', $column_chart );
3101
3102 $workbook->close();
3103
3104 __END__
3105
3106 Download this example:
3107 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_pareto.pl>
3108
3109 Example: chart_stock.pl
3110 A demo of a Stock chart in Excel::Writer::XLSX.
3111
3112 Source code for this example:
3113
3114 #!/usr/bin/perl
3115
3116 #######################################################################
3117 #
3118 # A demo of a Stock chart in Excel::Writer::XLSX.
3119 #
3120 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
3121 #
3122
3123 use strict;
3124 use warnings;
3125 use Excel::Writer::XLSX;
3126 use Excel::Writer::XLSX;
3127
3128 my $workbook = Excel::Writer::XLSX->new( 'chart_stock.xlsx' );
3129 my $worksheet = $workbook->add_worksheet();
3130 my $bold = $workbook->add_format( bold => 1 );
3131 my $date_format = $workbook->add_format( num_format => 'dd/mm/yyyy' );
3132 my $chart = $workbook->add_chart( type => 'stock', embedded => 1 );
3133
3134
3135 # Add the worksheet data that the charts will refer to.
3136 my $headings = [ 'Date', 'High', 'Low', 'Close' ];
3137 my $data = [
3138
3139 [ '2007-01-01T', '2007-01-02T', '2007-01-03T', '2007-01-04T', '2007-01-05T' ],
3140 [ 27.2, 25.03, 19.05, 20.34, 18.5 ],
3141 [ 23.49, 19.55, 15.12, 17.84, 16.34 ],
3142 [ 25.45, 23.05, 17.32, 20.45, 17.34 ],
3143
3144 ];
3145
3146 $worksheet->write( 'A1', $headings, $bold );
3147
3148 for my $row ( 0 .. 4 ) {
3149 $worksheet->write_date_time( $row+1, 0, $data->[0]->[$row], $date_format );
3150 $worksheet->write( $row+1, 1, $data->[1]->[$row] );
3151 $worksheet->write( $row+1, 2, $data->[2]->[$row] );
3152 $worksheet->write( $row+1, 3, $data->[3]->[$row] );
3153
3154 }
3155
3156 $worksheet->set_column( 'A:D', 11 );
3157
3158 # Add a series for each of the High-Low-Close columns.
3159 $chart->add_series(
3160 categories => '=Sheet1!$A$2:$A$6',
3161 values => '=Sheet1!$B$2:$B$6',
3162 );
3163
3164 $chart->add_series(
3165 categories => '=Sheet1!$A$2:$A$6',
3166 values => '=Sheet1!$C$2:$C$6',
3167 );
3168
3169 $chart->add_series(
3170 categories => '=Sheet1!$A$2:$A$6',
3171 values => '=Sheet1!$D$2:$D$6',
3172 );
3173
3174 # Add a chart title and some axis labels.
3175 $chart->set_title ( name => 'High-Low-Close', );
3176 $chart->set_x_axis( name => 'Date', );
3177 $chart->set_y_axis( name => 'Share price', );
3178
3179
3180 $worksheet->insert_chart( 'E9', $chart );
3181
3182 $workbook->close();
3183
3184 __END__
3185
3186 Download this example:
3187 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_stock.pl>
3188
3189 Example: chart_data_table.pl
3190 A demo of an Column chart with a data table on the X-axis using
3191 Excel::Writer::XLSX.
3192
3193 Source code for this example:
3194
3195 #!/usr/bin/perl
3196
3197 #######################################################################
3198 #
3199 # A demo of an Column chart with a data table on the X-axis using
3200 # Excel::Writer::XLSX.
3201 #
3202 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
3203 #
3204
3205 use strict;
3206 use warnings;
3207 use Excel::Writer::XLSX;
3208
3209 my $workbook = Excel::Writer::XLSX->new( 'chart_data_table.xlsx' );
3210 my $worksheet = $workbook->add_worksheet();
3211 my $bold = $workbook->add_format( bold => 1 );
3212
3213 # Add the worksheet data that the charts will refer to.
3214 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
3215 my $data = [
3216 [ 2, 3, 4, 5, 6, 7 ],
3217 [ 10, 40, 50, 20, 10, 50 ],
3218 [ 30, 60, 70, 50, 40, 30 ],
3219
3220 ];
3221
3222 $worksheet->write( 'A1', $headings, $bold );
3223 $worksheet->write( 'A2', $data );
3224
3225 # Create a column chart with a data table.
3226 my $chart1 = $workbook->add_chart( type => 'column', embedded => 1 );
3227
3228 # Configure the first series.
3229 $chart1->add_series(
3230 name => '=Sheet1!$B$1',
3231 categories => '=Sheet1!$A$2:$A$7',
3232 values => '=Sheet1!$B$2:$B$7',
3233 );
3234
3235 # Configure second series. Note alternative use of array ref to define
3236 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
3237 $chart1->add_series(
3238 name => '=Sheet1!$C$1',
3239 categories => [ 'Sheet1', 1, 6, 0, 0 ],
3240 values => [ 'Sheet1', 1, 6, 2, 2 ],
3241 );
3242
3243 # Add a chart title and some axis labels.
3244 $chart1->set_title( name => 'Chart with Data Table' );
3245 $chart1->set_x_axis( name => 'Test number' );
3246 $chart1->set_y_axis( name => 'Sample length (mm)' );
3247
3248 # Set a default data table on the X-Axis.
3249 $chart1->set_table();
3250
3251 # Insert the chart into the worksheet (with an offset).
3252 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
3253
3254
3255 #
3256 # Create a second chart.
3257 #
3258 my $chart2 = $workbook->add_chart( type => 'column', embedded => 1 );
3259
3260 # Configure the first series.
3261 $chart2->add_series(
3262 name => '=Sheet1!$B$1',
3263 categories => '=Sheet1!$A$2:$A$7',
3264 values => '=Sheet1!$B$2:$B$7',
3265 );
3266
3267 # Configure second series.
3268 $chart2->add_series(
3269 name => '=Sheet1!$C$1',
3270 categories => [ 'Sheet1', 1, 6, 0, 0 ],
3271 values => [ 'Sheet1', 1, 6, 2, 2 ],
3272 );
3273
3274 # Add a chart title and some axis labels.
3275 $chart2->set_title( name => 'Data Table with legend keys' );
3276 $chart2->set_x_axis( name => 'Test number' );
3277 $chart2->set_y_axis( name => 'Sample length (mm)' );
3278
3279 # Set a data table on the X-Axis with the legend keys showm.
3280 $chart2->set_table( show_keys => 1 );
3281
3282 # Hide the chart legend since the keys are show on the data table.
3283 $chart2->set_legend( position => 'none' );
3284
3285 # Insert the chart into the worksheet (with an offset).
3286 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
3287
3288 $workbook->close();
3289
3290 __END__
3291
3292 Download this example:
3293 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_data_table.pl>
3294
3295 Example: chart_data_tools.pl
3296 A demo of an various Excel chart data tools that are available via an
3297 Excel::Writer::XLSX chart.
3298
3299 These include, Trendlines, Data Labels, Error Bars, Drop Lines, High-
3300 Low Lines and Up-Down Bars.
3301
3302 Source code for this example:
3303
3304 #!/usr/bin/perl
3305
3306 #######################################################################
3307 #
3308 # A demo of an various Excel chart data tools that are available via
3309 # an Excel::Writer::XLSX chart.
3310 #
3311 # These include, Trendlines, Data Labels, Error Bars, Drop Lines,
3312 # High-Low Lines and Up-Down Bars.
3313 #
3314 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
3315 #
3316
3317 use strict;
3318 use warnings;
3319 use Excel::Writer::XLSX;
3320
3321 my $workbook = Excel::Writer::XLSX->new( 'chart_data_tools.xlsx' );
3322 my $worksheet = $workbook->add_worksheet();
3323 my $bold = $workbook->add_format( bold => 1 );
3324
3325 # Add the worksheet data that the charts will refer to.
3326 my $headings = [ 'Number', 'Data 1', 'Data 2' ];
3327 my $data = [
3328 [ 2, 3, 4, 5, 6, 7 ],
3329 [ 10, 40, 50, 20, 10, 50 ],
3330 [ 30, 60, 70, 50, 40, 30 ],
3331
3332 ];
3333
3334 $worksheet->write( 'A1', $headings, $bold );
3335 $worksheet->write( 'A2', $data );
3336
3337
3338 #######################################################################
3339 #
3340 # Trendline example.
3341 #
3342
3343 # Create a Line chart.
3344 my $chart1 = $workbook->add_chart( type => 'line', embedded => 1 );
3345
3346 # Configure the first series with a polynomial trendline.
3347 $chart1->add_series(
3348 categories => '=Sheet1!$A$2:$A$7',
3349 values => '=Sheet1!$B$2:$B$7',
3350 trendline => {
3351 type => 'polynomial',
3352 order => 3,
3353 },
3354 );
3355
3356 # Configure the second series with a moving average trendline.
3357 $chart1->add_series(
3358 categories => '=Sheet1!$A$2:$A$7',
3359 values => '=Sheet1!$C$2:$C$7',
3360 trendline => { type => 'linear' },
3361 );
3362
3363 # Add a chart title. and some axis labels.
3364 $chart1->set_title( name => 'Chart with Trendlines' );
3365
3366 # Insert the chart into the worksheet (with an offset).
3367 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
3368
3369
3370 #######################################################################
3371 #
3372 # Data Labels and Markers example.
3373 #
3374
3375 # Create a Line chart.
3376 my $chart2 = $workbook->add_chart( type => 'line', embedded => 1 );
3377
3378 # Configure the first series.
3379 $chart2->add_series(
3380 categories => '=Sheet1!$A$2:$A$7',
3381 values => '=Sheet1!$B$2:$B$7',
3382 data_labels => { value => 1 },
3383 marker => { type => 'automatic' },
3384 );
3385
3386 # Configure the second series.
3387 $chart2->add_series(
3388 categories => '=Sheet1!$A$2:$A$7',
3389 values => '=Sheet1!$C$2:$C$7',
3390 );
3391
3392 # Add a chart title. and some axis labels.
3393 $chart2->set_title( name => 'Chart with Data Labels and Markers' );
3394
3395 # Insert the chart into the worksheet (with an offset).
3396 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
3397
3398
3399 #######################################################################
3400 #
3401 # Error Bars example.
3402 #
3403
3404 # Create a Line chart.
3405 my $chart3 = $workbook->add_chart( type => 'line', embedded => 1 );
3406
3407 # Configure the first series.
3408 $chart3->add_series(
3409 categories => '=Sheet1!$A$2:$A$7',
3410 values => '=Sheet1!$B$2:$B$7',
3411 y_error_bars => { type => 'standard_error' },
3412 );
3413
3414 # Configure the second series.
3415 $chart3->add_series(
3416 categories => '=Sheet1!$A$2:$A$7',
3417 values => '=Sheet1!$C$2:$C$7',
3418 );
3419
3420 # Add a chart title. and some axis labels.
3421 $chart3->set_title( name => 'Chart with Error Bars' );
3422
3423 # Insert the chart into the worksheet (with an offset).
3424 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
3425
3426
3427 #######################################################################
3428 #
3429 # Up-Down Bars example.
3430 #
3431
3432 # Create a Line chart.
3433 my $chart4 = $workbook->add_chart( type => 'line', embedded => 1 );
3434
3435 # Add the Up-Down Bars.
3436 $chart4->set_up_down_bars();
3437
3438 # Configure the first series.
3439 $chart4->add_series(
3440 categories => '=Sheet1!$A$2:$A$7',
3441 values => '=Sheet1!$B$2:$B$7',
3442 );
3443
3444 # Configure the second series.
3445 $chart4->add_series(
3446 categories => '=Sheet1!$A$2:$A$7',
3447 values => '=Sheet1!$C$2:$C$7',
3448 );
3449
3450 # Add a chart title. and some axis labels.
3451 $chart4->set_title( name => 'Chart with Up-Down Bars' );
3452
3453 # Insert the chart into the worksheet (with an offset).
3454 $worksheet->insert_chart( 'D50', $chart4, { x_offset => 25, y_offset => 10 } );
3455
3456
3457 #######################################################################
3458 #
3459 # High-Low Lines example.
3460 #
3461
3462 # Create a Line chart.
3463 my $chart5 = $workbook->add_chart( type => 'line', embedded => 1 );
3464
3465 # Add the High-Low lines.
3466 $chart5->set_high_low_lines();
3467
3468 # Configure the first series.
3469 $chart5->add_series(
3470 categories => '=Sheet1!$A$2:$A$7',
3471 values => '=Sheet1!$B$2:$B$7',
3472 );
3473
3474 # Configure the second series.
3475 $chart5->add_series(
3476 categories => '=Sheet1!$A$2:$A$7',
3477 values => '=Sheet1!$C$2:$C$7',
3478 );
3479
3480 # Add a chart title. and some axis labels.
3481 $chart5->set_title( name => 'Chart with High-Low Lines' );
3482
3483 # Insert the chart into the worksheet (with an offset).
3484 $worksheet->insert_chart( 'D66', $chart5, { x_offset => 25, y_offset => 10 } );
3485
3486
3487 #######################################################################
3488 #
3489 # Drop Lines example.
3490 #
3491
3492 # Create a Line chart.
3493 my $chart6 = $workbook->add_chart( type => 'line', embedded => 1 );
3494
3495 # Add Drop Lines.
3496 $chart6->set_drop_lines();
3497
3498 # Configure the first series.
3499 $chart6->add_series(
3500 categories => '=Sheet1!$A$2:$A$7',
3501 values => '=Sheet1!$B$2:$B$7',
3502 );
3503
3504 # Configure the second series.
3505 $chart6->add_series(
3506 categories => '=Sheet1!$A$2:$A$7',
3507 values => '=Sheet1!$C$2:$C$7',
3508 );
3509
3510 # Add a chart title. and some axis labels.
3511 $chart6->set_title( name => 'Chart with Drop Lines' );
3512
3513 # Insert the chart into the worksheet (with an offset).
3514 $worksheet->insert_chart( 'D82', $chart6, { x_offset => 25, y_offset => 10 } );
3515
3516 $workbook->close();
3517
3518 __END__
3519
3520 Download this example:
3521 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_data_tools.pl>
3522
3523 Example: chart_data_labels.pl
3524 A demo of an various Excel chart data label features that are available
3525 via an Excel::Writer::XLSX chart.
3526
3527 #!/usr/bin/perl
3528
3529 #######################################################################
3530 #
3531 # A demo of an various Excel chart data label features that are available
3532 # via an Excel::Writer::XLSX chart.
3533 #
3534 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
3535 #
3536
3537 use strict;
3538 use warnings;
3539 use Excel::Writer::XLSX;
3540
3541 my $workbook = Excel::Writer::XLSX->new( 'chart_data_labels.xlsx' );
3542 my $worksheet = $workbook->add_worksheet();
3543 my $bold = $workbook->add_format( bold => 1 );
3544
3545 # Add the worksheet data that the charts will refer to.
3546 my $headings = [ 'Number', 'Data', 'Text' ];
3547 my $data = [
3548 [ 2, 3, 4, 5, 6, 7 ],
3549 [ 20, 10, 20, 30, 40, 30 ],
3550 [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun' ],
3551 ];
3552
3553 $worksheet->write( 'A1', $headings, $bold );
3554 $worksheet->write( 'A2', $data );
3555
3556
3557 #######################################################################
3558 #
3559 # Example with standard data labels.
3560 #
3561
3562 # Create a Column chart.
3563 my $chart1 = $workbook->add_chart( type => 'column', embedded => 1 );
3564
3565 # Configure the data series and add the data labels.
3566 $chart1->add_series(
3567 categories => '=Sheet1!$A$2:$A$7',
3568 values => '=Sheet1!$B$2:$B$7',
3569 data_labels => { value => 1 },
3570 );
3571
3572 # Add a chart title.
3573 $chart1->set_title( name => 'Chart with standard data labels' );
3574
3575 # Turn off the chart legend.
3576 $chart1->set_legend( none => 1 );
3577
3578 # Insert the chart into the worksheet (with an offset).
3579 $worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } );
3580
3581
3582 #######################################################################
3583 #
3584 # Example with value and category data labels.
3585 #
3586
3587 # Create a Column chart.
3588 my $chart2 = $workbook->add_chart( type => 'column', embedded => 1 );
3589
3590 # Configure the data series and add the data labels.
3591 $chart2->add_series(
3592 categories => '=Sheet1!$A$2:$A$7',
3593 values => '=Sheet1!$B$2:$B$7',
3594 data_labels => { value => 1, category => 1 },
3595 );
3596
3597 # Add a chart title.
3598 $chart2->set_title( name => 'Category and Value data labels' );
3599
3600 # Turn off the chart legend.
3601 $chart2->set_legend( none => 1 );
3602
3603 # Insert the chart into the worksheet (with an offset).
3604 $worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } );
3605
3606
3607 #######################################################################
3608 #
3609 # Example with standard data labels with different font.
3610 #
3611
3612 # Create a Column chart.
3613 my $chart3 = $workbook->add_chart( type => 'column', embedded => 1 );
3614
3615 # Configure the data series and add the data labels.
3616 $chart3->add_series(
3617 categories => '=Sheet1!$A$2:$A$7',
3618 values => '=Sheet1!$B$2:$B$7',
3619 data_labels => { value => 1,
3620 font => { bold => 1,
3621 color => 'red',
3622 rotation => -30} },
3623 );
3624
3625 # Add a chart title.
3626 $chart3->set_title( name => 'Data labels with user defined font' );
3627
3628 # Turn off the chart legend.
3629 $chart3->set_legend( none => 1 );
3630
3631 # Insert the chart into the worksheet (with an offset).
3632 $worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } );
3633
3634
3635 #######################################################################
3636 #
3637 # Example with standard data labels and formatting.
3638 #
3639
3640 # Create a Column chart.
3641 my $chart4 = $workbook->add_chart( type => 'column', embedded => 1 );
3642
3643 # Configure the data series and add the data labels.
3644 $chart4->add_series(
3645 categories => '=Sheet1!$A$2:$A$7',
3646 values => '=Sheet1!$B$2:$B$7',
3647 data_labels => { value => 1,
3648 border => {color => 'red'},
3649 fill => {color => 'yellow'} },
3650 );
3651
3652 # Add a chart title.
3653 $chart4->set_title( name => 'Data labels with formatting' );
3654
3655 # Turn off the chart legend.
3656 $chart4->set_legend( none => 1 );
3657
3658 # Insert the chart into the worksheet (with an offset).
3659 $worksheet->insert_chart( 'D50', $chart4, { x_offset => 25, y_offset => 10 } );
3660
3661
3662 #######################################################################
3663 #
3664 # Example with custom string data labels.
3665 #
3666
3667 # Create a Column chart.
3668 my $chart5 = $workbook->add_chart( type => 'column', embedded => 1 );
3669
3670 # Some custom labels.
3671 my $custom_labels = [
3672 { value => 'Amy' },
3673 { value => 'Bea' },
3674 { value => 'Eva' },
3675 { value => 'Fay' },
3676 { value => 'Liv' },
3677 { value => 'Una' },
3678 ];
3679
3680
3681 # Configure the data series and add the data labels.
3682 $chart5->add_series(
3683 categories => '=Sheet1!$A$2:$A$7',
3684 values => '=Sheet1!$B$2:$B$7',
3685 data_labels => { value => 1, custom => $custom_labels },
3686 );
3687
3688 # Add a chart title.
3689 $chart5->set_title( name => 'Chart with custom string data labels' );
3690
3691 # Turn off the chart legend.
3692 $chart5->set_legend( none => 1 );
3693
3694 # Insert the chart into the worksheet (with an offset).
3695 $worksheet->insert_chart( 'D66', $chart5, { x_offset => 25, y_offset => 10 } );
3696
3697
3698 #######################################################################
3699 #
3700 # Example with custom data labels from cells.
3701 #
3702
3703 # Create a Column chart.
3704 my $chart6 = $workbook->add_chart( type => 'column', embedded => 1 );
3705
3706 # Some custom labels.
3707 $custom_labels = [
3708 { value => '=Sheet1!$C$2' },
3709 { value => '=Sheet1!$C$3' },
3710 { value => '=Sheet1!$C$4' },
3711 { value => '=Sheet1!$C$5' },
3712 { value => '=Sheet1!$C$6' },
3713 { value => '=Sheet1!$C$7' },
3714 ];
3715
3716
3717 # Configure the data series and add the data labels.
3718 $chart6->add_series(
3719 categories => '=Sheet1!$A$2:$A$7',
3720 values => '=Sheet1!$B$2:$B$7',
3721 data_labels => { value => 1, custom => $custom_labels },
3722 );
3723
3724 # Add a chart title.
3725 $chart6->set_title( name => 'Chart with custom data labels from cells' );
3726
3727 # Turn off the chart legend.
3728 $chart6->set_legend( none => 1 );
3729
3730 # Insert the chart into the worksheet (with an offset).
3731 $worksheet->insert_chart( 'D82', $chart6, { x_offset => 25, y_offset => 10 } );
3732
3733
3734 #######################################################################
3735 #
3736 # Example with custom and default data labels.
3737 #
3738
3739 # Create a Column chart.
3740 my $chart7 = $workbook->add_chart( type => 'column', embedded => 1 );
3741
3742 # Some custom labels. The undef items will get the default value.
3743 # We also set a font for the custom items as an extra example.
3744 $custom_labels = [
3745 { value => '=Sheet1!$C$2', font => { color => 'red' } },
3746 undef,
3747 { value => '=Sheet1!$C$4', font => { color => 'red' } },
3748 { value => '=Sheet1!$C$5', font => { color => 'red' } },
3749 ];
3750
3751
3752 # Configure the data series and add the data labels.
3753 $chart7->add_series(
3754 categories => '=Sheet1!$A$2:$A$7',
3755 values => '=Sheet1!$B$2:$B$7',
3756 data_labels => { value => 1, custom => $custom_labels },
3757 );
3758
3759 # Add a chart title.
3760 $chart7->set_title( name => 'Mixed custom and default data labels' );
3761
3762 # Turn off the chart legend.
3763 $chart7->set_legend( none => 1 );
3764
3765 # Insert the chart into the worksheet (with an offset).
3766 $worksheet->insert_chart( 'D98', $chart7, { x_offset => 25, y_offset => 10 } );
3767
3768
3769 #######################################################################
3770 #
3771 # Example with deleted custom data labels.
3772 #
3773
3774 # Create a Column chart.
3775 my $chart8 = $workbook->add_chart( type => 'column', embedded => 1 );
3776
3777 # Some deleted custom labels and defaults (undef). This allows us to
3778 # highlight certain values such as the minimum and maximum.
3779 $custom_labels = [
3780 { delete => 1 },
3781 undef,
3782 { delete => 1 },
3783 { delete => 1 },
3784 undef,
3785 { delete => 1 },
3786 ];
3787
3788 # Configure the data series and add the data labels.
3789 $chart8->add_series(
3790 categories => '=Sheet1!$A$2:$A$7',
3791 values => '=Sheet1!$B$2:$B$7',
3792 data_labels => { value => 1, custom => $custom_labels },
3793 );
3794
3795 # Add a chart title.
3796 $chart8->set_title( name => 'Chart with deleted data labels' );
3797
3798 # Turn off the chart legend.
3799 $chart8->set_legend( none => 1 );
3800
3801 # Insert the chart into the worksheet (with an offset).
3802 $worksheet->insert_chart( 'D114', $chart8, { x_offset => 25, y_offset => 10 } );
3803
3804
3805 #######################################################################
3806 #
3807 # Example with custom string data labels and formatting.
3808 #
3809
3810 # Create a Column chart.
3811 my $chart9 = $workbook->add_chart( type => 'column', embedded => 1 );
3812
3813 # Some custom labels.
3814 $custom_labels = [
3815 { value => 'Amy', border => {color => 'blue'} },
3816 { value => 'Bea' },
3817 { value => 'Eva' },
3818 { value => 'Fay' },
3819 { value => 'Liv' },
3820 { value => 'Una', fill => {color => 'green'} },
3821 ];
3822
3823
3824 # Configure the data series and add the data labels.
3825 $chart9->add_series(
3826 categories => '=Sheet1!$A$2:$A$7',
3827 values => '=Sheet1!$B$2:$B$7',
3828 data_labels => { value => 1,
3829 custom => $custom_labels,
3830 border => {color => 'red'},
3831 fill => {color => 'yellow'} },
3832 );
3833
3834 # Add a chart title.
3835 $chart9->set_title( name => 'Chart with custom labels and formatting' );
3836
3837 # Turn off the chart legend.
3838 $chart9->set_legend( none => 1 );
3839
3840 # Insert the chart into the worksheet (with an offset).
3841 $worksheet->insert_chart( 'D130', $chart9, { x_offset => 25, y_offset => 10 } );
3842
3843
3844 $workbook->close();
3845
3846 __END__
3847
3848 Download this example:
3849 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_data_labels.pl>
3850
3851 Example: chart_clustered.pl
3852 A demo of a clustered category chart in Excel::Writer::XLSX.
3853
3854 #!/usr/bin/perl
3855
3856 #######################################################################
3857 #
3858 # A demo of a clustered category chart in Excel::Writer::XLSX.
3859 #
3860 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
3861 #
3862
3863 use strict;
3864 use warnings;
3865 use Excel::Writer::XLSX;
3866
3867 my $workbook = Excel::Writer::XLSX->new( 'chart_clustered.xlsx' );
3868 my $worksheet = $workbook->add_worksheet();
3869 my $bold = $workbook->add_format( bold => 1 );
3870
3871 # Add the worksheet data that the charts will refer to.
3872 my $headings = [ 'Types', 'Sub Type', 'Value 1', 'Value 2', 'Value 3' ];
3873 my $data = [
3874 [ 'Type 1', 'Sub Type A', 5000, 8000, 6000 ],
3875 [ '', 'Sub Type B', 2000, 3000, 4000 ],
3876 [ '', 'Sub Type C', 250, 1000, 2000 ],
3877 [ 'Type 2', 'Sub Type D', 6000, 6000, 6500 ],
3878 [ '', 'Sub Type E', 500, 300, 200 ],
3879 ];
3880
3881 $worksheet->write( 'A1', $headings, $bold );
3882 $worksheet->write_col( 'A2', $data );
3883
3884 # Create a new chart object. In this case an embedded chart.
3885 my $chart = $workbook->add_chart( type => 'column', embedded => 1 );
3886
3887 # Configure the series. Note, that the categories are 2D ranges (from column A
3888 # to column B). This creates the clusters. The series are shown as formula
3889 # strings for clarity but you can also use the array syntax. See the docs.
3890 $chart->add_series(
3891 name => '=Sheet1!$C$1',
3892 categories => '=Sheet1!$A$2:$B$6',
3893 values => '=Sheet1!$C$2:$C$6',
3894 );
3895
3896 $chart->add_series(
3897 name => '=Sheet1!$D$1',
3898 categories => '=Sheet1!$A$2:$B$6',
3899 values => '=Sheet1!$D$2:$D$6',
3900 );
3901
3902 $chart->add_series(
3903 name => '=Sheet1!$E$1',
3904 categories => '=Sheet1!$A$2:$B$6',
3905 values => '=Sheet1!$E$2:$E$6',
3906 );
3907
3908 # Set the Excel chart style.
3909 $chart->set_style( 37 );
3910
3911 # Turn off the legend.
3912 $chart->set_legend( position => 'none' );
3913
3914 # Insert the chart into the worksheet.
3915 $worksheet->insert_chart( 'G3', $chart );
3916
3917 $workbook->close();
3918
3919 __END__
3920
3921 Download this example:
3922 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_clustered.pl>
3923
3924 Example: chart_styles.pl
3925 An example showing all 48 default chart styles available in Excel 2007
3926 using Excel::Writer::XLSX.. Note, these styles are not the same as the
3927 styles available in Excel 2013.
3928
3929 #!/usr/bin/perl
3930
3931 #######################################################################
3932 #
3933 # An example showing all 48 default chart styles available in Excel 2007
3934 # using Excel::Writer::XLSX.. Note, these styles are not the same as the
3935 # styles available in Excel 2013.
3936 #
3937 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
3938 #
3939
3940 use strict;
3941 use warnings;
3942 use Excel::Writer::XLSX;
3943
3944 my $workbook = Excel::Writer::XLSX->new( 'chart_styles.xlsx' );
3945
3946 # Show the styles for all of these chart types.
3947 my @chart_types = ( 'column', 'area', 'line', 'pie' );
3948
3949
3950 for my $chart_type ( @chart_types ) {
3951
3952 # Add a worksheet for each chart type.
3953 my $worksheet = $workbook->add_worksheet( ucfirst( $chart_type ) );
3954 $worksheet->set_zoom( 30 );
3955 my $style_number = 1;
3956
3957 # Create 48 charts, each with a different style.
3958 for ( my $row_num = 0 ; $row_num < 90 ; $row_num += 15 ) {
3959 for ( my $col_num = 0 ; $col_num < 64 ; $col_num += 8 ) {
3960
3961 my $chart = $workbook->add_chart(
3962 type => $chart_type,
3963 embedded => 1
3964 );
3965
3966 $chart->add_series( values => '=Data!$A$1:$A$6' );
3967 $chart->set_title( name => 'Style ' . $style_number );
3968 $chart->set_legend( none => 1 );
3969 $chart->set_style( $style_number );
3970
3971 $worksheet->insert_chart( $row_num, $col_num, $chart );
3972 $style_number++;
3973 }
3974 }
3975 }
3976
3977 # Create a worksheet with data for the charts.
3978 my $data = [ 10, 40, 50, 20, 10, 50 ];
3979 my $data_worksheet = $workbook->add_worksheet( 'Data' );
3980 $data_worksheet->write_col( 'A1', $data );
3981 $data_worksheet->hide();
3982
3983 $workbook->close();
3984
3985 __END__
3986
3987 Download this example:
3988 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_styles.pl>
3989
3990 Example: chart_gauge.pl
3991 A demo of an Gauge Chart in Excel::Writer::XLSX.
3992
3993 A Gauge Chart isn't a native chart type in Excel. It is constructed by
3994 combining a doughnut chart and a pie chart and by using some non-filled
3995 elements. This example follows the following online example of how to
3996 create a Gauge Chart in Excel:
3997 https://www.excel-easy.com/examples/gauge-chart.html
3998
3999 #!/usr/bin/perl
4000
4001 #######################################################################
4002 #
4003 # A demo of an Gauge Chart in Excel::Writer::XLSX.
4004 #
4005 # A Gauge Chart isn't a native chart type in Excel. It is constructed by
4006 # combining a doughnut chart and a pie chart and by using some non-filled
4007 # elements. This example follows the following online example of how to create
4008 # a Gauge Chart in Excel: https://www.excel-easy.com/examples/gauge-chart.html
4009 #
4010 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
4011 #
4012
4013 use strict;
4014 use warnings;
4015 use Excel::Writer::XLSX;
4016
4017 my $workbook = Excel::Writer::XLSX->new( 'chart_gauge.xlsx' );
4018 my $worksheet = $workbook->add_worksheet();
4019
4020 my $chart_doughnut = $workbook->add_chart( type => 'doughnut', embedded => 1 );
4021 my $chart_pie = $workbook->add_chart( type => 'pie', embedded => 1 );
4022
4023 # Add some data for the Doughnut and Pie charts. This is set up so the
4024 # gauge goes from 0-100. It is initially set at 75%.
4025 $worksheet->write_col( 'H2', ['Donut', 25, 50, 25, 100] );
4026 $worksheet->write_col( 'I2', ['Pie', 75, 1, '=200-I4-I3'] );
4027
4028 # Configure the doughnut chart as the background for the gauge.
4029 $chart_doughnut->add_series(
4030 name => '=Sheet1!$H$2',
4031 values => '=Sheet1!$H$3:$H$6',
4032 points => [
4033 { fill => { color => 'green' } },
4034 { fill => { color => 'yellow' } },
4035 { fill => { color => 'red' } },
4036 { fill => { none => 1 } },
4037 ],
4038 );
4039
4040 # Rotate chart so the gauge parts are above the horizontal.
4041 $chart_doughnut->set_rotation( 270 );
4042
4043 # Turn off the chart legend.
4044 $chart_doughnut->set_legend( none => 1 );
4045
4046 # Turn off the chart fill and border.
4047 $chart_doughnut->set_chartarea(
4048 border => { none => 1 },
4049 fill => { none => 1 },
4050 );
4051
4052 # Configure the pie chart as the needle for the gauge.
4053 $chart_pie->add_series(
4054 name => '=Sheet1!$I$2',
4055 values => '=Sheet1!$I$3:$I$6',
4056 points => [
4057 { fill => { none => 1 } },
4058 { fill => { color => 'black' } },
4059 { fill => { none => 1 } },
4060 ],
4061 );
4062
4063 # Rotate the pie chart/needle to align with the doughnut/gauge.
4064 $chart_pie->set_rotation( 270 );
4065
4066 # Combine the pie and doughnut charts.
4067 $chart_doughnut->combine($chart_pie);
4068
4069 # Insert the chart into the worksheet.
4070 $worksheet->insert_chart( 'A1', $chart_doughnut );
4071
4072 $workbook->close();
4073
4074 Download this example:
4075 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/chart_gauge.pl>
4076
4077 Example: colors.pl
4078 Demonstrates Excel::Writer::XLSX's named colours and the Excel colour
4079 palette.
4080
4081 The set_custom_color() Worksheet method can be used to override one of
4082 the built-in palette values with a more suitable colour. See the main
4083 docs.
4084
4085 Source code for this example:
4086
4087 #!/usr/bin/perl -w
4088
4089 ################################################################################
4090 #
4091 # Demonstrates Excel::Writer::XLSX's named colours and the Excel colour
4092 # palette.
4093 #
4094 # The set_custom_color() Worksheet method can be used to override one of the
4095 # built-in palette values with a more suitable colour. See the main docs.
4096 #
4097 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
4098 #
4099
4100 use strict;
4101 use Excel::Writer::XLSX;
4102
4103 my $workbook = Excel::Writer::XLSX->new( 'colors.xlsx' );
4104
4105 # Some common formats
4106 my $center = $workbook->add_format( align => 'center' );
4107 my $heading = $workbook->add_format( align => 'center', bold => 1 );
4108
4109
4110 ######################################################################
4111 #
4112 # Demonstrate the named colors.
4113 #
4114
4115 my %colors = (
4116 0x08, 'black',
4117 0x0C, 'blue',
4118 0x10, 'brown',
4119 0x0F, 'cyan',
4120 0x17, 'gray',
4121 0x11, 'green',
4122 0x0B, 'lime',
4123 0x0E, 'magenta',
4124 0x12, 'navy',
4125 0x35, 'orange',
4126 0x21, 'pink',
4127 0x14, 'purple',
4128 0x0A, 'red',
4129 0x16, 'silver',
4130 0x09, 'white',
4131 0x0D, 'yellow',
4132
4133 );
4134
4135 my $worksheet1 = $workbook->add_worksheet( 'Named colors' );
4136
4137 $worksheet1->set_column( 0, 3, 15 );
4138
4139 $worksheet1->write( 0, 0, "Index", $heading );
4140 $worksheet1->write( 0, 1, "Index", $heading );
4141 $worksheet1->write( 0, 2, "Name", $heading );
4142 $worksheet1->write( 0, 3, "Color", $heading );
4143
4144 my $i = 1;
4145
4146 while ( my ( $index, $color ) = each %colors ) {
4147 my $format = $workbook->add_format(
4148 fg_color => $color,
4149 pattern => 1,
4150 border => 1
4151 );
4152
4153 $worksheet1->write( $i + 1, 0, $index, $center );
4154 $worksheet1->write( $i + 1, 1, sprintf( "0x%02X", $index ), $center );
4155 $worksheet1->write( $i + 1, 2, $color, $center );
4156 $worksheet1->write( $i + 1, 3, '', $format );
4157 $i++;
4158 }
4159
4160
4161 ######################################################################
4162 #
4163 # Demonstrate the standard Excel colors in the range 8..63.
4164 #
4165
4166 my $worksheet2 = $workbook->add_worksheet( 'Standard colors' );
4167
4168 $worksheet2->set_column( 0, 3, 15 );
4169
4170 $worksheet2->write( 0, 0, "Index", $heading );
4171 $worksheet2->write( 0, 1, "Index", $heading );
4172 $worksheet2->write( 0, 2, "Color", $heading );
4173 $worksheet2->write( 0, 3, "Name", $heading );
4174
4175 for my $i ( 8 .. 63 ) {
4176 my $format = $workbook->add_format(
4177 fg_color => $i,
4178 pattern => 1,
4179 border => 1
4180 );
4181
4182 $worksheet2->write( ( $i - 7 ), 0, $i, $center );
4183 $worksheet2->write( ( $i - 7 ), 1, sprintf( "0x%02X", $i ), $center );
4184 $worksheet2->write( ( $i - 7 ), 2, '', $format );
4185
4186 # Add the color names
4187 if ( exists $colors{$i} ) {
4188 $worksheet2->write( ( $i - 7 ), 3, $colors{$i}, $center );
4189
4190 }
4191 }
4192
4193
4194 ######################################################################
4195 #
4196 # Demonstrate the Html colors.
4197 #
4198
4199
4200
4201 %colors = (
4202 '#000000', 'black',
4203 '#0000FF', 'blue',
4204 '#800000', 'brown',
4205 '#00FFFF', 'cyan',
4206 '#808080', 'gray',
4207 '#008000', 'green',
4208 '#00FF00', 'lime',
4209 '#FF00FF', 'magenta',
4210 '#000080', 'navy',
4211 '#FF6600', 'orange',
4212 '#FF00FF', 'pink',
4213 '#800080', 'purple',
4214 '#FF0000', 'red',
4215 '#C0C0C0', 'silver',
4216 '#FFFFFF', 'white',
4217 '#FFFF00', 'yellow',
4218 );
4219
4220 my $worksheet3 = $workbook->add_worksheet( 'Html colors' );
4221
4222 $worksheet3->set_column( 0, 3, 15 );
4223
4224 $worksheet3->write( 0, 0, "Html", $heading );
4225 $worksheet3->write( 0, 1, "Name", $heading );
4226 $worksheet3->write( 0, 2, "Color", $heading );
4227
4228 $i = 1;
4229
4230 while ( my ( $html_color, $color ) = each %colors ) {
4231 my $format = $workbook->add_format(
4232 fg_color => $html_color,
4233 pattern => 1,
4234 border => 1
4235 );
4236
4237 $worksheet3->write( $i + 1, 1, $html_color, $center );
4238 $worksheet3->write( $i + 1, 2, $color, $center );
4239 $worksheet3->write( $i + 1, 3, '', $format );
4240 $i++;
4241 }
4242
4243 $workbook->close();
4244
4245 __END__
4246
4247 Download this example:
4248 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/colors.pl>
4249
4250 Example: comments1.pl
4251 This example demonstrates writing cell comments.
4252
4253 A cell comment is indicated in Excel by a small red triangle in the
4254 upper right-hand corner of the cell.
4255
4256 For more advanced comment options see comments2.pl.
4257
4258 Source code for this example:
4259
4260 #!/usr/bin/perl
4261
4262 ###############################################################################
4263 #
4264 # This example demonstrates writing cell comments.
4265 #
4266 # A cell comment is indicated in Excel by a small red triangle in the upper
4267 # right-hand corner of the cell.
4268 #
4269 # For more advanced comment options see comments2.pl.
4270 #
4271 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
4272 #
4273
4274 use strict;
4275 use warnings;
4276 use Excel::Writer::XLSX;
4277
4278 my $workbook = Excel::Writer::XLSX->new( 'comments1.xlsx' );
4279 my $worksheet = $workbook->add_worksheet();
4280
4281
4282 $worksheet->write( 'A1', 'Hello' );
4283 $worksheet->write_comment( 'A1', 'This is a comment' );
4284
4285 $workbook->close();
4286
4287 __END__
4288
4289 Download this example:
4290 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/comments1.pl>
4291
4292 Example: comments2.pl
4293 This example demonstrates writing cell comments.
4294
4295 A cell comment is indicated in Excel by a small red triangle in the
4296 upper right-hand corner of the cell.
4297
4298 Each of the worksheets demonstrates different features of cell
4299 comments.
4300
4301 Source code for this example:
4302
4303 #!/usr/bin/perl
4304
4305 ###############################################################################
4306 #
4307 # This example demonstrates writing cell comments.
4308 #
4309 # A cell comment is indicated in Excel by a small red triangle in the upper
4310 # right-hand corner of the cell.
4311 #
4312 # Each of the worksheets demonstrates different features of cell comments.
4313 #
4314 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
4315 #
4316
4317 use strict;
4318 use warnings;
4319 use Excel::Writer::XLSX;
4320
4321 my $workbook = Excel::Writer::XLSX->new( 'comments2.xlsx' );
4322 my $text_wrap = $workbook->add_format( text_wrap => 1, valign => 'top' );
4323 my $worksheet1 = $workbook->add_worksheet();
4324 my $worksheet2 = $workbook->add_worksheet();
4325 my $worksheet3 = $workbook->add_worksheet();
4326 my $worksheet4 = $workbook->add_worksheet();
4327 my $worksheet5 = $workbook->add_worksheet();
4328 my $worksheet6 = $workbook->add_worksheet();
4329 my $worksheet7 = $workbook->add_worksheet();
4330 my $worksheet8 = $workbook->add_worksheet();
4331
4332
4333 # Variables that we will use in each example.
4334 my $cell_text = '';
4335 my $comment = '';
4336
4337
4338 ###############################################################################
4339 #
4340 # Example 1. Demonstrates a simple cell comments without formatting.
4341 # comments.
4342 #
4343
4344 # Set up some formatting.
4345 $worksheet1->set_column( 'C:C', 25 );
4346 $worksheet1->set_row( 2, 50 );
4347 $worksheet1->set_row( 5, 50 );
4348
4349
4350 # Simple ascii string.
4351 $cell_text = 'Hold the mouse over this cell to see the comment.';
4352
4353 $comment = 'This is a comment.';
4354
4355 $worksheet1->write( 'C3', $cell_text, $text_wrap );
4356 $worksheet1->write_comment( 'C3', $comment );
4357
4358 $cell_text = 'This is a UTF-8 string.';
4359 $comment = chr 0x263a;
4360
4361 $worksheet1->write( 'C6', $cell_text, $text_wrap );
4362 $worksheet1->write_comment( 'C6', $comment );
4363
4364
4365
4366 ###############################################################################
4367 #
4368 # Example 2. Demonstrates visible and hidden comments.
4369 #
4370
4371 # Set up some formatting.
4372 $worksheet2->set_column( 'C:C', 25 );
4373 $worksheet2->set_row( 2, 50 );
4374 $worksheet2->set_row( 5, 50 );
4375
4376
4377 $cell_text = 'This cell comment is visible.';
4378
4379 $comment = 'Hello.';
4380
4381 $worksheet2->write( 'C3', $cell_text, $text_wrap );
4382 $worksheet2->write_comment( 'C3', $comment, visible => 1 );
4383
4384
4385 $cell_text = "This cell comment isn't visible (the default).";
4386
4387 $comment = 'Hello.';
4388
4389 $worksheet2->write( 'C6', $cell_text, $text_wrap );
4390 $worksheet2->write_comment( 'C6', $comment );
4391
4392
4393 ###############################################################################
4394 #
4395 # Example 3. Demonstrates visible and hidden comments set at the worksheet
4396 # level.
4397 #
4398
4399 # Set up some formatting.
4400 $worksheet3->set_column( 'C:C', 25 );
4401 $worksheet3->set_row( 2, 50 );
4402 $worksheet3->set_row( 5, 50 );
4403 $worksheet3->set_row( 8, 50 );
4404
4405 # Make all comments on the worksheet visible.
4406 $worksheet3->show_comments();
4407
4408 $cell_text = 'This cell comment is visible, explicitly.';
4409
4410 $comment = 'Hello.';
4411
4412 $worksheet3->write( 'C3', $cell_text, $text_wrap );
4413 $worksheet3->write_comment( 'C3', $comment, visible => 1 );
4414
4415
4416 $cell_text =
4417 'This cell comment is also visible because ' . 'we used show_comments().';
4418
4419 $comment = 'Hello.';
4420
4421 $worksheet3->write( 'C6', $cell_text, $text_wrap );
4422 $worksheet3->write_comment( 'C6', $comment );
4423
4424
4425 $cell_text = 'However, we can still override it locally.';
4426
4427 $comment = 'Hello.';
4428
4429 $worksheet3->write( 'C9', $cell_text, $text_wrap );
4430 $worksheet3->write_comment( 'C9', $comment, visible => 0 );
4431
4432
4433 ###############################################################################
4434 #
4435 # Example 4. Demonstrates changes to the comment box dimensions.
4436 #
4437
4438 # Set up some formatting.
4439 $worksheet4->set_column( 'C:C', 25 );
4440 $worksheet4->set_row( 2, 50 );
4441 $worksheet4->set_row( 5, 50 );
4442 $worksheet4->set_row( 8, 50 );
4443 $worksheet4->set_row( 15, 50 );
4444
4445 $worksheet4->show_comments();
4446
4447 $cell_text = 'This cell comment is default size.';
4448
4449 $comment = 'Hello.';
4450
4451 $worksheet4->write( 'C3', $cell_text, $text_wrap );
4452 $worksheet4->write_comment( 'C3', $comment );
4453
4454
4455 $cell_text = 'This cell comment is twice as wide.';
4456
4457 $comment = 'Hello.';
4458
4459 $worksheet4->write( 'C6', $cell_text, $text_wrap );
4460 $worksheet4->write_comment( 'C6', $comment, x_scale => 2 );
4461
4462
4463 $cell_text = 'This cell comment is twice as high.';
4464
4465 $comment = 'Hello.';
4466
4467 $worksheet4->write( 'C9', $cell_text, $text_wrap );
4468 $worksheet4->write_comment( 'C9', $comment, y_scale => 2 );
4469
4470
4471 $cell_text = 'This cell comment is scaled in both directions.';
4472
4473 $comment = 'Hello.';
4474
4475 $worksheet4->write( 'C16', $cell_text, $text_wrap );
4476 $worksheet4->write_comment( 'C16', $comment, x_scale => 1.2, y_scale => 0.8 );
4477
4478
4479 $cell_text = 'This cell comment has width and height specified in pixels.';
4480
4481 $comment = 'Hello.';
4482
4483 $worksheet4->write( 'C19', $cell_text, $text_wrap );
4484 $worksheet4->write_comment( 'C19', $comment, width => 200, height => 20 );
4485
4486
4487 ###############################################################################
4488 #
4489 # Example 5. Demonstrates changes to the cell comment position.
4490 #
4491
4492 $worksheet5->set_column( 'C:C', 25 );
4493 $worksheet5->set_row( 2, 50 );
4494 $worksheet5->set_row( 5, 50 );
4495 $worksheet5->set_row( 8, 50 );
4496 $worksheet5->set_row( 11, 50 );
4497
4498 $worksheet5->show_comments();
4499
4500 $cell_text = 'This cell comment is in the default position.';
4501
4502 $comment = 'Hello.';
4503
4504 $worksheet5->write( 'C3', $cell_text, $text_wrap );
4505 $worksheet5->write_comment( 'C3', $comment );
4506
4507
4508 $cell_text = 'This cell comment has been moved to another cell.';
4509
4510 $comment = 'Hello.';
4511
4512 $worksheet5->write( 'C6', $cell_text, $text_wrap );
4513 $worksheet5->write_comment( 'C6', $comment, start_cell => 'E4' );
4514
4515
4516 $cell_text = 'This cell comment has been moved to another cell.';
4517
4518 $comment = 'Hello.';
4519
4520 $worksheet5->write( 'C9', $cell_text, $text_wrap );
4521 $worksheet5->write_comment( 'C9', $comment, start_row => 8, start_col => 4 );
4522
4523
4524 $cell_text = 'This cell comment has been shifted within its default cell.';
4525
4526 $comment = 'Hello.';
4527
4528 $worksheet5->write( 'C12', $cell_text, $text_wrap );
4529 $worksheet5->write_comment( 'C12', $comment, x_offset => 30, y_offset => 12 );
4530
4531
4532 ###############################################################################
4533 #
4534 # Example 6. Demonstrates changes to the comment background colour.
4535 #
4536
4537 $worksheet6->set_column( 'C:C', 25 );
4538 $worksheet6->set_row( 2, 50 );
4539 $worksheet6->set_row( 5, 50 );
4540 $worksheet6->set_row( 8, 50 );
4541
4542 $worksheet6->show_comments();
4543
4544 $cell_text = 'This cell comment has a different colour.';
4545
4546 $comment = 'Hello.';
4547
4548 $worksheet6->write( 'C3', $cell_text, $text_wrap );
4549 $worksheet6->write_comment( 'C3', $comment, color => 'green' );
4550
4551
4552 $cell_text = 'This cell comment has the default colour.';
4553
4554 $comment = 'Hello.';
4555
4556 $worksheet6->write( 'C6', $cell_text, $text_wrap );
4557 $worksheet6->write_comment( 'C6', $comment );
4558
4559
4560 $cell_text = 'This cell comment has a different colour.';
4561
4562 $comment = 'Hello.';
4563
4564 $worksheet6->write( 'C9', $cell_text, $text_wrap );
4565 $worksheet6->write_comment( 'C9', $comment, color => '#FF6600' );
4566
4567
4568 ###############################################################################
4569 #
4570 # Example 7. Demonstrates how to set the cell comment author.
4571 #
4572
4573 $worksheet7->set_column( 'C:C', 30 );
4574 $worksheet7->set_row( 2, 50 );
4575 $worksheet7->set_row( 5, 50 );
4576 $worksheet7->set_row( 8, 50 );
4577
4578 my $author = '';
4579 my $cell = 'C3';
4580
4581 $cell_text = "Move the mouse over this cell and you will see 'Cell commented "
4582 . "by $author' (blank) in the status bar at the bottom";
4583
4584 $comment = 'Hello.';
4585
4586 $worksheet7->write( $cell, $cell_text, $text_wrap );
4587 $worksheet7->write_comment( $cell, $comment );
4588
4589
4590 $author = 'Perl';
4591 $cell = 'C6';
4592 $cell_text = "Move the mouse over this cell and you will see 'Cell commented "
4593 . "by $author' in the status bar at the bottom";
4594
4595 $comment = 'Hello.';
4596
4597 $worksheet7->write( $cell, $cell_text, $text_wrap );
4598 $worksheet7->write_comment( $cell, $comment, author => $author );
4599
4600
4601 $author = chr 0x20AC;
4602 $cell = 'C9';
4603 $cell_text = "Move the mouse over this cell and you will see 'Cell commented "
4604 . "by $author' in the status bar at the bottom";
4605 $comment = 'Hello.';
4606
4607 $worksheet7->write( $cell, $cell_text, $text_wrap );
4608 $worksheet7->write_comment( $cell, $comment, author => $author );
4609
4610
4611
4612
4613 ###############################################################################
4614 #
4615 # Example 8. Demonstrates the need to explicitly set the row height.
4616 #
4617
4618 # Set up some formatting.
4619 $worksheet8->set_column( 'C:C', 25 );
4620 $worksheet8->set_row( 2, 80 );
4621
4622 $worksheet8->show_comments();
4623
4624
4625 $cell_text =
4626 'The height of this row has been adjusted explicitly using '
4627 . 'set_row(). The size of the comment box is adjusted '
4628 . 'accordingly by Excel::Writer::XLSX.';
4629
4630 $comment = 'Hello.';
4631
4632 $worksheet8->write( 'C3', $cell_text, $text_wrap );
4633 $worksheet8->write_comment( 'C3', $comment );
4634
4635
4636 $cell_text =
4637 'The height of this row has been adjusted by Excel due to the '
4638 . 'text wrap property being set. Unfortunately this means that '
4639 . 'the height of the row is unknown to Excel::Writer::XLSX at '
4640 . "run time and thus the comment box is stretched as well.\n\n"
4641 . 'Use set_row() to specify the row height explicitly to avoid '
4642 . 'this problem.';
4643
4644 $comment = 'Hello.';
4645
4646 $worksheet8->write( 'C6', $cell_text, $text_wrap );
4647 $worksheet8->write_comment( 'C6', $comment );
4648
4649 $workbook->close();
4650
4651 __END__
4652
4653 Download this example:
4654 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/comments2.pl>
4655
4656 Example: conditional_format.pl
4657 Example of how to add conditional formatting to an Excel::Writer::XLSX
4658 file.
4659
4660 Conditional formatting allows you to apply a format to a cell or a
4661 range of cells based on certain criteria.
4662
4663 Source code for this example:
4664
4665 #!/usr/bin/perl
4666
4667 ###############################################################################
4668 #
4669 # Example of how to add conditional formatting to an Excel::Writer::XLSX file.
4670 #
4671 # Conditional formatting allows you to apply a format to a cell or a range of
4672 # cells based on certain criteria.
4673 #
4674 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
4675 #
4676
4677 use strict;
4678 use warnings;
4679 use Excel::Writer::XLSX;
4680
4681 my $workbook = Excel::Writer::XLSX->new( 'conditional_format.xlsx' );
4682 my $worksheet1 = $workbook->add_worksheet();
4683 my $worksheet2 = $workbook->add_worksheet();
4684 my $worksheet3 = $workbook->add_worksheet();
4685 my $worksheet4 = $workbook->add_worksheet();
4686 my $worksheet5 = $workbook->add_worksheet();
4687 my $worksheet6 = $workbook->add_worksheet();
4688 my $worksheet7 = $workbook->add_worksheet();
4689 my $worksheet8 = $workbook->add_worksheet();
4690 my $worksheet9 = $workbook->add_worksheet();
4691
4692
4693 # Light red fill with dark red text.
4694 my $format1 = $workbook->add_format(
4695 bg_color => '#FFC7CE',
4696 color => '#9C0006',
4697
4698 );
4699
4700 # Green fill with dark green text.
4701 my $format2 = $workbook->add_format(
4702 bg_color => '#C6EFCE',
4703 color => '#006100',
4704
4705 );
4706
4707 # Some sample data to run the conditional formatting against.
4708 my $data = [
4709 [ 34, 72, 38, 30, 75, 48, 75, 66, 84, 86 ],
4710 [ 6, 24, 1, 84, 54, 62, 60, 3, 26, 59 ],
4711 [ 28, 79, 97, 13, 85, 93, 93, 22, 5, 14 ],
4712 [ 27, 71, 40, 17, 18, 79, 90, 93, 29, 47 ],
4713 [ 88, 25, 33, 23, 67, 1, 59, 79, 47, 36 ],
4714 [ 24, 100, 20, 88, 29, 33, 38, 54, 54, 88 ],
4715 [ 6, 57, 88, 28, 10, 26, 37, 7, 41, 48 ],
4716 [ 52, 78, 1, 96, 26, 45, 47, 33, 96, 36 ],
4717 [ 60, 54, 81, 66, 81, 90, 80, 93, 12, 55 ],
4718 [ 70, 5, 46, 14, 71, 19, 66, 36, 41, 21 ],
4719 ];
4720
4721
4722 ###############################################################################
4723 #
4724 # Example 1.
4725 #
4726 my $caption = 'Cells with values >= 50 are in light red. '
4727 . 'Values < 50 are in light green.';
4728
4729 # Write the data.
4730 $worksheet1->write( 'A1', $caption );
4731 $worksheet1->write_col( 'B3', $data );
4732
4733 # Write a conditional format over a range.
4734 $worksheet1->conditional_formatting( 'B3:K12',
4735 {
4736 type => 'cell',
4737 criteria => '>=',
4738 value => 50,
4739 format => $format1,
4740 }
4741 );
4742
4743 # Write another conditional format over the same range.
4744 $worksheet1->conditional_formatting( 'B3:K12',
4745 {
4746 type => 'cell',
4747 criteria => '<',
4748 value => 50,
4749 format => $format2,
4750 }
4751 );
4752
4753
4754 ###############################################################################
4755 #
4756 # Example 2.
4757 #
4758 $caption = 'Values between 30 and 70 are in light red. '
4759 . 'Values outside that range are in light green.';
4760
4761 $worksheet2->write( 'A1', $caption );
4762 $worksheet2->write_col( 'B3', $data );
4763
4764 $worksheet2->conditional_formatting( 'B3:K12',
4765 {
4766 type => 'cell',
4767 criteria => 'between',
4768 minimum => 30,
4769 maximum => 70,
4770 format => $format1,
4771 }
4772 );
4773
4774 $worksheet2->conditional_formatting( 'B3:K12',
4775 {
4776 type => 'cell',
4777 criteria => 'not between',
4778 minimum => 30,
4779 maximum => 70,
4780 format => $format2,
4781 }
4782 );
4783
4784
4785 ###############################################################################
4786 #
4787 # Example 3.
4788 #
4789 $caption = 'Duplicate values are in light red. '
4790 . 'Unique values are in light green.';
4791
4792 $worksheet3->write( 'A1', $caption );
4793 $worksheet3->write_col( 'B3', $data );
4794
4795 $worksheet3->conditional_formatting( 'B3:K12',
4796 {
4797 type => 'duplicate',
4798 format => $format1,
4799 }
4800 );
4801
4802 $worksheet3->conditional_formatting( 'B3:K12',
4803 {
4804 type => 'unique',
4805 format => $format2,
4806 }
4807 );
4808
4809
4810 ###############################################################################
4811 #
4812 # Example 4.
4813 #
4814 $caption = 'Above average values are in light red. '
4815 . 'Below average values are in light green.';
4816
4817 $worksheet4->write( 'A1', $caption );
4818 $worksheet4->write_col( 'B3', $data );
4819
4820 $worksheet4->conditional_formatting( 'B3:K12',
4821 {
4822 type => 'average',
4823 criteria => 'above',
4824 format => $format1,
4825 }
4826 );
4827
4828 $worksheet4->conditional_formatting( 'B3:K12',
4829 {
4830 type => 'average',
4831 criteria => 'below',
4832 format => $format2,
4833 }
4834 );
4835
4836
4837 ###############################################################################
4838 #
4839 # Example 5.
4840 #
4841 $caption = 'Top 10 values are in light red. '
4842 . 'Bottom 10 values are in light green.';
4843
4844 $worksheet5->write( 'A1', $caption );
4845 $worksheet5->write_col( 'B3', $data );
4846
4847 $worksheet5->conditional_formatting( 'B3:K12',
4848 {
4849 type => 'top',
4850 value => '10',
4851 format => $format1,
4852 }
4853 );
4854
4855 $worksheet5->conditional_formatting( 'B3:K12',
4856 {
4857 type => 'bottom',
4858 value => '10',
4859 format => $format2,
4860 }
4861 );
4862
4863
4864 ###############################################################################
4865 #
4866 # Example 6.
4867 #
4868 $caption = 'Cells with values >= 50 are in light red. '
4869 . 'Values < 50 are in light green. Non-contiguous ranges.';
4870
4871 # Write the data.
4872 $worksheet6->write( 'A1', $caption );
4873 $worksheet6->write_col( 'B3', $data );
4874
4875 # Write a conditional format over a range.
4876 $worksheet6->conditional_formatting( 'B3:K6,B9:K12',
4877 {
4878 type => 'cell',
4879 criteria => '>=',
4880 value => 50,
4881 format => $format1,
4882 }
4883 );
4884
4885 # Write another conditional format over the same range.
4886 $worksheet6->conditional_formatting( 'B3:K6,B9:K12',
4887 {
4888 type => 'cell',
4889 criteria => '<',
4890 value => 50,
4891 format => $format2,
4892 }
4893 );
4894
4895
4896 ###############################################################################
4897 #
4898 # Example 7.
4899 #
4900 $caption = 'Examples of color scales with default and user colors.';
4901
4902 $data = [ 1 .. 12 ];
4903
4904 $worksheet7->write( 'A1', $caption );
4905
4906 $worksheet7->write ( 'B2', "2 Color Scale" );
4907 $worksheet7->write_col( 'B3', $data );
4908
4909 $worksheet7->write ( 'D2', "2 Color Scale + user colors" );
4910 $worksheet7->write_col( 'D3', $data );
4911
4912 $worksheet7->write ( 'G2', "3 Color Scale" );
4913 $worksheet7->write_col( 'G3', $data );
4914
4915 $worksheet7->write ( 'I2', "3 Color Scale + user colors" );
4916 $worksheet7->write_col( 'I3', $data );
4917
4918
4919 $worksheet7->conditional_formatting( 'B3:B14',
4920 {
4921 type => '2_color_scale',
4922 }
4923 );
4924
4925 $worksheet7->conditional_formatting( 'D3:D14',
4926 {
4927 type => '3_color_scale',
4928 }
4929 );
4930
4931 $worksheet7->conditional_formatting( 'G3:G14',
4932 {
4933 type => '2_color_scale',
4934 min_color => "#FF0000",
4935 max_color => "#00FF00",
4936
4937 }
4938 );
4939
4940 $worksheet7->conditional_formatting( 'I3:I14',
4941 {
4942 type => '3_color_scale',
4943 min_color => "#C5D9F1",
4944 mid_color => "#8DB4E3",
4945 max_color => "#538ED5",
4946 }
4947 );
4948
4949
4950 ###############################################################################
4951 #
4952 # Example 8.
4953 #
4954 $caption = 'Examples of data bars.';
4955
4956 $data = [ 1 .. 12 ];
4957
4958 $worksheet8->write( 'A1', $caption );
4959
4960 $worksheet8->write ( 'B2', "Default data bars" );
4961 $worksheet8->write_col( 'B3', $data );
4962
4963 $worksheet8->write ( 'D2', "Bars only" );
4964 $worksheet8->write_col( 'D3', $data );
4965
4966 $worksheet8->write ( 'F2', "With user color" );
4967 $worksheet8->write_col( 'F3', $data );
4968
4969 $worksheet8->write ( 'H2', "Solid bars" );
4970 $worksheet8->write_col( 'H3', $data );
4971
4972 $worksheet8->write ( 'J2', "Right to left" );
4973 $worksheet8->write_col( 'J3', $data );
4974
4975 $data = [-1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0];
4976
4977 $worksheet8->write ( 'L2', "Excel 2010 style" );
4978 $worksheet8->write_col( 'L3', $data );
4979
4980 $worksheet8->write ( 'N2', "Negative same as positive" );
4981 $worksheet8->write_col( 'N3', $data );
4982
4983
4984 $worksheet8->conditional_formatting( 'B3:B14',
4985 {
4986 type => 'data_bar'
4987 }
4988 );
4989
4990 $worksheet8->conditional_formatting( 'D3:D14',
4991 {
4992 type => 'data_bar',
4993 bar_only => 1
4994 }
4995 );
4996
4997 $worksheet8->conditional_formatting( 'F3:F14',
4998 {
4999 type => 'data_bar',
5000 bar_color => '#63C384'
5001 }
5002 );
5003
5004 $worksheet8->conditional_formatting( 'H3:H14',
5005 {
5006 type => 'data_bar',
5007 bar_solid => 1
5008 }
5009 );
5010
5011 $worksheet8->conditional_formatting( 'J3:J14',
5012 {
5013 type => 'data_bar',
5014 bar_direction => 'right'
5015 }
5016 );
5017
5018 $worksheet8->conditional_formatting( 'L3:L14',
5019 {
5020 type => 'data_bar',
5021 data_bar_2010 => 1
5022 }
5023 );
5024
5025 $worksheet8->conditional_formatting( 'N3:N14',
5026 {
5027 type => 'data_bar',
5028 bar_negative_color_same => 1,
5029 bar_negative_border_color_same => 1
5030 }
5031 );
5032
5033
5034 ###############################################################################
5035 #
5036 # Example 9.
5037 #
5038 $caption = 'Examples of conditional formats with icon sets.';
5039
5040 $data = [
5041 [ 1, 2, 3 ],
5042 [ 1, 2, 3 ],
5043 [ 1, 2, 3 ],
5044 [ 1, 2, 3 ],
5045 [ 1, 2, 3, 4 ],
5046 [ 1, 2, 3, 4, 5 ],
5047 [ 1, 2, 3, 4, 5 ],
5048 ];
5049
5050 $worksheet9->write( 'A1', $caption );
5051 $worksheet9->write_col( 'B3', $data );
5052
5053 $worksheet9->conditional_formatting( 'B3:D3',
5054 {
5055 type => 'icon_set',
5056 icon_style => '3_traffic_lights',
5057 }
5058 );
5059
5060 $worksheet9->conditional_formatting( 'B4:D4',
5061 {
5062 type => 'icon_set',
5063 icon_style => '3_traffic_lights',
5064 reverse_icons => 1,
5065 }
5066 );
5067
5068 $worksheet9->conditional_formatting( 'B5:D5',
5069 {
5070 type => 'icon_set',
5071 icon_style => '3_traffic_lights',
5072 icons_only => 1,
5073 }
5074 );
5075
5076 $worksheet9->conditional_formatting( 'B6:D6',
5077 {
5078 type => 'icon_set',
5079 icon_style => '3_arrows',
5080 }
5081 );
5082
5083 $worksheet9->conditional_formatting( 'B7:E8',
5084 {
5085 type => 'icon_set',
5086 icon_style => '4_arrows',
5087 }
5088 );
5089
5090 $worksheet9->conditional_formatting( 'B8:F8',
5091 {
5092 type => 'icon_set',
5093 icon_style => '5_arrows',
5094 }
5095 );
5096
5097
5098 $worksheet9->conditional_formatting( 'B9:F9',
5099 {
5100 type => 'icon_set',
5101 icon_style => '5_ratings',
5102 }
5103 );
5104
5105 $workbook->close();
5106
5107 __END__
5108
5109 Download this example:
5110 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/conditional_format.pl>
5111
5112 Example: data_validate.pl
5113 Example of how to add data validation and dropdown lists to an
5114 Excel::Writer::XLSX file.
5115
5116 Data validation is a feature of Excel which allows you to restrict the
5117 data that a user enters in a cell and to display help and warning
5118 messages. It also allows you to restrict input to values in a drop down
5119 list.
5120
5121 Source code for this example:
5122
5123 #!/usr/bin/perl
5124
5125 ###############################################################################
5126 #
5127 # Example of how to add data validation and dropdown lists to an
5128 # Excel::Writer::XLSX file.
5129 #
5130 # Data validation is a feature of Excel which allows you to restrict the data
5131 # that a user enters in a cell and to display help and warning messages. It
5132 # also allows you to restrict input to values in a drop down list.
5133 #
5134 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
5135 #
5136
5137 use strict;
5138 use warnings;
5139 use Excel::Writer::XLSX;
5140
5141 my $workbook = Excel::Writer::XLSX->new( 'data_validate.xlsx' );
5142 my $worksheet = $workbook->add_worksheet();
5143
5144 # Add a format for the header cells.
5145 my $header_format = $workbook->add_format(
5146 border => 1,
5147 bg_color => '#C6EFCE',
5148 bold => 1,
5149 text_wrap => 1,
5150 valign => 'vcenter',
5151 indent => 1,
5152 );
5153
5154 # Set up layout of the worksheet.
5155 $worksheet->set_column( 'A:A', 68 );
5156 $worksheet->set_column( 'B:B', 15 );
5157 $worksheet->set_column( 'D:D', 15 );
5158 $worksheet->set_row( 0, 36 );
5159 $worksheet->set_selection( 'B3' );
5160
5161
5162 # Write the header cells and some data that will be used in the examples.
5163 my $row = 0;
5164 my $txt;
5165 my $heading1 = 'Some examples of data validation in Excel::Writer::XLSX';
5166 my $heading2 = 'Enter values in this column';
5167 my $heading3 = 'Sample Data';
5168
5169 $worksheet->write( 'A1', $heading1, $header_format );
5170 $worksheet->write( 'B1', $heading2, $header_format );
5171 $worksheet->write( 'D1', $heading3, $header_format );
5172
5173 $worksheet->write( 'D3', [ 'Integers', 1, 10 ] );
5174 $worksheet->write( 'D4', [ 'List data', 'open', 'high', 'close' ] );
5175 $worksheet->write( 'D5', [ 'Formula', '=AND(F5=50,G5=60)', 50, 60 ] );
5176
5177
5178 #
5179 # Example 1. Limiting input to an integer in a fixed range.
5180 #
5181 $txt = 'Enter an integer between 1 and 10';
5182 $row += 2;
5183
5184 $worksheet->write( $row, 0, $txt );
5185 $worksheet->data_validation(
5186 $row, 1,
5187 {
5188 validate => 'integer',
5189 criteria => 'between',
5190 minimum => 1,
5191 maximum => 10,
5192 }
5193 );
5194
5195
5196 #
5197 # Example 2. Limiting input to an integer outside a fixed range.
5198 #
5199 $txt = 'Enter an integer that is not between 1 and 10 (using cell references)';
5200 $row += 2;
5201
5202 $worksheet->write( $row, 0, $txt );
5203 $worksheet->data_validation(
5204 $row, 1,
5205 {
5206 validate => 'integer',
5207 criteria => 'not between',
5208 minimum => '=E3',
5209 maximum => '=F3',
5210 }
5211 );
5212
5213
5214 #
5215 # Example 3. Limiting input to an integer greater than a fixed value.
5216 #
5217 $txt = 'Enter an integer greater than 0';
5218 $row += 2;
5219
5220 $worksheet->write( $row, 0, $txt );
5221 $worksheet->data_validation(
5222 $row, 1,
5223 {
5224 validate => 'integer',
5225 criteria => '>',
5226 value => 0,
5227 }
5228 );
5229
5230
5231 #
5232 # Example 4. Limiting input to an integer less than a fixed value.
5233 #
5234 $txt = 'Enter an integer less than 10';
5235 $row += 2;
5236
5237 $worksheet->write( $row, 0, $txt );
5238 $worksheet->data_validation(
5239 $row, 1,
5240 {
5241 validate => 'integer',
5242 criteria => '<',
5243 value => 10,
5244 }
5245 );
5246
5247
5248 #
5249 # Example 5. Limiting input to a decimal in a fixed range.
5250 #
5251 $txt = 'Enter a decimal between 0.1 and 0.5';
5252 $row += 2;
5253
5254 $worksheet->write( $row, 0, $txt );
5255 $worksheet->data_validation(
5256 $row, 1,
5257 {
5258 validate => 'decimal',
5259 criteria => 'between',
5260 minimum => 0.1,
5261 maximum => 0.5,
5262 }
5263 );
5264
5265
5266 #
5267 # Example 6. Limiting input to a value in a dropdown list.
5268 #
5269 $txt = 'Select a value from a drop down list';
5270 $row += 2;
5271
5272 $worksheet->write( $row, 0, $txt );
5273 $worksheet->data_validation(
5274 $row, 1,
5275 {
5276 validate => 'list',
5277 source => [ 'open', 'high', 'close' ],
5278 }
5279 );
5280
5281
5282 #
5283 # Example 6. Limiting input to a value in a dropdown list.
5284 #
5285 $txt = 'Select a value from a drop down list (using a cell range)';
5286 $row += 2;
5287
5288 $worksheet->write( $row, 0, $txt );
5289 $worksheet->data_validation(
5290 $row, 1,
5291 {
5292 validate => 'list',
5293 source => '=$E$4:$G$4',
5294 }
5295 );
5296
5297
5298 #
5299 # Example 7. Limiting input to a date in a fixed range.
5300 #
5301 $txt = 'Enter a date between 1/1/2008 and 12/12/2008';
5302 $row += 2;
5303
5304 $worksheet->write( $row, 0, $txt );
5305 $worksheet->data_validation(
5306 $row, 1,
5307 {
5308 validate => 'date',
5309 criteria => 'between',
5310 minimum => '2008-01-01T',
5311 maximum => '2008-12-12T',
5312 }
5313 );
5314
5315
5316 #
5317 # Example 8. Limiting input to a time in a fixed range.
5318 #
5319 $txt = 'Enter a time between 6:00 and 12:00';
5320 $row += 2;
5321
5322 $worksheet->write( $row, 0, $txt );
5323 $worksheet->data_validation(
5324 $row, 1,
5325 {
5326 validate => 'time',
5327 criteria => 'between',
5328 minimum => 'T06:00',
5329 maximum => 'T12:00',
5330 }
5331 );
5332
5333
5334 #
5335 # Example 9. Limiting input to a string greater than a fixed length.
5336 #
5337 $txt = 'Enter a string longer than 3 characters';
5338 $row += 2;
5339
5340 $worksheet->write( $row, 0, $txt );
5341 $worksheet->data_validation(
5342 $row, 1,
5343 {
5344 validate => 'length',
5345 criteria => '>',
5346 value => 3,
5347 }
5348 );
5349
5350
5351 #
5352 # Example 10. Limiting input based on a formula.
5353 #
5354 $txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"';
5355 $row += 2;
5356
5357 $worksheet->write( $row, 0, $txt );
5358 $worksheet->data_validation(
5359 $row, 1,
5360 {
5361 validate => 'custom',
5362 value => '=AND(F5=50,G5=60)',
5363 }
5364 );
5365
5366
5367 #
5368 # Example 11. Displaying and modify data validation messages.
5369 #
5370 $txt = 'Displays a message when you select the cell';
5371 $row += 2;
5372
5373 $worksheet->write( $row, 0, $txt );
5374 $worksheet->data_validation(
5375 $row, 1,
5376 {
5377 validate => 'integer',
5378 criteria => 'between',
5379 minimum => 1,
5380 maximum => 100,
5381 input_title => 'Enter an integer:',
5382 input_message => 'between 1 and 100',
5383 }
5384 );
5385
5386
5387 #
5388 # Example 12. Displaying and modify data validation messages.
5389 #
5390 $txt = 'Display a custom error message when integer isn\'t between 1 and 100';
5391 $row += 2;
5392
5393 $worksheet->write( $row, 0, $txt );
5394 $worksheet->data_validation(
5395 $row, 1,
5396 {
5397 validate => 'integer',
5398 criteria => 'between',
5399 minimum => 1,
5400 maximum => 100,
5401 input_title => 'Enter an integer:',
5402 input_message => 'between 1 and 100',
5403 error_title => 'Input value is not valid!',
5404 error_message => 'It should be an integer between 1 and 100',
5405 }
5406 );
5407
5408
5409 #
5410 # Example 13. Displaying and modify data validation messages.
5411 #
5412 $txt =
5413 'Display a custom information message when integer isn\'t between 1 and 100';
5414 $row += 2;
5415
5416 $worksheet->write( $row, 0, $txt );
5417 $worksheet->data_validation(
5418 $row, 1,
5419 {
5420 validate => 'integer',
5421 criteria => 'between',
5422 minimum => 1,
5423 maximum => 100,
5424 input_title => 'Enter an integer:',
5425 input_message => 'between 1 and 100',
5426 error_title => 'Input value is not valid!',
5427 error_message => 'It should be an integer between 1 and 100',
5428 error_type => 'information',
5429 }
5430 );
5431
5432 $workbook->close();
5433
5434 __END__
5435
5436 Download this example:
5437 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/data_validate.pl>
5438
5439 Example: date_time.pl
5440 Excel::Writer::XLSX example of writing dates and times using the
5441 write_date_time() Worksheet method.
5442
5443 Source code for this example:
5444
5445 #!/usr/bin/perl
5446
5447 ###############################################################################
5448 #
5449 # Excel::Writer::XLSX example of writing dates and times using the
5450 # write_date_time() Worksheet method.
5451 #
5452 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
5453 #
5454
5455 use strict;
5456 use warnings;
5457 use Excel::Writer::XLSX;
5458
5459
5460 # Create a new workbook and add a worksheet
5461 my $workbook = Excel::Writer::XLSX->new( 'date_time.xlsx' );
5462 my $worksheet = $workbook->add_worksheet();
5463 my $bold = $workbook->add_format( bold => 1 );
5464
5465
5466 # Expand the first columns so that the date is visible.
5467 $worksheet->set_column( "A:B", 30 );
5468
5469
5470 # Write the column headers
5471 $worksheet->write( 'A1', 'Formatted date', $bold );
5472 $worksheet->write( 'B1', 'Format', $bold );
5473
5474
5475 # Examples date and time formats. In the output file compare how changing
5476 # the format codes change the appearance of the date.
5477 #
5478 my @date_formats = (
5479 'dd/mm/yy',
5480 'mm/dd/yy',
5481 '',
5482 'd mm yy',
5483 'dd mm yy',
5484 '',
5485 'dd m yy',
5486 'dd mm yy',
5487 'dd mmm yy',
5488 'dd mmmm yy',
5489 '',
5490 'dd mm y',
5491 'dd mm yyy',
5492 'dd mm yyyy',
5493 '',
5494 'd mmmm yyyy',
5495 '',
5496 'dd/mm/yy',
5497 'dd/mm/yy hh:mm',
5498 'dd/mm/yy hh:mm:ss',
5499 'dd/mm/yy hh:mm:ss.000',
5500 '',
5501 'hh:mm',
5502 'hh:mm:ss',
5503 'hh:mm:ss.000',
5504 );
5505
5506
5507 # Write the same date and time using each of the above formats. The empty
5508 # string formats create a blank line to make the example clearer.
5509 #
5510 my $row = 0;
5511 for my $date_format ( @date_formats ) {
5512 $row++;
5513 next if $date_format eq '';
5514
5515 # Create a format for the date or time.
5516 my $format = $workbook->add_format(
5517 num_format => $date_format,
5518 align => 'left'
5519 );
5520
5521 # Write the same date using different formats.
5522 $worksheet->write_date_time( $row, 0, '2004-08-01T12:30:45.123', $format );
5523 $worksheet->write( $row, 1, $date_format );
5524 }
5525
5526
5527 # The following is an example of an invalid date. It is written as a string
5528 # instead of a number. This is also Excel's default behaviour.
5529 #
5530 $row += 2;
5531 $worksheet->write_date_time( $row, 0, '2004-13-01T12:30:45.123' );
5532 $worksheet->write( $row, 1, 'Invalid date. Written as string.', $bold );
5533
5534 $workbook->close();
5535
5536 __END__
5537
5538 Download this example:
5539 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/date_time.pl>
5540
5541 Example: defined_name.pl
5542 Example of how to create defined names in an Excel::Writer::XLSX file.
5543
5544 This method is used to define a user friendly name to represent a
5545 value, a single cell or a range of cells in a workbook.
5546
5547 Source code for this example:
5548
5549 #!/usr/bin/perl
5550
5551 ##############################################################################
5552 #
5553 # Example of how to create defined names in an Excel::Writer::XLSX file.
5554 #
5555 # This method is used to define a user friendly name to represent a value,
5556 # a single cell or a range of cells in a workbook.
5557 #
5558 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
5559 #
5560
5561 use strict;
5562 use warnings;
5563 use Excel::Writer::XLSX;
5564
5565 my $workbook = Excel::Writer::XLSX->new( 'defined_name.xlsx' );
5566 my $worksheet1 = $workbook->add_worksheet();
5567 my $worksheet2 = $workbook->add_worksheet();
5568
5569 # Define some global/workbook names.
5570 $workbook->define_name( 'Exchange_rate', '=0.96' );
5571 $workbook->define_name( 'Sales', '=Sheet1!$G$1:$H$10' );
5572
5573 # Define a local/worksheet name.
5574 $workbook->define_name( 'Sheet2!Sales', '=Sheet2!$G$1:$G$10' );
5575
5576 # Write some text in the file and one of the defined names in a formula.
5577 for my $worksheet ( $workbook->sheets() ) {
5578 $worksheet->set_column( 'A:A', 45 );
5579 $worksheet->write( 'A1', 'This worksheet contains some defined names.' );
5580 $worksheet->write( 'A2', 'See Formulas -> Name Manager above.' );
5581 $worksheet->write( 'A3', 'Example formula in cell B3 ->' );
5582
5583 $worksheet->write( 'B3', '=Exchange_rate' );
5584 }
5585
5586 $workbook->close();
5587
5588 __END__
5589
5590 Download this example:
5591 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/defined_name.pl>
5592
5593 Example: diag_border.pl
5594 A simple formatting example that demonstrates how to add a diagonal
5595 cell border with Excel::Writer::XLSX
5596
5597 Source code for this example:
5598
5599 #!/usr/bin/perl -w
5600
5601 ##############################################################################
5602 #
5603 # A simple formatting example that demonstrates how to add a diagonal cell
5604 # border with Excel::Writer::XLSX
5605 #
5606 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
5607 #
5608
5609 use strict;
5610 use Excel::Writer::XLSX;
5611
5612
5613 my $workbook = Excel::Writer::XLSX->new( 'diag_border.xlsx' );
5614 my $worksheet = $workbook->add_worksheet();
5615
5616
5617 my $format1 = $workbook->add_format( diag_type => 1 );
5618
5619 my $format2 = $workbook->add_format( diag_type => 2 );
5620
5621 my $format3 = $workbook->add_format( diag_type => 3 );
5622
5623 my $format4 = $workbook->add_format(
5624 diag_type => 3,
5625 diag_border => 7,
5626 diag_color => 'red',
5627 );
5628
5629
5630 $worksheet->write( 'B3', 'Text', $format1 );
5631 $worksheet->write( 'B6', 'Text', $format2 );
5632 $worksheet->write( 'B9', 'Text', $format3 );
5633 $worksheet->write( 'B12', 'Text', $format4 );
5634
5635 $workbook->close();
5636
5637 __END__
5638
5639 Download this example:
5640 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/diag_border.pl>
5641
5642 Example: filehandle.pl
5643 Example of using Excel::Writer::XLSX to write Excel files to different
5644 filehandles.
5645
5646 Source code for this example:
5647
5648 #!/usr/bin/perl
5649
5650 ###############################################################################
5651 #
5652 # Example of using Excel::Writer::XLSX to write Excel files to different
5653 # filehandles.
5654 #
5655 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
5656 #
5657
5658 use strict;
5659 use warnings;
5660 use Excel::Writer::XLSX;
5661 use IO::Scalar;
5662
5663
5664 ###############################################################################
5665 #
5666 # Example 1. This demonstrates the standard way of creating an Excel file by
5667 # specifying a file name.
5668 #
5669
5670 my $workbook1 = Excel::Writer::XLSX->new( 'fh_01.xlsx' );
5671 my $worksheet1 = $workbook1->add_worksheet();
5672
5673 $worksheet1->write( 0, 0, 'Hi Excel 1' );
5674
5675 $workbook1->close();
5676
5677
5678 ###############################################################################
5679 #
5680 # Example 2. Write an Excel file to an existing filehandle.
5681 #
5682
5683 open TEST, '>', 'fh_02.xlsx' or die "Couldn't open file: $!";
5684 binmode TEST; # Always do this regardless of whether the platform requires it.
5685
5686 my $workbook2 = Excel::Writer::XLSX->new( \*TEST );
5687 my $worksheet2 = $workbook2->add_worksheet();
5688
5689 $worksheet2->write( 0, 0, 'Hi Excel 2' );
5690
5691 $workbook2->close();
5692
5693 ###############################################################################
5694 #
5695 # Example 3. Write an Excel file to an existing OO style filehandle.
5696 #
5697
5698 my $fh = FileHandle->new( '> fh_03.xlsx' ) or die "Couldn't open file: $!";
5699
5700 binmode( $fh );
5701
5702 my $workbook3 = Excel::Writer::XLSX->new( $fh );
5703 my $worksheet3 = $workbook3->add_worksheet();
5704
5705 $worksheet3->write( 0, 0, 'Hi Excel 3' );
5706
5707 $workbook3->close();
5708
5709
5710 ###############################################################################
5711 #
5712 # Example 4. Write an Excel file to a string via IO::Scalar. Please refer to
5713 # the IO::Scalar documentation for further details.
5714 #
5715
5716 my $xlsx_str;
5717
5718 tie *XLSX, 'IO::Scalar', \$xlsx_str;
5719
5720 my $workbook4 = Excel::Writer::XLSX->new( \*XLSX );
5721 my $worksheet4 = $workbook4->add_worksheet();
5722
5723 $worksheet4->write( 0, 0, 'Hi Excel 4' );
5724 $workbook4->close(); # This is required before we use the scalar
5725
5726
5727 # The Excel file is now in $xlsx_str. As a demonstration, print it to a file.
5728 open TMP, '>', 'fh_04.xlsx' or die "Couldn't open file: $!";
5729 binmode TMP;
5730 print TMP $xlsx_str;
5731 close TMP;
5732
5733
5734 ###############################################################################
5735 #
5736 # Example 5. Write an Excel file to a string via IO::Scalar's newer interface.
5737 # Please refer to the IO::Scalar documentation for further details.
5738 #
5739 my $xlsx_str2;
5740
5741 my $fh5 = IO::Scalar->new( \$xlsx_str2 );
5742
5743 my $workbook5 = Excel::Writer::XLSX->new( $fh5 );
5744 my $worksheet5 = $workbook5->add_worksheet();
5745
5746 $worksheet5->write( 0, 0, 'Hi Excel 5' );
5747 $workbook5->close(); # This is required before we use the scalar
5748
5749 # The Excel file is now in $xlsx_str. As a demonstration, print it to a file.
5750 open TMP, '>', 'fh_05.xlsx' or die "Couldn't open file: $!";
5751 binmode TMP;
5752 print TMP $xlsx_str2;
5753 close TMP;
5754
5755 __END__
5756
5757 Download this example:
5758 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/filehandle.pl>
5759
5760 Example: headers.pl
5761 This program shows several examples of how to set up headers and
5762 footers with Excel::Writer::XLSX.
5763
5764 The control characters used in the header/footer strings are:
5765
5766 Control Category Description
5767 ======= ======== ===========
5768 &L Justification Left
5769 &C Center
5770 &R Right
5771
5772 &P Information Page number
5773 &N Total number of pages
5774 &D Date
5775 &T Time
5776 &F File name
5777 &A Worksheet name
5778
5779 &fontsize Font Font size
5780 &"font,style" Font name and style
5781 &U Single underline
5782 &E Double underline
5783 &S Strikethrough
5784 &X Superscript
5785 &Y Subscript
5786
5787 &[Picture] Images Image placeholder
5788 &G Same as &[Picture]
5789
5790 && Miscellaneous Literal ampersand &
5791
5792 See the main Excel::Writer::XLSX documentation for more information.
5793
5794 Source code for this example:
5795
5796 #!/usr/bin/perl
5797
5798 ######################################################################
5799 #
5800 # This program shows several examples of how to set up headers and
5801 # footers with Excel::Writer::XLSX.
5802 #
5803 # The control characters used in the header/footer strings are:
5804 #
5805 # Control Category Description
5806 # ======= ======== ===========
5807 # &L Justification Left
5808 # &C Center
5809 # &R Right
5810 #
5811 # &P Information Page number
5812 # &N Total number of pages
5813 # &D Date
5814 # &T Time
5815 # &F File name
5816 # &A Worksheet name
5817 #
5818 # &fontsize Font Font size
5819 # &"font,style" Font name and style
5820 # &U Single underline
5821 # &E Double underline
5822 # &S Strikethrough
5823 # &X Superscript
5824 # &Y Subscript
5825 #
5826 # &[Picture] Images Image placeholder
5827 # &G Same as &[Picture]
5828 #
5829 # && Miscellaneous Literal ampersand &
5830 #
5831 # See the main Excel::Writer::XLSX documentation for more information.
5832 #
5833 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
5834 #
5835
5836
5837 use strict;
5838 use warnings;
5839 use Excel::Writer::XLSX;
5840
5841 my $workbook = Excel::Writer::XLSX->new( 'headers.xlsx' );
5842 my $preview = 'Select Print Preview to see the header and footer';
5843
5844
5845 ######################################################################
5846 #
5847 # A simple example to start
5848 #
5849 my $worksheet1 = $workbook->add_worksheet( 'Simple' );
5850 my $header1 = '&CHere is some centred text.';
5851 my $footer1 = '&LHere is some left aligned text.';
5852
5853 $worksheet1->set_header( $header1 );
5854 $worksheet1->set_footer( $footer1 );
5855
5856 $worksheet1->set_column( 'A:A', 50 );
5857 $worksheet1->write( 'A1', $preview );
5858
5859
5860 ######################################################################
5861 #
5862 # A simple example to start
5863 #
5864 my $worksheet2 = $workbook->add_worksheet( 'Image' );
5865 my $header2 = '&L&[Picture]';
5866
5867 # Adjust the page top margin to allow space for the header image.
5868 $worksheet2->set_margin_top(1.75);
5869
5870 $worksheet2->set_header( $header2, 0.3, {image_left => 'republic.png'});
5871
5872 $worksheet2->set_column( 'A:A', 50 );
5873 $worksheet2->write( 'A1', $preview );
5874
5875
5876 ######################################################################
5877 #
5878 # This is an example of some of the header/footer variables.
5879 #
5880 my $worksheet3 = $workbook->add_worksheet( 'Variables' );
5881 my $header3 = '&LPage &P of &N' . '&CFilename: &F' . '&RSheetname: &A';
5882 my $footer3 = '&LCurrent date: &D' . '&RCurrent time: &T';
5883
5884 $worksheet3->set_header( $header3 );
5885 $worksheet3->set_footer( $footer3 );
5886
5887 $worksheet3->set_column( 'A:A', 50 );
5888 $worksheet3->write( 'A1', $preview );
5889 $worksheet3->write( 'A21', 'Next sheet' );
5890 $worksheet3->set_h_pagebreaks( 20 );
5891
5892
5893 ######################################################################
5894 #
5895 # This example shows how to use more than one font
5896 #
5897 my $worksheet4 = $workbook->add_worksheet( 'Mixed fonts' );
5898 my $header4 = q(&C&"Courier New,Bold"Hello &"Arial,Italic"World);
5899 my $footer4 = q(&C&"Symbol"e&"Arial" = mc&X2);
5900
5901 $worksheet4->set_header( $header4 );
5902 $worksheet4->set_footer( $footer4 );
5903
5904 $worksheet4->set_column( 'A:A', 50 );
5905 $worksheet4->write( 'A1', $preview );
5906
5907
5908 ######################################################################
5909 #
5910 # Example of line wrapping
5911 #
5912 my $worksheet5 = $workbook->add_worksheet( 'Word wrap' );
5913 my $header5 = "&CHeading 1\nHeading 2";
5914
5915 $worksheet5->set_header( $header5 );
5916
5917 $worksheet5->set_column( 'A:A', 50 );
5918 $worksheet5->write( 'A1', $preview );
5919
5920
5921 ######################################################################
5922 #
5923 # Example of inserting a literal ampersand &
5924 #
5925 my $worksheet6 = $workbook->add_worksheet( 'Ampersand' );
5926 my $header6 = '&CCuriouser && Curiouser - Attorneys at Law';
5927
5928 $worksheet6->set_header( $header6 );
5929
5930 $worksheet6->set_column( 'A:A', 50 );
5931 $worksheet6->write( 'A1', $preview );
5932
5933 $workbook->close();
5934
5935 __END__
5936
5937 Download this example:
5938 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/headers.pl>
5939
5940 Example: hide_row_col.pl
5941 Example of how to hide rows and columns in Excel::Writer::XLSX. In
5942 order to hide rows without setting each one, (of approximately 1
5943 million rows), Excel uses an optimisation to hide all rows that don't
5944 have data.
5945
5946 Source code for this example:
5947
5948 #!/usr/bin/perl
5949
5950 ###############################################################################
5951 #
5952 # Example of how to hide rows and columns in Excel::Writer::XLSX. In order to
5953 # hide rows without setting each one, (of approximately 1 million rows),
5954 # Excel uses an optimisation to hide all rows that don't have data.
5955 #
5956 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
5957 #
5958
5959 use strict;
5960 use warnings;
5961 use Excel::Writer::XLSX;
5962
5963 my $workbook = Excel::Writer::XLSX->new( 'hide_row_col.xlsx' );
5964 my $worksheet = $workbook->add_worksheet();
5965
5966
5967 # Write some data.
5968 $worksheet->write( 'D1', 'Some hidden columns.' );
5969 $worksheet->write( 'A8', 'Some hidden rows.' );
5970
5971 # Hide all rows without data.
5972 $worksheet->set_default_row( undef, 1 );
5973
5974 # Set emptys row that we do want to display. All other will be hidden.
5975 for my $row (1 .. 6) {
5976 $worksheet->set_row( $row, 15 );
5977 }
5978
5979 # Hide a range of columns.
5980 $worksheet->set_column( 'G:XFD', undef, undef, 1);
5981
5982 $workbook->close();
5983
5984 __END__
5985
5986 Download this example:
5987 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/hide_row_col.pl>
5988
5989 Example: hide_sheet.pl
5990 Example of how to hide a worksheet with Excel::Writer::XLSX.
5991
5992 Source code for this example:
5993
5994 #!/usr/bin/perl
5995
5996 #######################################################################
5997 #
5998 # Example of how to hide a worksheet with Excel::Writer::XLSX.
5999 #
6000 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6001 #
6002
6003 use strict;
6004 use warnings;
6005 use Excel::Writer::XLSX;
6006
6007 my $workbook = Excel::Writer::XLSX->new( 'hide_sheet.xlsx' );
6008 my $worksheet1 = $workbook->add_worksheet();
6009 my $worksheet2 = $workbook->add_worksheet();
6010 my $worksheet3 = $workbook->add_worksheet();
6011
6012 $worksheet1->set_column( 'A:A', 30 );
6013 $worksheet2->set_column( 'A:A', 30 );
6014 $worksheet3->set_column( 'A:A', 30 );
6015
6016 # Sheet2 won't be visible until it is unhidden in Excel.
6017 $worksheet2->hide();
6018
6019 $worksheet1->write( 0, 0, 'Sheet2 is hidden' );
6020 $worksheet2->write( 0, 0, "Now it's my turn to find you." );
6021 $worksheet3->write( 0, 0, 'Sheet2 is hidden' );
6022
6023 $workbook->close();
6024
6025 __END__
6026
6027 Download this example:
6028 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/hide_sheet.pl>
6029
6030 Example: hyperlink1.pl
6031 Example of how to use the Excel::Writer::XLSX module to write
6032 hyperlinks
6033
6034 See also hyperlink2.pl for worksheet URL examples.
6035
6036 Source code for this example:
6037
6038 #!/usr/bin/perl
6039
6040 ###############################################################################
6041 #
6042 # Example of how to use the Excel::Writer::XLSX module to write hyperlinks
6043 #
6044 # See also hyperlink2.pl for worksheet URL examples.
6045 #
6046 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6047 #
6048
6049 use strict;
6050 use warnings;
6051 use Excel::Writer::XLSX;
6052
6053 # Create a new workbook and add a worksheet
6054 my $workbook = Excel::Writer::XLSX->new( 'hyperlink.xlsx' );
6055
6056
6057 my $worksheet = $workbook->add_worksheet( 'Hyperlinks' );
6058
6059 # Format the first column
6060 $worksheet->set_column( 'A:A', 30 );
6061 $worksheet->set_selection( 'B1' );
6062
6063 # Add a user defined hyperlink format.
6064 my $red_format = $workbook->add_format(
6065 color => 'red',
6066 bold => 1,
6067 underline => 1,
6068 size => 12,
6069 );
6070
6071 # Add an alternate description string to the URL.
6072 my $str = 'Perl home.';
6073
6074 # Add a "tool tip" to the URL.
6075 my $tip = 'Get the latest Perl news here.';
6076
6077
6078 # Write some hyperlinks. Unspecified or undefined format paraamters will be
6079 # replace with the defuault Excel hyperlink style.
6080 $worksheet->write( 'A1', 'http://www.perl.com/' );
6081 $worksheet->write( 'A3', 'http://www.perl.com/', undef, $str );
6082 $worksheet->write( 'A5', 'http://www.perl.com/', undef, $str, $tip );
6083 $worksheet->write( 'A7', 'http://www.perl.com/', $red_format );
6084 $worksheet->write( 'A9', 'mailto:jmcnamara@cpan.org', undef, 'Mail me' );
6085
6086 # Write a URL that isn't a hyperlink
6087 $worksheet->write_string( 'A11', 'http://www.perl.com/' );
6088
6089 $workbook->close();
6090
6091 __END__
6092
6093 Download this example:
6094 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/hyperlink1.pl>
6095
6096 Example: hyperlink2.pl
6097 Example of how to use the Excel::Writer::XLSX module to write internal
6098 and external hyperlinks.
6099
6100 If you wish to run this program and follow the hyperlinks you should
6101 create the following directory structure:
6102
6103 C:\ -- Temp --+-- Europe
6104 |
6105 \-- Asia
6106
6107 See also hyperlink1.pl for web URL examples.
6108
6109 #!/usr/bin/perl
6110
6111 ###############################################################################
6112 #
6113 # Example of how to use the Excel::Writer::XLSX module to write internal and
6114 # external hyperlinks.
6115 #
6116 # If you wish to run this program and follow the hyperlinks you should create
6117 # the following directory structure:
6118 #
6119 # C:\ -- Temp --+-- Europe
6120 # |
6121 # \-- Asia
6122 #
6123 #
6124 # See also hyperlink1.pl for web URL examples.
6125 #
6126 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6127 #
6128
6129
6130 use strict;
6131 use warnings;
6132 use Excel::Writer::XLSX;
6133
6134 # Create three workbooks:
6135 # C:\Temp\Europe\Ireland.xlsx
6136 # C:\Temp\Europe\Italy.xlsx
6137 # C:\Temp\Asia\China.xlsx
6138 #
6139
6140 my $ireland = Excel::Writer::XLSX->new( 'C:\Temp\Europe\Ireland.xlsx' );
6141
6142 my $ire_links = $ireland->add_worksheet( 'Links' );
6143 my $ire_sales = $ireland->add_worksheet( 'Sales' );
6144 my $ire_data = $ireland->add_worksheet( 'Product Data' );
6145 my $ire_url_format = $ireland->get_default_url_format();
6146
6147
6148 my $italy = Excel::Writer::XLSX->new( 'C:\Temp\Europe\Italy.xlsx' );
6149
6150 my $ita_links = $italy->add_worksheet( 'Links' );
6151 my $ita_sales = $italy->add_worksheet( 'Sales' );
6152 my $ita_data = $italy->add_worksheet( 'Product Data' );
6153 my $ita_url_format = $italy->get_default_url_format();
6154
6155
6156 my $china = Excel::Writer::XLSX->new( 'C:\Temp\Asia\China.xlsx' );
6157
6158 my $cha_links = $china->add_worksheet( 'Links' );
6159 my $cha_sales = $china->add_worksheet( 'Sales' );
6160 my $cha_data = $china->add_worksheet( 'Product Data' );
6161 my $cha_url_format = $china->get_default_url_format();
6162
6163
6164 # Add an alternative format
6165 my $format = $ireland->add_format( color => 'green', bold => 1 );
6166 $ire_links->set_column( 'A:B', 25 );
6167
6168
6169 ###############################################################################
6170 #
6171 # Examples of internal links
6172 #
6173 $ire_links->write( 'A1', 'Internal links', $format );
6174
6175 # Internal link
6176 $ire_links->write_url( 'A2', 'internal:Sales!A2', $ire_url_format );
6177
6178 # Internal link to a range
6179 $ire_links->write_url( 'A3', 'internal:Sales!A3:D3', $ire_url_format );
6180
6181 # Internal link with an alternative string
6182 $ire_links->write_url( 'A4', 'internal:Sales!A4', $ire_url_format, 'Link' );
6183
6184 # Internal link with an alternative format
6185 $ire_links->write_url( 'A5', 'internal:Sales!A5', $format );
6186
6187 # Internal link with an alternative string and format
6188 $ire_links->write_url( 'A6', 'internal:Sales!A6', $ire_url_format, 'Link' );
6189
6190 # Internal link (spaces in worksheet name)
6191 $ire_links->write_url( 'A7', q{internal:'Product Data'!A7}, $ire_url_format );
6192
6193
6194 ###############################################################################
6195 #
6196 # Examples of external links
6197 #
6198 $ire_links->write( 'B1', 'External links', $format );
6199
6200 # External link to a local file
6201 $ire_links->write_url( 'B2', 'external:Italy.xlsx', $ire_url_format );
6202
6203 # External link to a local file with worksheet
6204 $ire_links->write_url( 'B3', 'external:Italy.xlsx#Sales!B3', $ire_url_format );
6205
6206 # External link to a local file with worksheet and alternative string
6207 $ire_links->write_url( 'B4', 'external:Italy.xlsx#Sales!B4', $ire_url_format, 'Link' );
6208
6209 # External link to a local file with worksheet and format
6210 $ire_links->write_url( 'B5', 'external:Italy.xlsx#Sales!B5', $format );
6211
6212 # External link to a remote file, absolute path
6213 $ire_links->write_url( 'B6', 'external:C:/Temp/Asia/China.xlsx', $ire_url_format );
6214
6215 # External link to a remote file, relative path
6216 $ire_links->write_url( 'B7', 'external:../Asia/China.xlsx', $ire_url_format );
6217
6218 # External link to a remote file with worksheet
6219 $ire_links->write_url( 'B8', 'external:C:/Temp/Asia/China.xlsx#Sales!B8', $ire_url_format );
6220
6221 # External link to a remote file with worksheet (with spaces in the name)
6222 $ire_links->write_url( 'B9', q{external:C:/Temp/Asia/China.xlsx#'Product Data'!B9}, $ire_url_format );
6223
6224
6225 ###############################################################################
6226 #
6227 # Some utility links to return to the main sheet
6228 #
6229 $ire_sales->write_url( 'A2', 'internal:Links!A2', $ire_url_format, 'Back' );
6230 $ire_sales->write_url( 'A3', 'internal:Links!A3', $ire_url_format, 'Back' );
6231 $ire_sales->write_url( 'A4', 'internal:Links!A4', $ire_url_format, 'Back' );
6232 $ire_sales->write_url( 'A5', 'internal:Links!A5', $ire_url_format, 'Back' );
6233 $ire_sales->write_url( 'A6', 'internal:Links!A6', $ire_url_format, 'Back' );
6234 $ire_data->write_url ( 'A7', 'internal:Links!A7', $ire_url_format, 'Back' );
6235
6236 $ita_links->write_url( 'A1', 'external:Ireland.xlsx#Links!B2', $ita_url_format, 'Back' );
6237 $ita_sales->write_url( 'B3', 'external:Ireland.xlsx#Links!B3', $ita_url_format, 'Back' );
6238 $ita_sales->write_url( 'B4', 'external:Ireland.xlsx#Links!B4', $ita_url_format, 'Back' );
6239 $ita_sales->write_url( 'B5', 'external:Ireland.xlsx#Links!B5', $ita_url_format, 'Back' );
6240 $cha_links->write_url( 'A1', 'external:C:/Temp/Europe/Ireland.xlsx#Links!B6', $cha_url_format, 'Back' );
6241 $cha_sales->write_url( 'B8', 'external:C:/Temp/Europe/Ireland.xlsx#Links!B8', $cha_url_format, 'Back' );
6242 $cha_data->write_url ( 'B9', 'external:C:/Temp/Europe/Ireland.xlsx#Links!B9', $cha_url_format, 'Back' );
6243
6244 $ireland->close();
6245 $italy->close();
6246 $china->close();
6247
6248 __END__
6249
6250 Download this example:
6251 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/hyperlink2.pl>
6252
6253 Example: indent.pl
6254 A simple formatting example using Excel::Writer::XLSX.
6255
6256 This program demonstrates the indentation cell format.
6257
6258 Source code for this example:
6259
6260 #!/usr/bin/perl -w
6261
6262 ##############################################################################
6263 #
6264 # A simple formatting example using Excel::Writer::XLSX.
6265 #
6266 # This program demonstrates the indentation cell format.
6267 #
6268 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6269 #
6270
6271
6272 use strict;
6273 use Excel::Writer::XLSX;
6274
6275 my $workbook = Excel::Writer::XLSX->new( 'indent.xlsx' );
6276
6277 my $worksheet = $workbook->add_worksheet();
6278 my $indent1 = $workbook->add_format( indent => 1 );
6279 my $indent2 = $workbook->add_format( indent => 2 );
6280
6281 $worksheet->set_column( 'A:A', 40 );
6282
6283
6284 $worksheet->write( 'A1', "This text is indented 1 level", $indent1 );
6285 $worksheet->write( 'A2', "This text is indented 2 levels", $indent2 );
6286
6287 $workbook->close();
6288
6289 __END__
6290
6291 Download this example:
6292 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/indent.pl>
6293
6294 Example: ignore_errors.pl
6295 An example of turning off worksheet cells errors/warnings using the
6296 Excel::Writer::XLSX module.
6297
6298 #!/usr/bin/perl -w
6299
6300 #######################################################################
6301 #
6302 # An example of turning off worksheet cells errors/warnings using the
6303 # Excel::Writer::XLSX module.
6304 #
6305 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6306 #
6307
6308 use strict;
6309 use Excel::Writer::XLSX;
6310
6311 my $workbook = Excel::Writer::XLSX->new( 'ignore_errors.xlsx' );
6312 my $worksheet = $workbook->add_worksheet();
6313
6314 # Write strings that looks like numbers. This will cause an Excel warning.
6315 $worksheet->write_string('C2', '123');
6316 $worksheet->write_string('C3', '123');
6317
6318 # Write a divide by zero formula. This will also cause an Excel warning.
6319 $worksheet->write_formula('C5', '=1/0');
6320 $worksheet->write_formula('C6', '=1/0');
6321
6322 # Turn off some of the warnings:
6323 $worksheet->ignore_errors({number_stored_as_text => 'C3', eval_error => 'C6'});
6324
6325 # Write some descriptions for the cells and make the column wider for clarity.
6326 $worksheet->set_column('B:B', 16);
6327 $worksheet->write('B2', 'Warning:');
6328 $worksheet->write('B3', 'Warning turned off:');
6329 $worksheet->write('B5', 'Warning:');
6330 $worksheet->write('B6', 'Warning turned off:');
6331
6332 $workbook->close();
6333
6334 __END__
6335
6336 Download this example:
6337 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/ignore_errors.pl>
6338
6339 Example: macros.pl
6340 An example of adding macros to an Excel::Writer::XLSX file using a VBA
6341 project file extracted from an existing Excel xlsm file.
6342
6343 The "extract_vba" utility supplied with Excel::Writer::XLSX can be used
6344 to extract the vbaProject.bin file.
6345
6346 An embedded macro is connected to a form button on the worksheet.
6347
6348 Source code for this example:
6349
6350 #!/usr/bin/perl
6351
6352 #######################################################################
6353 #
6354 # An example of adding macros to an Excel::Writer::XLSX file using
6355 # a VBA project file extracted from an existing Excel xlsm file.
6356 #
6357 # The C<extract_vba> utility supplied with Excel::Writer::XLSX can be
6358 # used to extract the vbaProject.bin file.
6359 #
6360 # An embedded macro is connected to a form button on the worksheet.
6361 #
6362 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6363 #
6364
6365 use strict;
6366 use warnings;
6367 use Excel::Writer::XLSX;
6368
6369 # Note the file extension should be .xlsm.
6370 my $workbook = Excel::Writer::XLSX->new( 'macros.xlsm' );
6371 my $worksheet = $workbook->add_worksheet();
6372
6373 $worksheet->set_column( 'A:A', 30 );
6374
6375 # Add the VBA project binary.
6376 $workbook->add_vba_project( './vbaProject.bin' );
6377
6378 # Show text for the end user.
6379 $worksheet->write( 'A3', 'Press the button to say hello.' );
6380
6381 # Add a button tied to a macro in the VBA project.
6382 $worksheet->insert_button(
6383 'B3',
6384 {
6385 macro => 'say_hello',
6386 caption => 'Press Me',
6387 width => 80,
6388 height => 30
6389 }
6390 );
6391
6392 $workbook->close();
6393
6394 __END__
6395
6396 Download this example:
6397 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/macros.pl>
6398
6399 Example: merge1.pl
6400 Simple example of merging cells using the Excel::Writer::XLSX module.
6401
6402 This example merges three cells using the "Centre Across Selection"
6403 alignment which was the Excel 5 method of achieving a merge. For a more
6404 modern approach use the merge_range() worksheet method instead. See
6405 the merge3.pl - merge6.pl programs.
6406
6407 Source code for this example:
6408
6409 #!/usr/bin/perl
6410
6411 ###############################################################################
6412 #
6413 # Simple example of merging cells using the Excel::Writer::XLSX module.
6414 #
6415 # This example merges three cells using the "Centre Across Selection"
6416 # alignment which was the Excel 5 method of achieving a merge. For a more
6417 # modern approach use the merge_range() worksheet method instead.
6418 # See the merge3.pl - merge6.pl programs.
6419 #
6420 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6421 #
6422
6423 use strict;
6424 use warnings;
6425 use Excel::Writer::XLSX;
6426
6427 # Create a new workbook and add a worksheet
6428 my $workbook = Excel::Writer::XLSX->new( 'merge1.xlsx' );
6429 my $worksheet = $workbook->add_worksheet();
6430
6431
6432 # Increase the cell size of the merged cells to highlight the formatting.
6433 $worksheet->set_column( 'B:D', 20 );
6434 $worksheet->set_row( 2, 30 );
6435
6436
6437 # Create a merge format
6438 my $format = $workbook->add_format( center_across => 1 );
6439
6440
6441 # Only one cell should contain text, the others should be blank.
6442 $worksheet->write( 2, 1, "Center across selection", $format );
6443 $worksheet->write_blank( 2, 2, $format );
6444 $worksheet->write_blank( 2, 3, $format );
6445
6446 $workbook->close();
6447
6448 __END__
6449
6450 Download this example:
6451 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/merge1.pl>
6452
6453 Example: merge2.pl
6454 Simple example of merging cells using the Excel::Writer::XLSX module
6455
6456 This example merges three cells using the "Centre Across Selection"
6457 alignment which was the Excel 5 method of achieving a merge. For a more
6458 modern approach use the merge_range() worksheet method instead. See
6459 the merge3.pl - merge6.pl programs.
6460
6461 Source code for this example:
6462
6463 #!/usr/bin/perl
6464
6465 ###############################################################################
6466 #
6467 # Simple example of merging cells using the Excel::Writer::XLSX module
6468 #
6469 # This example merges three cells using the "Centre Across Selection"
6470 # alignment which was the Excel 5 method of achieving a merge. For a more
6471 # modern approach use the merge_range() worksheet method instead.
6472 # See the merge3.pl - merge6.pl programs.
6473 #
6474 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6475 #
6476
6477 use strict;
6478 use warnings;
6479 use Excel::Writer::XLSX;
6480
6481 # Create a new workbook and add a worksheet
6482 my $workbook = Excel::Writer::XLSX->new( 'merge2.xlsx' );
6483 my $worksheet = $workbook->add_worksheet();
6484
6485
6486 # Increase the cell size of the merged cells to highlight the formatting.
6487 $worksheet->set_column( 1, 2, 30 );
6488 $worksheet->set_row( 2, 40 );
6489
6490
6491 # Create a merged format
6492 my $format = $workbook->add_format(
6493 center_across => 1,
6494 bold => 1,
6495 size => 15,
6496 pattern => 1,
6497 border => 6,
6498 color => 'white',
6499 fg_color => 'green',
6500 border_color => 'yellow',
6501 align => 'vcenter',
6502 );
6503
6504
6505 # Only one cell should contain text, the others should be blank.
6506 $worksheet->write( 2, 1, "Center across selection", $format );
6507 $worksheet->write_blank( 2, 2, $format );
6508
6509 $workbook->close();
6510
6511 __END__
6512
6513 Download this example:
6514 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/merge2.pl>
6515
6516 Example: merge3.pl
6517 Example of how to use Excel::Writer::XLSX to write a hyperlink in a
6518 merged cell.
6519
6520 Source code for this example:
6521
6522 #!/usr/bin/perl
6523
6524 ###############################################################################
6525 #
6526 # Example of how to use Excel::Writer::XLSX to write a hyperlink in a
6527 # merged cell.
6528 #
6529 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6530 #
6531
6532 use strict;
6533 use warnings;
6534 use Excel::Writer::XLSX;
6535
6536 # Create a new workbook and add a worksheet
6537 my $workbook = Excel::Writer::XLSX->new( 'merge3.xlsx' );
6538 my $worksheet = $workbook->add_worksheet();
6539
6540
6541 # Increase the cell size of the merged cells to highlight the formatting.
6542 $worksheet->set_row( $_, 30 ) for ( 3, 6, 7 );
6543 $worksheet->set_column( 'B:D', 20 );
6544
6545
6546 ###############################################################################
6547 #
6548 # Example: Merge cells containing a hyperlink using merge_range().
6549 #
6550 my $format = $workbook->add_format(
6551 border => 1,
6552 underline => 1,
6553 color => 'blue',
6554 align => 'center',
6555 valign => 'vcenter',
6556 );
6557
6558 # Merge 3 cells
6559 $worksheet->merge_range( 'B4:D4', 'http://www.perl.com', $format );
6560
6561
6562 # Merge 3 cells over two rows
6563 $worksheet->merge_range( 'B7:D8', 'http://www.perl.com', $format );
6564
6565
6566 $workbook->close();
6567
6568 __END__
6569
6570 Download this example:
6571 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/merge3.pl>
6572
6573 Example: merge4.pl
6574 Example of how to use the Excel::Writer::XLSX merge_range() workbook
6575 method with complex formatting.
6576
6577 Source code for this example:
6578
6579 #!/usr/bin/perl
6580
6581 ###############################################################################
6582 #
6583 # Example of how to use the Excel::Writer::XLSX merge_range() workbook
6584 # method with complex formatting.
6585 #
6586 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6587 #
6588
6589 use strict;
6590 use warnings;
6591 use Excel::Writer::XLSX;
6592
6593 # Create a new workbook and add a worksheet
6594 my $workbook = Excel::Writer::XLSX->new( 'merge4.xlsx' );
6595 my $worksheet = $workbook->add_worksheet();
6596
6597
6598 # Increase the cell size of the merged cells to highlight the formatting.
6599 $worksheet->set_row( $_, 30 ) for ( 1 .. 11 );
6600 $worksheet->set_column( 'B:D', 20 );
6601
6602
6603 ###############################################################################
6604 #
6605 # Example 1: Text centered vertically and horizontally
6606 #
6607 my $format1 = $workbook->add_format(
6608 border => 6,
6609 bold => 1,
6610 color => 'red',
6611 valign => 'vcenter',
6612 align => 'center',
6613 );
6614
6615
6616 $worksheet->merge_range( 'B2:D3', 'Vertical and horizontal', $format1 );
6617
6618
6619 ###############################################################################
6620 #
6621 # Example 2: Text aligned to the top and left
6622 #
6623 my $format2 = $workbook->add_format(
6624 border => 6,
6625 bold => 1,
6626 color => 'red',
6627 valign => 'top',
6628 align => 'left',
6629 );
6630
6631
6632 $worksheet->merge_range( 'B5:D6', 'Aligned to the top and left', $format2 );
6633
6634
6635 ###############################################################################
6636 #
6637 # Example 3: Text aligned to the bottom and right
6638 #
6639 my $format3 = $workbook->add_format(
6640 border => 6,
6641 bold => 1,
6642 color => 'red',
6643 valign => 'bottom',
6644 align => 'right',
6645 );
6646
6647
6648 $worksheet->merge_range( 'B8:D9', 'Aligned to the bottom and right', $format3 );
6649
6650
6651 ###############################################################################
6652 #
6653 # Example 4: Text justified (i.e. wrapped) in the cell
6654 #
6655 my $format4 = $workbook->add_format(
6656 border => 6,
6657 bold => 1,
6658 color => 'red',
6659 valign => 'top',
6660 align => 'justify',
6661 );
6662
6663
6664 $worksheet->merge_range( 'B11:D12', 'Justified: ' . 'so on and ' x 18,
6665 $format4 );
6666
6667 $workbook->close();
6668
6669 __END__
6670
6671 Download this example:
6672 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/merge4.pl>
6673
6674 Example: merge5.pl
6675 Example of how to use the Excel::Writer::XLSX merge_cells() workbook
6676 method with complex formatting and rotation.
6677
6678 Source code for this example:
6679
6680 #!/usr/bin/perl
6681
6682 ###############################################################################
6683 #
6684 # Example of how to use the Excel::Writer::XLSX merge_cells() workbook
6685 # method with complex formatting and rotation.
6686 #
6687 #
6688 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6689 #
6690
6691 use strict;
6692 use warnings;
6693 use Excel::Writer::XLSX;
6694
6695 # Create a new workbook and add a worksheet
6696 my $workbook = Excel::Writer::XLSX->new( 'merge5.xlsx' );
6697 my $worksheet = $workbook->add_worksheet();
6698
6699
6700 # Increase the cell size of the merged cells to highlight the formatting.
6701 $worksheet->set_row( $_, 36 ) for ( 3 .. 8 );
6702 $worksheet->set_column( $_, $_, 15 ) for ( 1, 3, 5 );
6703
6704
6705 ###############################################################################
6706 #
6707 # Rotation 1, letters run from top to bottom
6708 #
6709 my $format1 = $workbook->add_format(
6710 border => 6,
6711 bold => 1,
6712 color => 'red',
6713 valign => 'vcentre',
6714 align => 'centre',
6715 rotation => 270,
6716 );
6717
6718
6719 $worksheet->merge_range( 'B4:B9', 'Rotation 270', $format1 );
6720
6721
6722 ###############################################################################
6723 #
6724 # Rotation 2, 90° anticlockwise
6725 #
6726 my $format2 = $workbook->add_format(
6727 border => 6,
6728 bold => 1,
6729 color => 'red',
6730 valign => 'vcentre',
6731 align => 'centre',
6732 rotation => 90,
6733 );
6734
6735
6736 $worksheet->merge_range( 'D4:D9', 'Rotation 90°', $format2 );
6737
6738
6739 ###############################################################################
6740 #
6741 # Rotation 3, 90° clockwise
6742 #
6743 my $format3 = $workbook->add_format(
6744 border => 6,
6745 bold => 1,
6746 color => 'red',
6747 valign => 'vcentre',
6748 align => 'centre',
6749 rotation => -90,
6750 );
6751
6752
6753 $worksheet->merge_range( 'F4:F9', 'Rotation -90°', $format3 );
6754
6755 $workbook->close();
6756
6757 __END__
6758
6759 Download this example:
6760 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/merge5.pl>
6761
6762 Example: merge6.pl
6763 Example of how to use the Excel::Writer::XLSX merge_cells() workbook
6764 method with Unicode strings.
6765
6766 Source code for this example:
6767
6768 #!/usr/bin/perl
6769
6770 ###############################################################################
6771 #
6772 # Example of how to use the Excel::Writer::XLSX merge_cells() workbook
6773 # method with Unicode strings.
6774 #
6775 #
6776 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
6777 #
6778
6779 use strict;
6780 use warnings;
6781 use Excel::Writer::XLSX;
6782
6783 # Create a new workbook and add a worksheet
6784 my $workbook = Excel::Writer::XLSX->new( 'merge6.xlsx' );
6785 my $worksheet = $workbook->add_worksheet();
6786
6787
6788 # Increase the cell size of the merged cells to highlight the formatting.
6789 $worksheet->set_row( $_, 36 ) for 2 .. 9;
6790 $worksheet->set_column( 'B:D', 25 );
6791
6792
6793 # Format for the merged cells.
6794 my $format = $workbook->add_format(
6795 border => 6,
6796 bold => 1,
6797 color => 'red',
6798 size => 20,
6799 valign => 'vcentre',
6800 align => 'left',
6801 indent => 1,
6802 );
6803
6804
6805 ###############################################################################
6806 #
6807 # Write an Ascii string.
6808 #
6809 $worksheet->merge_range( 'B3:D4', 'ASCII: A simple string', $format );
6810
6811
6812 ###############################################################################
6813 #
6814 # Write a UTF-8 Unicode string.
6815 #
6816 my $smiley = chr 0x263a;
6817 $worksheet->merge_range( 'B6:D7', "UTF-8: A Unicode smiley $smiley", $format );
6818
6819 $workbook->close();
6820
6821 __END__
6822
6823 Download this example:
6824 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/merge6.pl>
6825
6826 Example: mod_perl1.pl
6827 Example of how to use the Excel::Writer::XLSX module to send an Excel
6828 file to a browser using mod_perl 1 and Apache
6829
6830 This module ties *XLSX directly to Apache, and with the correct
6831 content-disposition/types it will prompt the user to save the file, or
6832 open it at this location.
6833
6834 This script is a modification of the Excel::Writer::XLSX cgi.pl
6835 example.
6836
6837 Change the name of this file to Cgi.pm. Change the package location to
6838 wherever you locate this package. In the example below it is located
6839 in the Excel::Writer::XLSX directory.
6840
6841 Your httpd.conf entry for this module, should you choose to use it as a
6842 stand alone app, should look similar to the following:
6843
6844 <Location /spreadsheet-test>
6845 SetHandler perl-script
6846 PerlHandler Excel::Writer::XLSX::Cgi
6847 PerlSendHeader On
6848 </Location>
6849
6850 The PerlHandler name above and the package name below *have* to match.
6851
6852 ###############################################################################
6853 #
6854 # Example of how to use the Excel::Writer::XLSX module to send an Excel
6855 # file to a browser using mod_perl 1 and Apache
6856 #
6857 # This module ties *XLSX directly to Apache, and with the correct
6858 # content-disposition/types it will prompt the user to save
6859 # the file, or open it at this location.
6860 #
6861 # This script is a modification of the Excel::Writer::XLSX cgi.pl example.
6862 #
6863 # Change the name of this file to Cgi.pm.
6864 # Change the package location to wherever you locate this package.
6865 # In the example below it is located in the Excel::Writer::XLSX directory.
6866 #
6867 # Your httpd.conf entry for this module, should you choose to use it
6868 # as a stand alone app, should look similar to the following:
6869 #
6870 # <Location /spreadsheet-test>
6871 # SetHandler perl-script
6872 # PerlHandler Excel::Writer::XLSX::Cgi
6873 # PerlSendHeader On
6874 # </Location>
6875 #
6876 # The PerlHandler name above and the package name below *have* to match.
6877
6878 # Apr 2001, Thomas Sullivan, webmaster@860.org
6879 # Feb 2001, John McNamara, jmcnamara@cpan.org
6880
6881 package Excel::Writer::XLSX::Cgi;
6882
6883 ##########################################
6884 # Pragma Definitions
6885 ##########################################
6886 use strict;
6887
6888 ##########################################
6889 # Required Modules
6890 ##########################################
6891 use Apache::Constants qw(:common);
6892 use Apache::Request;
6893 use Apache::URI; # This may not be needed
6894 use Excel::Writer::XLSX;
6895
6896 ##########################################
6897 # Main App Body
6898 ##########################################
6899 sub handler {
6900
6901 # New apache object
6902 # Should you decide to use it.
6903 my $r = Apache::Request->new( shift );
6904
6905 # Set the filename and send the content type
6906 # This will appear when they save the spreadsheet
6907 my $filename = "cgitest.xlsx";
6908
6909 ####################################################
6910 ## Send the content type headers
6911 ####################################################
6912 print "Content-disposition: attachment;filename=$filename\n";
6913 print "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n\n";
6914
6915 ####################################################
6916 # Tie a filehandle to Apache's STDOUT.
6917 # Create a new workbook and add a worksheet.
6918 ####################################################
6919 tie *XLSX => 'Apache';
6920 binmode( *XLSX );
6921
6922 my $workbook = Excel::Writer::XLSX->new( \*XLSX );
6923 my $worksheet = $workbook->add_worksheet();
6924
6925
6926 # Set the column width for column 1
6927 $worksheet->set_column( 0, 0, 20 );
6928
6929
6930 # Create a format
6931 my $format = $workbook->add_format();
6932 $format->set_bold();
6933 $format->set_size( 15 );
6934 $format->set_color( 'blue' );
6935
6936
6937 # Write to the workbook
6938 $worksheet->write( 0, 0, "Hi Excel!", $format );
6939
6940 # You must close the workbook for Content-disposition
6941 $workbook->close();
6942 }
6943
6944 1;
6945
6946 Download this example:
6947 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/mod_perl1.pl>
6948
6949 Example: mod_perl2.pl
6950 Example of how to use the Excel::Writer::XLSX module to send an Excel
6951 file to a browser using mod_perl 2 and Apache.
6952
6953 This module ties *XLSX directly to Apache, and with the correct
6954 content-disposition/types it will prompt the user to save the file, or
6955 open it at this location.
6956
6957 This script is a modification of the Excel::Writer::XLSX cgi.pl
6958 example.
6959
6960 Change the name of this file to MP2Test.pm. Change the package
6961 location to wherever you locate this package. In the example below it
6962 is located in the Excel::Writer::XLSX directory.
6963
6964 Your httpd.conf entry for this module, should you choose to use it as a
6965 stand alone app, should look similar to the following:
6966
6967 PerlModule Apache2::RequestRec
6968 PerlModule APR::Table
6969 PerlModule Apache2::RequestIO
6970
6971 <Location /spreadsheet-test>
6972 SetHandler perl-script
6973 PerlResponseHandler Excel::Writer::XLSX::MP2Test
6974 </Location>
6975
6976 The PerlResponseHandler must match the package name below.
6977
6978 ###############################################################################
6979 #
6980 # Example of how to use the Excel::Writer::XLSX module to send an Excel
6981 # file to a browser using mod_perl 2 and Apache.
6982 #
6983 # This module ties *XLSX directly to Apache, and with the correct
6984 # content-disposition/types it will prompt the user to save
6985 # the file, or open it at this location.
6986 #
6987 # This script is a modification of the Excel::Writer::XLSX cgi.pl example.
6988 #
6989 # Change the name of this file to MP2Test.pm.
6990 # Change the package location to wherever you locate this package.
6991 # In the example below it is located in the Excel::Writer::XLSX directory.
6992 #
6993 # Your httpd.conf entry for this module, should you choose to use it
6994 # as a stand alone app, should look similar to the following:
6995 #
6996 # PerlModule Apache2::RequestRec
6997 # PerlModule APR::Table
6998 # PerlModule Apache2::RequestIO
6999 #
7000 # <Location /spreadsheet-test>
7001 # SetHandler perl-script
7002 # PerlResponseHandler Excel::Writer::XLSX::MP2Test
7003 # </Location>
7004 #
7005 # The PerlResponseHandler must match the package name below.
7006
7007 # Jun 2004, Matisse Enzer, matisse@matisse.net (mod_perl 2 version)
7008 # Apr 2001, Thomas Sullivan, webmaster@860.org
7009 # Feb 2001, John McNamara, jmcnamara@cpan.org
7010
7011 package Excel::Writer::XLSX::MP2Test;
7012
7013 ##########################################
7014 # Pragma Definitions
7015 ##########################################
7016 use strict;
7017
7018 ##########################################
7019 # Required Modules
7020 ##########################################
7021 use Apache2::Const -compile => qw( :common );
7022 use Excel::Writer::XLSX;
7023
7024 ##########################################
7025 # Main App Body
7026 ##########################################
7027 sub handler {
7028 my ( $r ) = @_; # Apache request object is passed to handler in mod_perl 2
7029
7030 # Set the filename and send the content type
7031 # This will appear when they save the spreadsheet
7032 my $filename = "mod_perl2_test.xlsx";
7033
7034 ####################################################
7035 ## Send the content type headers the mod_perl 2 way
7036 ####################################################
7037 $r->headers_out->{'Content-Disposition'} = "attachment;filename=$filename";
7038 $r->content_type( 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
7039
7040 ####################################################
7041 # Tie a filehandle to Apache's STDOUT.
7042 # Create a new workbook and add a worksheet.
7043 ####################################################
7044 tie *XLSX => $r; # The mod_perl 2 way. Tie to the Apache::RequestRec object
7045 binmode( *XLSX );
7046
7047 my $workbook = Excel::Writer::XLSX->new( \*XLSX );
7048 my $worksheet = $workbook->add_worksheet();
7049
7050
7051 # Set the column width for column 1
7052 $worksheet->set_column( 0, 0, 20 );
7053
7054
7055 # Create a format
7056 my $format = $workbook->add_format();
7057 $format->set_bold();
7058 $format->set_size( 15 );
7059 $format->set_color( 'blue' );
7060
7061
7062 # Write to the workbook
7063 $worksheet->write( 0, 0, 'Hi Excel! from ' . $r->hostname, $format );
7064
7065 # You must close the workbook for Content-disposition
7066 $workbook->close();
7067 return Apache2::Const::OK;
7068 }
7069
7070 1;
7071
7072 Download this example:
7073 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/mod_perl2.pl>
7074
7075 Example: outline.pl
7076 Example of how use Excel::Writer::XLSX to generate Excel outlines and
7077 grouping.
7078
7079 Excel allows you to group rows or columns so that they can be hidden or
7080 displayed with a single mouse click. This feature is referred to as
7081 outlines.
7082
7083 Outlines can reduce complex data down to a few salient sub-totals or
7084 summaries.
7085
7086 This feature is best viewed in Excel but the following is an ASCII
7087 representation of what a worksheet with three outlines might look like.
7088 Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
7089 level 1. The lines at the left hand side are called outline level bars.
7090
7091 ------------------------------------------
7092 1 2 3 | | A | B | C | D | ...
7093 ------------------------------------------
7094 _ | 1 | A | | | | ...
7095 | _ | 2 | B | | | | ...
7096 | | | 3 | (C) | | | | ...
7097 | | | 4 | (D) | | | | ...
7098 | - | 5 | E | | | | ...
7099 | _ | 6 | F | | | | ...
7100 | | | 7 | (G) | | | | ...
7101 | | | 8 | (H) | | | | ...
7102 | - | 9 | I | | | | ...
7103 - | . | ... | ... | ... | ... | ...
7104
7105 Clicking the minus sign on each of the level 2 outlines will collapse
7106 and hide the data as shown in the next figure. The minus sign changes
7107 to a plus sign to indicate that the data in the outline is hidden.
7108
7109 ------------------------------------------
7110 1 2 3 | | A | B | C | D | ...
7111 ------------------------------------------
7112 _ | 1 | A | | | | ...
7113 | | 2 | B | | | | ...
7114 | + | 5 | E | | | | ...
7115 | | 6 | F | | | | ...
7116 | + | 9 | I | | | | ...
7117 - | . | ... | ... | ... | ... | ...
7118
7119 Clicking on the minus sign on the level 1 outline will collapse the
7120 remaining rows as follows:
7121
7122 ------------------------------------------
7123 1 2 3 | | A | B | C | D | ...
7124 ------------------------------------------
7125 | 1 | A | | | | ...
7126 + | . | ... | ... | ... | ... | ...
7127
7128 See the main Excel::Writer::XLSX documentation for more information.
7129
7130 Source code for this example:
7131
7132 #!/usr/bin/perl
7133
7134 ###############################################################################
7135 #
7136 # Example of how use Excel::Writer::XLSX to generate Excel outlines and
7137 # grouping.
7138 #
7139 #
7140 # Excel allows you to group rows or columns so that they can be hidden or
7141 # displayed with a single mouse click. This feature is referred to as outlines.
7142 #
7143 # Outlines can reduce complex data down to a few salient sub-totals or
7144 # summaries.
7145 #
7146 # This feature is best viewed in Excel but the following is an ASCII
7147 # representation of what a worksheet with three outlines might look like.
7148 # Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
7149 # level 1. The lines at the left hand side are called outline level bars.
7150 #
7151 #
7152 # ------------------------------------------
7153 # 1 2 3 | | A | B | C | D | ...
7154 # ------------------------------------------
7155 # _ | 1 | A | | | | ...
7156 # | _ | 2 | B | | | | ...
7157 # | | | 3 | (C) | | | | ...
7158 # | | | 4 | (D) | | | | ...
7159 # | - | 5 | E | | | | ...
7160 # | _ | 6 | F | | | | ...
7161 # | | | 7 | (G) | | | | ...
7162 # | | | 8 | (H) | | | | ...
7163 # | - | 9 | I | | | | ...
7164 # - | . | ... | ... | ... | ... | ...
7165 #
7166 #
7167 # Clicking the minus sign on each of the level 2 outlines will collapse and
7168 # hide the data as shown in the next figure. The minus sign changes to a plus
7169 # sign to indicate that the data in the outline is hidden.
7170 #
7171 # ------------------------------------------
7172 # 1 2 3 | | A | B | C | D | ...
7173 # ------------------------------------------
7174 # _ | 1 | A | | | | ...
7175 # | | 2 | B | | | | ...
7176 # | + | 5 | E | | | | ...
7177 # | | 6 | F | | | | ...
7178 # | + | 9 | I | | | | ...
7179 # - | . | ... | ... | ... | ... | ...
7180 #
7181 #
7182 # Clicking on the minus sign on the level 1 outline will collapse the remaining
7183 # rows as follows:
7184 #
7185 # ------------------------------------------
7186 # 1 2 3 | | A | B | C | D | ...
7187 # ------------------------------------------
7188 # | 1 | A | | | | ...
7189 # + | . | ... | ... | ... | ... | ...
7190 #
7191 # See the main Excel::Writer::XLSX documentation for more information.
7192 #
7193 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7194 #
7195
7196
7197 use strict;
7198 use warnings;
7199 use Excel::Writer::XLSX;
7200
7201 # Create a new workbook and add some worksheets
7202 my $workbook = Excel::Writer::XLSX->new( 'outline.xlsx' );
7203 my $worksheet1 = $workbook->add_worksheet( 'Outlined Rows' );
7204 my $worksheet2 = $workbook->add_worksheet( 'Collapsed Rows' );
7205 my $worksheet3 = $workbook->add_worksheet( 'Outline Columns' );
7206 my $worksheet4 = $workbook->add_worksheet( 'Outline levels' );
7207
7208 # Add a general format
7209 my $bold = $workbook->add_format( bold => 1 );
7210
7211
7212 ###############################################################################
7213 #
7214 # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
7215 # functions so that it looks like the type of automatic outlines that are
7216 # generated when you use the Excel Data->SubTotals menu item.
7217 #
7218
7219
7220 # For outlines the important parameters are $hidden and $level. Rows with the
7221 # same $level are grouped together. The group will be collapsed if $hidden is
7222 # non-zero. $height and $XF are assigned default values if they are undef.
7223 #
7224 # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
7225 #
7226 $worksheet1->set_row( 1, undef, undef, 0, 2 );
7227 $worksheet1->set_row( 2, undef, undef, 0, 2 );
7228 $worksheet1->set_row( 3, undef, undef, 0, 2 );
7229 $worksheet1->set_row( 4, undef, undef, 0, 2 );
7230 $worksheet1->set_row( 5, undef, undef, 0, 1 );
7231
7232 $worksheet1->set_row( 6, undef, undef, 0, 2 );
7233 $worksheet1->set_row( 7, undef, undef, 0, 2 );
7234 $worksheet1->set_row( 8, undef, undef, 0, 2 );
7235 $worksheet1->set_row( 9, undef, undef, 0, 2 );
7236 $worksheet1->set_row( 10, undef, undef, 0, 1 );
7237
7238
7239 # Add a column format for clarity
7240 $worksheet1->set_column( 'A:A', 20 );
7241
7242 # Add the data, labels and formulas
7243 $worksheet1->write( 'A1', 'Region', $bold );
7244 $worksheet1->write( 'A2', 'North' );
7245 $worksheet1->write( 'A3', 'North' );
7246 $worksheet1->write( 'A4', 'North' );
7247 $worksheet1->write( 'A5', 'North' );
7248 $worksheet1->write( 'A6', 'North Total', $bold );
7249
7250 $worksheet1->write( 'B1', 'Sales', $bold );
7251 $worksheet1->write( 'B2', 1000 );
7252 $worksheet1->write( 'B3', 1200 );
7253 $worksheet1->write( 'B4', 900 );
7254 $worksheet1->write( 'B5', 1200 );
7255 $worksheet1->write( 'B6', '=SUBTOTAL(9,B2:B5)', $bold );
7256
7257 $worksheet1->write( 'A7', 'South' );
7258 $worksheet1->write( 'A8', 'South' );
7259 $worksheet1->write( 'A9', 'South' );
7260 $worksheet1->write( 'A10', 'South' );
7261 $worksheet1->write( 'A11', 'South Total', $bold );
7262
7263 $worksheet1->write( 'B7', 400 );
7264 $worksheet1->write( 'B8', 600 );
7265 $worksheet1->write( 'B9', 500 );
7266 $worksheet1->write( 'B10', 600 );
7267 $worksheet1->write( 'B11', '=SUBTOTAL(9,B7:B10)', $bold );
7268
7269 $worksheet1->write( 'A12', 'Grand Total', $bold );
7270 $worksheet1->write( 'B12', '=SUBTOTAL(9,B2:B10)', $bold );
7271
7272
7273 ###############################################################################
7274 #
7275 # Example 2: Create a worksheet with outlined rows. This is the same as the
7276 # previous example except that the rows are collapsed.
7277 # Note: We need to indicate the row that contains the collapsed symbol '+'
7278 # with the optional parameter, $collapsed.
7279
7280 # The group will be collapsed if $hidden is non-zero.
7281 # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
7282 #
7283 $worksheet2->set_row( 1, undef, undef, 1, 2 );
7284 $worksheet2->set_row( 2, undef, undef, 1, 2 );
7285 $worksheet2->set_row( 3, undef, undef, 1, 2 );
7286 $worksheet2->set_row( 4, undef, undef, 1, 2 );
7287 $worksheet2->set_row( 5, undef, undef, 1, 1 );
7288
7289 $worksheet2->set_row( 6, undef, undef, 1, 2 );
7290 $worksheet2->set_row( 7, undef, undef, 1, 2 );
7291 $worksheet2->set_row( 8, undef, undef, 1, 2 );
7292 $worksheet2->set_row( 9, undef, undef, 1, 2 );
7293 $worksheet2->set_row( 10, undef, undef, 1, 1 );
7294 $worksheet2->set_row( 11, undef, undef, 0, 0, 1 );
7295
7296
7297 # Add a column format for clarity
7298 $worksheet2->set_column( 'A:A', 20 );
7299
7300 # Add the data, labels and formulas
7301 $worksheet2->write( 'A1', 'Region', $bold );
7302 $worksheet2->write( 'A2', 'North' );
7303 $worksheet2->write( 'A3', 'North' );
7304 $worksheet2->write( 'A4', 'North' );
7305 $worksheet2->write( 'A5', 'North' );
7306 $worksheet2->write( 'A6', 'North Total', $bold );
7307
7308 $worksheet2->write( 'B1', 'Sales', $bold );
7309 $worksheet2->write( 'B2', 1000 );
7310 $worksheet2->write( 'B3', 1200 );
7311 $worksheet2->write( 'B4', 900 );
7312 $worksheet2->write( 'B5', 1200 );
7313 $worksheet2->write( 'B6', '=SUBTOTAL(9,B2:B5)', $bold );
7314
7315 $worksheet2->write( 'A7', 'South' );
7316 $worksheet2->write( 'A8', 'South' );
7317 $worksheet2->write( 'A9', 'South' );
7318 $worksheet2->write( 'A10', 'South' );
7319 $worksheet2->write( 'A11', 'South Total', $bold );
7320
7321 $worksheet2->write( 'B7', 400 );
7322 $worksheet2->write( 'B8', 600 );
7323 $worksheet2->write( 'B9', 500 );
7324 $worksheet2->write( 'B10', 600 );
7325 $worksheet2->write( 'B11', '=SUBTOTAL(9,B7:B10)', $bold );
7326
7327 $worksheet2->write( 'A12', 'Grand Total', $bold );
7328 $worksheet2->write( 'B12', '=SUBTOTAL(9,B2:B10)', $bold );
7329
7330
7331 ###############################################################################
7332 #
7333 # Example 3: Create a worksheet with outlined columns.
7334 #
7335 my $data = [
7336 [ 'Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ' Total' ],
7337 [ 'North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)' ],
7338 [ 'South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)' ],
7339 [ 'East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)' ],
7340 [ 'West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G5)' ],
7341 ];
7342
7343 # Add bold format to the first row
7344 $worksheet3->set_row( 0, undef, $bold );
7345
7346 # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
7347 $worksheet3->set_column( 'A:A', 10, $bold );
7348 $worksheet3->set_column( 'B:G', 5, undef, 0, 1 );
7349 $worksheet3->set_column( 'H:H', 10 );
7350
7351 # Write the data and a formula
7352 $worksheet3->write_col( 'A1', $data );
7353 $worksheet3->write( 'H6', '=SUM(H2:H5)', $bold );
7354
7355
7356 ###############################################################################
7357 #
7358 # Example 4: Show all possible outline levels.
7359 #
7360 my $levels = [
7361 "Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6",
7362 "Level 7", "Level 6", "Level 5", "Level 4", "Level 3", "Level 2",
7363 "Level 1"
7364 ];
7365
7366
7367 $worksheet4->write_col( 'A1', $levels );
7368
7369 $worksheet4->set_row( 0, undef, undef, undef, 1 );
7370 $worksheet4->set_row( 1, undef, undef, undef, 2 );
7371 $worksheet4->set_row( 2, undef, undef, undef, 3 );
7372 $worksheet4->set_row( 3, undef, undef, undef, 4 );
7373 $worksheet4->set_row( 4, undef, undef, undef, 5 );
7374 $worksheet4->set_row( 5, undef, undef, undef, 6 );
7375 $worksheet4->set_row( 6, undef, undef, undef, 7 );
7376 $worksheet4->set_row( 7, undef, undef, undef, 6 );
7377 $worksheet4->set_row( 8, undef, undef, undef, 5 );
7378 $worksheet4->set_row( 9, undef, undef, undef, 4 );
7379 $worksheet4->set_row( 10, undef, undef, undef, 3 );
7380 $worksheet4->set_row( 11, undef, undef, undef, 2 );
7381 $worksheet4->set_row( 12, undef, undef, undef, 1 );
7382
7383 $workbook->close();
7384
7385 __END__
7386
7387 Download this example:
7388 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/outline.pl>
7389
7390 Example: outline_collapsed.pl
7391 Example of how to use Excel::Writer::XLSX to generate Excel outlines
7392 and grouping.
7393
7394 These examples focus mainly on collapsed outlines. See also the
7395 outlines.pl example program for more general examples.
7396
7397 Source code for this example:
7398
7399 #!/usr/bin/perl
7400
7401 ###############################################################################
7402 #
7403 # Example of how to use Excel::Writer::XLSX to generate Excel outlines and
7404 # grouping.
7405 #
7406 # These examples focus mainly on collapsed outlines. See also the
7407 # outlines.pl example program for more general examples.
7408 #
7409 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7410 #
7411
7412 use strict;
7413 use warnings;
7414 use Excel::Writer::XLSX;
7415
7416 # Create a new workbook and add some worksheets
7417 my $workbook = Excel::Writer::XLSX->new( 'outline_collapsed.xlsx' );
7418 my $worksheet1 = $workbook->add_worksheet( 'Outlined Rows' );
7419 my $worksheet2 = $workbook->add_worksheet( 'Collapsed Rows 1' );
7420 my $worksheet3 = $workbook->add_worksheet( 'Collapsed Rows 2' );
7421 my $worksheet4 = $workbook->add_worksheet( 'Collapsed Rows 3' );
7422 my $worksheet5 = $workbook->add_worksheet( 'Outline Columns' );
7423 my $worksheet6 = $workbook->add_worksheet( 'Collapsed Columns' );
7424
7425
7426 # Add a general format
7427 my $bold = $workbook->add_format( bold => 1 );
7428
7429
7430 #
7431 # This function will generate the same data and sub-totals on each worksheet.
7432 #
7433 sub create_sub_totals {
7434
7435 my $worksheet = $_[0];
7436
7437 # Add a column format for clarity
7438 $worksheet->set_column( 'A:A', 20 );
7439
7440 # Add the data, labels and formulas
7441 $worksheet->write( 'A1', 'Region', $bold );
7442 $worksheet->write( 'A2', 'North' );
7443 $worksheet->write( 'A3', 'North' );
7444 $worksheet->write( 'A4', 'North' );
7445 $worksheet->write( 'A5', 'North' );
7446 $worksheet->write( 'A6', 'North Total', $bold );
7447
7448 $worksheet->write( 'B1', 'Sales', $bold );
7449 $worksheet->write( 'B2', 1000 );
7450 $worksheet->write( 'B3', 1200 );
7451 $worksheet->write( 'B4', 900 );
7452 $worksheet->write( 'B5', 1200 );
7453 $worksheet->write( 'B6', '=SUBTOTAL(9,B2:B5)', $bold );
7454
7455 $worksheet->write( 'A7', 'South' );
7456 $worksheet->write( 'A8', 'South' );
7457 $worksheet->write( 'A9', 'South' );
7458 $worksheet->write( 'A10', 'South' );
7459 $worksheet->write( 'A11', 'South Total', $bold );
7460
7461 $worksheet->write( 'B7', 400 );
7462 $worksheet->write( 'B8', 600 );
7463 $worksheet->write( 'B9', 500 );
7464 $worksheet->write( 'B10', 600 );
7465 $worksheet->write( 'B11', '=SUBTOTAL(9,B7:B10)', $bold );
7466
7467 $worksheet->write( 'A12', 'Grand Total', $bold );
7468 $worksheet->write( 'B12', '=SUBTOTAL(9,B2:B10)', $bold );
7469
7470 }
7471
7472
7473 ###############################################################################
7474 #
7475 # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
7476 # functions so that it looks like the type of automatic outlines that are
7477 # generated when you use the Excel Data->SubTotals menu item.
7478 #
7479
7480 # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
7481 $worksheet1->set_row( 1, undef, undef, 0, 2 );
7482 $worksheet1->set_row( 2, undef, undef, 0, 2 );
7483 $worksheet1->set_row( 3, undef, undef, 0, 2 );
7484 $worksheet1->set_row( 4, undef, undef, 0, 2 );
7485 $worksheet1->set_row( 5, undef, undef, 0, 1 );
7486
7487 $worksheet1->set_row( 6, undef, undef, 0, 2 );
7488 $worksheet1->set_row( 7, undef, undef, 0, 2 );
7489 $worksheet1->set_row( 8, undef, undef, 0, 2 );
7490 $worksheet1->set_row( 9, undef, undef, 0, 2 );
7491 $worksheet1->set_row( 10, undef, undef, 0, 1 );
7492
7493 # Write the sub-total data that is common to the row examples.
7494 create_sub_totals( $worksheet1 );
7495
7496
7497 ###############################################################################
7498 #
7499 # Example 2: Create a worksheet with collapsed outlined rows.
7500 # This is the same as the example 1 except that the all rows are collapsed.
7501 # Note: We need to indicate the row that contains the collapsed symbol '+' with
7502 # the optional parameter, $collapsed.
7503
7504 $worksheet2->set_row( 1, undef, undef, 1, 2 );
7505 $worksheet2->set_row( 2, undef, undef, 1, 2 );
7506 $worksheet2->set_row( 3, undef, undef, 1, 2 );
7507 $worksheet2->set_row( 4, undef, undef, 1, 2 );
7508 $worksheet2->set_row( 5, undef, undef, 1, 1 );
7509
7510 $worksheet2->set_row( 6, undef, undef, 1, 2 );
7511 $worksheet2->set_row( 7, undef, undef, 1, 2 );
7512 $worksheet2->set_row( 8, undef, undef, 1, 2 );
7513 $worksheet2->set_row( 9, undef, undef, 1, 2 );
7514 $worksheet2->set_row( 10, undef, undef, 1, 1 );
7515
7516 $worksheet2->set_row( 11, undef, undef, 0, 0, 1 );
7517
7518 # Write the sub-total data that is common to the row examples.
7519 create_sub_totals( $worksheet2 );
7520
7521
7522 ###############################################################################
7523 #
7524 # Example 3: Create a worksheet with collapsed outlined rows.
7525 # Same as the example 1 except that the two sub-totals are collapsed.
7526
7527 $worksheet3->set_row( 1, undef, undef, 1, 2 );
7528 $worksheet3->set_row( 2, undef, undef, 1, 2 );
7529 $worksheet3->set_row( 3, undef, undef, 1, 2 );
7530 $worksheet3->set_row( 4, undef, undef, 1, 2 );
7531 $worksheet3->set_row( 5, undef, undef, 0, 1, 1 );
7532
7533 $worksheet3->set_row( 6, undef, undef, 1, 2 );
7534 $worksheet3->set_row( 7, undef, undef, 1, 2 );
7535 $worksheet3->set_row( 8, undef, undef, 1, 2 );
7536 $worksheet3->set_row( 9, undef, undef, 1, 2 );
7537 $worksheet3->set_row( 10, undef, undef, 0, 1, 1 );
7538
7539
7540 # Write the sub-total data that is common to the row examples.
7541 create_sub_totals( $worksheet3 );
7542
7543
7544 ###############################################################################
7545 #
7546 # Example 4: Create a worksheet with outlined rows.
7547 # Same as the example 1 except that the two sub-totals are collapsed.
7548
7549 $worksheet4->set_row( 1, undef, undef, 1, 2 );
7550 $worksheet4->set_row( 2, undef, undef, 1, 2 );
7551 $worksheet4->set_row( 3, undef, undef, 1, 2 );
7552 $worksheet4->set_row( 4, undef, undef, 1, 2 );
7553 $worksheet4->set_row( 5, undef, undef, 1, 1, 1 );
7554
7555 $worksheet4->set_row( 6, undef, undef, 1, 2 );
7556 $worksheet4->set_row( 7, undef, undef, 1, 2 );
7557 $worksheet4->set_row( 8, undef, undef, 1, 2 );
7558 $worksheet4->set_row( 9, undef, undef, 1, 2 );
7559 $worksheet4->set_row( 10, undef, undef, 1, 1, 1 );
7560
7561 $worksheet4->set_row( 11, undef, undef, 0, 0, 1 );
7562
7563 # Write the sub-total data that is common to the row examples.
7564 create_sub_totals( $worksheet4 );
7565
7566
7567 ###############################################################################
7568 #
7569 # Example 5: Create a worksheet with outlined columns.
7570 #
7571 my $data = [
7572 [ 'Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Total' ],
7573 [ 'North', 50, 20, 15, 25, 65, 80,, '=SUM(B2:G2)' ],
7574 [ 'South', 10, 20, 30, 50, 50, 50,, '=SUM(B3:G3)' ],
7575 [ 'East', 45, 75, 50, 15, 75, 100,, '=SUM(B4:G4)' ],
7576 [ 'West', 15, 15, 55, 35, 20, 50,, '=SUM(B5:G6)' ],
7577 ];
7578
7579 # Add bold format to the first row
7580 $worksheet5->set_row( 0, undef, $bold );
7581
7582 # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
7583 $worksheet5->set_column( 'A:A', 10, $bold );
7584 $worksheet5->set_column( 'B:G', 5, undef, 0, 1 );
7585 $worksheet5->set_column( 'H:H', 10 );
7586
7587 # Write the data and a formula
7588 $worksheet5->write_col( 'A1', $data );
7589 $worksheet5->write( 'H6', '=SUM(H2:H5)', $bold );
7590
7591
7592 ###############################################################################
7593 #
7594 # Example 6: Create a worksheet with collapsed outlined columns.
7595 # This is the same as the previous example except collapsed columns.
7596
7597 # Add bold format to the first row
7598 $worksheet6->set_row( 0, undef, $bold );
7599
7600 # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
7601 $worksheet6->set_column( 'A:A', 10, $bold );
7602 $worksheet6->set_column( 'B:G', 5, undef, 1, 1 );
7603 $worksheet6->set_column( 'H:H', 10, undef, 0, 0, 1 );
7604
7605 # Write the data and a formula
7606 $worksheet6->write_col( 'A1', $data );
7607 $worksheet6->write( 'H6', '=SUM(H2:H5)', $bold );
7608
7609 $workbook->close();
7610
7611 __END__
7612
7613 Download this example:
7614 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/outline_collapsed.pl>
7615
7616 Example: panes.pl
7617 Example of using the Excel::Writer::XLSX module to create worksheet
7618 panes.
7619
7620 Source code for this example:
7621
7622 #!/usr/bin/perl
7623
7624 #######################################################################
7625 #
7626 # Example of using the Excel::Writer::XLSX module to create worksheet panes.
7627 #
7628 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7629 #
7630
7631 use strict;
7632 use warnings;
7633 use Excel::Writer::XLSX;
7634
7635 my $workbook = Excel::Writer::XLSX->new( 'panes.xlsx' );
7636
7637 my $worksheet1 = $workbook->add_worksheet( 'Panes 1' );
7638 my $worksheet2 = $workbook->add_worksheet( 'Panes 2' );
7639 my $worksheet3 = $workbook->add_worksheet( 'Panes 3' );
7640 my $worksheet4 = $workbook->add_worksheet( 'Panes 4' );
7641
7642 # Freeze panes
7643 $worksheet1->freeze_panes( 1, 0 ); # 1 row
7644
7645 $worksheet2->freeze_panes( 0, 1 ); # 1 column
7646 $worksheet3->freeze_panes( 1, 1 ); # 1 row and column
7647
7648 # Split panes.
7649 # The divisions must be specified in terms of row and column dimensions.
7650 # The default row height is 15 and the default column width is 8.43
7651 #
7652 $worksheet4->split_panes( 15, 8.43 ); # 1 row and column
7653
7654
7655 #######################################################################
7656 #
7657 # Set up some formatting and text to highlight the panes
7658 #
7659
7660 my $header = $workbook->add_format(
7661 align => 'center',
7662 valign => 'vcenter',
7663 fg_color => '#C3FFC0',
7664 );
7665
7666 my $center = $workbook->add_format( align => 'center' );
7667
7668
7669 #######################################################################
7670 #
7671 # Sheet 1
7672 #
7673
7674 $worksheet1->set_column( 'A:I', 16 );
7675 $worksheet1->set_row( 0, 20 );
7676 $worksheet1->set_selection( 'C3' );
7677
7678 for my $i ( 0 .. 8 ) {
7679 $worksheet1->write( 0, $i, 'Scroll down', $header );
7680 }
7681
7682 for my $i ( 1 .. 100 ) {
7683 for my $j ( 0 .. 8 ) {
7684 $worksheet1->write( $i, $j, $i + 1, $center );
7685 }
7686 }
7687
7688
7689 #######################################################################
7690 #
7691 # Sheet 2
7692 #
7693
7694 $worksheet2->set_column( 'A:A', 16 );
7695 $worksheet2->set_selection( 'C3' );
7696
7697 for my $i ( 0 .. 49 ) {
7698 $worksheet2->set_row( $i, 15 );
7699 $worksheet2->write( $i, 0, 'Scroll right', $header );
7700 }
7701
7702 for my $i ( 0 .. 49 ) {
7703 for my $j ( 1 .. 25 ) {
7704 $worksheet2->write( $i, $j, $j, $center );
7705 }
7706 }
7707
7708
7709 #######################################################################
7710 #
7711 # Sheet 3
7712 #
7713
7714 $worksheet3->set_column( 'A:Z', 16 );
7715 $worksheet3->set_selection( 'C3' );
7716
7717 $worksheet3->write( 0, 0, '', $header );
7718
7719 for my $i ( 1 .. 25 ) {
7720 $worksheet3->write( 0, $i, 'Scroll down', $header );
7721 }
7722
7723 for my $i ( 1 .. 49 ) {
7724 $worksheet3->write( $i, 0, 'Scroll right', $header );
7725 }
7726
7727 for my $i ( 1 .. 49 ) {
7728 for my $j ( 1 .. 25 ) {
7729 $worksheet3->write( $i, $j, $j, $center );
7730 }
7731 }
7732
7733
7734 #######################################################################
7735 #
7736 # Sheet 4
7737 #
7738
7739 $worksheet4->set_selection( 'C3' );
7740
7741 for my $i ( 1 .. 25 ) {
7742 $worksheet4->write( 0, $i, 'Scroll', $center );
7743 }
7744
7745 for my $i ( 1 .. 49 ) {
7746 $worksheet4->write( $i, 0, 'Scroll', $center );
7747 }
7748
7749 for my $i ( 1 .. 49 ) {
7750 for my $j ( 1 .. 25 ) {
7751 $worksheet4->write( $i, $j, $j, $center );
7752 }
7753 }
7754
7755 $workbook->close();
7756
7757 __END__
7758
7759 Download this example:
7760 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/panes.pl>
7761
7762 Example: properties.pl
7763 An example of adding document properties to a Excel::Writer::XLSX file.
7764
7765 Source code for this example:
7766
7767 #!/usr/bin/perl
7768
7769 ##############################################################################
7770 #
7771 # An example of adding document properties to a Excel::Writer::XLSX file.
7772 #
7773 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7774 #
7775
7776 use strict;
7777 use warnings;
7778 use Excel::Writer::XLSX;
7779
7780 my $workbook = Excel::Writer::XLSX->new( 'properties.xlsx' );
7781 my $worksheet = $workbook->add_worksheet();
7782
7783
7784 $workbook->set_properties(
7785 title => 'This is an example spreadsheet',
7786 subject => 'With document properties',
7787 author => 'John McNamara',
7788 manager => 'Dr. Heinz Doofenshmirtz',
7789 company => 'of Wolves',
7790 category => 'Example spreadsheets',
7791 keywords => 'Sample, Example, Properties',
7792 comments => 'Created with Perl and Excel::Writer::XLSX',
7793 status => 'Quo',
7794 );
7795
7796
7797 $worksheet->set_column( 'A:A', 70 );
7798 $worksheet->write( 'A1', qq{Select 'Office Button -> Prepare -> Properties' to see the file properties.} );
7799
7800 $workbook->close();
7801
7802 __END__
7803
7804 Download this example:
7805 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/properties.pl>
7806
7807 Example: protection.pl
7808 Example of cell locking and formula hiding in an Excel worksheet via
7809 the Excel::Writer::XLSX module.
7810
7811 Source code for this example:
7812
7813 #!/usr/bin/perl
7814
7815 ########################################################################
7816 #
7817 # Example of cell locking and formula hiding in an Excel worksheet via
7818 # the Excel::Writer::XLSX module.
7819 #
7820 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7821 #
7822
7823 use strict;
7824 use warnings;
7825 use Excel::Writer::XLSX;
7826
7827 my $workbook = Excel::Writer::XLSX->new( 'protection.xlsx' );
7828 my $worksheet = $workbook->add_worksheet();
7829
7830 # Create some format objects
7831 my $unlocked = $workbook->add_format( locked => 0 );
7832 my $hidden = $workbook->add_format( hidden => 1 );
7833
7834 # Format the columns
7835 $worksheet->set_column( 'A:A', 45 );
7836 $worksheet->set_selection( 'B3' );
7837
7838 # Protect the worksheet
7839 $worksheet->protect();
7840
7841 # Examples of cell locking and hiding.
7842 $worksheet->write( 'A1', 'Cell B1 is locked. It cannot be edited.' );
7843 $worksheet->write_formula( 'B1', '=1+2', undef, 3 ); # Locked by default.
7844
7845 $worksheet->write( 'A2', 'Cell B2 is unlocked. It can be edited.' );
7846 $worksheet->write_formula( 'B2', '=1+2', $unlocked, 3 );
7847
7848 $worksheet->write( 'A3', "Cell B3 is hidden. The formula isn't visible." );
7849 $worksheet->write_formula( 'B3', '=1+2', $hidden, 3 );
7850
7851 $worksheet->write( 'A5', 'Use Menu->Tools->Protection->Unprotect Sheet' );
7852 $worksheet->write( 'A6', 'to remove the worksheet protection.' );
7853
7854 $workbook->close();
7855
7856 __END__
7857
7858 Download this example:
7859 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/protection.pl>
7860
7861 Example: rich_strings.pl
7862 An Excel::Writer::XLSX example showing how to use "rich strings", i.e.,
7863 strings with multiple formatting.
7864
7865 Source code for this example:
7866
7867 #!/usr/bin/perl
7868
7869 #######################################################################
7870 #
7871 # An Excel::Writer::XLSX example showing how to use "rich strings", i.e.,
7872 # strings with multiple formatting.
7873 #
7874 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7875 #
7876
7877 use strict;
7878 use warnings;
7879 use Excel::Writer::XLSX;
7880
7881 my $workbook = Excel::Writer::XLSX->new( 'rich_strings.xlsx' );
7882 my $worksheet = $workbook->add_worksheet();
7883
7884 $worksheet->set_column( 'A:A', 30 );
7885
7886 # Set some formats to use.
7887 my $bold = $workbook->add_format( bold => 1 );
7888 my $italic = $workbook->add_format( italic => 1 );
7889 my $red = $workbook->add_format( color => 'red' );
7890 my $blue = $workbook->add_format( color => 'blue' );
7891 my $center = $workbook->add_format( align => 'center' );
7892 my $super = $workbook->add_format( font_script => 1 );
7893
7894
7895 # Write some strings with multiple formats.
7896 $worksheet->write_rich_string( 'A1',
7897 'This is ', $bold, 'bold', ' and this is ', $italic, 'italic' );
7898
7899 $worksheet->write_rich_string( 'A3',
7900 'This is ', $red, 'red', ' and this is ', $blue, 'blue' );
7901
7902 $worksheet->write_rich_string( 'A5',
7903 'Some ', $bold, 'bold text', ' centered', $center );
7904
7905 $worksheet->write_rich_string( 'A7',
7906 $italic, 'j = k', $super, '(n-1)', $center );
7907
7908 $workbook->close();
7909
7910 __END__
7911
7912 Download this example:
7913 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/rich_strings.pl>
7914
7915 Example: right_to_left.pl
7916 Example of how to change the default worksheet direction from left-to-
7917 right to right-to-left as required by some eastern verions of Excel.
7918
7919 Source code for this example:
7920
7921 #!/usr/bin/perl
7922
7923 #######################################################################
7924 #
7925 # Example of how to change the default worksheet direction from
7926 # left-to-right to right-to-left as required by some eastern verions
7927 # of Excel.
7928 #
7929 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7930 #
7931
7932 use strict;
7933 use warnings;
7934 use Excel::Writer::XLSX;
7935
7936 my $workbook = Excel::Writer::XLSX->new( 'right_to_left.xlsx' );
7937 my $worksheet1 = $workbook->add_worksheet();
7938 my $worksheet2 = $workbook->add_worksheet();
7939
7940 $worksheet2->right_to_left();
7941
7942 $worksheet1->write( 0, 0, 'Hello' ); # A1, B1, C1, ...
7943 $worksheet2->write( 0, 0, 'Hello' ); # ..., C1, B1, A1
7944
7945 $workbook->close();
7946
7947 __END__
7948
7949 Download this example:
7950 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/right_to_left.pl>
7951
7952 Example: sales.pl
7953 Example of a sales worksheet to demonstrate several different features.
7954 Also uses functions from the Excel::Writer::XLSX::Utility module.
7955
7956 Source code for this example:
7957
7958 #!/usr/bin/perl -w
7959
7960 ###############################################################################
7961 #
7962 # Example of a sales worksheet to demonstrate several different features.
7963 # Also uses functions from the L<Excel::Writer::XLSX::Utility> module.
7964 #
7965 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7966 #
7967
7968 use strict;
7969 use Excel::Writer::XLSX;
7970 use Excel::Writer::XLSX::Utility;
7971
7972 # Create a new workbook and add a worksheet
7973 my $workbook = Excel::Writer::XLSX->new( 'sales.xlsx' );
7974 my $worksheet = $workbook->add_worksheet( 'May Sales' );
7975
7976
7977 # Set up some formats
7978 my %heading = (
7979 bold => 1,
7980 pattern => 1,
7981 fg_color => '#C3FFC0',
7982 border => 1,
7983 align => 'center',
7984 );
7985
7986 my %total = (
7987 bold => 1,
7988 top => 1,
7989 num_format => '$#,##0.00'
7990 );
7991
7992 my $heading = $workbook->add_format( %heading );
7993 my $total_format = $workbook->add_format( %total );
7994 my $price_format = $workbook->add_format( num_format => '$#,##0.00' );
7995 my $date_format = $workbook->add_format( num_format => 'mmm d yyy' );
7996
7997
7998 # Write the main headings
7999 $worksheet->freeze_panes( 1 ); # Freeze the first row
8000 $worksheet->write( 'A1', 'Item', $heading );
8001 $worksheet->write( 'B1', 'Quantity', $heading );
8002 $worksheet->write( 'C1', 'Price', $heading );
8003 $worksheet->write( 'D1', 'Total', $heading );
8004 $worksheet->write( 'E1', 'Date', $heading );
8005
8006 # Set the column widths
8007 $worksheet->set_column( 'A:A', 25 );
8008 $worksheet->set_column( 'B:B', 10 );
8009 $worksheet->set_column( 'C:E', 16 );
8010
8011
8012 # Extract the sales data from the __DATA__ section at the end of the file.
8013 # In reality this information would probably come from a database
8014 my @sales;
8015
8016 foreach my $line ( <DATA> ) {
8017 chomp $line;
8018 next if $line eq '';
8019
8020 # Simple-minded processing of CSV data. Refer to the Text::CSV_XS
8021 # and Text::xSV modules for a more complete CSV handling.
8022 my @items = split /,/, $line;
8023 push @sales, \@items;
8024 }
8025
8026
8027 # Write out the items from each row
8028 my $row = 1;
8029 foreach my $sale ( @sales ) {
8030
8031 $worksheet->write( $row, 0, @$sale[0] );
8032 $worksheet->write( $row, 1, @$sale[1] );
8033 $worksheet->write( $row, 2, @$sale[2], $price_format );
8034
8035 # Create a formula like '=B2*C2'
8036 my $formula =
8037 '=' . xl_rowcol_to_cell( $row, 1 ) . "*" . xl_rowcol_to_cell( $row, 2 );
8038
8039 $worksheet->write( $row, 3, $formula, $price_format );
8040
8041 # Parse the date
8042 my $date = xl_decode_date_US( @$sale[3] );
8043 $worksheet->write( $row, 4, $date, $date_format );
8044 $row++;
8045 }
8046
8047 # Create a formula to sum the totals, like '=SUM(D2:D6)'
8048 my $total = '=SUM(D2:' . xl_rowcol_to_cell( $row - 1, 3 ) . ")";
8049
8050 $worksheet->write( $row, 3, $total, $total_format );
8051
8052 $workbook->close();
8053
8054 __DATA__
8055 586 card,20,125.50,5/12/01
8056 Flat Screen Monitor,1,1300.00,5/12/01
8057 64 MB dimms,45,49.99,5/13/01
8058 15 GB HD,12,300.00,5/13/01
8059 Speakers (pair),5,15.50,5/14/01
8060
8061 Download this example:
8062 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/sales.pl>
8063
8064 Example: shape1.pl
8065 A simple example of how to use the Excel::Writer::XLSX module to add
8066 shapes to an Excel xlsx file.
8067
8068 Source code for this example:
8069
8070 #!/usr/bin/perl
8071
8072 #######################################################################
8073 #
8074 # A simple example of how to use the Excel::Writer::XLSX module to
8075 # add shapes to an Excel xlsx file.
8076 #
8077 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8078 #
8079
8080 use strict;
8081 use warnings;
8082 use Excel::Writer::XLSX;
8083
8084 my $workbook = Excel::Writer::XLSX->new( 'shape1.xlsx' );
8085 my $worksheet = $workbook->add_worksheet();
8086
8087 # Add a circle, with centered text.
8088 my $ellipse = $workbook->add_shape(
8089 type => 'ellipse',
8090 text => "Hello\nWorld",
8091 width => 60,
8092 height => 60
8093 );
8094
8095 $worksheet->insert_shape( 'A1', $ellipse, 50, 50 );
8096
8097 # Add a plus sign.
8098 my $plus = $workbook->add_shape( type => 'plus', width => 20, height => 20 );
8099 $worksheet->insert_shape( 'D8', $plus );
8100
8101 $workbook->close();
8102
8103 __END__
8104
8105 Download this example:
8106 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape1.pl>
8107
8108 Example: shape2.pl
8109 A simple example of how to use the Excel::Writer::XLSX module to modify
8110 shape properties in an Excel xlsx file.
8111
8112 Source code for this example:
8113
8114 #!/usr/bin/perl
8115
8116 #######################################################################
8117 #
8118 # A simple example of how to use the Excel::Writer::XLSX module to
8119 # modify shape properties in an Excel xlsx file.
8120 #
8121 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8122 #
8123
8124 use strict;
8125 use warnings;
8126 use Excel::Writer::XLSX;
8127
8128 my $workbook = Excel::Writer::XLSX->new( 'shape2.xlsx' );
8129 my $worksheet = $workbook->add_worksheet();
8130
8131 $worksheet->hide_gridlines( 2 );
8132
8133 my $plain = $workbook->add_shape(
8134 type => 'smileyFace',
8135 text => "Plain",
8136 width => 100,
8137 height => 100,
8138 );
8139
8140 my $bbformat = $workbook->add_format(
8141 color => 'red',
8142 font => 'Lucida Calligraphy',
8143 );
8144
8145 $bbformat->set_bold();
8146 $bbformat->set_underline();
8147 $bbformat->set_italic();
8148
8149 my $decor = $workbook->add_shape(
8150 type => 'smileyFace',
8151 text => "Decorated",
8152 rotation => 45,
8153 width => 200,
8154 height => 100,
8155 format => $bbformat,
8156 line_type => 'sysDot',
8157 line_weight => 3,
8158 fill => 'FFFF00',
8159 line => '3366FF',
8160 );
8161
8162 $worksheet->insert_shape( 'A1', $plain, 50, 50 );
8163 $worksheet->insert_shape( 'A1', $decor, 250, 50 );
8164
8165 $workbook->close();
8166
8167 __END__
8168
8169 Download this example:
8170 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape2.pl>
8171
8172 Example: shape3.pl
8173 A simple example of how to use the Excel::Writer::XLSX module to scale
8174 shapes in an Excel xlsx file.
8175
8176 Source code for this example:
8177
8178 #!/usr/bin/perl
8179
8180 #######################################################################
8181 #
8182 # A simple example of how to use the Excel::Writer::XLSX module to
8183 # scale shapes in an Excel xlsx file.
8184 #
8185 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8186 #
8187
8188 use strict;
8189 use warnings;
8190 use Excel::Writer::XLSX;
8191
8192 my $workbook = Excel::Writer::XLSX->new( 'shape3.xlsx' );
8193 my $worksheet = $workbook->add_worksheet();
8194
8195 my $normal = $workbook->add_shape(
8196 name => 'chip',
8197 type => 'diamond',
8198 text => "Normal",
8199 width => 100,
8200 height => 100,
8201 );
8202
8203 $worksheet->insert_shape( 'A1', $normal, 50, 50 );
8204 $normal->set_text( 'Scaled 3w x 2h' );
8205 $normal->set_name( 'Hope' );
8206 $worksheet->insert_shape( 'A1', $normal, 250, 50, 3, 2 );
8207
8208 $workbook->close();
8209
8210 __END__
8211
8212 Download this example:
8213 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape3.pl>
8214
8215 Example: shape4.pl
8216 A simple example of how to use the Excel::Writer::XLSX module to
8217 demonstrate stenciling in an Excel xlsx file.
8218
8219 Source code for this example:
8220
8221 #!/usr/bin/perl
8222
8223 #######################################################################
8224 #
8225 # A simple example of how to use the Excel::Writer::XLSX module to
8226 # demonstrate stenciling in an Excel xlsx file.
8227 #
8228 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8229 #
8230
8231 use strict;
8232 use warnings;
8233 use Excel::Writer::XLSX;
8234
8235 my $workbook = Excel::Writer::XLSX->new( 'shape4.xlsx' );
8236 my $worksheet = $workbook->add_worksheet();
8237
8238 $worksheet->hide_gridlines( 2 );
8239
8240 my $type = 'rect';
8241 my $shape = $workbook->add_shape(
8242 type => $type,
8243 width => 90,
8244 height => 90,
8245 );
8246
8247 for my $n ( 1 .. 10 ) {
8248
8249 # Change the last 5 rectangles to stars. Previously inserted shapes stay
8250 # as rectangles.
8251 $type = 'star5' if $n == 6;
8252 $shape->set_type( $type );
8253 $shape->set_text( "$type $n" );
8254 $worksheet->insert_shape( 'A1', $shape, $n * 100, 50 );
8255 }
8256
8257
8258 my $stencil = $workbook->add_shape(
8259 stencil => 1, # The default.
8260 width => 90,
8261 height => 90,
8262 text => 'started as a box',
8263 );
8264 $worksheet->insert_shape( 'A1', $stencil, 100, 150 );
8265
8266 $stencil->set_stencil( 0 );
8267 $worksheet->insert_shape( 'A1', $stencil, 200, 150 );
8268 $worksheet->insert_shape( 'A1', $stencil, 300, 150 );
8269
8270 # Ooops! Changed my mind. Change the rectangle to an ellipse (circle),
8271 # for the last two shapes.
8272 $stencil->set_type( 'ellipse' );
8273 $stencil->set_text( 'Now its a circle' );
8274
8275 $workbook->close();
8276
8277 __END__
8278
8279 Download this example:
8280 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape4.pl>
8281
8282 Example: shape5.pl
8283 A simple example of how to use the Excel::Writer::XLSX module to add
8284 shapes (objects and top/bottom connectors) to an Excel xlsx file.
8285
8286 Source code for this example:
8287
8288 #!/usr/bin/perl
8289
8290 #######################################################################
8291 #
8292 # A simple example of how to use the Excel::Writer::XLSX module to
8293 # add shapes (objects and top/bottom connectors) to an Excel xlsx file.
8294 #
8295 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8296 #
8297
8298 use strict;
8299 use warnings;
8300 use Excel::Writer::XLSX;
8301
8302 my $workbook = Excel::Writer::XLSX->new( 'shape5.xlsx' );
8303 my $worksheet = $workbook->add_worksheet();
8304
8305 my $s1 = $workbook->add_shape( type => 'ellipse', width => 60, height => 60 );
8306 $worksheet->insert_shape( 'A1', $s1, 50, 50 );
8307
8308 my $s2 = $workbook->add_shape( type => 'plus', width => 20, height => 20 );
8309 $worksheet->insert_shape( 'A1', $s2, 250, 200 );
8310
8311 # Create a connector to link the two shapes.
8312 my $cxn_shape = $workbook->add_shape( type => 'bentConnector3' );
8313
8314 # Link the start of the connector to the right side.
8315 $cxn_shape->set_start( $s1->get_id() );
8316 $cxn_shape->set_start_index( 4 ); # 4th connection pt, clockwise from top(0).
8317 $cxn_shape->set_start_side( 'b' ); # r)ight or b)ottom.
8318
8319 # Link the end of the connector to the left side.
8320 $cxn_shape->set_end( $s2->get_id() );
8321 $cxn_shape->set_end_index( 0 ); # clockwise from top(0).
8322 $cxn_shape->set_end_side( 't' ); # t)top.
8323
8324 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8325
8326 $workbook->close();
8327
8328 __END__
8329
8330 Download this example:
8331 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape5.pl>
8332
8333 Example: shape6.pl
8334 A simple example of how to use the Excel::Writer::XLSX module to add
8335 shapes (objects and right/left connectors) to an Excel xlsx file.
8336
8337 Source code for this example:
8338
8339 #!/usr/bin/perl
8340
8341 #######################################################################
8342 #
8343 # A simple example of how to use the Excel::Writer::XLSX module to
8344 # add shapes (objects and right/left connectors) to an Excel xlsx file.
8345 #
8346 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8347 #
8348
8349 use strict;
8350 use warnings;
8351 use Excel::Writer::XLSX;
8352
8353 my $workbook = Excel::Writer::XLSX->new( 'shape6.xlsx' );
8354 my $worksheet = $workbook->add_worksheet();
8355
8356 my $s1 = $workbook->add_shape( type => 'chevron', width => 60, height => 60 );
8357 $worksheet->insert_shape( 'A1', $s1, 50, 50 );
8358
8359 my $s2 = $workbook->add_shape( type => 'pentagon', width => 20, height => 20 );
8360 $worksheet->insert_shape( 'A1', $s2, 250, 200 );
8361
8362 # Create a connector to link the two shapes.
8363 my $cxn_shape = $workbook->add_shape( type => 'curvedConnector3' );
8364
8365 # Link the start of the connector to the right side.
8366 $cxn_shape->set_start( $s1->get_id() );
8367 $cxn_shape->set_start_index( 2 ); # 2nd connection pt, clockwise from top(0).
8368 $cxn_shape->set_start_side( 'r' ); # r)ight or b)ottom.
8369
8370 # Link the end of the connector to the left side.
8371 $cxn_shape->set_end( $s2->get_id() );
8372 $cxn_shape->set_end_index( 4 ); # 4th connection pt, clockwise from top(0).
8373 $cxn_shape->set_end_side( 'l' ); # l)eft or t)op.
8374
8375 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8376
8377 $workbook->close();
8378
8379 __END__
8380
8381 Download this example:
8382 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape6.pl>
8383
8384 Example: shape7.pl
8385 A simple example of how to use the Excel::Writer::XLSX module to add
8386 shapes and one-to-many connectors to an Excel xlsx file.
8387
8388 Source code for this example:
8389
8390 #!/usr/bin/perl
8391
8392 #######################################################################
8393 #
8394 # A simple example of how to use the Excel::Writer::XLSX module to
8395 # add shapes and one-to-many connectors to an Excel xlsx file.
8396 #
8397 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8398 #
8399
8400 use strict;
8401 use warnings;
8402 use Excel::Writer::XLSX;
8403
8404 my $workbook = Excel::Writer::XLSX->new( 'shape7.xlsx' );
8405 my $worksheet = $workbook->add_worksheet();
8406
8407 # Add a circle, with centered text. c is for circle, not center.
8408 my $cw = 60;
8409 my $ch = 60;
8410 my $cx = 210;
8411 my $cy = 190;
8412
8413 my $ellipse = $workbook->add_shape(
8414 type => 'ellipse',
8415 id => 2,
8416 text => "Hello\nWorld",
8417 width => $cw,
8418 height => $ch
8419 );
8420 $worksheet->insert_shape( 'A1', $ellipse, $cx, $cy );
8421
8422 # Add a plus sign at 4 different positions around the circle.
8423 my $pw = 20;
8424 my $ph = 20;
8425 my $px = 120;
8426 my $py = 250;
8427 my $plus =
8428 $workbook->add_shape( type => 'plus', id => 3, width => $pw, height => $ph );
8429 my $p1 = $worksheet->insert_shape( 'A1', $plus, 350, 350 );
8430 my $p2 = $worksheet->insert_shape( 'A1', $plus, 150, 350 );
8431 my $p3 = $worksheet->insert_shape( 'A1', $plus, 350, 150 );
8432 $plus->set_adjustments( 35 ); # change shape of plus symbol.
8433 my $p4 = $worksheet->insert_shape( 'A1', $plus, 150, 150 );
8434
8435 my $cxn_shape = $workbook->add_shape( type => 'bentConnector3', fill => 0 );
8436
8437 $cxn_shape->set_start( $ellipse->get_id() );
8438 $cxn_shape->set_start_index( 4 ); # 4nd connection pt, clockwise from top(0).
8439 $cxn_shape->set_start_side( 'b' ); # r)ight or b)ottom.
8440
8441 $cxn_shape->set_end( $p1->get_id() );
8442 $cxn_shape->set_end_index( 0 );
8443 $cxn_shape->set_end_side( 't' );
8444 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8445
8446 $cxn_shape->set_end( $p2->get_id() );
8447 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8448
8449 $cxn_shape->set_end( $p3->get_id() );
8450 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8451
8452 $cxn_shape->set_end( $p4->get_id() );
8453 $cxn_shape->set_adjustments( -50, 45, 120 );
8454 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8455
8456 $workbook->close();
8457
8458 __END__
8459
8460 Download this example:
8461 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape7.pl>
8462
8463 Example: shape8.pl
8464 A simple example of how to use the Excel::Writer::XLSX module to add
8465 shapes and one-to-many connectors to an Excel xlsx file.
8466
8467 Source code for this example:
8468
8469 #!/usr/bin/perl
8470
8471 #######################################################################
8472 #
8473 # A simple example of how to use the Excel::Writer::XLSX module to
8474 # add shapes and one-to-many connectors to an Excel xlsx file.
8475 #
8476 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8477 #
8478
8479 use strict;
8480 use warnings;
8481 use Excel::Writer::XLSX;
8482
8483 my $workbook = Excel::Writer::XLSX->new( 'shape8.xlsx' );
8484 my $worksheet = $workbook->add_worksheet();
8485
8486 # Add a circle, with centered text. c is for circle, not center.
8487 my $cw = 60;
8488 my $ch = 60;
8489 my $cx = 210;
8490 my $cy = 190;
8491
8492 my $ellipse = $workbook->add_shape(
8493 type => 'ellipse',
8494 id => 2,
8495 text => "Hello\nWorld",
8496 width => $cw,
8497 height => $ch
8498 );
8499 $worksheet->insert_shape( 'A1', $ellipse, $cx, $cy );
8500
8501 # Add a plus sign at 4 different positions around the circle.
8502 my $pw = 20;
8503 my $ph = 20;
8504 my $px = 120;
8505 my $py = 250;
8506 my $plus =
8507 $workbook->add_shape( type => 'plus', id => 3, width => $pw, height => $ph );
8508 my $p1 = $worksheet->insert_shape( 'A1', $plus, 350, 150 ); # 2:00
8509 my $p2 = $worksheet->insert_shape( 'A1', $plus, 350, 350 ); # 4:00
8510 my $p3 = $worksheet->insert_shape( 'A1', $plus, 150, 350 ); # 8:00
8511 my $p4 = $worksheet->insert_shape( 'A1', $plus, 150, 150 ); # 10:00
8512
8513 my $cxn_shape = $workbook->add_shape( type => 'bentConnector3', fill => 0 );
8514
8515 $cxn_shape->set_start( $ellipse->get_id() );
8516 $cxn_shape->set_start_index( 2 ); # 2nd connection pt, clockwise from top(0).
8517 $cxn_shape->set_start_side( 'r' ); # r)ight or b)ottom.
8518
8519 $cxn_shape->set_end( $p1->get_id() );
8520 $cxn_shape->set_end_index( 3 ); # 3rd connection point on plus, right side
8521 $cxn_shape->set_end_side( 'l' );
8522 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8523
8524 $cxn_shape->set_end( $p2->get_id() );
8525 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8526
8527 $cxn_shape->set_end( $p3->get_id() );
8528 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8529
8530 $cxn_shape->set_end( $p4->get_id() );
8531 $cxn_shape->set_adjustments( -50, 45, 120 );
8532 $worksheet->insert_shape( 'A1', $cxn_shape, 0, 0 );
8533
8534 $workbook->close();
8535
8536 __END__
8537
8538 Download this example:
8539 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape8.pl>
8540
8541 Example: shape_all.pl
8542 A simple example of how to use the Excel::Writer::XLSX module to add
8543 all shapes (as currently implemented) to an Excel xlsx file.
8544
8545 The list at the end consists of all the shape types defined as
8546 ST_ShapeType in ECMA-376, Office Open XML File Formats Part 4.
8547
8548 The grouping by worksheet name is for illustration only. It isn't part
8549 of the ECMA-376 standard.
8550
8551 Source code for this example:
8552
8553 #!/usr/bin/perl
8554
8555 #######################################################################
8556 #
8557 # A simple example of how to use the Excel::Writer::XLSX module to
8558 # add all shapes (as currently implemented) to an Excel xlsx file.
8559 #
8560 # The list at the end consists of all the shape types defined as
8561 # ST_ShapeType in ECMA-376, Office Open XML File Formats Part 4.
8562 #
8563 # The grouping by worksheet name is for illustration only. It isn't
8564 # part of the ECMA-376 standard.
8565 #
8566 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8567 #
8568
8569 use strict;
8570 use warnings;
8571 use Excel::Writer::XLSX;
8572
8573 my $workbook = Excel::Writer::XLSX->new( 'shape_all.xlsx' );
8574
8575 my ( $worksheet, $last_sheet, $shape, $r ) = ( 0, '', '', undef, 0 );
8576
8577 while ( <DATA> ) {
8578 chomp;
8579 next unless m/^\w/; # Skip blank lines and comments.
8580
8581 my ( $sheet, $name ) = split( /\t/, $_ );
8582 if ( $last_sheet ne $sheet ) {
8583 $worksheet = $workbook->add_worksheet( $sheet );
8584 $r = 2;
8585 }
8586 $last_sheet = $sheet;
8587 $shape = $workbook->add_shape(
8588 type => $name,
8589 text => $name,
8590 width => 90,
8591 height => 90
8592 );
8593
8594 # Connectors can not have labels, so write the connector name in the cell
8595 # to the left.
8596 $worksheet->write( $r, 0, $name ) if $sheet eq 'Connector';
8597 $worksheet->insert_shape( $r, 2, $shape, 0, 0 );
8598 $r += 5;
8599 }
8600
8601 $workbook->close();
8602
8603 __END__
8604 Action actionButtonBackPrevious
8605 Action actionButtonBeginning
8606 Action actionButtonBlank
8607 Action actionButtonDocument
8608 Action actionButtonEnd
8609 Action actionButtonForwardNext
8610 Action actionButtonHelp
8611 Action actionButtonHome
8612 Action actionButtonInformation
8613 Action actionButtonMovie
8614 Action actionButtonReturn
8615 Action actionButtonSound
8616 Arrow bentArrow
8617 Arrow bentUpArrow
8618 Arrow circularArrow
8619 Arrow curvedDownArrow
8620 Arrow curvedLeftArrow
8621 Arrow curvedRightArrow
8622 Arrow curvedUpArrow
8623 Arrow downArrow
8624 Arrow leftArrow
8625 Arrow leftCircularArrow
8626 Arrow leftRightArrow
8627 Arrow leftRightCircularArrow
8628 Arrow leftRightUpArrow
8629 Arrow leftUpArrow
8630 Arrow notchedRightArrow
8631 Arrow quadArrow
8632 Arrow rightArrow
8633 Arrow stripedRightArrow
8634 Arrow swooshArrow
8635 Arrow upArrow
8636 Arrow upDownArrow
8637 Arrow uturnArrow
8638 Basic blockArc
8639 Basic can
8640 Basic chevron
8641 Basic cube
8642 Basic decagon
8643 Basic diamond
8644 Basic dodecagon
8645 Basic donut
8646 Basic ellipse
8647 Basic funnel
8648 Basic gear6
8649 Basic gear9
8650 Basic heart
8651 Basic heptagon
8652 Basic hexagon
8653 Basic homePlate
8654 Basic lightningBolt
8655 Basic line
8656 Basic lineInv
8657 Basic moon
8658 Basic nonIsoscelesTrapezoid
8659 Basic noSmoking
8660 Basic octagon
8661 Basic parallelogram
8662 Basic pentagon
8663 Basic pie
8664 Basic pieWedge
8665 Basic plaque
8666 Basic rect
8667 Basic round1Rect
8668 Basic round2DiagRect
8669 Basic round2SameRect
8670 Basic roundRect
8671 Basic rtTriangle
8672 Basic smileyFace
8673 Basic snip1Rect
8674 Basic snip2DiagRect
8675 Basic snip2SameRect
8676 Basic snipRoundRect
8677 Basic star10
8678 Basic star12
8679 Basic star16
8680 Basic star24
8681 Basic star32
8682 Basic star4
8683 Basic star5
8684 Basic star6
8685 Basic star7
8686 Basic star8
8687 Basic sun
8688 Basic teardrop
8689 Basic trapezoid
8690 Basic triangle
8691 Callout accentBorderCallout1
8692 Callout accentBorderCallout2
8693 Callout accentBorderCallout3
8694 Callout accentCallout1
8695 Callout accentCallout2
8696 Callout accentCallout3
8697 Callout borderCallout1
8698 Callout borderCallout2
8699 Callout borderCallout3
8700 Callout callout1
8701 Callout callout2
8702 Callout callout3
8703 Callout cloudCallout
8704 Callout downArrowCallout
8705 Callout leftArrowCallout
8706 Callout leftRightArrowCallout
8707 Callout quadArrowCallout
8708 Callout rightArrowCallout
8709 Callout upArrowCallout
8710 Callout upDownArrowCallout
8711 Callout wedgeEllipseCallout
8712 Callout wedgeRectCallout
8713 Callout wedgeRoundRectCallout
8714 Chart chartPlus
8715 Chart chartStar
8716 Chart chartX
8717 Connector bentConnector2
8718 Connector bentConnector3
8719 Connector bentConnector4
8720 Connector bentConnector5
8721 Connector curvedConnector2
8722 Connector curvedConnector3
8723 Connector curvedConnector4
8724 Connector curvedConnector5
8725 Connector straightConnector1
8726 FlowChart flowChartAlternateProcess
8727 FlowChart flowChartCollate
8728 FlowChart flowChartConnector
8729 FlowChart flowChartDecision
8730 FlowChart flowChartDelay
8731 FlowChart flowChartDisplay
8732 FlowChart flowChartDocument
8733 FlowChart flowChartExtract
8734 FlowChart flowChartInputOutput
8735 FlowChart flowChartInternalStorage
8736 FlowChart flowChartMagneticDisk
8737 FlowChart flowChartMagneticDrum
8738 FlowChart flowChartMagneticTape
8739 FlowChart flowChartManualInput
8740 FlowChart flowChartManualOperation
8741 FlowChart flowChartMerge
8742 FlowChart flowChartMultidocument
8743 FlowChart flowChartOfflineStorage
8744 FlowChart flowChartOffpageConnector
8745 FlowChart flowChartOnlineStorage
8746 FlowChart flowChartOr
8747 FlowChart flowChartPredefinedProcess
8748 FlowChart flowChartPreparation
8749 FlowChart flowChartProcess
8750 FlowChart flowChartPunchedCard
8751 FlowChart flowChartPunchedTape
8752 FlowChart flowChartSort
8753 FlowChart flowChartSummingJunction
8754 FlowChart flowChartTerminator
8755 Math mathDivide
8756 Math mathEqual
8757 Math mathMinus
8758 Math mathMultiply
8759 Math mathNotEqual
8760 Math mathPlus
8761 Star_Banner arc
8762 Star_Banner bevel
8763 Star_Banner bracePair
8764 Star_Banner bracketPair
8765 Star_Banner chord
8766 Star_Banner cloud
8767 Star_Banner corner
8768 Star_Banner diagStripe
8769 Star_Banner doubleWave
8770 Star_Banner ellipseRibbon
8771 Star_Banner ellipseRibbon2
8772 Star_Banner foldedCorner
8773 Star_Banner frame
8774 Star_Banner halfFrame
8775 Star_Banner horizontalScroll
8776 Star_Banner irregularSeal1
8777 Star_Banner irregularSeal2
8778 Star_Banner leftBrace
8779 Star_Banner leftBracket
8780 Star_Banner leftRightRibbon
8781 Star_Banner plus
8782 Star_Banner ribbon
8783 Star_Banner ribbon2
8784 Star_Banner rightBrace
8785 Star_Banner rightBracket
8786 Star_Banner verticalScroll
8787 Star_Banner wave
8788 Tabs cornerTabs
8789 Tabs plaqueTabs
8790 Tabs squareTabs
8791
8792 Download this example:
8793 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/shape_all.pl>
8794
8795 Example: sparklines1.pl
8796 Example of how to add sparklines to an Excel::Writer::XLSX file.
8797
8798 Sparklines are small charts that fit in a single cell and are used to
8799 show trends in data. See sparklines2.pl for examples of more complex
8800 sparkline formatting.
8801
8802 Source code for this example:
8803
8804 #!/usr/bin/perl
8805
8806 ###############################################################################
8807 #
8808 # Example of how to add sparklines to an Excel::Writer::XLSX file.
8809 #
8810 # Sparklines are small charts that fit in a single cell and are
8811 # used to show trends in data. See sparklines2.pl for examples
8812 # of more complex sparkline formatting.
8813 #
8814 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8815 #
8816
8817 use strict;
8818 use warnings;
8819 use Excel::Writer::XLSX;
8820
8821 my $workbook = Excel::Writer::XLSX->new( 'sparklines1.xlsx' );
8822 my $worksheet = $workbook->add_worksheet();
8823
8824 # Some sample data to plot.
8825 my $data = [
8826
8827 [ -2, 2, 3, -1, 0 ],
8828 [ 30, 20, 33, 20, 15 ],
8829 [ 1, -1, -1, 1, -1 ],
8830
8831 ];
8832
8833 # Write the sample data to the worksheet.
8834 $worksheet->write_col( 'A1', $data );
8835
8836
8837 # Add a line sparkline (the default) with markers.
8838 $worksheet->add_sparkline(
8839 {
8840 location => 'F1',
8841 range => 'Sheet1!A1:E1',
8842 markers => 1,
8843 }
8844 );
8845
8846 # Add a column sparkline with non-default style.
8847 $worksheet->add_sparkline(
8848 {
8849 location => 'F2',
8850 range => 'Sheet1!A2:E2',
8851 type => 'column',
8852 style => 12,
8853 }
8854 );
8855
8856 # Add a win/loss sparkline with negative values highlighted.
8857 $worksheet->add_sparkline(
8858 {
8859 location => 'F3',
8860 range => 'Sheet1!A3:E3',
8861 type => 'win_loss',
8862 negative_points => 1,
8863 }
8864 );
8865
8866 $workbook->close();
8867
8868 __END__
8869
8870 Download this example:
8871 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/sparklines1.pl>
8872
8873 Example: sparklines2.pl
8874 Example of how to add sparklines to an Excel::Writer::XLSX file.
8875
8876 Sparklines are small charts that fit in a single cell and are used to
8877 show trends in data. This example shows the majority of options that
8878 can be applied to sparklines.
8879
8880 Source code for this example:
8881
8882 #!/usr/bin/perl
8883
8884 ###############################################################################
8885 #
8886 # Example of how to add sparklines to an Excel::Writer::XLSX file.
8887 #
8888 # Sparklines are small charts that fit in a single cell and are
8889 # used to show trends in data. This example shows the majority of
8890 # options that can be applied to sparklines.
8891 #
8892 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
8893 #
8894
8895 use strict;
8896 use warnings;
8897 use Excel::Writer::XLSX;
8898
8899 my $workbook = Excel::Writer::XLSX->new( 'sparklines2.xlsx' );
8900 my $worksheet1 = $workbook->add_worksheet();
8901 my $worksheet2 = $workbook->add_worksheet();
8902 my $bold = $workbook->add_format( bold => 1 );
8903 my $str;
8904 my $row = 1;
8905
8906 # Set the columns widths to make the output clearer.
8907 $worksheet1->set_column( 'A:A', 14 );
8908 $worksheet1->set_column( 'B:B', 50 );
8909 $worksheet1->set_zoom( 150 );
8910
8911 # Headings.
8912 $worksheet1->write( 'A1', 'Sparkline', $bold );
8913 $worksheet1->write( 'B1', 'Description', $bold );
8914
8915
8916 ###############################################################################
8917 #
8918 $str = 'A default "line" sparkline.';
8919
8920 $worksheet1->add_sparkline(
8921 {
8922 location => 'A2',
8923 range => 'Sheet2!A1:J1',
8924 }
8925 );
8926
8927 $worksheet1->write( $row++, 1, $str );
8928
8929
8930 ###############################################################################
8931 #
8932 $str = 'A default "column" sparkline.';
8933
8934 $worksheet1->add_sparkline(
8935 {
8936 location => 'A3',
8937 range => 'Sheet2!A2:J2',
8938 type => 'column',
8939 }
8940 );
8941
8942 $worksheet1->write( $row++, 1, $str );
8943
8944
8945 ###############################################################################
8946 #
8947 $str = 'A default "win/loss" sparkline.';
8948
8949 $worksheet1->add_sparkline(
8950 {
8951 location => 'A4',
8952 range => 'Sheet2!A3:J3',
8953 type => 'win_loss',
8954 }
8955 );
8956
8957 $worksheet1->write( $row++, 1, $str );
8958 $row++;
8959
8960
8961 ###############################################################################
8962 #
8963 $str = 'Line with markers.';
8964
8965 $worksheet1->add_sparkline(
8966 {
8967 location => 'A6',
8968 range => 'Sheet2!A1:J1',
8969 markers => 1,
8970 }
8971 );
8972
8973 $worksheet1->write( $row++, 1, $str );
8974
8975
8976 ###############################################################################
8977 #
8978 $str = 'Line with high and low points.';
8979
8980 $worksheet1->add_sparkline(
8981 {
8982 location => 'A7',
8983 range => 'Sheet2!A1:J1',
8984 high_point => 1,
8985 low_point => 1,
8986 }
8987 );
8988
8989 $worksheet1->write( $row++, 1, $str );
8990
8991
8992 ###############################################################################
8993 #
8994 $str = 'Line with first and last point markers.';
8995
8996 $worksheet1->add_sparkline(
8997 {
8998 location => 'A8',
8999 range => 'Sheet2!A1:J1',
9000 first_point => 1,
9001 last_point => 1,
9002 }
9003 );
9004
9005 $worksheet1->write( $row++, 1, $str );
9006
9007
9008 ###############################################################################
9009 #
9010 $str = 'Line with negative point markers.';
9011
9012 $worksheet1->add_sparkline(
9013 {
9014 location => 'A9',
9015 range => 'Sheet2!A1:J1',
9016 negative_points => 1,
9017 }
9018 );
9019
9020 $worksheet1->write( $row++, 1, $str );
9021
9022
9023 ###############################################################################
9024 #
9025 $str = 'Line with axis.';
9026
9027 $worksheet1->add_sparkline(
9028 {
9029 location => 'A10',
9030 range => 'Sheet2!A1:J1',
9031 axis => 1,
9032 }
9033 );
9034
9035 $worksheet1->write( $row++, 1, $str );
9036 $row++;
9037
9038
9039 ###############################################################################
9040 #
9041 $str = 'Column with default style (1).';
9042
9043 $worksheet1->add_sparkline(
9044 {
9045 location => 'A12',
9046 range => 'Sheet2!A2:J2',
9047 type => 'column',
9048 }
9049 );
9050
9051 $worksheet1->write( $row++, 1, $str );
9052
9053
9054 ###############################################################################
9055 #
9056 $str = 'Column with style 2.';
9057
9058 $worksheet1->add_sparkline(
9059 {
9060 location => 'A13',
9061 range => 'Sheet2!A2:J2',
9062 type => 'column',
9063 style => 2,
9064 }
9065 );
9066
9067 $worksheet1->write( $row++, 1, $str );
9068
9069
9070 ###############################################################################
9071 #
9072 $str = 'Column with style 3.';
9073
9074 $worksheet1->add_sparkline(
9075 {
9076 location => 'A14',
9077 range => 'Sheet2!A2:J2',
9078 type => 'column',
9079 style => 3,
9080 }
9081 );
9082
9083 $worksheet1->write( $row++, 1, $str );
9084
9085
9086 ###############################################################################
9087 #
9088 $str = 'Column with style 4.';
9089
9090 $worksheet1->add_sparkline(
9091 {
9092 location => 'A15',
9093 range => 'Sheet2!A2:J2',
9094 type => 'column',
9095 style => 4,
9096 }
9097 );
9098
9099 $worksheet1->write( $row++, 1, $str );
9100
9101
9102 ###############################################################################
9103 #
9104 $str = 'Column with style 5.';
9105
9106 $worksheet1->add_sparkline(
9107 {
9108 location => 'A16',
9109 range => 'Sheet2!A2:J2',
9110 type => 'column',
9111 style => 5,
9112 }
9113 );
9114
9115 $worksheet1->write( $row++, 1, $str );
9116
9117
9118 ###############################################################################
9119 #
9120 $str = 'Column with style 6.';
9121
9122 $worksheet1->add_sparkline(
9123 {
9124 location => 'A17',
9125 range => 'Sheet2!A2:J2',
9126 type => 'column',
9127 style => 6,
9128 }
9129 );
9130
9131 $worksheet1->write( $row++, 1, $str );
9132
9133
9134 ###############################################################################
9135 #
9136 $str = 'Column with a user defined colour.';
9137
9138 $worksheet1->add_sparkline(
9139 {
9140 location => 'A18',
9141 range => 'Sheet2!A2:J2',
9142 type => 'column',
9143 series_color => '#E965E0',
9144 }
9145 );
9146
9147 $worksheet1->write( $row++, 1, $str );
9148 $row++;
9149
9150
9151 ###############################################################################
9152 #
9153 $str = 'A win/loss sparkline.';
9154
9155 $worksheet1->add_sparkline(
9156 {
9157 location => 'A20',
9158 range => 'Sheet2!A3:J3',
9159 type => 'win_loss',
9160 }
9161 );
9162
9163 $worksheet1->write( $row++, 1, $str );
9164
9165
9166 ###############################################################################
9167 #
9168 $str = 'A win/loss sparkline with negative points highlighted.';
9169
9170 $worksheet1->add_sparkline(
9171 {
9172 location => 'A21',
9173 range => 'Sheet2!A3:J3',
9174 type => 'win_loss',
9175 negative_points => 1,
9176 }
9177 );
9178
9179 $worksheet1->write( $row++, 1, $str );
9180 $row++;
9181
9182
9183 ###############################################################################
9184 #
9185 $str = 'A left to right column (the default).';
9186
9187 $worksheet1->add_sparkline(
9188 {
9189 location => 'A23',
9190 range => 'Sheet2!A4:J4',
9191 type => 'column',
9192 style => 20,
9193 }
9194 );
9195
9196 $worksheet1->write( $row++, 1, $str );
9197
9198
9199 ###############################################################################
9200 #
9201 $str = 'A right to left column.';
9202
9203 $worksheet1->add_sparkline(
9204 {
9205 location => 'A24',
9206 range => 'Sheet2!A4:J4',
9207 type => 'column',
9208 style => 20,
9209 reverse => 1,
9210 }
9211 );
9212
9213 $worksheet1->write( $row++, 1, $str );
9214
9215
9216 ###############################################################################
9217 #
9218 $str = 'Sparkline and text in one cell.';
9219
9220 $worksheet1->add_sparkline(
9221 {
9222 location => 'A25',
9223 range => 'Sheet2!A4:J4',
9224 type => 'column',
9225 style => 20,
9226 }
9227 );
9228
9229 $worksheet1->write( $row, 0, 'Growth' );
9230 $worksheet1->write( $row++, 1, $str );
9231 $row++;
9232
9233
9234 ###############################################################################
9235 #
9236 $str = 'A grouped sparkline. Changes are applied to all three.';
9237
9238 $worksheet1->add_sparkline(
9239 {
9240 location => [ 'A27', 'A28', 'A29' ],
9241 range => [ 'Sheet2!A5:J5', 'Sheet2!A6:J6', 'Sheet2!A7:J7' ],
9242 markers => 1,
9243 }
9244 );
9245
9246 $worksheet1->write( $row++, 1, $str );
9247
9248
9249
9250
9251 ###############################################################################
9252 #
9253 # Create a second worksheet with data to plot.
9254 #
9255
9256 $worksheet2->set_column( 'A:J', 11 );
9257
9258 my $data = [
9259
9260 # Simple line data.
9261 [ -2, 2, 3, -1, 0, -2, 3, 2, 1, 0 ],
9262
9263 # Simple column data.
9264 [ 30, 20, 33, 20, 15, 5, 5, 15, 10, 15 ],
9265
9266 # Simple win/loss data.
9267 [ 1, 1, -1, -1, 1, -1, 1, 1, 1, -1 ],
9268
9269 # Unbalanced histogram.
9270 [ 5, 6, 7, 10, 15, 20, 30, 50, 70, 100 ],
9271
9272 # Data for the grouped sparkline example.
9273 [ -2, 2, 3, -1, 0, -2, 3, 2, 1, 0 ],
9274 [ 3, -1, 0, -2, 3, 2, 1, 0, 2, 1 ],
9275 [ 0, -2, 3, 2, 1, 0, 1, 2, 3, 1 ],
9276
9277
9278 ];
9279
9280 # Write the sample data to the worksheet.
9281 $worksheet2->write_col( 'A1', $data );
9282
9283 $workbook->close();
9284
9285 __END__
9286
9287 Download this example:
9288 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/sparklines2.pl>
9289
9290 Example: stats_ext.pl
9291 Example of formatting using the Excel::Writer::XLSX module
9292
9293 This is a simple example of how to use functions that reference cells
9294 in other worksheets within the same workbook.
9295
9296 Source code for this example:
9297
9298 #!/usr/bin/perl -w
9299
9300 ###############################################################################
9301 #
9302 # Example of formatting using the Excel::Writer::XLSX module
9303 #
9304 # This is a simple example of how to use functions that reference cells in
9305 # other worksheets within the same workbook.
9306 #
9307 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
9308 #
9309
9310 use strict;
9311 use Excel::Writer::XLSX;
9312
9313 # Create a new workbook and add a worksheet
9314 my $workbook = Excel::Writer::XLSX->new( 'stats_ext.xlsx' );
9315 my $worksheet1 = $workbook->add_worksheet( 'Test results' );
9316 my $worksheet2 = $workbook->add_worksheet( 'Data' );
9317
9318 # Set the column width for columns 1
9319 $worksheet1->set_column( 'A:A', 20 );
9320
9321
9322 # Create a format for the headings
9323 my $heading = $workbook->add_format();
9324 $heading->set_bold();
9325
9326 # Create a numerical format
9327 my $numformat = $workbook->add_format();
9328 $numformat->set_num_format( '0.00' );
9329
9330
9331 # Write some statistical functions
9332 $worksheet1->write( 'A1', 'Count', $heading );
9333 $worksheet1->write( 'B1', '=COUNT(Data!B2:B9)' );
9334
9335 $worksheet1->write( 'A2', 'Sum', $heading );
9336 $worksheet1->write( 'B2', '=SUM(Data!B2:B9)' );
9337
9338 $worksheet1->write( 'A3', 'Average', $heading );
9339 $worksheet1->write( 'B3', '=AVERAGE(Data!B2:B9)' );
9340
9341 $worksheet1->write( 'A4', 'Min', $heading );
9342 $worksheet1->write( 'B4', '=MIN(Data!B2:B9)' );
9343
9344 $worksheet1->write( 'A5', 'Max', $heading );
9345 $worksheet1->write( 'B5', '=MAX(Data!B2:B9)' );
9346
9347 $worksheet1->write( 'A6', 'Standard Deviation', $heading );
9348 $worksheet1->write( 'B6', '=STDEV(Data!B2:B9)' );
9349
9350 $worksheet1->write( 'A7', 'Kurtosis', $heading );
9351 $worksheet1->write( 'B7', '=KURT(Data!B2:B9)' );
9352
9353
9354 # Write the sample data
9355 $worksheet2->write( 'A1', 'Sample', $heading );
9356 $worksheet2->write( 'A2', 1 );
9357 $worksheet2->write( 'A3', 2 );
9358 $worksheet2->write( 'A4', 3 );
9359 $worksheet2->write( 'A5', 4 );
9360 $worksheet2->write( 'A6', 5 );
9361 $worksheet2->write( 'A7', 6 );
9362 $worksheet2->write( 'A8', 7 );
9363 $worksheet2->write( 'A9', 8 );
9364
9365 $worksheet2->write( 'B1', 'Length', $heading );
9366 $worksheet2->write( 'B2', 25.4, $numformat );
9367 $worksheet2->write( 'B3', 25.4, $numformat );
9368 $worksheet2->write( 'B4', 24.8, $numformat );
9369 $worksheet2->write( 'B5', 25.0, $numformat );
9370 $worksheet2->write( 'B6', 25.3, $numformat );
9371 $worksheet2->write( 'B7', 24.9, $numformat );
9372 $worksheet2->write( 'B8', 25.2, $numformat );
9373 $worksheet2->write( 'B9', 24.8, $numformat );
9374
9375 $workbook->close();
9376
9377 __END__
9378
9379 Download this example:
9380 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/stats_ext.pl>
9381
9382 Example: stocks.pl
9383 Example of formatting using the Excel::Writer::XLSX module
9384
9385 This example shows how to use a conditional numerical format with
9386 colours to indicate if a share price has gone up or down.
9387
9388 Source code for this example:
9389
9390 #!/usr/bin/perl -w
9391
9392 ###############################################################################
9393 #
9394 # Example of formatting using the Excel::Writer::XLSX module
9395 #
9396 # This example shows how to use a conditional numerical format
9397 # with colours to indicate if a share price has gone up or down.
9398 #
9399 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
9400 #
9401
9402 use strict;
9403 use Excel::Writer::XLSX;
9404
9405 # Create a new workbook and add a worksheet
9406 my $workbook = Excel::Writer::XLSX->new( 'stocks.xlsx' );
9407 my $worksheet = $workbook->add_worksheet();
9408
9409 # Set the column width for columns 1, 2, 3 and 4
9410 $worksheet->set_column( 0, 3, 15 );
9411
9412
9413 # Create a format for the column headings
9414 my $header = $workbook->add_format();
9415 $header->set_bold();
9416 $header->set_size( 12 );
9417 $header->set_color( 'blue' );
9418
9419
9420 # Create a format for the stock price
9421 my $f_price = $workbook->add_format();
9422 $f_price->set_align( 'left' );
9423 $f_price->set_num_format( '$0.00' );
9424
9425
9426 # Create a format for the stock volume
9427 my $f_volume = $workbook->add_format();
9428 $f_volume->set_align( 'left' );
9429 $f_volume->set_num_format( '#,##0' );
9430
9431
9432 # Create a format for the price change. This is an example of a conditional
9433 # format. The number is formatted as a percentage. If it is positive it is
9434 # formatted in green, if it is negative it is formatted in red and if it is
9435 # zero it is formatted as the default font colour (in this case black).
9436 # Note: the [Green] format produces an unappealing lime green. Try
9437 # [Color 10] instead for a dark green.
9438 #
9439 my $f_change = $workbook->add_format();
9440 $f_change->set_align( 'left' );
9441 $f_change->set_num_format( '[Green]0.0%;[Red]-0.0%;0.0%' );
9442
9443
9444 # Write out the data
9445 $worksheet->write( 0, 0, 'Company', $header );
9446 $worksheet->write( 0, 1, 'Price', $header );
9447 $worksheet->write( 0, 2, 'Volume', $header );
9448 $worksheet->write( 0, 3, 'Change', $header );
9449
9450 $worksheet->write( 1, 0, 'Damage Inc.' );
9451 $worksheet->write( 1, 1, 30.25, $f_price ); # $30.25
9452 $worksheet->write( 1, 2, 1234567, $f_volume ); # 1,234,567
9453 $worksheet->write( 1, 3, 0.085, $f_change ); # 8.5% in green
9454
9455 $worksheet->write( 2, 0, 'Dump Corp.' );
9456 $worksheet->write( 2, 1, 1.56, $f_price ); # $1.56
9457 $worksheet->write( 2, 2, 7564, $f_volume ); # 7,564
9458 $worksheet->write( 2, 3, -0.015, $f_change ); # -1.5% in red
9459
9460 $worksheet->write( 3, 0, 'Rev Ltd.' );
9461 $worksheet->write( 3, 1, 0.13, $f_price ); # $0.13
9462 $worksheet->write( 3, 2, 321, $f_volume ); # 321
9463 $worksheet->write( 3, 3, 0, $f_change ); # 0 in the font color (black)
9464
9465
9466 $workbook->close();
9467
9468 __END__
9469
9470 Download this example:
9471 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/stocks.pl>
9472
9473 Example: background.pl
9474 An example of setting a worksheet background image with
9475 Excel::Writer::XLSX.
9476
9477 #!/usr/bin/perl -w
9478
9479 #######################################################################
9480 #
9481 # An example of setting a worksheet background image with Excel::Writer::XLSX.
9482 #
9483 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
9484 #
9485
9486 use strict;
9487 use Excel::Writer::XLSX;
9488
9489 my $workbook = Excel::Writer::XLSX->new( 'background.xlsx' );
9490 my $worksheet = $workbook->add_worksheet();
9491
9492 $worksheet->set_background( 'republic.png' );
9493
9494 $workbook->close();
9495
9496 __END__
9497
9498 Download this example:
9499 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/background.pl>
9500
9501 Example: tab_colors.pl
9502 Example of how to set Excel worksheet tab colours.
9503
9504 Source code for this example:
9505
9506 #!/usr/bin/perl
9507
9508 #######################################################################
9509 #
9510 # Example of how to set Excel worksheet tab colours.
9511 #
9512 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
9513 #
9514
9515 use strict;
9516 use warnings;
9517 use Excel::Writer::XLSX;
9518
9519
9520 my $workbook = Excel::Writer::XLSX->new( 'tab_colors.xlsx' );
9521
9522 my $worksheet1 = $workbook->add_worksheet();
9523 my $worksheet2 = $workbook->add_worksheet();
9524 my $worksheet3 = $workbook->add_worksheet();
9525 my $worksheet4 = $workbook->add_worksheet();
9526
9527 # Worksheet1 will have the default tab colour.
9528 $worksheet2->set_tab_color( 'red' );
9529 $worksheet3->set_tab_color( 'green' );
9530 $worksheet4->set_tab_color( '#FF6600'); # Orange
9531
9532 $workbook->close();
9533
9534 __END__
9535
9536 Download this example:
9537 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/tab_colors.pl>
9538
9539 Example: tables.pl
9540 Example of how to add tables to an Excel::Writer::XLSX worksheet.
9541
9542 Tables in Excel are used to group rows and columns of data into a
9543 single structure that can be referenced in a formula or formatted
9544 collectively.
9545
9546 Source code for this example:
9547
9548 #!/usr/bin/perl
9549
9550 ###############################################################################
9551 #
9552 # Example of how to add tables to an Excel::Writer::XLSX worksheet.
9553 #
9554 # Tables in Excel are used to group rows and columns of data into a single
9555 # structure that can be referenced in a formula or formatted collectively.
9556 #
9557 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
9558 #
9559
9560 use strict;
9561 use warnings;
9562 use Excel::Writer::XLSX;
9563
9564 my $workbook = Excel::Writer::XLSX->new( 'tables.xlsx' );
9565 my $worksheet1 = $workbook->add_worksheet();
9566 my $worksheet2 = $workbook->add_worksheet();
9567 my $worksheet3 = $workbook->add_worksheet();
9568 my $worksheet4 = $workbook->add_worksheet();
9569 my $worksheet5 = $workbook->add_worksheet();
9570 my $worksheet6 = $workbook->add_worksheet();
9571 my $worksheet7 = $workbook->add_worksheet();
9572 my $worksheet8 = $workbook->add_worksheet();
9573 my $worksheet9 = $workbook->add_worksheet();
9574 my $worksheet10 = $workbook->add_worksheet();
9575 my $worksheet11 = $workbook->add_worksheet();
9576 my $worksheet12 = $workbook->add_worksheet();
9577 my $worksheet13 = $workbook->add_worksheet();
9578
9579 my $currency_format = $workbook->add_format( num_format => '$#,##0' );
9580
9581
9582 # Some sample data for the table.
9583 my $data = [
9584 [ 'Apples', 10000, 5000, 8000, 6000 ],
9585 [ 'Pears', 2000, 3000, 4000, 5000 ],
9586 [ 'Bananas', 6000, 6000, 6500, 6000 ],
9587 [ 'Oranges', 500, 300, 200, 700 ],
9588
9589 ];
9590
9591
9592 ###############################################################################
9593 #
9594 # Example 1.
9595 #
9596 my $caption = 'Default table with no data.';
9597
9598 # Set the columns widths.
9599 $worksheet1->set_column( 'B:G', 12 );
9600
9601 # Write the caption.
9602 $worksheet1->write( 'B1', $caption );
9603
9604 # Add a table to the worksheet.
9605 $worksheet1->add_table( 'B3:F7' );
9606
9607
9608 ###############################################################################
9609 #
9610 # Example 2.
9611 #
9612 $caption = 'Default table with data.';
9613
9614 # Set the columns widths.
9615 $worksheet2->set_column( 'B:G', 12 );
9616
9617 # Write the caption.
9618 $worksheet2->write( 'B1', $caption );
9619
9620 # Add a table to the worksheet.
9621 $worksheet2->add_table( 'B3:F7', { data => $data } );
9622
9623
9624 ###############################################################################
9625 #
9626 # Example 3.
9627 #
9628 $caption = 'Table without default autofilter.';
9629
9630 # Set the columns widths.
9631 $worksheet3->set_column( 'B:G', 12 );
9632
9633 # Write the caption.
9634 $worksheet3->write( 'B1', $caption );
9635
9636 # Add a table to the worksheet.
9637 $worksheet3->add_table( 'B3:F7', { autofilter => 0 } );
9638
9639 # Table data can also be written separately, as an array or individual cells.
9640 $worksheet3->write_col( 'B4', $data );
9641
9642
9643 ###############################################################################
9644 #
9645 # Example 4.
9646 #
9647 $caption = 'Table without default header row.';
9648
9649 # Set the columns widths.
9650 $worksheet4->set_column( 'B:G', 12 );
9651
9652 # Write the caption.
9653 $worksheet4->write( 'B1', $caption );
9654
9655 # Add a table to the worksheet.
9656 $worksheet4->add_table( 'B4:F7', { header_row => 0 } );
9657
9658 # Table data can also be written separately, as an array or individual cells.
9659 $worksheet4->write_col( 'B4', $data );
9660
9661
9662 ###############################################################################
9663 #
9664 # Example 5.
9665 #
9666 $caption = 'Default table with "First Column" and "Last Column" options.';
9667
9668 # Set the columns widths.
9669 $worksheet5->set_column( 'B:G', 12 );
9670
9671 # Write the caption.
9672 $worksheet5->write( 'B1', $caption );
9673
9674 # Add a table to the worksheet.
9675 $worksheet5->add_table( 'B3:F7', { first_column => 1, last_column => 1 } );
9676
9677 # Table data can also be written separately, as an array or individual cells.
9678 $worksheet5->write_col( 'B4', $data );
9679
9680
9681 ###############################################################################
9682 #
9683 # Example 6.
9684 #
9685 $caption = 'Table with banded columns but without default banded rows.';
9686
9687 # Set the columns widths.
9688 $worksheet6->set_column( 'B:G', 12 );
9689
9690 # Write the caption.
9691 $worksheet6->write( 'B1', $caption );
9692
9693 # Add a table to the worksheet.
9694 $worksheet6->add_table( 'B3:F7', { banded_rows => 0, banded_columns => 1 } );
9695
9696 # Table data can also be written separately, as an array or individual cells.
9697 $worksheet6->write_col( 'B4', $data );
9698
9699
9700 ###############################################################################
9701 #
9702 # Example 7.
9703 #
9704 $caption = 'Table with user defined column headers';
9705
9706 # Set the columns widths.
9707 $worksheet7->set_column( 'B:G', 12 );
9708
9709 # Write the caption.
9710 $worksheet7->write( 'B1', $caption );
9711
9712 # Add a table to the worksheet.
9713 $worksheet7->add_table(
9714 'B3:F7',
9715 {
9716 data => $data,
9717 columns => [
9718 { header => 'Product' },
9719 { header => 'Quarter 1' },
9720 { header => 'Quarter 2' },
9721 { header => 'Quarter 3' },
9722 { header => 'Quarter 4' },
9723 ]
9724 }
9725 );
9726
9727
9728 ###############################################################################
9729 #
9730 # Example 8.
9731 #
9732 $caption = 'Table with user defined column headers';
9733
9734 # Set the columns widths.
9735 $worksheet8->set_column( 'B:G', 12 );
9736
9737 # Write the caption.
9738 $worksheet8->write( 'B1', $caption );
9739
9740 # Add a table to the worksheet.
9741 $worksheet8->add_table(
9742 'B3:G7',
9743 {
9744 data => $data,
9745 columns => [
9746 { header => 'Product' },
9747 { header => 'Quarter 1' },
9748 { header => 'Quarter 2' },
9749 { header => 'Quarter 3' },
9750 { header => 'Quarter 4' },
9751 {
9752 header => 'Year',
9753 formula => '=SUM(Table8[@[Quarter 1]:[Quarter 4]])'
9754 },
9755 ]
9756 }
9757 );
9758
9759
9760 ###############################################################################
9761 #
9762 # Example 9.
9763 #
9764 $caption = 'Table with totals row (but no caption or totals).';
9765
9766 # Set the columns widths.
9767 $worksheet9->set_column( 'B:G', 12 );
9768
9769 # Write the caption.
9770 $worksheet9->write( 'B1', $caption );
9771
9772 # Add a table to the worksheet.
9773 $worksheet9->add_table(
9774 'B3:G8',
9775 {
9776 data => $data,
9777 total_row => 1,
9778 columns => [
9779 { header => 'Product' },
9780 { header => 'Quarter 1' },
9781 { header => 'Quarter 2' },
9782 { header => 'Quarter 3' },
9783 { header => 'Quarter 4' },
9784 {
9785 header => 'Year',
9786 formula => '=SUM(Table9[@[Quarter 1]:[Quarter 4]])'
9787 },
9788 ]
9789 }
9790 );
9791
9792
9793 ###############################################################################
9794 #
9795 # Example 10.
9796 #
9797 $caption = 'Table with totals row with user captions and functions.';
9798
9799 # Set the columns widths.
9800 $worksheet10->set_column( 'B:G', 12 );
9801
9802 # Write the caption.
9803 $worksheet10->write( 'B1', $caption );
9804
9805 # Add a table to the worksheet.
9806 $worksheet10->add_table(
9807 'B3:G8',
9808 {
9809 data => $data,
9810 total_row => 1,
9811 columns => [
9812 { header => 'Product', total_string => 'Totals' },
9813 { header => 'Quarter 1', total_function => 'sum' },
9814 { header => 'Quarter 2', total_function => 'sum' },
9815 { header => 'Quarter 3', total_function => 'sum' },
9816 { header => 'Quarter 4', total_function => 'sum' },
9817 {
9818 header => 'Year',
9819 formula => '=SUM(Table10[@[Quarter 1]:[Quarter 4]])',
9820 total_function => 'sum'
9821 },
9822 ]
9823 }
9824 );
9825
9826
9827 ###############################################################################
9828 #
9829 # Example 11.
9830 #
9831 $caption = 'Table with alternative Excel style.';
9832
9833 # Set the columns widths.
9834 $worksheet11->set_column( 'B:G', 12 );
9835
9836 # Write the caption.
9837 $worksheet11->write( 'B1', $caption );
9838
9839 # Add a table to the worksheet.
9840 $worksheet11->add_table(
9841 'B3:G8',
9842 {
9843 data => $data,
9844 style => 'Table Style Light 11',
9845 total_row => 1,
9846 columns => [
9847 { header => 'Product', total_string => 'Totals' },
9848 { header => 'Quarter 1', total_function => 'sum' },
9849 { header => 'Quarter 2', total_function => 'sum' },
9850 { header => 'Quarter 3', total_function => 'sum' },
9851 { header => 'Quarter 4', total_function => 'sum' },
9852 {
9853 header => 'Year',
9854 formula => '=SUM(Table11[@[Quarter 1]:[Quarter 4]])',
9855 total_function => 'sum'
9856 },
9857 ]
9858 }
9859 );
9860
9861
9862 ###############################################################################
9863 #
9864 # Example 12.
9865 #
9866 $caption = 'Table with no Excel style.';
9867
9868 # Set the columns widths.
9869 $worksheet12->set_column( 'B:G', 12 );
9870
9871 # Write the caption.
9872 $worksheet12->write( 'B1', $caption );
9873
9874 # Add a table to the worksheet.
9875 $worksheet12->add_table(
9876 'B3:G8',
9877 {
9878 data => $data,
9879 style => 'None',
9880 total_row => 1,
9881 columns => [
9882 { header => 'Product', total_string => 'Totals' },
9883 { header => 'Quarter 1', total_function => 'sum' },
9884 { header => 'Quarter 2', total_function => 'sum' },
9885 { header => 'Quarter 3', total_function => 'sum' },
9886 { header => 'Quarter 4', total_function => 'sum' },
9887 {
9888 header => 'Year',
9889 formula => '=SUM(Table12[@[Quarter 1]:[Quarter 4]])',
9890 total_function => 'sum'
9891 },
9892 ]
9893 }
9894 );
9895
9896
9897 ###############################################################################
9898 #
9899 # Example 13.
9900 #
9901 $caption = 'Table with column formats.';
9902
9903 # Set the columns widths.
9904 $worksheet13->set_column( 'B:G', 12 );
9905
9906 # Write the caption.
9907 $worksheet13->write( 'B1', $caption );
9908
9909 # Add a table to the worksheet.
9910 $worksheet13->add_table(
9911 'B3:G8',
9912 {
9913 data => $data,
9914 total_row => 1,
9915 columns => [
9916 { header => 'Product', total_string => 'Totals' },
9917 {
9918 header => 'Quarter 1',
9919 total_function => 'sum',
9920 format => $currency_format,
9921 },
9922 {
9923 header => 'Quarter 2',
9924 total_function => 'sum',
9925 format => $currency_format,
9926 },
9927 {
9928 header => 'Quarter 3',
9929 total_function => 'sum',
9930 format => $currency_format,
9931 },
9932 {
9933 header => 'Quarter 4',
9934 total_function => 'sum',
9935 format => $currency_format,
9936 },
9937 {
9938 header => 'Year',
9939 formula => '=SUM(Table13[@[Quarter 1]:[Quarter 4]])',
9940 total_function => 'sum',
9941 format => $currency_format,
9942 },
9943 ]
9944 }
9945 );
9946
9947
9948 $workbook->close();
9949
9950 __END__
9951
9952 Download this example:
9953 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/tables.pl>
9954
9955 Example: write_handler1.pl
9956 Example of how to add a user defined data handler to the
9957 Excel::Writer::XLSX write() method.
9958
9959 The following example shows how to add a handler for a 7 digit ID
9960 number.
9961
9962 Source code for this example:
9963
9964 #!/usr/bin/perl -w
9965
9966 ###############################################################################
9967 #
9968 # Example of how to add a user defined data handler to the
9969 # Excel::Writer::XLSX write() method.
9970 #
9971 # The following example shows how to add a handler for a 7 digit ID number.
9972 #
9973 #
9974 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
9975 #
9976
9977 use strict;
9978 use Excel::Writer::XLSX;
9979
9980
9981 my $workbook = Excel::Writer::XLSX->new( 'write_handler1.xlsx' );
9982 my $worksheet = $workbook->add_worksheet();
9983
9984
9985 ###############################################################################
9986 #
9987 # Add a handler for 7 digit id numbers. This is useful when you want a string
9988 # such as 0000001 written as a string instead of a number and thus preserve
9989 # the leading zeroes.
9990 #
9991 # Note: you can get the same effect using the keep_leading_zeros() method but
9992 # this serves as a simple example.
9993 #
9994 $worksheet->add_write_handler( qr[^\d{7}$], \&write_my_id );
9995
9996
9997 ###############################################################################
9998 #
9999 # The following function processes the data when a match is found.
10000 #
10001 sub write_my_id {
10002
10003 my $worksheet = shift;
10004
10005 return $worksheet->write_string( @_ );
10006 }
10007
10008
10009 # This format maintains the cell as text even if it is edited.
10010 my $id_format = $workbook->add_format( num_format => '@' );
10011
10012
10013 # Write some numbers in the user defined format
10014 $worksheet->write( 'A1', '0000000', $id_format );
10015 $worksheet->write( 'A2', '0000001', $id_format );
10016 $worksheet->write( 'A3', '0004000', $id_format );
10017 $worksheet->write( 'A4', '1234567', $id_format );
10018
10019 # Write some numbers that don't match the defined format
10020 $worksheet->write( 'A6', '000000', $id_format );
10021 $worksheet->write( 'A7', '000001', $id_format );
10022 $worksheet->write( 'A8', '004000', $id_format );
10023 $worksheet->write( 'A9', '123456', $id_format );
10024
10025 $workbook->close();
10026
10027 __END__
10028
10029 Download this example:
10030 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/write_handler1.pl>
10031
10032 Example: write_handler2.pl
10033 Example of how to add a user defined data handler to the
10034 Excel::Writer::XLSX write() method.
10035
10036 The following example shows how to add a handler for a 7 digit ID
10037 number. It adds an additional constraint to the write_handler1.pl in
10038 that it only filters data that isn't in the third column.
10039
10040 Source code for this example:
10041
10042 #!/usr/bin/perl -w
10043
10044 ###############################################################################
10045 #
10046 # Example of how to add a user defined data handler to the
10047 # Excel::Writer::XLSX write() method.
10048 #
10049 # The following example shows how to add a handler for a 7 digit ID number.
10050 # It adds an additional constraint to the write_handler1.pl in that it only
10051 # filters data that isn't in the third column.
10052 #
10053 #
10054 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10055 #
10056
10057 use strict;
10058 use Excel::Writer::XLSX;
10059
10060
10061 my $workbook = Excel::Writer::XLSX->new( 'write_handler2.xlsx' );
10062 my $worksheet = $workbook->add_worksheet();
10063
10064
10065 ###############################################################################
10066 #
10067 # Add a handler for 7 digit id numbers. This is useful when you want a string
10068 # such as 0000001 written as a string instead of a number and thus preserve
10069 # the leading zeroes.
10070 #
10071 # Note: you can get the same effect using the keep_leading_zeros() method but
10072 # this serves as a simple example.
10073 #
10074 $worksheet->add_write_handler( qr[^\d{7}$], \&write_my_id );
10075
10076
10077 ###############################################################################
10078 #
10079 # The following function processes the data when a match is found. The handler
10080 # is set up so that it only filters data if it is in the third column.
10081 #
10082 sub write_my_id {
10083
10084 my $worksheet = shift;
10085 my $col = $_[1];
10086
10087 # col is zero based
10088 if ( $col != 2 ) {
10089 return $worksheet->write_string( @_ );
10090 }
10091 else {
10092
10093 # Reject the match and return control to write()
10094 return undef;
10095 }
10096
10097 }
10098
10099
10100 # This format maintains the cell as text even if it is edited.
10101 my $id_format = $workbook->add_format( num_format => '@' );
10102
10103
10104 # Write some numbers in the user defined format
10105 $worksheet->write( 'A1', '0000000', $id_format );
10106 $worksheet->write( 'B1', '0000001', $id_format );
10107 $worksheet->write( 'C1', '0000002', $id_format );
10108 $worksheet->write( 'D1', '0000003', $id_format );
10109
10110 $workbook->close();
10111
10112 __END__
10113
10114 Download this example:
10115 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/write_handler2.pl>
10116
10117 Example: write_handler3.pl
10118 Example of how to add a user defined data handler to the
10119 Excel::Writer::XLSX write() method.
10120
10121 The following example shows how to add a handler for dates in a
10122 specific format.
10123
10124 See write_handler4.pl for a more rigorous example with error handling.
10125
10126 Source code for this example:
10127
10128 #!/usr/bin/perl -w
10129
10130 ###############################################################################
10131 #
10132 # Example of how to add a user defined data handler to the
10133 # Excel::Writer::XLSX write() method.
10134 #
10135 # The following example shows how to add a handler for dates in a specific
10136 # format.
10137 #
10138 # See write_handler4.pl for a more rigorous example with error handling.
10139 #
10140 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10141 #
10142
10143 use strict;
10144 use Excel::Writer::XLSX;
10145
10146
10147 my $workbook = Excel::Writer::XLSX->new( 'write_handler3.xlsx' );
10148 my $worksheet = $workbook->add_worksheet();
10149 my $date_format = $workbook->add_format( num_format => 'dd/mm/yy' );
10150
10151
10152 ###############################################################################
10153 #
10154 # Add a handler to match dates in the following format: d/m/yyyy
10155 #
10156 # The day and month can be single or double digits.
10157 #
10158 $worksheet->add_write_handler( qr[^\d{1,2}/\d{1,2}/\d{4}$], \&write_my_date );
10159
10160
10161 ###############################################################################
10162 #
10163 # The following function processes the data when a match is found.
10164 # See write_handler4.pl for a more rigorous example with error handling.
10165 #
10166 sub write_my_date {
10167
10168 my $worksheet = shift;
10169 my @args = @_;
10170
10171 my $token = $args[2];
10172 $token =~ qr[^(\d{1,2})/(\d{1,2})/(\d{4})$];
10173
10174 # Change to the date format required by write_date_time().
10175 my $date = sprintf "%4d-%02d-%02dT", $3, $2, $1;
10176
10177 $args[2] = $date;
10178
10179 return $worksheet->write_date_time( @args );
10180 }
10181
10182
10183 # Write some dates in the user defined format
10184 $worksheet->write( 'A1', '22/12/2004', $date_format );
10185 $worksheet->write( 'A2', '1/1/1995', $date_format );
10186 $worksheet->write( 'A3', '01/01/1995', $date_format );
10187
10188 $workbook->close();
10189
10190 __END__
10191
10192 Download this example:
10193 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/write_handler3.pl>
10194
10195 Example: write_handler4.pl
10196 Example of how to add a user defined data handler to the
10197 Excel::Writer::XLSX write() method.
10198
10199 The following example shows how to add a handler for dates in a
10200 specific format.
10201
10202 This is a more rigorous version of write_handler3.pl.
10203
10204 Source code for this example:
10205
10206 #!/usr/bin/perl -w
10207
10208 ###############################################################################
10209 #
10210 # Example of how to add a user defined data handler to the
10211 # Excel::Writer::XLSX write() method.
10212 #
10213 # The following example shows how to add a handler for dates in a specific
10214 # format.
10215 #
10216 # This is a more rigorous version of write_handler3.pl.
10217 #
10218 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10219 #
10220
10221 use strict;
10222 use Excel::Writer::XLSX;
10223
10224
10225 my $workbook = Excel::Writer::XLSX->new( 'write_handler4.xlsx' );
10226 my $worksheet = $workbook->add_worksheet();
10227 my $date_format = $workbook->add_format( num_format => 'dd/mm/yy' );
10228
10229
10230 ###############################################################################
10231 #
10232 # Add a handler to match dates in the following formats: d/m/yy, d/m/yyyy
10233 #
10234 # The day and month can be single or double digits and the year can be 2 or 4
10235 # digits.
10236 #
10237 $worksheet->add_write_handler( qr[^\d{1,2}/\d{1,2}/\d{2,4}$], \&write_my_date );
10238
10239
10240 ###############################################################################
10241 #
10242 # The following function processes the data when a match is found.
10243 #
10244 sub write_my_date {
10245
10246 my $worksheet = shift;
10247 my @args = @_;
10248
10249 my $token = $args[2];
10250
10251 if ( $token =~ qr[^(\d{1,2})/(\d{1,2})/(\d{2,4})$] ) {
10252
10253 my $day = $1;
10254 my $mon = $2;
10255 my $year = $3;
10256
10257 # Use a window for 2 digit dates. This will keep some ragged Perl
10258 # programmer employed in thirty years time. :-)
10259 if ( length $year == 2 ) {
10260 if ( $year < 50 ) {
10261 $year += 2000;
10262 }
10263 else {
10264 $year += 1900;
10265 }
10266 }
10267
10268 my $date = sprintf "%4d-%02d-%02dT", $year, $mon, $day;
10269
10270 # Convert the ISO ISO8601 style string to an Excel date
10271 $date = $worksheet->convert_date_time( $date );
10272
10273 if ( defined $date ) {
10274
10275 # Date was valid
10276 $args[2] = $date;
10277 return $worksheet->write_number( @args );
10278 }
10279 else {
10280
10281 # Not a valid date therefore write as a string
10282 return $worksheet->write_string( @args );
10283 }
10284 }
10285 else {
10286
10287 # Shouldn't happen if the same match is used in the re and sub.
10288 return undef;
10289 }
10290 }
10291
10292
10293 # Write some dates in the user defined format
10294 $worksheet->write( 'A1', '22/12/2004', $date_format );
10295 $worksheet->write( 'A2', '22/12/04', $date_format );
10296 $worksheet->write( 'A3', '2/12/04', $date_format );
10297 $worksheet->write( 'A4', '2/5/04', $date_format );
10298 $worksheet->write( 'A5', '2/5/95', $date_format );
10299 $worksheet->write( 'A6', '2/5/1995', $date_format );
10300
10301 # Some erroneous dates
10302 $worksheet->write( 'A8', '2/5/1895', $date_format ); # Date out of Excel range
10303 $worksheet->write( 'A9', '29/2/2003', $date_format ); # Invalid leap day
10304 $worksheet->write( 'A10', '50/50/50', $date_format ); # Matches but isn't a date
10305
10306 $workbook->close();
10307
10308 __END__
10309
10310 Download this example:
10311 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/write_handler4.pl>
10312
10313 Example: write_to_scalar.pl
10314 An example of writing an Excel::Writer::XLSX file to a perl scalar.
10315
10316 #!/usr/bin/perl
10317
10318 ##############################################################################
10319 #
10320 # An example of writing an Excel::Writer::XLSX file to a perl scalar.
10321 #
10322 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10323 #
10324
10325 use strict;
10326 use warnings;
10327 use Excel::Writer::XLSX;
10328
10329 # Use a scalar as a filehandle.
10330 open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
10331
10332
10333 # Spreadsheet::WriteExce accepts filehandle as well as file names.
10334 my $workbook = Excel::Writer::XLSX->new( $fh );
10335 my $worksheet = $workbook->add_worksheet();
10336
10337 $worksheet->write( 0, 0, 'Hi Excel!' );
10338
10339 $workbook->close();
10340
10341
10342 # The Excel file in now in $str. Remember to binmode() the output
10343 # filehandle before printing it.
10344 open my $out_fh, '>', 'write_to_scalar.xlsx'
10345 or die "Failed to open out filehandle: $!";
10346
10347 binmode $out_fh;
10348 print $out_fh $str;
10349 close $out_fh;
10350
10351 __END__
10352
10353 Download this example:
10354 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/write_to_scalar.pl>
10355
10356 Example: unicode_2022_jp.pl
10357 A simple example of converting some Unicode text to an Excel file using
10358 Excel::Writer::XLSX.
10359
10360 This example generates some Japanese from a file with ISO-2022-JP
10361 encoded text.
10362
10363 Source code for this example:
10364
10365 #!/usr/bin/perl
10366
10367 ##############################################################################
10368 #
10369 # A simple example of converting some Unicode text to an Excel file using
10370 # Excel::Writer::XLSX.
10371 #
10372 # This example generates some Japanese from a file with ISO-2022-JP
10373 # encoded text.
10374 #
10375 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10376 #
10377
10378 use strict;
10379 use warnings;
10380 use Excel::Writer::XLSX;
10381
10382
10383 my $workbook = Excel::Writer::XLSX->new( 'unicode_2022_jp.xlsx' );
10384
10385 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10386
10387 my $worksheet = $workbook->add_worksheet();
10388 $worksheet->set_column( 'A:A', 50 );
10389
10390
10391 my $file = 'unicode_2022_jp.txt';
10392
10393 open FH, '<:encoding(iso-2022-jp)', $file or die "Couldn't open $file: $!\n";
10394
10395 my $row = 0;
10396
10397 while ( <FH> ) {
10398 next if /^#/; # Ignore the comments in the sample file.
10399 chomp;
10400 $worksheet->write( $row++, 0, $_ );
10401 }
10402
10403 $workbook->close();
10404
10405 __END__
10406
10407 Download this example:
10408 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_2022_jp.pl>
10409
10410 Example: unicode_8859_11.pl
10411 A simple example of converting some Unicode text to an Excel file using
10412 Excel::Writer::XLSX.
10413
10414 This example generates some Thai from a file with ISO-8859-11 encoded
10415 text.
10416
10417 Source code for this example:
10418
10419 #!/usr/bin/perl
10420
10421 ##############################################################################
10422 #
10423 # A simple example of converting some Unicode text to an Excel file using
10424 # Excel::Writer::XLSX.
10425 #
10426 # This example generates some Thai from a file with ISO-8859-11 encoded text.
10427 #
10428 #
10429 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10430 #
10431
10432 use strict;
10433 use warnings;
10434 use Excel::Writer::XLSX;
10435
10436
10437 my $workbook = Excel::Writer::XLSX->new( 'unicode_8859_11.xlsx' );
10438
10439 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10440
10441 my $worksheet = $workbook->add_worksheet();
10442 $worksheet->set_column( 'A:A', 50 );
10443
10444
10445 my $file = 'unicode_8859_11.txt';
10446
10447 open FH, '<:encoding(iso-8859-11)', $file or die "Couldn't open $file: $!\n";
10448
10449 my $row = 0;
10450
10451 while ( <FH> ) {
10452 next if /^#/; # Ignore the comments in the sample file.
10453 chomp;
10454 $worksheet->write( $row++, 0, $_ );
10455 }
10456
10457 $workbook->close();
10458
10459 __END__
10460
10461 Download this example:
10462 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_8859_11.pl>
10463
10464 Example: unicode_8859_7.pl
10465 A simple example of converting some Unicode text to an Excel file using
10466 Excel::Writer::XLSX.
10467
10468 This example generates some Greek from a file with ISO-8859-7 encoded
10469 text.
10470
10471 Source code for this example:
10472
10473 #!/usr/bin/perl
10474
10475 ##############################################################################
10476 #
10477 # A simple example of converting some Unicode text to an Excel file using
10478 # Excel::Writer::XLSX.
10479 #
10480 # This example generates some Greek from a file with ISO-8859-7 encoded text.
10481 #
10482 #
10483 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10484 #
10485
10486 use strict;
10487 use warnings;
10488 use Excel::Writer::XLSX;
10489
10490
10491 my $workbook = Excel::Writer::XLSX->new( 'unicode_8859_7.xlsx' );
10492
10493 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10494
10495 my $worksheet = $workbook->add_worksheet();
10496 $worksheet->set_column( 'A:A', 50 );
10497
10498
10499 my $file = 'unicode_8859_7.txt';
10500
10501 open FH, '<:encoding(iso-8859-7)', $file or die "Couldn't open $file: $!\n";
10502
10503 my $row = 0;
10504
10505 while ( <FH> ) {
10506 next if /^#/; # Ignore the comments in the sample file.
10507 chomp;
10508 $worksheet->write( $row++, 0, $_ );
10509 }
10510
10511 $workbook->close();
10512
10513 __END__
10514
10515 Download this example:
10516 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_8859_7.pl>
10517
10518 Example: unicode_big5.pl
10519 A simple example of converting some Unicode text to an Excel file using
10520 Excel::Writer::XLSX.
10521
10522 This example generates some Chinese from a file with BIG5 encoded text.
10523
10524 Source code for this example:
10525
10526 #!/usr/bin/perl
10527
10528 ##############################################################################
10529 #
10530 # A simple example of converting some Unicode text to an Excel file using
10531 # Excel::Writer::XLSX.
10532 #
10533 # This example generates some Chinese from a file with BIG5 encoded text.
10534 #
10535 #
10536 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10537 #
10538
10539 use strict;
10540 use warnings;
10541 use Excel::Writer::XLSX;
10542
10543
10544 my $workbook = Excel::Writer::XLSX->new( 'unicode_big5.xlsx' );
10545
10546 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10547
10548 my $worksheet = $workbook->add_worksheet();
10549 $worksheet->set_column( 'A:A', 80 );
10550
10551
10552 my $file = 'unicode_big5.txt';
10553
10554 open FH, '<:encoding(big5)', $file or die "Couldn't open $file: $!\n";
10555
10556 my $row = 0;
10557
10558 while ( <FH> ) {
10559 next if /^#/; # Ignore the comments in the sample file.
10560 chomp;
10561 $worksheet->write( $row++, 0, $_ );
10562 }
10563
10564 $workbook->close();
10565
10566 __END__
10567
10568 Download this example:
10569 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_big5.pl>
10570
10571 Example: unicode_cp1251.pl
10572 A simple example of converting some Unicode text to an Excel file using
10573 Excel::Writer::XLSX.
10574
10575 This example generates some Russian from a file with CP1251 encoded
10576 text.
10577
10578 Source code for this example:
10579
10580 #!/usr/bin/perl
10581
10582 ##############################################################################
10583 #
10584 # A simple example of converting some Unicode text to an Excel file using
10585 # Excel::Writer::XLSX.
10586 #
10587 # This example generates some Russian from a file with CP1251 encoded text.
10588 #
10589 #
10590 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10591 #
10592
10593 use strict;
10594 use warnings;
10595 use Excel::Writer::XLSX;
10596
10597
10598 my $workbook = Excel::Writer::XLSX->new( 'unicode_cp1251.xlsx' );
10599
10600 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10601
10602 my $worksheet = $workbook->add_worksheet();
10603 $worksheet->set_column( 'A:A', 50 );
10604
10605
10606 my $file = 'unicode_cp1251.txt';
10607
10608 open FH, '<:encoding(cp1251)', $file or die "Couldn't open $file: $!\n";
10609
10610 my $row = 0;
10611
10612 while ( <FH> ) {
10613 next if /^#/; # Ignore the comments in the sample file.
10614 chomp;
10615 $worksheet->write( $row++, 0, $_ );
10616 }
10617
10618 $workbook->close();
10619
10620 __END__
10621
10622 Download this example:
10623 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_cp1251.pl>
10624
10625 Example: unicode_cp1256.pl
10626 A simple example of converting some Unicode text to an Excel file using
10627 Excel::Writer::XLSX.
10628
10629 This example generates some Arabic text from a CP-1256 encoded file.
10630
10631 Source code for this example:
10632
10633 #!/usr/bin/perl
10634
10635 ##############################################################################
10636 #
10637 # A simple example of converting some Unicode text to an Excel file using
10638 # Excel::Writer::XLSX.
10639 #
10640 # This example generates some Arabic text from a CP-1256 encoded file.
10641 #
10642 #
10643 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10644 #
10645
10646 use strict;
10647 use warnings;
10648 use Excel::Writer::XLSX;
10649
10650
10651 my $workbook = Excel::Writer::XLSX->new( 'unicode_cp1256.xlsx' );
10652
10653 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10654
10655 my $worksheet = $workbook->add_worksheet();
10656 $worksheet->set_column( 'A:A', 50 );
10657
10658
10659 my $file = 'unicode_cp1256.txt';
10660
10661 open FH, '<:encoding(cp1256)', $file or die "Couldn't open $file: $!\n";
10662
10663 my $row = 0;
10664
10665 while ( <FH> ) {
10666 next if /^#/; # Ignore the comments in the sample file.
10667 chomp;
10668 $worksheet->write( $row++, 0, $_ );
10669 }
10670
10671 $workbook->close();
10672
10673 __END__
10674
10675 Download this example:
10676 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_cp1256.pl>
10677
10678 Example: unicode_cyrillic.pl
10679 A simple example of writing some Russian cyrillic text using
10680 Excel::Writer::XLSX.
10681
10682 Source code for this example:
10683
10684 #!/usr/bin/perl
10685
10686 ##############################################################################
10687 #
10688 # A simple example of writing some Russian cyrillic text using
10689 # Excel::Writer::XLSX.
10690 #
10691 #
10692 #
10693 #
10694 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10695 #
10696
10697 use strict;
10698 use warnings;
10699 use Excel::Writer::XLSX;
10700
10701
10702 # In this example we generate utf8 strings from character data but in a
10703 # real application we would expect them to come from an external source.
10704 #
10705
10706
10707 # Create a Russian worksheet name in utf8.
10708 my $sheet = pack "U*", 0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438,
10709 0x0446, 0x0430;
10710
10711
10712 # Create a Russian string.
10713 my $str = pack "U*", 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441,
10714 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C,
10715 0x0438, 0x0440, 0x0021;
10716
10717
10718 my $workbook = Excel::Writer::XLSX->new( 'unicode_cyrillic.xlsx' );
10719
10720 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10721
10722 my $worksheet = $workbook->add_worksheet( $sheet . '1' );
10723
10724 $worksheet->set_column( 'A:A', 18 );
10725 $worksheet->write( 'A1', $str );
10726
10727 $workbook->close();
10728
10729 __END__
10730
10731 Download this example:
10732 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_cyrillic.pl>
10733
10734 Example: unicode_koi8r.pl
10735 A simple example of converting some Unicode text to an Excel file using
10736 Excel::Writer::XLSX.
10737
10738 This example generates some Russian from a file with KOI8-R encoded
10739 text.
10740
10741 Source code for this example:
10742
10743 #!/usr/bin/perl
10744
10745 ##############################################################################
10746 #
10747 # A simple example of converting some Unicode text to an Excel file using
10748 # Excel::Writer::XLSX.
10749 #
10750 # This example generates some Russian from a file with KOI8-R encoded text.
10751 #
10752 #
10753 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10754 #
10755
10756 use strict;
10757 use warnings;
10758 use Excel::Writer::XLSX;
10759
10760
10761 my $workbook = Excel::Writer::XLSX->new( 'unicode_koi8r.xlsx' );
10762
10763 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10764
10765 my $worksheet = $workbook->add_worksheet();
10766 $worksheet->set_column( 'A:A', 50 );
10767
10768
10769 my $file = 'unicode_koi8r.txt';
10770
10771 open FH, '<:encoding(koi8-r)', $file or die "Couldn't open $file: $!\n";
10772
10773 my $row = 0;
10774
10775 while ( <FH> ) {
10776 next if /^#/; # Ignore the comments in the sample file.
10777 chomp;
10778 $worksheet->write( $row++, 0, $_ );
10779 }
10780
10781 $workbook->close();
10782
10783 __END__
10784
10785 Download this example:
10786 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_koi8r.pl>
10787
10788 Example: unicode_polish_utf8.pl
10789 A simple example of converting some Unicode text to an Excel file using
10790 Excel::Writer::XLSX.
10791
10792 This example generates some Polish from a file with UTF8 encoded text.
10793
10794 Source code for this example:
10795
10796 #!/usr/bin/perl
10797
10798 ##############################################################################
10799 #
10800 # A simple example of converting some Unicode text to an Excel file using
10801 # Excel::Writer::XLSX.
10802 #
10803 # This example generates some Polish from a file with UTF8 encoded text.
10804 #
10805 #
10806 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10807 #
10808
10809 use strict;
10810 use warnings;
10811 use Excel::Writer::XLSX;
10812
10813
10814 my $workbook = Excel::Writer::XLSX->new( 'unicode_polish_utf8.xlsx' );
10815
10816 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10817
10818 my $worksheet = $workbook->add_worksheet();
10819 $worksheet->set_column( 'A:A', 50 );
10820
10821
10822 my $file = 'unicode_polish_utf8.txt';
10823
10824 open FH, '<:encoding(utf8)', $file or die "Couldn't open $file: $!\n";
10825
10826 my $row = 0;
10827
10828 while ( <FH> ) {
10829 next if /^#/; # Ignore the comments in the sample file.
10830 chomp;
10831 $worksheet->write( $row++, 0, $_ );
10832 }
10833
10834 $workbook->close();
10835
10836 __END__
10837
10838 Download this example:
10839 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_polish_utf8.pl>
10840
10841 Example: unicode_shift_jis.pl
10842 A simple example of converting some Unicode text to an Excel file using
10843 Excel::Writer::XLSX.
10844
10845 This example generates some Japenese text from a file with Shift-JIS
10846 encoded text.
10847
10848 Source code for this example:
10849
10850 #!/usr/bin/perl
10851
10852 ##############################################################################
10853 #
10854 # A simple example of converting some Unicode text to an Excel file using
10855 # Excel::Writer::XLSX.
10856 #
10857 # This example generates some Japenese text from a file with Shift-JIS
10858 # encoded text.
10859 #
10860 # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10861 #
10862
10863 use strict;
10864 use warnings;
10865 use Excel::Writer::XLSX;
10866
10867
10868 my $workbook = Excel::Writer::XLSX->new( 'unicode_shift_jis.xlsx' );
10869
10870 die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
10871
10872 my $worksheet = $workbook->add_worksheet();
10873 $worksheet->set_column( 'A:A', 50 );
10874
10875
10876 my $file = 'unicode_shift_jis.txt';
10877
10878 open FH, '<:encoding(shiftjis)', $file or die "Couldn't open $file: $!\n";
10879
10880 my $row = 0;
10881
10882 while ( <FH> ) {
10883 next if /^#/; # Ignore the comments in the sample file.
10884 chomp;
10885 $worksheet->write( $row++, 0, $_ );
10886 }
10887
10888 $workbook->close();
10889
10890 __END__
10891
10892 Download this example:
10893 <http://cpansearch.perl.org/src/JMCNAMARA/Excel-Writer-XLSX-1.09/examples/unicode_shift_jis.pl>
10894
10896 John McNamara jmcnamara@cpan.org
10897
10898 Contributed examples contain the original author's name.
10899
10901 Copyright 2000-2021, John McNamara.
10902
10903 All Rights Reserved. This module is free software. It may be used,
10904 redistributed and/or modified under the same terms as Perl itself.
10905
10906
10907
10908perl v5.36.0 2022-07-22 Excel::Writer::XLSX::Examples(3)