.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Excel::Writer::XLSX::Examples 3pm" .TH Excel::Writer::XLSX::Examples 3pm "2023-04-12" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Examples \- Excel::Writer::XLSX example programs. .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is a documentation only module showing the examples that are included in the Excel::Writer::XLSX distribution. .PP This file was auto-generated via the gen_examples_pod.pl program that is also included in the examples directory. .SH "Example programs" .IX Header "Example programs" The following is a list of the 95 example programs that are included in the Excel::Writer::XLSX distribution. .IP "\(bu" 4 \&\*(L"Example: a_simple.pl\*(R" A simple demo of some of the features. .IP "\(bu" 4 \&\*(L"Example: bug_report.pl\*(R" A template for submitting bug reports. .IP "\(bu" 4 \&\*(L"Example: demo.pl\*(R" A demo of some of the available features. .IP "\(bu" 4 \&\*(L"Example: formats.pl\*(R" All the available formatting on several worksheets. .IP "\(bu" 4 \&\*(L"Example: regions.pl\*(R" A simple example of multiple worksheets. .IP "\(bu" 4 \&\*(L"Example: stats.pl\*(R" Basic formulas and functions. .IP "\(bu" 4 \&\*(L"Example: autofit.pl\*(R" Examples of simulated worksheet autofit. .IP "\(bu" 4 \&\*(L"Example: autofilter.pl\*(R" Examples of worksheet autofilters. .IP "\(bu" 4 \&\*(L"Example: array_formula.pl\*(R" Examples of how to write array formulas. .IP "\(bu" 4 \&\*(L"Example: cgi.pl\*(R" A simple \s-1CGI\s0 program. .IP "\(bu" 4 \&\*(L"Example: chart_area.pl\*(R" A demo of area style charts. .IP "\(bu" 4 \&\*(L"Example: chart_bar.pl\*(R" A demo of bar (vertical histogram) style charts. .IP "\(bu" 4 \&\*(L"Example: chart_column.pl\*(R" A demo of column (histogram) style charts. .IP "\(bu" 4 \&\*(L"Example: chart_line.pl\*(R" A demo of line style charts. .IP "\(bu" 4 \&\*(L"Example: chart_pie.pl\*(R" A demo of pie style charts. .IP "\(bu" 4 \&\*(L"Example: chart_doughnut.pl\*(R" A demo of doughnut style charts. .IP "\(bu" 4 \&\*(L"Example: chart_radar.pl\*(R" A demo of radar style charts. .IP "\(bu" 4 \&\*(L"Example: chart_scatter.pl\*(R" A demo of scatter style charts. .IP "\(bu" 4 \&\*(L"Example: chart_secondary_axis.pl\*(R" A demo of a line chart with a secondary axis. .IP "\(bu" 4 \&\*(L"Example: chart_combined.pl\*(R" A demo of a combined column and line chart. .IP "\(bu" 4 \&\*(L"Example: chart_pareto.pl\*(R" A demo of a combined Pareto chart. .IP "\(bu" 4 \&\*(L"Example: chart_stock.pl\*(R" A demo of stock style charts. .IP "\(bu" 4 \&\*(L"Example: chart_data_table.pl\*(R" A demo of a chart with a data table on the axis. .IP "\(bu" 4 \&\*(L"Example: chart_data_tools.pl\*(R" A demo of charts with data highlighting options. .IP "\(bu" 4 \&\*(L"Example: chart_data_labels.pl\*(R" A demo of standard and custom chart data labels. .IP "\(bu" 4 \&\*(L"Example: chart_clustered.pl\*(R" A demo of a chart with a clustered axis. .IP "\(bu" 4 \&\*(L"Example: chart_styles.pl\*(R" A demo of the available chart styles. .IP "\(bu" 4 \&\*(L"Example: chart_gauge.pl\*(R" A demo of a gauge style chart. .IP "\(bu" 4 \&\*(L"Example: colors.pl\*(R" A demo of the colour palette and named colours. .IP "\(bu" 4 \&\*(L"Example: comments1.pl\*(R" Add comments to worksheet cells. .IP "\(bu" 4 \&\*(L"Example: comments2.pl\*(R" Add comments with advanced options. .IP "\(bu" 4 \&\*(L"Example: conditional_format.pl\*(R" Add conditional formats to a range of cells. .IP "\(bu" 4 \&\*(L"Example: data_validate.pl\*(R" An example of data validation and dropdown lists. .IP "\(bu" 4 \&\*(L"Example: date_time.pl\*(R" Write dates and times with \fBwrite_date_time()\fR. .IP "\(bu" 4 \&\*(L"Example: defined_name.pl\*(R" Example of how to create defined names. .IP "\(bu" 4 \&\*(L"Example: diag_border.pl\*(R" A simple example of diagonal cell borders. .IP "\(bu" 4 \&\*(L"Example: dynamic_arrays.pl\*(R" Example of using new Excel 365 dynamic functions. .IP "\(bu" 4 \&\*(L"Example: filehandle.pl\*(R" Examples of working with filehandles. .IP "\(bu" 4 \&\*(L"Example: headers.pl\*(R" Examples of worksheet headers and footers. .IP "\(bu" 4 \&\*(L"Example: hide_row_col.pl\*(R" Example of hiding rows and columns. .IP "\(bu" 4 \&\*(L"Example: hide_sheet.pl\*(R" Simple example of hiding a worksheet. .IP "\(bu" 4 \&\*(L"Example: hyperlink1.pl\*(R" Shows how to create web hyperlinks. .IP "\(bu" 4 \&\*(L"Example: hyperlink2.pl\*(R" Examples of internal and external hyperlinks. .IP "\(bu" 4 \&\*(L"Example: indent.pl\*(R" An example of cell indentation. .IP "\(bu" 4 \&\*(L"Example: ignore_errors.pl\*(R" An example of turning off worksheet cells errors/warnings. .IP "\(bu" 4 \&\*(L"Example: lambda.pl\*(R" Example of using the Excel 365 \s-1\fBLAMBDA\s0()\fR function. .IP "\(bu" 4 \&\*(L"Example: macros.pl\*(R" An example of adding macros from an existing file. .IP "\(bu" 4 \&\*(L"Example: merge1.pl\*(R" A simple example of cell merging. .IP "\(bu" 4 \&\*(L"Example: merge2.pl\*(R" A simple example of cell merging with formatting. .IP "\(bu" 4 \&\*(L"Example: merge3.pl\*(R" Add hyperlinks to merged cells. .IP "\(bu" 4 \&\*(L"Example: merge4.pl\*(R" An advanced example of merging with formatting. .IP "\(bu" 4 \&\*(L"Example: merge5.pl\*(R" An advanced example of merging with formatting. .IP "\(bu" 4 \&\*(L"Example: merge6.pl\*(R" An example of merging with Unicode strings. .IP "\(bu" 4 \&\*(L"Example: mod_perl1.pl\*(R" A simple mod_perl 1 program. .IP "\(bu" 4 \&\*(L"Example: mod_perl2.pl\*(R" A simple mod_perl 2 program. .IP "\(bu" 4 \&\*(L"Example: outline.pl\*(R" An example of outlines and grouping. .IP "\(bu" 4 \&\*(L"Example: outline_collapsed.pl\*(R" An example of collapsed outlines. .IP "\(bu" 4 \&\*(L"Example: panes.pl\*(R" An example of how to create panes. .IP "\(bu" 4 \&\*(L"Example: properties.pl\*(R" Add document properties to a workbook. .IP "\(bu" 4 \&\*(L"Example: protection.pl\*(R" Example of cell locking and formula hiding. .IP "\(bu" 4 \&\*(L"Example: rich_strings.pl\*(R" Example of strings with multiple formats. .IP "\(bu" 4 \&\*(L"Example: right_to_left.pl\*(R" Change default sheet direction to right to left. .IP "\(bu" 4 \&\*(L"Example: sales.pl\*(R" An example of a simple sales spreadsheet. .IP "\(bu" 4 \&\*(L"Example: shape1.pl\*(R" Insert shapes in worksheet. .IP "\(bu" 4 \&\*(L"Example: shape2.pl\*(R" Insert shapes in worksheet. With properties. .IP "\(bu" 4 \&\*(L"Example: shape3.pl\*(R" Insert shapes in worksheet. Scaled. .IP "\(bu" 4 \&\*(L"Example: shape4.pl\*(R" Insert shapes in worksheet. With modification. .IP "\(bu" 4 \&\*(L"Example: shape5.pl\*(R" Insert shapes in worksheet. With connections. .IP "\(bu" 4 \&\*(L"Example: shape6.pl\*(R" Insert shapes in worksheet. With connections. .IP "\(bu" 4 \&\*(L"Example: shape7.pl\*(R" Insert shapes in worksheet. One to many connections. .IP "\(bu" 4 \&\*(L"Example: shape8.pl\*(R" Insert shapes in worksheet. One to many connections. .IP "\(bu" 4 \&\*(L"Example: shape_all.pl\*(R" Demo of all the available shape and connector types. .IP "\(bu" 4 \&\*(L"Example: sparklines1.pl\*(R" Simple sparklines demo. .IP "\(bu" 4 \&\*(L"Example: sparklines2.pl\*(R" Sparklines demo showing formatting options. .IP "\(bu" 4 \&\*(L"Example: stats_ext.pl\*(R" Same as stats.pl with external references. .IP "\(bu" 4 \&\*(L"Example: stocks.pl\*(R" Demonstrates conditional formatting. .IP "\(bu" 4 \&\*(L"Example: watermark.pl\*(R" Example of how to set a watermark image for a worksheet. .IP "\(bu" 4 \&\*(L"Example: background.pl\*(R" Example of how to set the background image for a worksheet. .IP "\(bu" 4 \&\*(L"Example: tab_colors.pl\*(R" Example of how to set worksheet tab colours. .IP "\(bu" 4 \&\*(L"Example: tables.pl\*(R" Add Excel tables to a worksheet. .IP "\(bu" 4 \&\*(L"Example: write_handler1.pl\*(R" Example of extending the \fBwrite()\fR method. Step 1. .IP "\(bu" 4 \&\*(L"Example: write_handler2.pl\*(R" Example of extending the \fBwrite()\fR method. Step 2. .IP "\(bu" 4 \&\*(L"Example: write_handler3.pl\*(R" Example of extending the \fBwrite()\fR method. Step 3. .IP "\(bu" 4 \&\*(L"Example: write_handler4.pl\*(R" Example of extending the \fBwrite()\fR method. Step 4. .IP "\(bu" 4 \&\*(L"Example: write_to_scalar.pl\*(R" Example of writing an Excel file to a Perl scalar. .IP "\(bu" 4 \&\*(L"Example: unicode_2022_jp.pl\*(R" Japanese: \s-1ISO\-2022\-JP.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_8859_11.pl\*(R" Thai: \s-1ISO\-8859_11.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_8859_7.pl\*(R" Greek: \s-1ISO\-8859_7.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_big5.pl\*(R" Chinese: \s-1BIG5.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_cp1251.pl\*(R" Russian: \s-1CP1251.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_cp1256.pl\*(R" Arabic: \s-1CP1256.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_cyrillic.pl\*(R" Russian: Cyrillic. .IP "\(bu" 4 \&\*(L"Example: unicode_koi8r.pl\*(R" Russian: \s-1KOI8\-R.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_polish_utf8.pl\*(R" Polish : \s-1UTF8.\s0 .IP "\(bu" 4 \&\*(L"Example: unicode_shift_jis.pl\*(R" Japanese: Shift \s-1JIS.\s0 .SS "Example: a_simple.pl" .IX Subsection "Example: a_simple.pl" A simple example of how to use the Excel::Writer::XLSX module to write text and numbers to an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # write text and numbers to an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& # Create a new workbook called simple.xls and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqa_simple.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # The general syntax is write($row, $column, $token). Note that row and \& # column are zero indexed \& # \& \& # Write some text \& $worksheet\->write( 0, 0, "Hi Excel!" ); \& \& \& # Write some numbers \& $worksheet\->write( 2, 0, 3 ); # Writes 3 \& $worksheet\->write( 3, 0, 3.00000 ); # Writes 3 \& $worksheet\->write( 4, 0, 3.00001 ); # Writes 3.00001 \& $worksheet\->write( 5, 0, 3.14159 ); # TeX revision no.? \& \& \& # Write some formulas \& $worksheet\->write( 7, 0, \*(Aq=A3 + A6\*(Aq ); \& $worksheet\->write( 8, 0, \*(Aq=IF(A5>3,"Yes", "No")\*(Aq ); \& \& \& # Write a hyperlink \& $worksheet\->write( 10, 0, \*(Aqhttp://www.perl.com/\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: bug_report.pl" .IX Subsection "Example: bug_report.pl" A template for submitting a bug report. .PP Run this program and read the output from the command line. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A template for submitting a bug report. \& # \& # Run this program and read the output from the command line. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& \& print << \*(AqHINTS_1\*(Aq; \& \& REPORTING A BUG OR ASKING A QUESTION \& \& Feel free to report bugs or ask questions. However, to save time \& consider the following steps first: \& \& Read the documentation: \& \& The Excel::Writer::XLSX documentation has been refined in \& response to user questions. Therefore, if you have a question it is \& possible that someone else has asked it before you and that it is \& already addressed in the documentation. Since there is a lot of \& documentation to get through you should at least read the table of \& contents and search for keywords that you are interested in. \& \& Look at the example programs: \& \& There are over 80 example programs shipped with the standard \& Excel::Writer::XLSX distribution. Many of these were created \& in response to user questions. Try to identify an example program \& that corresponds to your query and adapt it to your needs. \& \& HINTS_1 \& print "Press enter ..."; ; \& \& print << \*(AqHINTS_2\*(Aq; \& \& If you submit a bug report here are some pointers. \& \& 1. Put "Excel::Writer::XLSX:" at the beginning of the subject line. \& This helps to filter genuine messages from spam. \& \& 2. Describe the problems as clearly and as concisely as possible. \& \& 3. Send a sample program. It is often easier to describe a problem in \& code than in written prose. \& \& 4. The sample program should be as small as possible to demonstrate the \& problem. Don\*(Aqt copy and past large sections of your program. The \& program should also be self contained and working. \& \& A sample bug report is generated below. If you use this format then it \& will help to analyse your question and respond to it more quickly. \& \& Please don\*(Aqt send patches without contacting the author first. \& \& \& HINTS_2 \& print "Press enter ..."; ; \& \& \& print << \*(AqEMAIL\*(Aq; \& \& ======================================================================= \& \& To: John McNamara \& Subject: Excel::Writer::XLSX: Problem with something. \& \& Hi John, \& \& I am using Excel::Writer::XLSX and I have encountered a problem. I \& want it to do SOMETHING but the module appears to do SOMETHING_ELSE. \& \& Here is some code that demonstrates the problem. \& \& #!/usr/bin/perl \-w \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new("reload.xls"); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->write(0, 0, "Hi Excel!"); \& \& $workbook\->close(); \& \& _\|_END_\|_ \& \& My automatically generated system details are as follows: \& EMAIL \& \& \& print "\en Perl version : $]"; \& print "\en OS name : $^O"; \& print "\en Module versions: (not all are required)\en"; \& \& \& my @modules = qw( \& Excel::Writer::XLSX \& Spreadsheet::WriteExcel \& Archive::Zip \& XML::Writer \& IO::File \& File::Temp \& ); \& \& \& for my $module (@modules) { \& my $version; \& eval "require $module"; \& \& if (not $@) { \& $version = $module\->VERSION; \& $version = \*(Aq(unknown)\*(Aq if not defined $version; \& } \& else { \& $version = \*(Aq(not installed)\*(Aq; \& } \& \& printf "%21s%\-24s\et%s\en", "", $module, $version; \& } \& \& \& print << "BYE"; \& Yours etc., \& \& A. Person \& \-\- \& \& BYE \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: demo.pl" .IX Subsection "Example: demo.pl" A simple demo of some of the features of Excel::Writer::XLSX. .PP This program is used to create the project screenshot for Freshmeat: .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # A simple demo of some of the features of Excel::Writer::XLSX. \& # \& # This program is used to create the project screenshot for Freshmeat: \& # L \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqdemo.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet( \*(AqDemo\*(Aq ); \& my $worksheet2 = $workbook\->add_worksheet( \*(AqAnother sheet\*(Aq ); \& my $worksheet3 = $workbook\->add_worksheet( \*(AqAnd another\*(Aq ); \& \& my $bold = $workbook\->add_format( bold => 1 ); \& \& \& ####################################################################### \& # \& # Write a general heading \& # \& $worksheet\->set_column( \*(AqA:A\*(Aq, 36, $bold ); \& $worksheet\->set_column( \*(AqB:B\*(Aq, 20 ); \& $worksheet\->set_row( 0, 40 ); \& \& my $heading = $workbook\->add_format( \& bold => 1, \& color => \*(Aqblue\*(Aq, \& size => 16, \& merge => 1, \& align => \*(Aqvcenter\*(Aq, \& ); \& \& my @headings = ( \*(AqFeatures of Excel::Writer::XLSX\*(Aq, \*(Aq\*(Aq ); \& $worksheet\->write_row( \*(AqA1\*(Aq, \e@headings, $heading ); \& \& \& ####################################################################### \& # \& # Some text examples \& # \& my $text_format = $workbook\->add_format( \& bold => 1, \& italic => 1, \& color => \*(Aqred\*(Aq, \& size => 18, \& font => \*(AqLucida Calligraphy\*(Aq \& ); \& \& \& $worksheet\->write( \*(AqA2\*(Aq, "Text" ); \& $worksheet\->write( \*(AqB2\*(Aq, "Hello Excel" ); \& $worksheet\->write( \*(AqA3\*(Aq, "Formatted text" ); \& $worksheet\->write( \*(AqB3\*(Aq, "Hello Excel", $text_format ); \& $worksheet\->write( \*(AqA4\*(Aq, "Unicode text" ); \& $worksheet\->write( \*(AqB4\*(Aq, "\ex{0410} \ex{0411} \ex{0412} \ex{0413} \ex{0414}" ); \& \& ####################################################################### \& # \& # Some numeric examples \& # \& my $num1_format = $workbook\->add_format( num_format => \*(Aq$#,##0.00\*(Aq ); \& my $num2_format = $workbook\->add_format( num_format => \*(Aq d mmmm yyy\*(Aq ); \& \& \& $worksheet\->write( \*(AqA5\*(Aq, "Numbers" ); \& $worksheet\->write( \*(AqB5\*(Aq, 1234.56 ); \& $worksheet\->write( \*(AqA6\*(Aq, "Formatted numbers" ); \& $worksheet\->write( \*(AqB6\*(Aq, 1234.56, $num1_format ); \& $worksheet\->write( \*(AqA7\*(Aq, "Formatted numbers" ); \& $worksheet\->write( \*(AqB7\*(Aq, 37257, $num2_format ); \& \& \& ####################################################################### \& # \& # Formulae \& # \& $worksheet\->set_selection( \*(AqB8\*(Aq ); \& $worksheet\->write( \*(AqA8\*(Aq, \*(AqFormulas and functions, "=SIN(PI()/4)"\*(Aq ); \& $worksheet\->write( \*(AqB8\*(Aq, \*(Aq=SIN(PI()/4)\*(Aq ); \& \& \& ####################################################################### \& # \& # Hyperlinks \& # \& $worksheet\->write( \*(AqA9\*(Aq, "Hyperlinks" ); \& $worksheet\->write( \*(AqB9\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq ); \& \& \& ####################################################################### \& # \& # Images \& # \& $worksheet\->write( \*(AqA10\*(Aq, "Images" ); \& $worksheet\->insert_image( \*(AqB10\*(Aq, \*(Aqrepublic.png\*(Aq, \& { x_offset => 16, y_offset => 8 } ); \& \& \& ####################################################################### \& # \& # Misc \& # \& $worksheet\->write( \*(AqA18\*(Aq, "Page/printer setup" ); \& $worksheet\->write( \*(AqA19\*(Aq, "Multiple worksheets" ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: formats.pl" .IX Subsection "Example: formats.pl" Examples of formatting using the Excel::Writer::XLSX module. .PP This program demonstrates almost all possible formatting options. It is worth running this program and viewing the output Excel file if you are interested in the various formatting possibilities. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Examples of formatting using the Excel::Writer::XLSX module. \& # \& # This program demonstrates almost all possible formatting options. It is worth \& # running this program and viewing the output Excel file if you are interested \& # in the various formatting possibilities. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqformats.xlsx\*(Aq ); \& \& # Some common formats \& my $center = $workbook\->add_format( align => \*(Aqcenter\*(Aq ); \& my $heading = $workbook\->add_format( align => \*(Aqcenter\*(Aq, bold => 1 ); \& \& # The named colors \& my %colors = ( \& 0x08, \*(Aqblack\*(Aq, \& 0x0C, \*(Aqblue\*(Aq, \& 0x10, \*(Aqbrown\*(Aq, \& 0x0F, \*(Aqcyan\*(Aq, \& 0x17, \*(Aqgray\*(Aq, \& 0x11, \*(Aqgreen\*(Aq, \& 0x0B, \*(Aqlime\*(Aq, \& 0x0E, \*(Aqmagenta\*(Aq, \& 0x12, \*(Aqnavy\*(Aq, \& 0x35, \*(Aqorange\*(Aq, \& 0x21, \*(Aqpink\*(Aq, \& 0x14, \*(Aqpurple\*(Aq, \& 0x0A, \*(Aqred\*(Aq, \& 0x16, \*(Aqsilver\*(Aq, \& 0x09, \*(Aqwhite\*(Aq, \& 0x0D, \*(Aqyellow\*(Aq, \& \& ); \& \& # Call these subroutines to demonstrate different formatting options \& intro(); \& fonts(); \& named_colors(); \& standard_colors(); \& numeric_formats(); \& borders(); \& patterns(); \& alignment(); \& misc(); \& \& # Note: this is required \& $workbook\->close(); \& \& \& ###################################################################### \& # \& # Intro. \& # \& sub intro { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqIntroduction\*(Aq ); \& \& $worksheet\->set_column( 0, 0, 60 ); \& \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_size( 14 ); \& $format\->set_color( \*(Aqblue\*(Aq ); \& $format\->set_align( \*(Aqcenter\*(Aq ); \& \& my $format2 = $workbook\->add_format(); \& $format2\->set_bold(); \& $format2\->set_color( \*(Aqblue\*(Aq ); \& \& my $format3 = $workbook\->add_format( \& color => \*(Aqblue\*(Aq, \& underline => 1, \& ); \& \& $worksheet\->write( 2, 0, \*(AqThis workbook demonstrates some of\*(Aq, $format ); \& $worksheet\->write( 3, 0, \*(Aqthe formatting options provided by\*(Aq, $format ); \& $worksheet\->write( 4, 0, \*(Aqthe Excel::Writer::XLSX module.\*(Aq, $format ); \& $worksheet\->write( \*(AqA7\*(Aq, \*(AqSections:\*(Aq, $format2 ); \& \& $worksheet\->write( \*(AqA8\*(Aq, "internal:Fonts!A1", \*(AqFonts\*(Aq, $format3 ); \& \& $worksheet\->write( \*(AqA9\*(Aq, "internal:\*(AqNamed colors\*(Aq!A1", \& \*(AqNamed colors\*(Aq, $format3 ); \& \& $worksheet\->write( \& \*(AqA10\*(Aq, \& "internal:\*(AqStandard colors\*(Aq!A1", \& \*(AqStandard colors\*(Aq, $format3 \& ); \& \& $worksheet\->write( \& \*(AqA11\*(Aq, \& "internal:\*(AqNumeric formats\*(Aq!A1", \& \*(AqNumeric formats\*(Aq, $format3 \& ); \& \& $worksheet\->write( \*(AqA12\*(Aq, "internal:Borders!A1", \*(AqBorders\*(Aq, $format3 ); \& $worksheet\->write( \*(AqA13\*(Aq, "internal:Patterns!A1", \*(AqPatterns\*(Aq, $format3 ); \& $worksheet\->write( \*(AqA14\*(Aq, "internal:Alignment!A1", \*(AqAlignment\*(Aq, $format3 ); \& $worksheet\->write( \*(AqA15\*(Aq, "internal:Miscellaneous!A1", \*(AqMiscellaneous\*(Aq, \& $format3 ); \& \& } \& \& \& ###################################################################### \& # \& # Demonstrate the named colors. \& # \& sub named_colors { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqNamed colors\*(Aq ); \& \& $worksheet\->set_column( 0, 3, 15 ); \& \& $worksheet\->write( 0, 0, "Index", $heading ); \& $worksheet\->write( 0, 1, "Index", $heading ); \& $worksheet\->write( 0, 2, "Name", $heading ); \& $worksheet\->write( 0, 3, "Color", $heading ); \& \& my $i = 1; \& \& while ( my ( $index, $color ) = each %colors ) { \& my $format = $workbook\->add_format( \& bg_color => $color, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet\->write( $i + 1, 0, $index, $center ); \& $worksheet\->write( $i + 1, 1, sprintf( "0x%02X", $index ), $center ); \& $worksheet\->write( $i + 1, 2, $color, $center ); \& $worksheet\->write( $i + 1, 3, \*(Aq\*(Aq, $format ); \& $i++; \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel colors in the range 8..63. \& # \& sub standard_colors { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqStandard colors\*(Aq ); \& \& $worksheet\->set_column( 0, 3, 15 ); \& \& $worksheet\->write( 0, 0, "Index", $heading ); \& $worksheet\->write( 0, 1, "Index", $heading ); \& $worksheet\->write( 0, 2, "Color", $heading ); \& $worksheet\->write( 0, 3, "Name", $heading ); \& \& for my $i ( 8 .. 63 ) { \& my $format = $workbook\->add_format( \& bg_color => $i, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet\->write( ( $i \- 7 ), 0, $i, $center ); \& $worksheet\->write( ( $i \- 7 ), 1, sprintf( "0x%02X", $i ), $center ); \& $worksheet\->write( ( $i \- 7 ), 2, \*(Aq\*(Aq, $format ); \& \& # Add the color names \& if ( exists $colors{$i} ) { \& $worksheet\->write( ( $i \- 7 ), 3, $colors{$i}, $center ); \& \& } \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard numeric formats. \& # \& sub numeric_formats { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqNumeric formats\*(Aq ); \& \& $worksheet\->set_column( 0, 4, 15 ); \& $worksheet\->set_column( 5, 5, 45 ); \& \& $worksheet\->write( 0, 0, "Index", $heading ); \& $worksheet\->write( 0, 1, "Index", $heading ); \& $worksheet\->write( 0, 2, "Unformatted", $heading ); \& $worksheet\->write( 0, 3, "Formatted", $heading ); \& $worksheet\->write( 0, 4, "Negative", $heading ); \& $worksheet\->write( 0, 5, "Format", $heading ); \& \& #<<< \& my @formats; \& push @formats, [ 0x00, 1234.567, 0, \*(AqGeneral\*(Aq ]; \& push @formats, [ 0x01, 1234.567, 0, \*(Aq0\*(Aq ]; \& push @formats, [ 0x02, 1234.567, 0, \*(Aq0.00\*(Aq ]; \& push @formats, [ 0x03, 1234.567, 0, \*(Aq#,##0\*(Aq ]; \& push @formats, [ 0x04, 1234.567, 0, \*(Aq#,##0.00\*(Aq ]; \& push @formats, [ 0x05, 1234.567, \-1234.567, \*(Aq($#,##0_);($#,##0)\*(Aq ]; \& push @formats, [ 0x06, 1234.567, \-1234.567, \*(Aq($#,##0_);[Red]($#,##0)\*(Aq ]; \& push @formats, [ 0x07, 1234.567, \-1234.567, \*(Aq($#,##0.00_);($#,##0.00)\*(Aq ]; \& push @formats, [ 0x08, 1234.567, \-1234.567, \*(Aq($#,##0.00_);[Red]($#,##0.00)\*(Aq ]; \& push @formats, [ 0x09, 0.567, 0, \*(Aq0%\*(Aq ]; \& push @formats, [ 0x0a, 0.567, 0, \*(Aq0.00%\*(Aq ]; \& push @formats, [ 0x0b, 1234.567, 0, \*(Aq0.00E+00\*(Aq ]; \& push @formats, [ 0x0c, 0.75, 0, \*(Aq# ?/?\*(Aq ]; \& push @formats, [ 0x0d, 0.3125, 0, \*(Aq# ??/??\*(Aq ]; \& push @formats, [ 0x0e, 36892.521, 0, \*(Aqm/d/yy\*(Aq ]; \& push @formats, [ 0x0f, 36892.521, 0, \*(Aqd\-mmm\-yy\*(Aq ]; \& push @formats, [ 0x10, 36892.521, 0, \*(Aqd\-mmm\*(Aq ]; \& push @formats, [ 0x11, 36892.521, 0, \*(Aqmmm\-yy\*(Aq ]; \& push @formats, [ 0x12, 36892.521, 0, \*(Aqh:mm AM/PM\*(Aq ]; \& push @formats, [ 0x13, 36892.521, 0, \*(Aqh:mm:ss AM/PM\*(Aq ]; \& push @formats, [ 0x14, 36892.521, 0, \*(Aqh:mm\*(Aq ]; \& push @formats, [ 0x15, 36892.521, 0, \*(Aqh:mm:ss\*(Aq ]; \& push @formats, [ 0x16, 36892.521, 0, \*(Aqm/d/yy h:mm\*(Aq ]; \& push @formats, [ 0x25, 1234.567, \-1234.567, \*(Aq(#,##0_);(#,##0)\*(Aq ]; \& push @formats, [ 0x26, 1234.567, \-1234.567, \*(Aq(#,##0_);[Red](#,##0)\*(Aq ]; \& push @formats, [ 0x27, 1234.567, \-1234.567, \*(Aq(#,##0.00_);(#,##0.00)\*(Aq ]; \& push @formats, [ 0x28, 1234.567, \-1234.567, \*(Aq(#,##0.00_);[Red](#,##0.00)\*(Aq ]; \& push @formats, [ 0x29, 1234.567, \-1234.567, \*(Aq_(* #,##0_);_(* (#,##0);_(* "\-"_);_(@_)\*(Aq ]; \& push @formats, [ 0x2a, 1234.567, \-1234.567, \*(Aq_($* #,##0_);_($* (#,##0);_($* "\-"_);_(@_)\*(Aq ]; \& push @formats, [ 0x2b, 1234.567, \-1234.567, \*(Aq_(* #,##0.00_);_(* (#,##0.00);_(* "\-"??_);_(@_)\*(Aq ]; \& push @formats, [ 0x2c, 1234.567, \-1234.567, \*(Aq_($* #,##0.00_);_($* (#,##0.00);_($* "\-"??_);_(@_)\*(Aq ]; \& push @formats, [ 0x2d, 36892.521, 0, \*(Aqmm:ss\*(Aq ]; \& push @formats, [ 0x2e, 3.0153, 0, \*(Aq[h]:mm:ss\*(Aq ]; \& push @formats, [ 0x2f, 36892.521, 0, \*(Aqmm:ss.0\*(Aq ]; \& push @formats, [ 0x30, 1234.567, 0, \*(Aq##0.0E+0\*(Aq ]; \& push @formats, [ 0x31, 1234.567, 0, \*(Aq@\*(Aq ]; \& #>>> \& \& my $i; \& foreach my $format ( @formats ) { \& my $style = $workbook\->add_format(); \& $style\->set_num_format( $format\->[0] ); \& \& $i++; \& $worksheet\->write( $i, 0, $format\->[0], $center ); \& $worksheet\->write( $i, 1, sprintf( "0x%02X", $format\->[0] ), $center ); \& $worksheet\->write( $i, 2, $format\->[1], $center ); \& $worksheet\->write( $i, 3, $format\->[1], $style ); \& \& if ( $format\->[2] ) { \& $worksheet\->write( $i, 4, $format\->[2], $style ); \& } \& \& $worksheet\->write_string( $i, 5, $format\->[3] ); \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the font options. \& # \& sub fonts { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqFonts\*(Aq ); \& \& $worksheet\->set_column( 0, 0, 30 ); \& $worksheet\->set_column( 1, 1, 10 ); \& \& $worksheet\->write( 0, 0, "Font name", $heading ); \& $worksheet\->write( 0, 1, "Font size", $heading ); \& \& my @fonts; \& push @fonts, [ 10, \*(AqArial\*(Aq ]; \& push @fonts, [ 12, \*(AqArial\*(Aq ]; \& push @fonts, [ 14, \*(AqArial\*(Aq ]; \& push @fonts, [ 12, \*(AqArial Black\*(Aq ]; \& push @fonts, [ 12, \*(AqArial Narrow\*(Aq ]; \& push @fonts, [ 12, \*(AqCentury Schoolbook\*(Aq ]; \& push @fonts, [ 12, \*(AqCourier\*(Aq ]; \& push @fonts, [ 12, \*(AqCourier New\*(Aq ]; \& push @fonts, [ 12, \*(AqGaramond\*(Aq ]; \& push @fonts, [ 12, \*(AqImpact\*(Aq ]; \& push @fonts, [ 12, \*(AqLucida Handwriting\*(Aq ]; \& push @fonts, [ 12, \*(AqTimes New Roman\*(Aq ]; \& push @fonts, [ 12, \*(AqSymbol\*(Aq ]; \& push @fonts, [ 12, \*(AqWingdings\*(Aq ]; \& push @fonts, [ 12, \*(AqA font that doesn\e\*(Aqt exist\*(Aq ]; \& \& my $i; \& foreach my $font ( @fonts ) { \& my $format = $workbook\->add_format(); \& \& $format\->set_size( $font\->[0] ); \& $format\->set_font( $font\->[1] ); \& \& $i++; \& $worksheet\->write( $i, 0, $font\->[1], $format ); \& $worksheet\->write( $i, 1, $font\->[0], $format ); \& } \& \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel border styles. \& # \& sub borders { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqBorders\*(Aq ); \& \& $worksheet\->set_column( 0, 4, 10 ); \& $worksheet\->set_column( 5, 5, 40 ); \& \& $worksheet\->write( 0, 0, "Index", $heading ); \& $worksheet\->write( 0, 1, "Index", $heading ); \& $worksheet\->write( 0, 3, "Style", $heading ); \& $worksheet\->write( 0, 5, "The style is highlighted in red for ", $heading ); \& $worksheet\->write( 1, 5, "emphasis, the default color is black.", \& $heading ); \& \& for my $i ( 0 .. 13 ) { \& my $format = $workbook\->add_format(); \& $format\->set_border( $i ); \& $format\->set_border_color( \*(Aqred\*(Aq ); \& $format\->set_align( \*(Aqcenter\*(Aq ); \& \& $worksheet\->write( ( 2 * ( $i + 1 ) ), 0, $i, $center ); \& $worksheet\->write( ( 2 * ( $i + 1 ) ), \& 1, sprintf( "0x%02X", $i ), $center ); \& \& $worksheet\->write( ( 2 * ( $i + 1 ) ), 3, "Border", $format ); \& } \& \& $worksheet\->write( 30, 0, "Diag type", $heading ); \& $worksheet\->write( 30, 1, "Index", $heading ); \& $worksheet\->write( 30, 3, "Style", $heading ); \& $worksheet\->write( 30, 5, "Diagonal Border styles", $heading ); \& \& for my $i ( 1 .. 3 ) { \& my $format = $workbook\->add_format(); \& $format\->set_diag_type( $i ); \& $format\->set_diag_border( 1 ); \& $format\->set_diag_color( \*(Aqred\*(Aq ); \& $format\->set_align( \*(Aqcenter\*(Aq ); \& \& $worksheet\->write( ( 2 * ( $i + 15 ) ), 0, $i, $center ); \& $worksheet\->write( ( 2 * ( $i + 15 ) ), \& 1, sprintf( "0x%02X", $i ), $center ); \& \& $worksheet\->write( ( 2 * ( $i + 15 ) ), 3, "Border", $format ); \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel cell patterns. \& # \& sub patterns { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqPatterns\*(Aq ); \& \& $worksheet\->set_column( 0, 4, 10 ); \& $worksheet\->set_column( 5, 5, 50 ); \& \& $worksheet\->write( 0, 0, "Index", $heading ); \& $worksheet\->write( 0, 1, "Index", $heading ); \& $worksheet\->write( 0, 3, "Pattern", $heading ); \& \& $worksheet\->write( 0, 5, "The background colour has been set to silver.", \& $heading ); \& $worksheet\->write( 1, 5, "The foreground colour has been set to green.", \& $heading ); \& \& for my $i ( 0 .. 18 ) { \& my $format = $workbook\->add_format(); \& \& $format\->set_pattern( $i ); \& $format\->set_bg_color( \*(Aqsilver\*(Aq ); \& $format\->set_fg_color( \*(Aqgreen\*(Aq ); \& $format\->set_align( \*(Aqcenter\*(Aq ); \& \& $worksheet\->write( ( 2 * ( $i + 1 ) ), 0, $i, $center ); \& $worksheet\->write( ( 2 * ( $i + 1 ) ), \& 1, sprintf( "0x%02X", $i ), $center ); \& \& $worksheet\->write( ( 2 * ( $i + 1 ) ), 3, "Pattern", $format ); \& \& if ( $i == 1 ) { \& $worksheet\->write( ( 2 * ( $i + 1 ) ), \& 5, "This is solid colour, the most useful pattern.", $heading ); \& } \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel cell alignments. \& # \& sub alignment { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqAlignment\*(Aq ); \& \& $worksheet\->set_column( 0, 7, 12 ); \& $worksheet\->set_row( 0, 40 ); \& $worksheet\->set_selection( 7, 0 ); \& \& my $format01 = $workbook\->add_format(); \& my $format02 = $workbook\->add_format(); \& my $format03 = $workbook\->add_format(); \& my $format04 = $workbook\->add_format(); \& my $format05 = $workbook\->add_format(); \& my $format06 = $workbook\->add_format(); \& my $format07 = $workbook\->add_format(); \& my $format08 = $workbook\->add_format(); \& my $format09 = $workbook\->add_format(); \& my $format10 = $workbook\->add_format(); \& my $format11 = $workbook\->add_format(); \& my $format12 = $workbook\->add_format(); \& my $format13 = $workbook\->add_format(); \& my $format14 = $workbook\->add_format(); \& my $format15 = $workbook\->add_format(); \& my $format16 = $workbook\->add_format(); \& my $format17 = $workbook\->add_format(); \& \& $format02\->set_align( \*(Aqtop\*(Aq ); \& $format03\->set_align( \*(Aqbottom\*(Aq ); \& $format04\->set_align( \*(Aqvcenter\*(Aq ); \& $format05\->set_align( \*(Aqvjustify\*(Aq ); \& $format06\->set_text_wrap(); \& \& $format07\->set_align( \*(Aqleft\*(Aq ); \& $format08\->set_align( \*(Aqright\*(Aq ); \& $format09\->set_align( \*(Aqcenter\*(Aq ); \& $format10\->set_align( \*(Aqfill\*(Aq ); \& $format11\->set_align( \*(Aqjustify\*(Aq ); \& $format12\->set_merge(); \& \& $format13\->set_rotation( 45 ); \& $format14\->set_rotation( \-45 ); \& $format15\->set_rotation( 270 ); \& \& $format16\->set_shrink(); \& $format17\->set_indent( 1 ); \& \& $worksheet\->write( 0, 0, \*(AqVertical\*(Aq, $heading ); \& $worksheet\->write( 0, 1, \*(Aqtop\*(Aq, $format02 ); \& $worksheet\->write( 0, 2, \*(Aqbottom\*(Aq, $format03 ); \& $worksheet\->write( 0, 3, \*(Aqvcenter\*(Aq, $format04 ); \& $worksheet\->write( 0, 4, \*(Aqvjustify\*(Aq, $format05 ); \& $worksheet\->write( 0, 5, "text\enwrap", $format06 ); \& \& $worksheet\->write( 2, 0, \*(AqHorizontal\*(Aq, $heading ); \& $worksheet\->write( 2, 1, \*(Aqleft\*(Aq, $format07 ); \& $worksheet\->write( 2, 2, \*(Aqright\*(Aq, $format08 ); \& $worksheet\->write( 2, 3, \*(Aqcenter\*(Aq, $format09 ); \& $worksheet\->write( 2, 4, \*(Aqfill\*(Aq, $format10 ); \& $worksheet\->write( 2, 5, \*(Aqjustify\*(Aq, $format11 ); \& \& $worksheet\->write( 3, 1, \*(Aqmerge\*(Aq, $format12 ); \& $worksheet\->write( 3, 2, \*(Aq\*(Aq, $format12 ); \& \& $worksheet\->write( 3, 3, \*(AqShrink \*(Aq x 3, $format16 ); \& $worksheet\->write( 3, 4, \*(AqIndent\*(Aq, $format17 ); \& \& \& $worksheet\->write( 5, 0, \*(AqRotation\*(Aq, $heading ); \& $worksheet\->write( 5, 1, \*(AqRotate 45\*(Aq, $format13 ); \& $worksheet\->write( 6, 1, \*(AqRotate \-45\*(Aq, $format14 ); \& $worksheet\->write( 7, 1, \*(AqRotate 270\*(Aq, $format15 ); \& } \& \& \& ###################################################################### \& # \& # Demonstrate other miscellaneous features. \& # \& sub misc { \& \& my $worksheet = $workbook\->add_worksheet( \*(AqMiscellaneous\*(Aq ); \& \& $worksheet\->set_column( 2, 2, 25 ); \& \& my $format01 = $workbook\->add_format(); \& my $format02 = $workbook\->add_format(); \& my $format03 = $workbook\->add_format(); \& my $format04 = $workbook\->add_format(); \& my $format05 = $workbook\->add_format(); \& my $format06 = $workbook\->add_format(); \& my $format07 = $workbook\->add_format(); \& \& $format01\->set_underline( 0x01 ); \& $format02\->set_underline( 0x02 ); \& $format03\->set_underline( 0x21 ); \& $format04\->set_underline( 0x22 ); \& $format05\->set_font_strikeout(); \& $format06\->set_font_outline(); \& $format07\->set_font_shadow(); \& \& $worksheet\->write( 1, 2, \*(AqUnderline 0x01\*(Aq, $format01 ); \& $worksheet\->write( 3, 2, \*(AqUnderline 0x02\*(Aq, $format02 ); \& $worksheet\->write( 5, 2, \*(AqUnderline 0x21\*(Aq, $format03 ); \& $worksheet\->write( 7, 2, \*(AqUnderline 0x22\*(Aq, $format04 ); \& $worksheet\->write( 9, 2, \*(AqStrikeout\*(Aq, $format05 ); \& $worksheet\->write( 11, 2, \*(AqOutline (Macintosh only)\*(Aq, $format06 ); \& $worksheet\->write( 13, 2, \*(AqShadow (Macintosh only)\*(Aq, $format07 ); \& } \& \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: regions.pl" .IX Subsection "Example: regions.pl" An example of how to use the Excel::Writer::XLSX module to write a basic Excel workbook with multiple worksheets. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # An example of how to use the Excel::Writer::XLSX module to write a basic \& # Excel workbook with multiple worksheets. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& # Create a new Excel workbook \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqregions.xlsx\*(Aq ); \& \& # Add some worksheets \& my $north = $workbook\->add_worksheet( "North" ); \& my $south = $workbook\->add_worksheet( "South" ); \& my $east = $workbook\->add_worksheet( "East" ); \& my $west = $workbook\->add_worksheet( "West" ); \& \& # Add a Format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_color( \*(Aqblue\*(Aq ); \& \& # Add a caption to each worksheet \& foreach my $worksheet ( $workbook\->sheets() ) { \& $worksheet\->write( 0, 0, "Sales", $format ); \& } \& \& # Write some data \& $north\->write( 0, 1, 200000 ); \& $south\->write( 0, 1, 100000 ); \& $east\->write( 0, 1, 150000 ); \& $west\->write( 0, 1, 100000 ); \& \& # Set the active worksheet \& $south\->activate(); \& \& # Set the width of the first column \& $south\->set_column( 0, 0, 20 ); \& \& # Set the active cell \& $south\->set_selection( 0, 1 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: stats.pl" .IX Subsection "Example: stats.pl" A simple example of how to use functions with the Excel::Writer::XLSX module. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # A simple example of how to use functions with the Excel::Writer::XLSX \& # module. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqstats.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet( \*(AqTest data\*(Aq ); \& \& # Set the column width for columns 1 \& $worksheet\->set_column( 0, 0, 20 ); \& \& \& # Create a format for the headings \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& \& \& # Write the sample data \& $worksheet\->write( 0, 0, \*(AqSample\*(Aq, $format ); \& $worksheet\->write( 0, 1, 1 ); \& $worksheet\->write( 0, 2, 2 ); \& $worksheet\->write( 0, 3, 3 ); \& $worksheet\->write( 0, 4, 4 ); \& $worksheet\->write( 0, 5, 5 ); \& $worksheet\->write( 0, 6, 6 ); \& $worksheet\->write( 0, 7, 7 ); \& $worksheet\->write( 0, 8, 8 ); \& \& $worksheet\->write( 1, 0, \*(AqLength\*(Aq, $format ); \& $worksheet\->write( 1, 1, 25.4 ); \& $worksheet\->write( 1, 2, 25.4 ); \& $worksheet\->write( 1, 3, 24.8 ); \& $worksheet\->write( 1, 4, 25.0 ); \& $worksheet\->write( 1, 5, 25.3 ); \& $worksheet\->write( 1, 6, 24.9 ); \& $worksheet\->write( 1, 7, 25.2 ); \& $worksheet\->write( 1, 8, 24.8 ); \& \& # Write some statistical functions \& $worksheet\->write( 4, 0, \*(AqCount\*(Aq, $format ); \& $worksheet\->write( 4, 1, \*(Aq=COUNT(B1:I1)\*(Aq ); \& \& $worksheet\->write( 5, 0, \*(AqSum\*(Aq, $format ); \& $worksheet\->write( 5, 1, \*(Aq=SUM(B2:I2)\*(Aq ); \& \& $worksheet\->write( 6, 0, \*(AqAverage\*(Aq, $format ); \& $worksheet\->write( 6, 1, \*(Aq=AVERAGE(B2:I2)\*(Aq ); \& \& $worksheet\->write( 7, 0, \*(AqMin\*(Aq, $format ); \& $worksheet\->write( 7, 1, \*(Aq=MIN(B2:I2)\*(Aq ); \& \& $worksheet\->write( 8, 0, \*(AqMax\*(Aq, $format ); \& $worksheet\->write( 8, 1, \*(Aq=MAX(B2:I2)\*(Aq ); \& \& $worksheet\->write( 9, 0, \*(AqStandard Deviation\*(Aq, $format ); \& $worksheet\->write( 9, 1, \*(Aq=STDEV(B2:I2)\*(Aq ); \& \& $worksheet\->write( 10, 0, \*(AqKurtosis\*(Aq, $format ); \& $worksheet\->write( 10, 1, \*(Aq=KURT(B2:I2)\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: autofit.pl" .IX Subsection "Example: autofit.pl" An example of using simulated autofit to automatically adjust the width of worksheet columns based on the data in the cells. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # An example of using simulated autofit to automatically adjust the width of \& # worksheet columns based on the data in the cells. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqautofit.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Write some worksheet data to demonstrate autofitting. \& $worksheet\->write( 0, 0, "Foo" ); \& $worksheet\->write( 1, 0, "Food" ); \& $worksheet\->write( 2, 0, "Foody" ); \& $worksheet\->write( 3, 0, "Froody" ); \& \& $worksheet\->write( 0, 1, 12345 ); \& $worksheet\->write( 1, 1, 12345678 ); \& $worksheet\->write( 2, 1, 12345 ); \& \& $worksheet\->write( 0, 2, "Some longer text" ); \& \& $worksheet\->write( 0, 3, "http://ww.google.com" ); \& $worksheet\->write( 1, 3, "https://github.com" ); \& \& # Autofit the worksheet. \& $worksheet\->autofit(); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: autofilter.pl" .IX Subsection "Example: autofilter.pl" An example of how to create autofilters with Excel::Writer::XLSX. .PP An autofilter is a way of adding drop down lists to the headers of a 2D range of worksheet data. This allows users to filter the data based on simple criteria so that some data is shown and some is hidden. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # An example of how to create autofilters with Excel::Writer::XLSX. \& # \& # An autofilter is a way of adding drop down lists to the headers of a 2D range \& # of worksheet data. This allows users to filter the data based on \& # simple criteria so that some data is shown and some is hidden. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqautofilter.xlsx\*(Aq ); \& \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& my $worksheet4 = $workbook\->add_worksheet(); \& my $worksheet5 = $workbook\->add_worksheet(); \& my $worksheet6 = $workbook\->add_worksheet(); \& my $worksheet7 = $workbook\->add_worksheet(); \& \& my $bold = $workbook\->add_format( bold => 1 ); \& \& \& # Extract the data embedded at the end of this file. \& my @headings = split \*(Aq \*(Aq, ; \& my @data; \& push @data, [split] while ; \& \& \& # Set up several sheets with the same data. \& for my $worksheet ( $workbook\->sheets() ) { \& $worksheet\->set_column( \*(AqA:D\*(Aq, 12 ); \& $worksheet\->set_row( 0, 20, $bold ); \& $worksheet\->write( \*(AqA1\*(Aq, \e@headings ); \& } \& \& \& ############################################################################### \& # \& # Example 1. Autofilter without conditions. \& # \& \& $worksheet1\->autofilter( \*(AqA1:D51\*(Aq ); \& $worksheet1\->write( \*(AqA2\*(Aq, [ [@data] ] ); \& \& \& ############################################################################### \& # \& # \& # Example 2. Autofilter with a filter condition in the first column. \& # \& \& # The range in this example is the same as above but in row\-column notation. \& $worksheet2\->autofilter( 0, 0, 50, 3 ); \& \& # The placeholder "Region" in the filter is ignored and can be any string \& # that adds clarity to the expression. \& # \& $worksheet2\->filter_column( 0, \*(AqRegion eq East\*(Aq ); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& my $row = 1; \& \& for my $row_data ( @data ) { \& my $region = $row_data\->[0]; \& \& if ( $region eq \*(AqEast\*(Aq ) { \& \& # Row is visible. \& } \& else { \& \& # Hide row. \& $worksheet2\->set_row( $row, undef, undef, 1 ); \& } \& \& $worksheet2\->write( $row++, 0, $row_data ); \& } \& \& \& ############################################################################### \& # \& # \& # Example 3. Autofilter with a dual filter condition in one of the columns. \& # \& \& $worksheet3\->autofilter( \*(AqA1:D51\*(Aq ); \& \& $worksheet3\->filter_column( \*(AqA\*(Aq, \*(Aqx eq East or x eq South\*(Aq ); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data ( @data ) { \& my $region = $row_data\->[0]; \& \& if ( $region eq \*(AqEast\*(Aq or $region eq \*(AqSouth\*(Aq ) { \& \& # Row is visible. \& } \& else { \& \& # Hide row. \& $worksheet3\->set_row( $row, undef, undef, 1 ); \& } \& \& $worksheet3\->write( $row++, 0, $row_data ); \& } \& \& \& ############################################################################### \& # \& # \& # Example 4. Autofilter with filter conditions in two columns. \& # \& \& $worksheet4\->autofilter( \*(AqA1:D51\*(Aq ); \& \& $worksheet4\->filter_column( \*(AqA\*(Aq, \*(Aqx eq East\*(Aq ); \& $worksheet4\->filter_column( \*(AqC\*(Aq, \*(Aqx > 3000 and x < 8000\*(Aq ); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data ( @data ) { \& my $region = $row_data\->[0]; \& my $volume = $row_data\->[2]; \& \& if ( $region eq \*(AqEast\*(Aq \& and $volume > 3000 \& and $volume < 8000 ) \& { \& \& # Row is visible. \& } \& else { \& \& # Hide row. \& $worksheet4\->set_row( $row, undef, undef, 1 ); \& } \& \& $worksheet4\->write( $row++, 0, $row_data ); \& } \& \& \& ############################################################################### \& # \& # \& # Example 5. Autofilter with filter list condition in one of the columns. \& # \& \& $worksheet5\->autofilter( \*(AqA1:D51\*(Aq ); \& \& $worksheet5\->filter_column_list( \*(AqA\*(Aq, (\*(AqEast\*(Aq, \*(AqNorth\*(Aq, \*(AqSouth\*(Aq) ); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data ( @data ) { \& my $region = $row_data\->[0]; \& \& if ( $region eq \*(AqEast\*(Aq or $region eq \*(AqNorth\*(Aq or $region eq \*(AqSouth\*(Aq ) { \& \& # Row is visible. \& } \& else { \& \& # Hide row. \& $worksheet5\->set_row( $row, undef, undef, 1 ); \& } \& \& $worksheet5\->write( $row++, 0, $row_data ); \& } \& \& \& ############################################################################### \& # \& # \& # Example 6. Autofilter with filter for blanks. \& # \& \& # Create a blank cell in our test data. \& $data[5]\->[0] = \*(Aq\*(Aq; \& \& \& $worksheet6\->autofilter( \*(AqA1:D51\*(Aq ); \& $worksheet6\->filter_column( \*(AqA\*(Aq, \*(Aqx == Blanks\*(Aq ); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data ( @data ) { \& my $region = $row_data\->[0]; \& \& if ( $region eq \*(Aq\*(Aq ) { \& \& # Row is visible. \& } \& else { \& \& # Hide row. \& $worksheet6\->set_row( $row, undef, undef, 1 ); \& } \& \& $worksheet6\->write( $row++, 0, $row_data ); \& } \& \& \& ############################################################################### \& # \& # \& # Example 7. Autofilter with filter for non\-blanks. \& # \& \& \& $worksheet7\->autofilter( \*(AqA1:D51\*(Aq ); \& $worksheet7\->filter_column( \*(AqA\*(Aq, \*(Aqx == NonBlanks\*(Aq ); \& \& # \& # Hide the rows that don\*(Aqt match the filter criteria. \& # \& $row = 1; \& \& for my $row_data ( @data ) { \& my $region = $row_data\->[0]; \& \& if ( $region ne \*(Aq\*(Aq ) { \& \& # Row is visible. \& } \& else { \& \& # Hide row. \& $worksheet7\->set_row( $row, undef, undef, 1 ); \& } \& \& $worksheet7\->write( $row++, 0, $row_data ); \& } \& \& $workbook\->close(); \& \& _\|_DATA_\|_ \& Region Item Volume Month \& East Apple 9000 July \& East Apple 5000 July \& South Orange 9000 September \& North Apple 2000 November \& West Apple 9000 November \& South Pear 7000 October \& North Pear 9000 August \& West Orange 1000 December \& West Grape 1000 November \& South Pear 10000 April \& West Grape 6000 January \& South Orange 3000 May \& North Apple 3000 December \& South Apple 7000 February \& West Grape 1000 December \& East Grape 8000 February \& South Grape 10000 June \& West Pear 7000 December \& South Apple 2000 October \& East Grape 7000 December \& North Grape 6000 April \& East Pear 8000 February \& North Apple 7000 August \& North Orange 7000 July \& North Apple 6000 June \& South Grape 8000 September \& West Apple 3000 October \& South Orange 10000 November \& West Grape 4000 July \& North Orange 5000 August \& East Orange 1000 November \& East Orange 4000 October \& North Grape 5000 August \& East Apple 1000 December \& South Apple 10000 March \& East Grape 7000 October \& West Grape 1000 September \& East Grape 10000 October \& South Orange 8000 March \& North Apple 4000 July \& South Orange 5000 July \& West Apple 4000 June \& East Apple 5000 April \& North Pear 3000 August \& East Grape 9000 November \& North Orange 8000 October \& East Apple 10000 June \& South Pear 1000 December \& North Grape 10000 July \& East Grape 6000 February .Ve .PP Download this example: .SS "Example: array_formula.pl" .IX Subsection "Example: array_formula.pl" Example of how to use the Excel::Writer::XLSX module to write simple array formulas. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX module to write simple \& # array formulas. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqarray_formula.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Write some test data. \& $worksheet\->write( \*(AqB1\*(Aq, [ [ 500, 10 ], [ 300, 15 ] ] ); \& $worksheet\->write( \*(AqB5\*(Aq, [ [ 1, 2, 3 ], [ 20234, 21003, 10000 ] ] ); \& \& # Write an array formula that returns a single value \& $worksheet\->write( \*(AqA1\*(Aq, \*(Aq{=SUM(B1:C1*B2:C2)}\*(Aq ); \& \& # Same as above but more verbose. \& $worksheet\->write_array_formula( \*(AqA2:A2\*(Aq, \*(Aq{=SUM(B1:C1*B2:C2)}\*(Aq ); \& \& # Write an array formula that returns a range of values \& $worksheet\->write_array_formula( \*(AqA5:A7\*(Aq, \*(Aq{=TREND(C5:C7,B5:B7)}\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: cgi.pl" .IX Subsection "Example: cgi.pl" Example of how to use the Excel::Writer::XLSX module to send an Excel file to a browser in a \s-1CGI\s0 program. .PP On Windows the hash-bang line should be something like: .PP .Vb 1 \& #!C:\ePerl\ebin\eperl.exe .Ve .PP The \*(L"Content-Disposition\*(R" line will cause a prompt to be generated to save the file. If you want to stream the file to the browser instead, comment out that line as shown below. .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX module to send an Excel \& # file to a browser in a CGI program. \& # \& # On Windows the hash\-bang line should be something like: \& # \& # #!C:\ePerl\ebin\eperl.exe \& # \& # The "Content\-Disposition" line will cause a prompt to be generated to save \& # the file. If you want to stream the file to the browser instead, comment out \& # that line as shown below. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Set the filename and send the content type \& my $filename = "cgitest.xlsx"; \& \& print "Content\-type: application/vnd.openxmlformats\-officedocument.spreadsheetml.sheet\en"; \& \& # The Content\-Disposition will generate a prompt to save the file. If you want \& # to stream the file to the browser, comment out the following line. \& print "Content\-Disposition: attachment; filename=$filename\en"; \& print "\en"; \& \& # Redirect the output to STDOUT. Binmode the filehandle in case it is needed. \& binmode STDOUT; \& \& my $workbook = Excel::Writer::XLSX\->new( \e*STDOUT ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Set the column width for column 1 \& $worksheet\->set_column( 0, 0, 20 ); \& \& \& # Create a format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_size( 15 ); \& $format\->set_color( \*(Aqblue\*(Aq ); \& \& \& # Write to the workbook \& $worksheet\->write( 0, 0, "Hi Excel!", $format ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_area.pl" .IX Subsection "Example: chart_area.pl" A demo of an Area chart in Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of an Area chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_area.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqBatch 1\*(Aq, \*(AqBatch 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 40, 40, 50, 30, 25, 50 ], \& [ 30, 25, 30, 10, 5, 10 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqarea\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. Note alternative use of array ref to define \& # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart1\->set_title ( name => \*(AqResults of sample analysis\*(Aq ); \& $chart1\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart1\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart1\->set_style( 11 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a stacked chart sub\-type \& # \& my $chart2 = $workbook\->add_chart( \& type => \*(Aqarea\*(Aq, \& embedded => 1, \& subtype => \*(Aqstacked\*(Aq \& ); \& \& # Configure the first series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart2\->set_title ( name => \*(AqStacked Chart\*(Aq ); \& $chart2\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart2\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart2\->set_style( 12 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a percent stacked chart sub\-type \& # \& my $chart3 = $workbook\->add_chart( \& type => \*(Aqarea\*(Aq, \& embedded => 1, \& subtype => \*(Aqpercent_stacked\*(Aq \& ); \& \& # Configure the first series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart3\->set_title ( name => \*(AqPercent Stacked Chart\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart3\->set_style( 13 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_bar.pl" .IX Subsection "Example: chart_bar.pl" A demo of an Bar chart in Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of an Bar chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_bar.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqBatch 1\*(Aq, \*(AqBatch 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 10, 40, 50, 20, 10, 50 ], \& [ 30, 60, 70, 50, 40, 30 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqbar\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. Note alternative use of array ref to define \& # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart1\->set_title ( name => \*(AqResults of sample analysis\*(Aq ); \& $chart1\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart1\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart1\->set_style( 11 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a stacked chart sub\-type \& # \& my $chart2 = $workbook\->add_chart( \& type => \*(Aqbar\*(Aq, \& embedded => 1, \& subtype => \*(Aqstacked\*(Aq \& ); \& \& # Configure the first series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart2\->set_title ( name => \*(AqStacked Chart\*(Aq ); \& $chart2\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart2\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart2\->set_style( 12 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a percent stacked chart sub\-type \& # \& my $chart3 = $workbook\->add_chart( \& type => \*(Aqbar\*(Aq, \& embedded => 1, \& subtype => \*(Aqpercent_stacked\*(Aq \& ); \& \& # Configure the first series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart3\->set_title ( name => \*(AqPercent Stacked Chart\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart3\->set_style( 13 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_column.pl" .IX Subsection "Example: chart_column.pl" A demo of a Column chart in Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a Column chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_column.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqBatch 1\*(Aq, \*(AqBatch 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 10, 40, 50, 20, 10, 50 ], \& [ 30, 60, 70, 50, 40, 30 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. Note alternative use of array ref to define \& # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart1\->set_title ( name => \*(AqResults of sample analysis\*(Aq ); \& $chart1\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart1\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart1\->set_style( 11 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a stacked chart sub\-type \& # \& my $chart2 = $workbook\->add_chart( \& type => \*(Aqcolumn\*(Aq, \& embedded => 1, \& subtype => \*(Aqstacked\*(Aq \& ); \& \& # Configure the first series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart2\->set_title ( name => \*(AqStacked Chart\*(Aq ); \& $chart2\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart2\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart2\->set_style( 12 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a percent stacked chart sub\-type \& # \& my $chart3 = $workbook\->add_chart( \& type => \*(Aqcolumn\*(Aq, \& embedded => 1, \& subtype => \*(Aqpercent_stacked\*(Aq \& ); \& \& # Configure the first series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart3\->set_title ( name => \*(AqPercent Stacked Chart\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart3\->set_style( 13 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_line.pl" .IX Subsection "Example: chart_line.pl" A demo of a Line chart in Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a Line chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_line.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqBatch 1\*(Aq, \*(AqBatch 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 10, 40, 50, 20, 10, 50 ], \& [ 30, 60, 70, 50, 40, 30 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. Note alternative use of array ref to define \& # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& $chart\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart\->set_title ( name => \*(AqResults of sample analysis\*(Aq ); \& $chart\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Colors with white outline and shadow. \& $chart\->set_style( 10 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a stacked chart sub\-type \& # \& my $chart2 = $workbook\->add_chart( \& type => \*(Aqline\*(Aq, \& embedded => 1, \& subtype => \*(Aqstacked\*(Aq \& ); \& \& # Configure the first series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart2\->set_title ( name => \*(AqStacked Chart\*(Aq ); \& $chart2\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart2\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart2\->set_style( 12 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a percent stacked chart sub\-type \& # \& my $chart3 = $workbook\->add_chart( \& type => \*(Aqline\*(Aq, \& embedded => 1, \& subtype => \*(Aqpercent_stacked\*(Aq \& ); \& \& # Configure the first series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart3\->set_title ( name => \*(AqPercent Stacked Chart\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart3\->set_style( 13 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_pie.pl" .IX Subsection "Example: chart_pie.pl" A demo of a Pie chart in Excel::Writer::XLSX. .PP The demo also shows how to set segment colours. It is possible to define chart colors for most types of Excel::Writer::XLSX charts via the \&\fBadd_series()\fR method. However, Pie and Doughtnut charts are a special case since each segment is represented as a point so it is necessary to assign formatting to each point in the series. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a Pie chart in Excel::Writer::XLSX. \& # \& # The demo also shows how to set segment colours. It is possible to define \& # chart colors for most types of Excel::Writer::XLSX charts via the \& # add_series() method. However, Pie and Doughtnut charts are a special case \& # since each segment is represented as a point so it is necessary to assign \& # formatting to each point in the series. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_pie.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqCategory\*(Aq, \*(AqValues\*(Aq ]; \& my $data = [ \& [ \*(AqApple\*(Aq, \*(AqCherry\*(Aq, \*(AqPecan\*(Aq ], \& [ 60, 30, 10 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqpie\*(Aq, embedded => 1 ); \& \& # Configure the series. Note the use of the array ref to define ranges: \& # [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& # See below for an alternative syntax. \& $chart1\->add_series( \& name => \*(AqPie sales data\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 3, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 3, 1, 1 ], \& ); \& \& # Add a title. \& $chart1\->set_title( name => \*(AqPopular Pie Types\*(Aq ); \& \& # Set an Excel chart style. Colors with white outline and shadow. \& $chart1\->set_style( 10 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqC2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a Pie chart with user defined segment colors. \& # \& \& # Create an example Pie chart like above. \& my $chart2 = $workbook\->add_chart( type => \*(Aqpie\*(Aq, embedded => 1 ); \& \& # Configure the series and add user defined segment colours. \& $chart2\->add_series( \& name => \*(AqPie sales data\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& points => [ \& { fill => { color => \*(Aq#5ABA10\*(Aq } }, \& { fill => { color => \*(Aq#FE110E\*(Aq } }, \& { fill => { color => \*(Aq#CA5C05\*(Aq } }, \& ], \& ); \& \& # Add a title. \& $chart2\->set_title( name => \*(AqPie Chart with user defined colors\*(Aq ); \& \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqC18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a Pie chart with rotation of the segments. \& # \& \& # Create an example Pie chart like above. \& my $chart3 = $workbook\->add_chart( type => \*(Aqpie\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart3\->add_series( \& name => \*(AqPie sales data\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& ); \& \& # Add a title. \& $chart3\->set_title( name => \*(AqPie Chart with segment rotation\*(Aq ); \& \& # Change the angle/rotation of the first segment. \& $chart3\->set_rotation(90); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqC34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_doughnut.pl" .IX Subsection "Example: chart_doughnut.pl" A demo of a Doughnut chart in Excel::Writer::XLSX. .PP The demo also shows how to set segment colours. It is possible to define chart colors for most types of Excel::Writer::XLSX charts via the \&\fBadd_series()\fR method. However, Pie and Doughtnut charts are a special case since each segment is represented as a point so it is necessary to assign formatting to each point in the series. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a Doughnut chart in Excel::Writer::XLSX. \& # \& # The demo also shows how to set segment colours. It is possible to define \& # chart colors for most types of Excel::Writer::XLSX charts via the \& # add_series() method. However, Pie and Doughtnut charts are a special case \& # since each segment is represented as a point so it is necessary to assign \& # formatting to each point in the series. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_doughnut.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqCategory\*(Aq, \*(AqValues\*(Aq ]; \& my $data = [ \& [ \*(AqGlazed\*(Aq, \*(AqChocolate\*(Aq, \*(AqCream\*(Aq ], \& [ 50, 35, 15 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqdoughnut\*(Aq, embedded => 1 ); \& \& # Configure the series. Note the use of the array ref to define ranges: \& # [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& # See below for an alternative syntax. \& $chart1\->add_series( \& name => \*(AqDoughnut sales data\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 3, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 3, 1, 1 ], \& ); \& \& # Add a title. \& $chart1\->set_title( name => \*(AqPopular Doughnut Types\*(Aq ); \& \& # Set an Excel chart style. Colors with white outline and shadow. \& $chart1\->set_style( 10 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqC2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a Doughnut chart with user defined segment colors. \& # \& \& # Create an example Doughnut chart like above. \& my $chart2 = $workbook\->add_chart( type => \*(Aqdoughnut\*(Aq, embedded => 1 ); \& \& # Configure the series and add user defined segment colours. \& $chart2\->add_series( \& name => \*(AqDoughnut sales data\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& points => [ \& { fill => { color => \*(Aq#FA58D0\*(Aq } }, \& { fill => { color => \*(Aq#61210B\*(Aq } }, \& { fill => { color => \*(Aq#F5F6CE\*(Aq } }, \& ], \& ); \& \& # Add a title. \& $chart2\->set_title( name => \*(AqDoughnut Chart with user defined colors\*(Aq ); \& \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqC18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a Doughnut chart with rotation of the segments. \& # \& \& # Create an example Doughnut chart like above. \& my $chart3 = $workbook\->add_chart( type => \*(Aqdoughnut\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart3\->add_series( \& name => \*(AqDoughnut sales data\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& ); \& \& # Add a title. \& $chart3\->set_title( name => \*(AqDoughnut Chart with segment rotation\*(Aq ); \& \& # Change the angle/rotation of the first segment. \& $chart3\->set_rotation(90); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqC34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a Doughnut chart with user defined hole size. \& # \& \& # Create an example Doughnut chart like above. \& my $chart4 = $workbook\->add_chart( type => \*(Aqdoughnut\*(Aq, embedded => 1 ); \& \& # Configure the series. \& $chart4\->add_series( \& name => \*(AqDoughnut sales data\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$4\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$4\*(Aq, \& ); \& \& # Add a title. \& $chart4\->set_title( name => \*(AqDoughnut Chart with user defined hole size\*(Aq ); \& \& # Change the hole size. \& $chart4\->set_hole_size(33); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqC50\*(Aq, $chart4, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_radar.pl" .IX Subsection "Example: chart_radar.pl" A demo of an Radar chart in Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of an Radar chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_radar.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqBatch 1\*(Aq, \*(AqBatch 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 30, 60, 70, 50, 40, 30 ], \& [ 25, 40, 50, 30, 50, 40 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqradar\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. Note alternative use of array ref to define \& # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title. \& $chart1\->set_title ( name => \*(AqResults of sample analysis\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart1\->set_style( 11 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a with_markers chart sub\-type \& # \& my $chart2 = $workbook\->add_chart( \& type => \*(Aqradar\*(Aq, \& embedded => 1, \& subtype => \*(Aqwith_markers\*(Aq \& ); \& \& # Configure the first series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title. \& $chart2\->set_title ( name => \*(AqStacked Chart\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart2\->set_style( 12 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a filled chart sub\-type \& # \& my $chart3 = $workbook\->add_chart( \& type => \*(Aqradar\*(Aq, \& embedded => 1, \& subtype => \*(Aqfilled\*(Aq \& ); \& \& # Configure the first series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title. \& $chart3\->set_title ( name => \*(AqPercent Stacked Chart\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart3\->set_style( 13 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_scatter.pl" .IX Subsection "Example: chart_scatter.pl" A demo of a Scatter chart in Excel::Writer::XLSX. Other subtypes are also supported such as markers_only (the default), straight_with_markers, straight, smooth_with_markers and smooth. See the main documentation for more details. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a Scatter chart in Excel::Writer::XLSX. Other subtypes are \& # also supported such as markers_only (the default), straight_with_markers, \& # straight, smooth_with_markers and smooth. See the main documentation for \& # more details. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_scatter.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqBatch 1\*(Aq, \*(AqBatch 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 10, 40, 50, 20, 10, 50 ], \& [ 30, 60, 70, 50, 40, 30 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqscatter\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. Note alternative use of array ref to define \& # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart1\->set_title ( name => \*(AqResults of sample analysis\*(Aq ); \& $chart1\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart1\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart1\->set_style( 11 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a scatter chart sub\-type with straight lines and markers. \& # \& my $chart2 = $workbook\->add_chart( \& type => \*(Aqscatter\*(Aq, \& embedded => 1, \& subtype => \*(Aqstraight_with_markers\*(Aq \& ); \& \& # Configure the first series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart2\->set_title ( name => \*(AqStraight line with markers\*(Aq ); \& $chart2\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart2\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart2\->set_style( 12 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a scatter chart sub\-type with straight lines and no markers. \& # \& my $chart3 = $workbook\->add_chart( \& type => \*(Aqscatter\*(Aq, \& embedded => 1, \& subtype => \*(Aqstraight\*(Aq \& ); \& \& # Configure the first series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart3\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart3\->set_title ( name => \*(AqStraight line\*(Aq ); \& $chart3\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart3\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart3\->set_style( 13 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a scatter chart sub\-type with smooth lines and markers. \& # \& my $chart4 = $workbook\->add_chart( \& type => \*(Aqscatter\*(Aq, \& embedded => 1, \& subtype => \*(Aqsmooth_with_markers\*(Aq \& ); \& \& # Configure the first series. \& $chart4\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart4\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart4\->set_title ( name => \*(AqSmooth line with markers\*(Aq ); \& $chart4\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart4\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart4\->set_style( 14 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD51\*(Aq, $chart4, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a scatter chart sub\-type with smooth lines and no markers. \& # \& my $chart5 = $workbook\->add_chart( \& type => \*(Aqscatter\*(Aq, \& embedded => 1, \& subtype => \*(Aqsmooth\*(Aq \& ); \& \& # Configure the first series. \& $chart5\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart5\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart5\->set_title ( name => \*(AqSmooth line\*(Aq ); \& $chart5\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart5\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set an Excel chart style. Blue colors with white outline and shadow. \& $chart5\->set_style( 15 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD66\*(Aq, $chart5, { x_offset => 25, y_offset => 10 } ); \& \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_secondary_axis.pl" .IX Subsection "Example: chart_secondary_axis.pl" A demo of a Line chart with a secondary axis in Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a Line chart with a secondary axis in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_secondary_axis.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqAliens\*(Aq, \*(AqHumans\*(Aq, ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 10, 40, 50, 20, 10, 50 ], \& \& ]; \& \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure a series with a secondary axis \& $chart\->add_series( \& name => \*(Aq=Sheet1!$A$1\*(Aq, \& values => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& y2_axis => 1, \& ); \& \& $chart\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& $chart\->set_legend( position => \*(Aqright\*(Aq ); \& \& # Add a chart title and some axis labels. \& $chart\->set_title( name => \*(AqSurvey results\*(Aq ); \& $chart\->set_x_axis( name => \*(AqDays\*(Aq, ); \& $chart\->set_y_axis( name => \*(AqPopulation\*(Aq, major_gridlines => { visible => 0 } ); \& $chart\->set_y2_axis( name => \*(AqLaser wounds\*(Aq ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_combined.pl" .IX Subsection "Example: chart_combined.pl" An example of a Combined chart in Excel::Writer::XLSX. .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # An example of a Combined chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_combined.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqBatch 1\*(Aq, \*(AqBatch 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 10, 40, 50, 20, 10, 50 ], \& [ 30, 60, 70, 50, 40, 30 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # \& # In the first example we will create a combined column and line chart. \& # They will share the same X and Y axes. \& # \& \& # Create a new column chart. This will use this as the primary chart. \& my $column_chart1 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the data series for the primary chart. \& $column_chart1\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Create a new column chart. This will use this as the secondary chart. \& my $line_chart1 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the data series for the secondary chart. \& $line_chart1\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& ); \& \& # Combine the charts. \& $column_chart1\->combine( $line_chart1 ); \& \& # Add a chart title and some axis labels. Note, this is done via the \& # primary chart. \& $column_chart1\->set_title( name => \*(AqCombined chart \- same Y axis\*(Aq ); \& $column_chart1\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $column_chart1\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& \& # Insert the chart into the worksheet \& $worksheet\->insert_chart( \*(AqE2\*(Aq, $column_chart1 ); \& \& # \& # In the second example we will create a similar combined column and line \& # chart except that the secondary chart will have a secondary Y axis. \& # \& \& # Create a new column chart. This will use this as the primary chart. \& my $column_chart2 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the data series for the primary chart. \& $column_chart2\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Create a new column chart. This will use this as the secondary chart. \& my $line_chart2 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the data series for the secondary chart. We also set a \& # secondary Y axis via (y2_axis). This is the only difference between \& # this and the first example, apart from the axis label below. \& $line_chart2\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& y2_axis => 1, \& ); \& \& # Combine the charts. \& $column_chart2\->combine( $line_chart2 ); \& \& # Add a chart title and some axis labels. \& $column_chart2\->set_title( name => \*(AqCombine chart \- secondary Y axis\*(Aq ); \& $column_chart2\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $column_chart2\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Note: the y2 properites are on the secondary chart. \& $line_chart2\->set_y2_axis( name => \*(AqTarget length (mm)\*(Aq ); \& \& \& # Insert the chart into the worksheet \& $worksheet\->insert_chart( \*(AqE18\*(Aq, $column_chart2 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_pareto.pl" .IX Subsection "Example: chart_pareto.pl" A demo of a Pareto chart in Excel::Writer::XLSX. .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a Pareto chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_pareto.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Formats used in the workbook. \& my $bold = $workbook\->add_format( bold => 1 ); \& my $percent_format = $workbook\->add_format( num_format => \*(Aq0.0%\*(Aq ); \& \& \& # Widen the columns for visibility. \& $worksheet\->set_column( \*(AqA:A\*(Aq, 15 ); \& $worksheet\->set_column( \*(AqB:C\*(Aq, 10 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqReason\*(Aq, \*(AqNumber\*(Aq, \*(AqPercentage\*(Aq ]; \& \& my $reasons = [ \& \*(AqTraffic\*(Aq, \*(AqChild care\*(Aq, \*(AqPublic Transport\*(Aq, \*(AqWeather\*(Aq, \& \*(AqOverslept\*(Aq, \*(AqEmergency\*(Aq, \& ]; \& \& my $numbers = [ 60, 40, 20, 15, 10, 5 ]; \& my $percents = [ 0.44, 0.667, 0.8, 0.9, 0.967, 1 ]; \& \& $worksheet\->write_row( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write_col( \*(AqA2\*(Aq, $reasons ); \& $worksheet\->write_col( \*(AqB2\*(Aq, $numbers ); \& $worksheet\->write_col( \*(AqC2\*(Aq, $percents, $percent_format ); \& \& \& # Create a new column chart. This will be the primary chart. \& my $column_chart = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Add a series. \& $column_chart\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Add a chart title. \& $column_chart\->set_title( name => \*(AqReasons for lateness\*(Aq ); \& \& # Turn off the chart legend. \& $column_chart\->set_legend( position => \*(Aqnone\*(Aq ); \& \& # Set the title and scale of the Y axes. Note, the secondary axis is set from \& # the primary chart. \& $column_chart\->set_y_axis( \& name => \*(AqRespondents (number)\*(Aq, \& min => 0, \& max => 120 \& ); \& $column_chart\->set_y2_axis( max => 1 ); \& \& # Create a new line chart. This will be the secondary chart. \& my $line_chart = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Add a series, on the secondary axis. \& $line_chart\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& marker => { type => \*(Aqautomatic\*(Aq }, \& y2_axis => 1, \& ); \& \& \& # Combine the charts. \& $column_chart\->combine( $line_chart ); \& \& # Insert the chart into the worksheet. \& $worksheet\->insert_chart( \*(AqF2\*(Aq, $column_chart ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_stock.pl" .IX Subsection "Example: chart_stock.pl" A demo of a Stock chart in Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a Stock chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_stock.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& my $date_format = $workbook\->add_format( num_format => \*(Aqdd/mm/yyyy\*(Aq ); \& my $chart = $workbook\->add_chart( type => \*(Aqstock\*(Aq, embedded => 1 ); \& \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqDate\*(Aq, \*(AqHigh\*(Aq, \*(AqLow\*(Aq, \*(AqClose\*(Aq ]; \& my $data = [ \& \& [ \*(Aq2007\-01\-01T\*(Aq, \*(Aq2007\-01\-02T\*(Aq, \*(Aq2007\-01\-03T\*(Aq, \*(Aq2007\-01\-04T\*(Aq, \*(Aq2007\-01\-05T\*(Aq ], \& [ 27.2, 25.03, 19.05, 20.34, 18.5 ], \& [ 23.49, 19.55, 15.12, 17.84, 16.34 ], \& [ 25.45, 23.05, 17.32, 20.45, 17.34 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& \& for my $row ( 0 .. 4 ) { \& $worksheet\->write_date_time( $row+1, 0, $data\->[0]\->[$row], $date_format ); \& $worksheet\->write( $row+1, 1, $data\->[1]\->[$row] ); \& $worksheet\->write( $row+1, 2, $data\->[2]\->[$row] ); \& $worksheet\->write( $row+1, 3, $data\->[3]\->[$row] ); \& \& } \& \& $worksheet\->set_column( \*(AqA:D\*(Aq, 11 ); \& \& # Add a series for each of the High\-Low\-Close columns. \& $chart\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$6\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$6\*(Aq, \& ); \& \& $chart\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$6\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$6\*(Aq, \& ); \& \& $chart\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$6\*(Aq, \& values => \*(Aq=Sheet1!$D$2:$D$6\*(Aq, \& ); \& \& # Add a chart title and some axis labels. \& $chart\->set_title ( name => \*(AqHigh\-Low\-Close\*(Aq, ); \& $chart\->set_x_axis( name => \*(AqDate\*(Aq, ); \& $chart\->set_y_axis( name => \*(AqShare price\*(Aq, ); \& \& \& $worksheet\->insert_chart( \*(AqE9\*(Aq, $chart ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_data_table.pl" .IX Subsection "Example: chart_data_table.pl" A demo of an Column chart with a data table on the X\-axis using Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of an Column chart with a data table on the X\-axis using \& # Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_data_table.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqBatch 1\*(Aq, \*(AqBatch 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 10, 40, 50, 20, 10, 50 ], \& [ 30, 60, 70, 50, 40, 30 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& # Create a column chart with a data table. \& my $chart1 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. Note alternative use of array ref to define \& # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. \& $chart1\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart1\->set_title( name => \*(AqChart with Data Table\*(Aq ); \& $chart1\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart1\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set a default data table on the X\-Axis. \& $chart1\->set_table(); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& # \& # Create a second chart. \& # \& my $chart2 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$B$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure second series. \& $chart2\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => [ \*(AqSheet1\*(Aq, 1, 6, 0, 0 ], \& values => [ \*(AqSheet1\*(Aq, 1, 6, 2, 2 ], \& ); \& \& # Add a chart title and some axis labels. \& $chart2\->set_title( name => \*(AqData Table with legend keys\*(Aq ); \& $chart2\->set_x_axis( name => \*(AqTest number\*(Aq ); \& $chart2\->set_y_axis( name => \*(AqSample length (mm)\*(Aq ); \& \& # Set a data table on the X\-Axis with the legend keys showm. \& $chart2\->set_table( show_keys => 1 ); \& \& # Hide the chart legend since the keys are show on the data table. \& $chart2\->set_legend( position => \*(Aqnone\*(Aq ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_data_tools.pl" .IX Subsection "Example: chart_data_tools.pl" A demo of an various Excel chart data tools that are available via an Excel::Writer::XLSX chart. .PP These include, Trendlines, Data Labels, Error Bars, Drop Lines, High-Low Lines and Up-Down Bars. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of an various Excel chart data tools that are available via \& # an Excel::Writer::XLSX chart. \& # \& # These include, Trendlines, Data Labels, Error Bars, Drop Lines, \& # High\-Low Lines and Up\-Down Bars. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_data_tools.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqData 1\*(Aq, \*(AqData 2\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 10, 40, 50, 20, 10, 50 ], \& [ 30, 60, 70, 50, 40, 30 ], \& \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& \& ####################################################################### \& # \& # Trendline example. \& # \& \& # Create a Line chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the first series with a polynomial trendline. \& $chart1\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& trendline => { \& type => \*(Aqpolynomial\*(Aq, \& order => 3, \& }, \& ); \& \& # Configure the second series with a moving average trendline. \& $chart1\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& trendline => { type => \*(Aqlinear\*(Aq }, \& ); \& \& # Add a chart title. and some axis labels. \& $chart1\->set_title( name => \*(AqChart with Trendlines\*(Aq ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Data Labels and Markers example. \& # \& \& # Create a Line chart. \& my $chart2 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1 }, \& marker => { type => \*(Aqautomatic\*(Aq }, \& ); \& \& # Configure the second series. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& ); \& \& # Add a chart title. and some axis labels. \& $chart2\->set_title( name => \*(AqChart with Data Labels and Markers\*(Aq ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Error Bars example. \& # \& \& # Create a Line chart. \& my $chart3 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the first series. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& y_error_bars => { type => \*(Aqstandard_error\*(Aq }, \& ); \& \& # Configure the second series. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& ); \& \& # Add a chart title. and some axis labels. \& $chart3\->set_title( name => \*(AqChart with Error Bars\*(Aq ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Up\-Down Bars example. \& # \& \& # Create a Line chart. \& my $chart4 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Add the Up\-Down Bars. \& $chart4\->set_up_down_bars(); \& \& # Configure the first series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure the second series. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& ); \& \& # Add a chart title. and some axis labels. \& $chart4\->set_title( name => \*(AqChart with Up\-Down Bars\*(Aq ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD50\*(Aq, $chart4, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # High\-Low Lines example. \& # \& \& # Create a Line chart. \& my $chart5 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Add the High\-Low lines. \& $chart5\->set_high_low_lines(); \& \& # Configure the first series. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure the second series. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& ); \& \& # Add a chart title. and some axis labels. \& $chart5\->set_title( name => \*(AqChart with High\-Low Lines\*(Aq ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD66\*(Aq, $chart5, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Drop Lines example. \& # \& \& # Create a Line chart. \& my $chart6 = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Add Drop Lines. \& $chart6\->set_drop_lines(); \& \& # Configure the first series. \& $chart6\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& ); \& \& # Configure the second series. \& $chart6\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$7\*(Aq, \& ); \& \& # Add a chart title. and some axis labels. \& $chart6\->set_title( name => \*(AqChart with Drop Lines\*(Aq ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD82\*(Aq, $chart6, { x_offset => 25, y_offset => 10 } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_data_labels.pl" .IX Subsection "Example: chart_data_labels.pl" A demo of an various Excel chart data label features that are available via an Excel::Writer::XLSX chart. .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of an various Excel chart data label features that are available \& # via an Excel::Writer::XLSX chart. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_data_labels.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqNumber\*(Aq, \*(AqData\*(Aq, \*(AqText\*(Aq ]; \& my $data = [ \& [ 2, 3, 4, 5, 6, 7 ], \& [ 20, 10, 20, 30, 40, 30 ], \& [ \*(AqJan\*(Aq, \*(AqFeb\*(Aq, \*(AqMar\*(Aq, \*(AqApr\*(Aq, \*(AqMay\*(Aq, \*(AqJun\*(Aq ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, $data ); \& \& \& ####################################################################### \& # \& # Example with standard data labels. \& # \& \& # Create a Column chart. \& my $chart1 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the data series and add the data labels. \& $chart1\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1 }, \& ); \& \& # Add a chart title. \& $chart1\->set_title( name => \*(AqChart with standard data labels\*(Aq ); \& \& # Turn off the chart legend. \& $chart1\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD2\*(Aq, $chart1, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Example with value and category data labels. \& # \& \& # Create a Column chart. \& my $chart2 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the data series and add the data labels. \& $chart2\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1, category => 1 }, \& ); \& \& # Add a chart title. \& $chart2\->set_title( name => \*(AqCategory and Value data labels\*(Aq ); \& \& # Turn off the chart legend. \& $chart2\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD18\*(Aq, $chart2, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Example with standard data labels with different font. \& # \& \& # Create a Column chart. \& my $chart3 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the data series and add the data labels. \& $chart3\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1, \& font => { bold => 1, \& color => \*(Aqred\*(Aq, \& rotation => \-30} }, \& ); \& \& # Add a chart title. \& $chart3\->set_title( name => \*(AqData labels with user defined font\*(Aq ); \& \& # Turn off the chart legend. \& $chart3\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD34\*(Aq, $chart3, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Example with standard data labels and formatting. \& # \& \& # Create a Column chart. \& my $chart4 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the data series and add the data labels. \& $chart4\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1, \& border => {color => \*(Aqred\*(Aq}, \& fill => {color => \*(Aqyellow\*(Aq} }, \& ); \& \& # Add a chart title. \& $chart4\->set_title( name => \*(AqData labels with formatting\*(Aq ); \& \& # Turn off the chart legend. \& $chart4\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD50\*(Aq, $chart4, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Example with custom string data labels. \& # \& \& # Create a Column chart. \& my $chart5 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Some custom labels. \& my $custom_labels = [ \& { value => \*(AqAmy\*(Aq }, \& { value => \*(AqBea\*(Aq }, \& { value => \*(AqEva\*(Aq }, \& { value => \*(AqFay\*(Aq }, \& { value => \*(AqLiv\*(Aq }, \& { value => \*(AqUna\*(Aq }, \& ]; \& \& \& # Configure the data series and add the data labels. \& $chart5\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1, custom => $custom_labels }, \& ); \& \& # Add a chart title. \& $chart5\->set_title( name => \*(AqChart with custom string data labels\*(Aq ); \& \& # Turn off the chart legend. \& $chart5\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD66\*(Aq, $chart5, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Example with custom data labels from cells. \& # \& \& # Create a Column chart. \& my $chart6 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Some custom labels. \& $custom_labels = [ \& { value => \*(Aq=Sheet1!$C$2\*(Aq }, \& { value => \*(Aq=Sheet1!$C$3\*(Aq }, \& { value => \*(Aq=Sheet1!$C$4\*(Aq }, \& { value => \*(Aq=Sheet1!$C$5\*(Aq }, \& { value => \*(Aq=Sheet1!$C$6\*(Aq }, \& { value => \*(Aq=Sheet1!$C$7\*(Aq }, \& ]; \& \& \& # Configure the data series and add the data labels. \& $chart6\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1, custom => $custom_labels }, \& ); \& \& # Add a chart title. \& $chart6\->set_title( name => \*(AqChart with custom data labels from cells\*(Aq ); \& \& # Turn off the chart legend. \& $chart6\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD82\*(Aq, $chart6, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Example with custom and default data labels. \& # \& \& # Create a Column chart. \& my $chart7 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Some custom labels. The undef items will get the default value. \& # We also set a font for the custom items as an extra example. \& $custom_labels = [ \& { value => \*(Aq=Sheet1!$C$2\*(Aq, font => { color => \*(Aqred\*(Aq } }, \& undef, \& { value => \*(Aq=Sheet1!$C$4\*(Aq, font => { color => \*(Aqred\*(Aq } }, \& { value => \*(Aq=Sheet1!$C$5\*(Aq, font => { color => \*(Aqred\*(Aq } }, \& ]; \& \& \& # Configure the data series and add the data labels. \& $chart7\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1, custom => $custom_labels }, \& ); \& \& # Add a chart title. \& $chart7\->set_title( name => \*(AqMixed custom and default data labels\*(Aq ); \& \& # Turn off the chart legend. \& $chart7\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD98\*(Aq, $chart7, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Example with deleted custom data labels. \& # \& \& # Create a Column chart. \& my $chart8 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Some deleted custom labels and defaults (undef). This allows us to \& # highlight certain values such as the minimum and maximum. \& $custom_labels = [ \& { delete => 1 }, \& undef, \& { delete => 1 }, \& { delete => 1 }, \& undef, \& { delete => 1 }, \& ]; \& \& # Configure the data series and add the data labels. \& $chart8\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1, custom => $custom_labels }, \& ); \& \& # Add a chart title. \& $chart8\->set_title( name => \*(AqChart with deleted data labels\*(Aq ); \& \& # Turn off the chart legend. \& $chart8\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD114\*(Aq, $chart8, { x_offset => 25, y_offset => 10 } ); \& \& \& ####################################################################### \& # \& # Example with custom string data labels and formatting. \& # \& \& # Create a Column chart. \& my $chart9 = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Some custom labels. \& $custom_labels = [ \& { value => \*(AqAmy\*(Aq, border => {color => \*(Aqblue\*(Aq} }, \& { value => \*(AqBea\*(Aq }, \& { value => \*(AqEva\*(Aq }, \& { value => \*(AqFay\*(Aq }, \& { value => \*(AqLiv\*(Aq }, \& { value => \*(AqUna\*(Aq, fill => {color => \*(Aqgreen\*(Aq} }, \& ]; \& \& \& # Configure the data series and add the data labels. \& $chart9\->add_series( \& categories => \*(Aq=Sheet1!$A$2:$A$7\*(Aq, \& values => \*(Aq=Sheet1!$B$2:$B$7\*(Aq, \& data_labels => { value => 1, \& custom => $custom_labels, \& border => {color => \*(Aqred\*(Aq}, \& fill => {color => \*(Aqyellow\*(Aq} }, \& ); \& \& # Add a chart title. \& $chart9\->set_title( name => \*(AqChart with custom labels and formatting\*(Aq ); \& \& # Turn off the chart legend. \& $chart9\->set_legend( none => 1 ); \& \& # Insert the chart into the worksheet (with an offset). \& $worksheet\->insert_chart( \*(AqD130\*(Aq, $chart9, { x_offset => 25, y_offset => 10 } ); \& \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_clustered.pl" .IX Subsection "Example: chart_clustered.pl" A demo of a clustered category chart in Excel::Writer::XLSX. .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of a clustered category chart in Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_clustered.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& # Add the worksheet data that the charts will refer to. \& my $headings = [ \*(AqTypes\*(Aq, \*(AqSub Type\*(Aq, \*(AqValue 1\*(Aq, \*(AqValue 2\*(Aq, \*(AqValue 3\*(Aq ]; \& my $data = [ \& [ \*(AqType 1\*(Aq, \*(AqSub Type A\*(Aq, 5000, 8000, 6000 ], \& [ \*(Aq\*(Aq, \*(AqSub Type B\*(Aq, 2000, 3000, 4000 ], \& [ \*(Aq\*(Aq, \*(AqSub Type C\*(Aq, 250, 1000, 2000 ], \& [ \*(AqType 2\*(Aq, \*(AqSub Type D\*(Aq, 6000, 6000, 6500 ], \& [ \*(Aq\*(Aq, \*(AqSub Type E\*(Aq, 500, 300, 200 ], \& ]; \& \& $worksheet\->write( \*(AqA1\*(Aq, $headings, $bold ); \& $worksheet\->write_col( \*(AqA2\*(Aq, $data ); \& \& # Create a new chart object. In this case an embedded chart. \& my $chart = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq, embedded => 1 ); \& \& # Configure the series. Note, that the categories are 2D ranges (from column A \& # to column B). This creates the clusters. The series are shown as formula \& # strings for clarity but you can also use the array syntax. See the docs. \& $chart\->add_series( \& name => \*(Aq=Sheet1!$C$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$B$6\*(Aq, \& values => \*(Aq=Sheet1!$C$2:$C$6\*(Aq, \& ); \& \& $chart\->add_series( \& name => \*(Aq=Sheet1!$D$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$B$6\*(Aq, \& values => \*(Aq=Sheet1!$D$2:$D$6\*(Aq, \& ); \& \& $chart\->add_series( \& name => \*(Aq=Sheet1!$E$1\*(Aq, \& categories => \*(Aq=Sheet1!$A$2:$B$6\*(Aq, \& values => \*(Aq=Sheet1!$E$2:$E$6\*(Aq, \& ); \& \& # Set the Excel chart style. \& $chart\->set_style( 37 ); \& \& # Turn off the legend. \& $chart\->set_legend( position => \*(Aqnone\*(Aq ); \& \& # Insert the chart into the worksheet. \& $worksheet\->insert_chart( \*(AqG3\*(Aq, $chart ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_styles.pl" .IX Subsection "Example: chart_styles.pl" An example showing all 48 default chart styles available in Excel 2007 using Excel::Writer::XLSX.. Note, these styles are not the same as the styles available in Excel 2013. .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # An example showing all 48 default chart styles available in Excel 2007 \& # using Excel::Writer::XLSX.. Note, these styles are not the same as the \& # styles available in Excel 2013. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_styles.xlsx\*(Aq ); \& \& # Show the styles for all of these chart types. \& my @chart_types = ( \*(Aqcolumn\*(Aq, \*(Aqarea\*(Aq, \*(Aqline\*(Aq, \*(Aqpie\*(Aq ); \& \& \& for my $chart_type ( @chart_types ) { \& \& # Add a worksheet for each chart type. \& my $worksheet = $workbook\->add_worksheet( ucfirst( $chart_type ) ); \& $worksheet\->set_zoom( 30 ); \& my $style_number = 1; \& \& # Create 48 charts, each with a different style. \& for ( my $row_num = 0 ; $row_num < 90 ; $row_num += 15 ) { \& for ( my $col_num = 0 ; $col_num < 64 ; $col_num += 8 ) { \& \& my $chart = $workbook\->add_chart( \& type => $chart_type, \& embedded => 1 \& ); \& \& $chart\->add_series( values => \*(Aq=Data!$A$1:$A$6\*(Aq ); \& $chart\->set_title( name => \*(AqStyle \*(Aq . $style_number ); \& $chart\->set_legend( none => 1 ); \& $chart\->set_style( $style_number ); \& \& $worksheet\->insert_chart( $row_num, $col_num, $chart ); \& $style_number++; \& } \& } \& } \& \& # Create a worksheet with data for the charts. \& my $data = [ 10, 40, 50, 20, 10, 50 ]; \& my $data_worksheet = $workbook\->add_worksheet( \*(AqData\*(Aq ); \& $data_worksheet\->write_col( \*(AqA1\*(Aq, $data ); \& $data_worksheet\->hide(); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: chart_gauge.pl" .IX Subsection "Example: chart_gauge.pl" A demo of an Gauge Chart in Excel::Writer::XLSX. .PP A Gauge Chart isn't a native chart type in Excel. It is constructed by combining a doughnut chart and a pie chart and by using some non-filled elements. This example follows the following online example of how to create a Gauge Chart in Excel: https://www.excel\-easy.com/examples/gauge\-chart.html .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A demo of an Gauge Chart in Excel::Writer::XLSX. \& # \& # A Gauge Chart isn\*(Aqt a native chart type in Excel. It is constructed by \& # combining a doughnut chart and a pie chart and by using some non\-filled \& # elements. This example follows the following online example of how to create \& # a Gauge Chart in Excel: https://www.excel\-easy.com/examples/gauge\-chart.html \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqchart_gauge.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& my $chart_doughnut = $workbook\->add_chart( type => \*(Aqdoughnut\*(Aq, embedded => 1 ); \& my $chart_pie = $workbook\->add_chart( type => \*(Aqpie\*(Aq, embedded => 1 ); \& \& # Add some data for the Doughnut and Pie charts. This is set up so the \& # gauge goes from 0\-100. It is initially set at 75%. \& $worksheet\->write_col( \*(AqH2\*(Aq, [\*(AqDonut\*(Aq, 25, 50, 25, 100] ); \& $worksheet\->write_col( \*(AqI2\*(Aq, [\*(AqPie\*(Aq, 75, 1, \*(Aq=200\-I4\-I3\*(Aq] ); \& \& # Configure the doughnut chart as the background for the gauge. \& $chart_doughnut\->add_series( \& name => \*(Aq=Sheet1!$H$2\*(Aq, \& values => \*(Aq=Sheet1!$H$3:$H$6\*(Aq, \& points => [ \& { fill => { color => \*(Aqgreen\*(Aq } }, \& { fill => { color => \*(Aqyellow\*(Aq } }, \& { fill => { color => \*(Aqred\*(Aq } }, \& { fill => { none => 1 } }, \& ], \& ); \& \& # Rotate chart so the gauge parts are above the horizontal. \& $chart_doughnut\->set_rotation( 270 ); \& \& # Turn off the chart legend. \& $chart_doughnut\->set_legend( none => 1 ); \& \& # Turn off the chart fill and border. \& $chart_doughnut\->set_chartarea( \& border => { none => 1 }, \& fill => { none => 1 }, \& ); \& \& # Configure the pie chart as the needle for the gauge. \& $chart_pie\->add_series( \& name => \*(Aq=Sheet1!$I$2\*(Aq, \& values => \*(Aq=Sheet1!$I$3:$I$6\*(Aq, \& points => [ \& { fill => { none => 1 } }, \& { fill => { color => \*(Aqblack\*(Aq } }, \& { fill => { none => 1 } }, \& ], \& ); \& \& # Rotate the pie chart/needle to align with the doughnut/gauge. \& $chart_pie\->set_rotation( 270 ); \& \& # Combine the pie and doughnut charts. \& $chart_doughnut\->combine($chart_pie); \& \& # Insert the chart into the worksheet. \& $worksheet\->insert_chart( \*(AqA1\*(Aq, $chart_doughnut ); \& \& $workbook\->close(); .Ve .PP Download this example: .SS "Example: colors.pl" .IX Subsection "Example: colors.pl" Demonstrates Excel::Writer::XLSX's named colours and the Excel colour palette. .PP The \fBset_custom_color()\fR Worksheet method can be used to override one of the built-in palette values with a more suitable colour. See the main docs. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ################################################################################ \& # \& # Demonstrates Excel::Writer::XLSX\*(Aqs named colours and the Excel colour \& # palette. \& # \& # The set_custom_color() Worksheet method can be used to override one of the \& # built\-in palette values with a more suitable colour. See the main docs. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqcolors.xlsx\*(Aq ); \& \& # Some common formats \& my $center = $workbook\->add_format( align => \*(Aqcenter\*(Aq ); \& my $heading = $workbook\->add_format( align => \*(Aqcenter\*(Aq, bold => 1 ); \& \& \& ###################################################################### \& # \& # Demonstrate the named colors. \& # \& \& my %colors = ( \& 0x08, \*(Aqblack\*(Aq, \& 0x0C, \*(Aqblue\*(Aq, \& 0x10, \*(Aqbrown\*(Aq, \& 0x0F, \*(Aqcyan\*(Aq, \& 0x17, \*(Aqgray\*(Aq, \& 0x11, \*(Aqgreen\*(Aq, \& 0x0B, \*(Aqlime\*(Aq, \& 0x0E, \*(Aqmagenta\*(Aq, \& 0x12, \*(Aqnavy\*(Aq, \& 0x35, \*(Aqorange\*(Aq, \& 0x21, \*(Aqpink\*(Aq, \& 0x14, \*(Aqpurple\*(Aq, \& 0x0A, \*(Aqred\*(Aq, \& 0x16, \*(Aqsilver\*(Aq, \& 0x09, \*(Aqwhite\*(Aq, \& 0x0D, \*(Aqyellow\*(Aq, \& \& ); \& \& my $worksheet1 = $workbook\->add_worksheet( \*(AqNamed colors\*(Aq ); \& \& $worksheet1\->set_column( 0, 3, 15 ); \& \& $worksheet1\->write( 0, 0, "Index", $heading ); \& $worksheet1\->write( 0, 1, "Index", $heading ); \& $worksheet1\->write( 0, 2, "Name", $heading ); \& $worksheet1\->write( 0, 3, "Color", $heading ); \& \& my $i = 1; \& \& while ( my ( $index, $color ) = each %colors ) { \& my $format = $workbook\->add_format( \& fg_color => $color, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet1\->write( $i + 1, 0, $index, $center ); \& $worksheet1\->write( $i + 1, 1, sprintf( "0x%02X", $index ), $center ); \& $worksheet1\->write( $i + 1, 2, $color, $center ); \& $worksheet1\->write( $i + 1, 3, \*(Aq\*(Aq, $format ); \& $i++; \& } \& \& \& ###################################################################### \& # \& # Demonstrate the standard Excel colors in the range 8..63. \& # \& \& my $worksheet2 = $workbook\->add_worksheet( \*(AqStandard colors\*(Aq ); \& \& $worksheet2\->set_column( 0, 3, 15 ); \& \& $worksheet2\->write( 0, 0, "Index", $heading ); \& $worksheet2\->write( 0, 1, "Index", $heading ); \& $worksheet2\->write( 0, 2, "Color", $heading ); \& $worksheet2\->write( 0, 3, "Name", $heading ); \& \& for my $i ( 8 .. 63 ) { \& my $format = $workbook\->add_format( \& fg_color => $i, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet2\->write( ( $i \- 7 ), 0, $i, $center ); \& $worksheet2\->write( ( $i \- 7 ), 1, sprintf( "0x%02X", $i ), $center ); \& $worksheet2\->write( ( $i \- 7 ), 2, \*(Aq\*(Aq, $format ); \& \& # Add the color names \& if ( exists $colors{$i} ) { \& $worksheet2\->write( ( $i \- 7 ), 3, $colors{$i}, $center ); \& \& } \& } \& \& \& ###################################################################### \& # \& # Demonstrate the Html colors. \& # \& \& \& \& %colors = ( \& \*(Aq#000000\*(Aq, \*(Aqblack\*(Aq, \& \*(Aq#0000FF\*(Aq, \*(Aqblue\*(Aq, \& \*(Aq#800000\*(Aq, \*(Aqbrown\*(Aq, \& \*(Aq#00FFFF\*(Aq, \*(Aqcyan\*(Aq, \& \*(Aq#808080\*(Aq, \*(Aqgray\*(Aq, \& \*(Aq#008000\*(Aq, \*(Aqgreen\*(Aq, \& \*(Aq#00FF00\*(Aq, \*(Aqlime\*(Aq, \& \*(Aq#FF00FF\*(Aq, \*(Aqmagenta\*(Aq, \& \*(Aq#000080\*(Aq, \*(Aqnavy\*(Aq, \& \*(Aq#FF6600\*(Aq, \*(Aqorange\*(Aq, \& \*(Aq#FF00FF\*(Aq, \*(Aqpink\*(Aq, \& \*(Aq#800080\*(Aq, \*(Aqpurple\*(Aq, \& \*(Aq#FF0000\*(Aq, \*(Aqred\*(Aq, \& \*(Aq#C0C0C0\*(Aq, \*(Aqsilver\*(Aq, \& \*(Aq#FFFFFF\*(Aq, \*(Aqwhite\*(Aq, \& \*(Aq#FFFF00\*(Aq, \*(Aqyellow\*(Aq, \& ); \& \& my $worksheet3 = $workbook\->add_worksheet( \*(AqHtml colors\*(Aq ); \& \& $worksheet3\->set_column( 0, 3, 15 ); \& \& $worksheet3\->write( 0, 0, "Html", $heading ); \& $worksheet3\->write( 0, 1, "Name", $heading ); \& $worksheet3\->write( 0, 2, "Color", $heading ); \& \& $i = 1; \& \& while ( my ( $html_color, $color ) = each %colors ) { \& my $format = $workbook\->add_format( \& fg_color => $html_color, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet3\->write( $i + 1, 1, $html_color, $center ); \& $worksheet3\->write( $i + 1, 2, $color, $center ); \& $worksheet3\->write( $i + 1, 3, \*(Aq\*(Aq, $format ); \& $i++; \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: comments1.pl" .IX Subsection "Example: comments1.pl" This example demonstrates writing cell comments. .PP A cell comment is indicated in Excel by a small red triangle in the upper right-hand corner of the cell. .PP For more advanced comment options see comments2.pl. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # This example demonstrates writing cell comments. \& # \& # A cell comment is indicated in Excel by a small red triangle in the upper \& # right\-hand corner of the cell. \& # \& # For more advanced comment options see comments2.pl. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqcomments1.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& $worksheet\->write( \*(AqA1\*(Aq, \*(AqHello\*(Aq ); \& $worksheet\->write_comment( \*(AqA1\*(Aq, \*(AqThis is a comment\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: comments2.pl" .IX Subsection "Example: comments2.pl" This example demonstrates writing cell comments. .PP A cell comment is indicated in Excel by a small red triangle in the upper right-hand corner of the cell. .PP Each of the worksheets demonstrates different features of cell comments. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # This example demonstrates writing cell comments. \& # \& # A cell comment is indicated in Excel by a small red triangle in the upper \& # right\-hand corner of the cell. \& # \& # Each of the worksheets demonstrates different features of cell comments. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqcomments2.xlsx\*(Aq ); \& my $text_wrap = $workbook\->add_format( text_wrap => 1, valign => \*(Aqtop\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& my $worksheet4 = $workbook\->add_worksheet(); \& my $worksheet5 = $workbook\->add_worksheet(); \& my $worksheet6 = $workbook\->add_worksheet(); \& my $worksheet7 = $workbook\->add_worksheet(); \& my $worksheet8 = $workbook\->add_worksheet(); \& \& \& # Variables that we will use in each example. \& my $cell_text = \*(Aq\*(Aq; \& my $comment = \*(Aq\*(Aq; \& \& \& ############################################################################### \& # \& # Example 1. Demonstrates a simple cell comments without formatting. \& # comments. \& # \& \& # Set up some formatting. \& $worksheet1\->set_column( \*(AqC:C\*(Aq, 25 ); \& $worksheet1\->set_row( 2, 50 ); \& $worksheet1\->set_row( 5, 50 ); \& \& \& # Simple ascii string. \& $cell_text = \*(AqHold the mouse over this cell to see the comment.\*(Aq; \& \& $comment = \*(AqThis is a comment.\*(Aq; \& \& $worksheet1\->write( \*(AqC3\*(Aq, $cell_text, $text_wrap ); \& $worksheet1\->write_comment( \*(AqC3\*(Aq, $comment ); \& \& $cell_text = \*(AqThis is a UTF\-8 string.\*(Aq; \& $comment = chr 0x263a; \& \& $worksheet1\->write( \*(AqC6\*(Aq, $cell_text, $text_wrap ); \& $worksheet1\->write_comment( \*(AqC6\*(Aq, $comment ); \& \& \& \& ############################################################################### \& # \& # Example 2. Demonstrates visible and hidden comments. \& # \& \& # Set up some formatting. \& $worksheet2\->set_column( \*(AqC:C\*(Aq, 25 ); \& $worksheet2\->set_row( 2, 50 ); \& $worksheet2\->set_row( 5, 50 ); \& \& \& $cell_text = \*(AqThis cell comment is visible.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet2\->write( \*(AqC3\*(Aq, $cell_text, $text_wrap ); \& $worksheet2\->write_comment( \*(AqC3\*(Aq, $comment, visible => 1 ); \& \& \& $cell_text = "This cell comment isn\*(Aqt visible (the default)."; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet2\->write( \*(AqC6\*(Aq, $cell_text, $text_wrap ); \& $worksheet2\->write_comment( \*(AqC6\*(Aq, $comment ); \& \& \& ############################################################################### \& # \& # Example 3. Demonstrates visible and hidden comments set at the worksheet \& # level. \& # \& \& # Set up some formatting. \& $worksheet3\->set_column( \*(AqC:C\*(Aq, 25 ); \& $worksheet3\->set_row( 2, 50 ); \& $worksheet3\->set_row( 5, 50 ); \& $worksheet3\->set_row( 8, 50 ); \& \& # Make all comments on the worksheet visible. \& $worksheet3\->show_comments(); \& \& $cell_text = \*(AqThis cell comment is visible, explicitly.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet3\->write( \*(AqC3\*(Aq, $cell_text, $text_wrap ); \& $worksheet3\->write_comment( \*(AqC3\*(Aq, $comment, visible => 1 ); \& \& \& $cell_text = \& \*(AqThis cell comment is also visible because \*(Aq . \*(Aqwe used show_comments().\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet3\->write( \*(AqC6\*(Aq, $cell_text, $text_wrap ); \& $worksheet3\->write_comment( \*(AqC6\*(Aq, $comment ); \& \& \& $cell_text = \*(AqHowever, we can still override it locally.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet3\->write( \*(AqC9\*(Aq, $cell_text, $text_wrap ); \& $worksheet3\->write_comment( \*(AqC9\*(Aq, $comment, visible => 0 ); \& \& \& ############################################################################### \& # \& # Example 4. Demonstrates changes to the comment box dimensions. \& # \& \& # Set up some formatting. \& $worksheet4\->set_column( \*(AqC:C\*(Aq, 25 ); \& $worksheet4\->set_row( 2, 50 ); \& $worksheet4\->set_row( 5, 50 ); \& $worksheet4\->set_row( 8, 50 ); \& $worksheet4\->set_row( 15, 50 ); \& \& $worksheet4\->show_comments(); \& \& $cell_text = \*(AqThis cell comment is default size.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write( \*(AqC3\*(Aq, $cell_text, $text_wrap ); \& $worksheet4\->write_comment( \*(AqC3\*(Aq, $comment ); \& \& \& $cell_text = \*(AqThis cell comment is twice as wide.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write( \*(AqC6\*(Aq, $cell_text, $text_wrap ); \& $worksheet4\->write_comment( \*(AqC6\*(Aq, $comment, x_scale => 2 ); \& \& \& $cell_text = \*(AqThis cell comment is twice as high.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write( \*(AqC9\*(Aq, $cell_text, $text_wrap ); \& $worksheet4\->write_comment( \*(AqC9\*(Aq, $comment, y_scale => 2 ); \& \& \& $cell_text = \*(AqThis cell comment is scaled in both directions.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write( \*(AqC16\*(Aq, $cell_text, $text_wrap ); \& $worksheet4\->write_comment( \*(AqC16\*(Aq, $comment, x_scale => 1.2, y_scale => 0.8 ); \& \& \& $cell_text = \*(AqThis cell comment has width and height specified in pixels.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet4\->write( \*(AqC19\*(Aq, $cell_text, $text_wrap ); \& $worksheet4\->write_comment( \*(AqC19\*(Aq, $comment, width => 200, height => 20 ); \& \& \& ############################################################################### \& # \& # Example 5. Demonstrates changes to the cell comment position. \& # \& \& $worksheet5\->set_column( \*(AqC:C\*(Aq, 25 ); \& $worksheet5\->set_row( 2, 50 ); \& $worksheet5\->set_row( 5, 50 ); \& $worksheet5\->set_row( 8, 50 ); \& $worksheet5\->set_row( 11, 50 ); \& \& $worksheet5\->show_comments(); \& \& $cell_text = \*(AqThis cell comment is in the default position.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet5\->write( \*(AqC3\*(Aq, $cell_text, $text_wrap ); \& $worksheet5\->write_comment( \*(AqC3\*(Aq, $comment ); \& \& \& $cell_text = \*(AqThis cell comment has been moved to another cell.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet5\->write( \*(AqC6\*(Aq, $cell_text, $text_wrap ); \& $worksheet5\->write_comment( \*(AqC6\*(Aq, $comment, start_cell => \*(AqE4\*(Aq ); \& \& \& $cell_text = \*(AqThis cell comment has been moved to another cell.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet5\->write( \*(AqC9\*(Aq, $cell_text, $text_wrap ); \& $worksheet5\->write_comment( \*(AqC9\*(Aq, $comment, start_row => 8, start_col => 4 ); \& \& \& $cell_text = \*(AqThis cell comment has been shifted within its default cell.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet5\->write( \*(AqC12\*(Aq, $cell_text, $text_wrap ); \& $worksheet5\->write_comment( \*(AqC12\*(Aq, $comment, x_offset => 30, y_offset => 12 ); \& \& \& ############################################################################### \& # \& # Example 6. Demonstrates changes to the comment background colour. \& # \& \& $worksheet6\->set_column( \*(AqC:C\*(Aq, 25 ); \& $worksheet6\->set_row( 2, 50 ); \& $worksheet6\->set_row( 5, 50 ); \& $worksheet6\->set_row( 8, 50 ); \& \& $worksheet6\->show_comments(); \& \& $cell_text = \*(AqThis cell comment has a different colour.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet6\->write( \*(AqC3\*(Aq, $cell_text, $text_wrap ); \& $worksheet6\->write_comment( \*(AqC3\*(Aq, $comment, color => \*(Aqgreen\*(Aq ); \& \& \& $cell_text = \*(AqThis cell comment has the default colour.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet6\->write( \*(AqC6\*(Aq, $cell_text, $text_wrap ); \& $worksheet6\->write_comment( \*(AqC6\*(Aq, $comment ); \& \& \& $cell_text = \*(AqThis cell comment has a different colour.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet6\->write( \*(AqC9\*(Aq, $cell_text, $text_wrap ); \& $worksheet6\->write_comment( \*(AqC9\*(Aq, $comment, color => \*(Aq#FF6600\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 7. Demonstrates how to set the cell comment author. \& # \& \& $worksheet7\->set_column( \*(AqC:C\*(Aq, 30 ); \& $worksheet7\->set_row( 2, 50 ); \& $worksheet7\->set_row( 5, 50 ); \& $worksheet7\->set_row( 8, 50 ); \& \& my $author = \*(Aq\*(Aq; \& my $cell = \*(AqC3\*(Aq; \& \& $cell_text = "Move the mouse over this cell and you will see \*(AqCell commented " \& . "by $author\*(Aq (blank) in the status bar at the bottom"; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet7\->write( $cell, $cell_text, $text_wrap ); \& $worksheet7\->write_comment( $cell, $comment ); \& \& \& $author = \*(AqPerl\*(Aq; \& $cell = \*(AqC6\*(Aq; \& $cell_text = "Move the mouse over this cell and you will see \*(AqCell commented " \& . "by $author\*(Aq in the status bar at the bottom"; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet7\->write( $cell, $cell_text, $text_wrap ); \& $worksheet7\->write_comment( $cell, $comment, author => $author ); \& \& \& $author = chr 0x20AC; \& $cell = \*(AqC9\*(Aq; \& $cell_text = "Move the mouse over this cell and you will see \*(AqCell commented " \& . "by $author\*(Aq in the status bar at the bottom"; \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet7\->write( $cell, $cell_text, $text_wrap ); \& $worksheet7\->write_comment( $cell, $comment, author => $author ); \& \& \& \& \& ############################################################################### \& # \& # Example 8. Demonstrates the need to explicitly set the row height. \& # \& \& # Set up some formatting. \& $worksheet8\->set_column( \*(AqC:C\*(Aq, 25 ); \& $worksheet8\->set_row( 2, 80 ); \& \& $worksheet8\->show_comments(); \& \& \& $cell_text = \& \*(AqThe height of this row has been adjusted explicitly using \*(Aq \& . \*(Aqset_row(). The size of the comment box is adjusted \*(Aq \& . \*(Aqaccordingly by Excel::Writer::XLSX.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet8\->write( \*(AqC3\*(Aq, $cell_text, $text_wrap ); \& $worksheet8\->write_comment( \*(AqC3\*(Aq, $comment ); \& \& \& $cell_text = \& \*(AqThe height of this row has been adjusted by Excel due to the \*(Aq \& . \*(Aqtext wrap property being set. Unfortunately this means that \*(Aq \& . \*(Aqthe height of the row is unknown to Excel::Writer::XLSX at \*(Aq \& . "run time and thus the comment box is stretched as well.\en\en" \& . \*(AqUse set_row() to specify the row height explicitly to avoid \*(Aq \& . \*(Aqthis problem.\*(Aq; \& \& $comment = \*(AqHello.\*(Aq; \& \& $worksheet8\->write( \*(AqC6\*(Aq, $cell_text, $text_wrap ); \& $worksheet8\->write_comment( \*(AqC6\*(Aq, $comment ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: conditional_format.pl" .IX Subsection "Example: conditional_format.pl" Example of how to add conditional formatting to an Excel::Writer::XLSX file. .PP Conditional formatting allows you to apply a format to a cell or a range of cells based on certain criteria. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to add conditional formatting to an Excel::Writer::XLSX file. \& # \& # Conditional formatting allows you to apply a format to a cell or a range of \& # cells based on certain criteria. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqconditional_format.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& my $worksheet4 = $workbook\->add_worksheet(); \& my $worksheet5 = $workbook\->add_worksheet(); \& my $worksheet6 = $workbook\->add_worksheet(); \& my $worksheet7 = $workbook\->add_worksheet(); \& my $worksheet8 = $workbook\->add_worksheet(); \& my $worksheet9 = $workbook\->add_worksheet(); \& \& \& # Light red fill with dark red text. \& my $format1 = $workbook\->add_format( \& bg_color => \*(Aq#FFC7CE\*(Aq, \& color => \*(Aq#9C0006\*(Aq, \& \& ); \& \& # Green fill with dark green text. \& my $format2 = $workbook\->add_format( \& bg_color => \*(Aq#C6EFCE\*(Aq, \& color => \*(Aq#006100\*(Aq, \& \& ); \& \& # Some sample data to run the conditional formatting against. \& my $data = [ \& [ 34, 72, 38, 30, 75, 48, 75, 66, 84, 86 ], \& [ 6, 24, 1, 84, 54, 62, 60, 3, 26, 59 ], \& [ 28, 79, 97, 13, 85, 93, 93, 22, 5, 14 ], \& [ 27, 71, 40, 17, 18, 79, 90, 93, 29, 47 ], \& [ 88, 25, 33, 23, 67, 1, 59, 79, 47, 36 ], \& [ 24, 100, 20, 88, 29, 33, 38, 54, 54, 88 ], \& [ 6, 57, 88, 28, 10, 26, 37, 7, 41, 48 ], \& [ 52, 78, 1, 96, 26, 45, 47, 33, 96, 36 ], \& [ 60, 54, 81, 66, 81, 90, 80, 93, 12, 55 ], \& [ 70, 5, 46, 14, 71, 19, 66, 36, 41, 21 ], \& ]; \& \& \& ############################################################################### \& # \& # Example 1. \& # \& my $caption = \*(AqCells with values >= 50 are in light red. \*(Aq \& . \*(AqValues < 50 are in light green.\*(Aq; \& \& # Write the data. \& $worksheet1\->write( \*(AqA1\*(Aq, $caption ); \& $worksheet1\->write_col( \*(AqB3\*(Aq, $data ); \& \& # Write a conditional format over a range. \& $worksheet1\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqcell\*(Aq, \& criteria => \*(Aq>=\*(Aq, \& value => 50, \& format => $format1, \& } \& ); \& \& # Write another conditional format over the same range. \& $worksheet1\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqcell\*(Aq, \& criteria => \*(Aq<\*(Aq, \& value => 50, \& format => $format2, \& } \& ); \& \& \& ############################################################################### \& # \& # Example 2. \& # \& $caption = \*(AqValues between 30 and 70 are in light red. \*(Aq \& . \*(AqValues outside that range are in light green.\*(Aq; \& \& $worksheet2\->write( \*(AqA1\*(Aq, $caption ); \& $worksheet2\->write_col( \*(AqB3\*(Aq, $data ); \& \& $worksheet2\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqcell\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 30, \& maximum => 70, \& format => $format1, \& } \& ); \& \& $worksheet2\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqcell\*(Aq, \& criteria => \*(Aqnot between\*(Aq, \& minimum => 30, \& maximum => 70, \& format => $format2, \& } \& ); \& \& \& ############################################################################### \& # \& # Example 3. \& # \& $caption = \*(AqDuplicate values are in light red. \*(Aq \& . \*(AqUnique values are in light green.\*(Aq; \& \& $worksheet3\->write( \*(AqA1\*(Aq, $caption ); \& $worksheet3\->write_col( \*(AqB3\*(Aq, $data ); \& \& $worksheet3\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqduplicate\*(Aq, \& format => $format1, \& } \& ); \& \& $worksheet3\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqunique\*(Aq, \& format => $format2, \& } \& ); \& \& \& ############################################################################### \& # \& # Example 4. \& # \& $caption = \*(AqAbove average values are in light red. \*(Aq \& . \*(AqBelow average values are in light green.\*(Aq; \& \& $worksheet4\->write( \*(AqA1\*(Aq, $caption ); \& $worksheet4\->write_col( \*(AqB3\*(Aq, $data ); \& \& $worksheet4\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqaverage\*(Aq, \& criteria => \*(Aqabove\*(Aq, \& format => $format1, \& } \& ); \& \& $worksheet4\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqaverage\*(Aq, \& criteria => \*(Aqbelow\*(Aq, \& format => $format2, \& } \& ); \& \& \& ############################################################################### \& # \& # Example 5. \& # \& $caption = \*(AqTop 10 values are in light red. \*(Aq \& . \*(AqBottom 10 values are in light green.\*(Aq; \& \& $worksheet5\->write( \*(AqA1\*(Aq, $caption ); \& $worksheet5\->write_col( \*(AqB3\*(Aq, $data ); \& \& $worksheet5\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqtop\*(Aq, \& value => \*(Aq10\*(Aq, \& format => $format1, \& } \& ); \& \& $worksheet5\->conditional_formatting( \*(AqB3:K12\*(Aq, \& { \& type => \*(Aqbottom\*(Aq, \& value => \*(Aq10\*(Aq, \& format => $format2, \& } \& ); \& \& \& ############################################################################### \& # \& # Example 6. \& # \& $caption = \*(AqCells with values >= 50 are in light red. \*(Aq \& . \*(AqValues < 50 are in light green. Non\-contiguous ranges.\*(Aq; \& \& # Write the data. \& $worksheet6\->write( \*(AqA1\*(Aq, $caption ); \& $worksheet6\->write_col( \*(AqB3\*(Aq, $data ); \& \& # Write a conditional format over a range. \& $worksheet6\->conditional_formatting( \*(AqB3:K6,B9:K12\*(Aq, \& { \& type => \*(Aqcell\*(Aq, \& criteria => \*(Aq>=\*(Aq, \& value => 50, \& format => $format1, \& } \& ); \& \& # Write another conditional format over the same range. \& $worksheet6\->conditional_formatting( \*(AqB3:K6,B9:K12\*(Aq, \& { \& type => \*(Aqcell\*(Aq, \& criteria => \*(Aq<\*(Aq, \& value => 50, \& format => $format2, \& } \& ); \& \& \& ############################################################################### \& # \& # Example 7. \& # \& $caption = \*(AqExamples of color scales with default and user colors.\*(Aq; \& \& $data = [ 1 .. 12 ]; \& \& $worksheet7\->write( \*(AqA1\*(Aq, $caption ); \& \& $worksheet7\->write ( \*(AqB2\*(Aq, "2 Color Scale" ); \& $worksheet7\->write_col( \*(AqB3\*(Aq, $data ); \& \& $worksheet7\->write ( \*(AqD2\*(Aq, "2 Color Scale + user colors" ); \& $worksheet7\->write_col( \*(AqD3\*(Aq, $data ); \& \& $worksheet7\->write ( \*(AqG2\*(Aq, "3 Color Scale" ); \& $worksheet7\->write_col( \*(AqG3\*(Aq, $data ); \& \& $worksheet7\->write ( \*(AqI2\*(Aq, "3 Color Scale + user colors" ); \& $worksheet7\->write_col( \*(AqI3\*(Aq, $data ); \& \& \& $worksheet7\->conditional_formatting( \*(AqB3:B14\*(Aq, \& { \& type => \*(Aq2_color_scale\*(Aq, \& } \& ); \& \& $worksheet7\->conditional_formatting( \*(AqD3:D14\*(Aq, \& { \& type => \*(Aq3_color_scale\*(Aq, \& } \& ); \& \& $worksheet7\->conditional_formatting( \*(AqG3:G14\*(Aq, \& { \& type => \*(Aq2_color_scale\*(Aq, \& min_color => "#FF0000", \& max_color => "#00FF00", \& \& } \& ); \& \& $worksheet7\->conditional_formatting( \*(AqI3:I14\*(Aq, \& { \& type => \*(Aq3_color_scale\*(Aq, \& min_color => "#C5D9F1", \& mid_color => "#8DB4E3", \& max_color => "#538ED5", \& } \& ); \& \& \& ############################################################################### \& # \& # Example 8. \& # \& $caption = \*(AqExamples of data bars.\*(Aq; \& \& $data = [ 1 .. 12 ]; \& \& $worksheet8\->write( \*(AqA1\*(Aq, $caption ); \& \& $worksheet8\->write ( \*(AqB2\*(Aq, "Default data bars" ); \& $worksheet8\->write_col( \*(AqB3\*(Aq, $data ); \& \& $worksheet8\->write ( \*(AqD2\*(Aq, "Bars only" ); \& $worksheet8\->write_col( \*(AqD3\*(Aq, $data ); \& \& $worksheet8\->write ( \*(AqF2\*(Aq, "With user color" ); \& $worksheet8\->write_col( \*(AqF3\*(Aq, $data ); \& \& $worksheet8\->write ( \*(AqH2\*(Aq, "Solid bars" ); \& $worksheet8\->write_col( \*(AqH3\*(Aq, $data ); \& \& $worksheet8\->write ( \*(AqJ2\*(Aq, "Right to left" ); \& $worksheet8\->write_col( \*(AqJ3\*(Aq, $data ); \& \& $data = [\-1, \-2, \-3, \-2, \-1, 0, 1, 2, 3, 2, 1, 0]; \& \& $worksheet8\->write ( \*(AqL2\*(Aq, "Excel 2010 style" ); \& $worksheet8\->write_col( \*(AqL3\*(Aq, $data ); \& \& $worksheet8\->write ( \*(AqN2\*(Aq, "Negative same as positive" ); \& $worksheet8\->write_col( \*(AqN3\*(Aq, $data ); \& \& \& $worksheet8\->conditional_formatting( \*(AqB3:B14\*(Aq, \& { \& type => \*(Aqdata_bar\*(Aq \& } \& ); \& \& $worksheet8\->conditional_formatting( \*(AqD3:D14\*(Aq, \& { \& type => \*(Aqdata_bar\*(Aq, \& bar_only => 1 \& } \& ); \& \& $worksheet8\->conditional_formatting( \*(AqF3:F14\*(Aq, \& { \& type => \*(Aqdata_bar\*(Aq, \& bar_color => \*(Aq#63C384\*(Aq \& } \& ); \& \& $worksheet8\->conditional_formatting( \*(AqH3:H14\*(Aq, \& { \& type => \*(Aqdata_bar\*(Aq, \& bar_solid => 1 \& } \& ); \& \& $worksheet8\->conditional_formatting( \*(AqJ3:J14\*(Aq, \& { \& type => \*(Aqdata_bar\*(Aq, \& bar_direction => \*(Aqright\*(Aq \& } \& ); \& \& $worksheet8\->conditional_formatting( \*(AqL3:L14\*(Aq, \& { \& type => \*(Aqdata_bar\*(Aq, \& data_bar_2010 => 1 \& } \& ); \& \& $worksheet8\->conditional_formatting( \*(AqN3:N14\*(Aq, \& { \& type => \*(Aqdata_bar\*(Aq, \& bar_negative_color_same => 1, \& bar_negative_border_color_same => 1 \& } \& ); \& \& \& ############################################################################### \& # \& # Example 9. \& # \& $caption = \*(AqExamples of conditional formats with icon sets.\*(Aq; \& \& $data = [ \& [ 1, 2, 3 ], \& [ 1, 2, 3 ], \& [ 1, 2, 3 ], \& [ 1, 2, 3 ], \& [ 1, 2, 3, 4 ], \& [ 1, 2, 3, 4, 5 ], \& [ 1, 2, 3, 4, 5 ], \& ]; \& \& $worksheet9\->write( \*(AqA1\*(Aq, $caption ); \& $worksheet9\->write_col( \*(AqB3\*(Aq, $data ); \& \& $worksheet9\->conditional_formatting( \*(AqB3:D3\*(Aq, \& { \& type => \*(Aqicon_set\*(Aq, \& icon_style => \*(Aq3_traffic_lights\*(Aq, \& } \& ); \& \& $worksheet9\->conditional_formatting( \*(AqB4:D4\*(Aq, \& { \& type => \*(Aqicon_set\*(Aq, \& icon_style => \*(Aq3_traffic_lights\*(Aq, \& reverse_icons => 1, \& } \& ); \& \& $worksheet9\->conditional_formatting( \*(AqB5:D5\*(Aq, \& { \& type => \*(Aqicon_set\*(Aq, \& icon_style => \*(Aq3_traffic_lights\*(Aq, \& icons_only => 1, \& } \& ); \& \& $worksheet9\->conditional_formatting( \*(AqB6:D6\*(Aq, \& { \& type => \*(Aqicon_set\*(Aq, \& icon_style => \*(Aq3_arrows\*(Aq, \& } \& ); \& \& $worksheet9\->conditional_formatting( \*(AqB7:E8\*(Aq, \& { \& type => \*(Aqicon_set\*(Aq, \& icon_style => \*(Aq4_arrows\*(Aq, \& } \& ); \& \& $worksheet9\->conditional_formatting( \*(AqB8:F8\*(Aq, \& { \& type => \*(Aqicon_set\*(Aq, \& icon_style => \*(Aq5_arrows\*(Aq, \& } \& ); \& \& \& $worksheet9\->conditional_formatting( \*(AqB9:F9\*(Aq, \& { \& type => \*(Aqicon_set\*(Aq, \& icon_style => \*(Aq5_ratings\*(Aq, \& } \& ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: data_validate.pl" .IX Subsection "Example: data_validate.pl" Example of how to add data validation and dropdown lists to an Excel::Writer::XLSX file. .PP Data validation is a feature of Excel which allows you to restrict the data that a user enters in a cell and to display help and warning messages. It also allows you to restrict input to values in a drop down list. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to add data validation and dropdown lists to an \& # Excel::Writer::XLSX file. \& # \& # Data validation is a feature of Excel which allows you to restrict the data \& # that a user enters in a cell and to display help and warning messages. It \& # also allows you to restrict input to values in a drop down list. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqdata_validate.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Add a format for the header cells. \& my $header_format = $workbook\->add_format( \& border => 1, \& bg_color => \*(Aq#C6EFCE\*(Aq, \& bold => 1, \& text_wrap => 1, \& valign => \*(Aqvcenter\*(Aq, \& indent => 1, \& ); \& \& # Set up layout of the worksheet. \& $worksheet\->set_column( \*(AqA:A\*(Aq, 68 ); \& $worksheet\->set_column( \*(AqB:B\*(Aq, 15 ); \& $worksheet\->set_column( \*(AqD:D\*(Aq, 15 ); \& $worksheet\->set_row( 0, 36 ); \& $worksheet\->set_selection( \*(AqB3\*(Aq ); \& \& \& # Write the header cells and some data that will be used in the examples. \& my $row = 0; \& my $txt; \& my $heading1 = \*(AqSome examples of data validation in Excel::Writer::XLSX\*(Aq; \& my $heading2 = \*(AqEnter values in this column\*(Aq; \& my $heading3 = \*(AqSample Data\*(Aq; \& \& $worksheet\->write( \*(AqA1\*(Aq, $heading1, $header_format ); \& $worksheet\->write( \*(AqB1\*(Aq, $heading2, $header_format ); \& $worksheet\->write( \*(AqD1\*(Aq, $heading3, $header_format ); \& \& $worksheet\->write( \*(AqD3\*(Aq, [ \*(AqIntegers\*(Aq, 1, 10 ] ); \& $worksheet\->write( \*(AqD4\*(Aq, [ \*(AqList data\*(Aq, \*(Aqopen\*(Aq, \*(Aqhigh\*(Aq, \*(Aqclose\*(Aq ] ); \& $worksheet\->write( \*(AqD5\*(Aq, [ \*(AqFormula\*(Aq, \*(Aq=AND(F5=50,G5=60)\*(Aq, 50, 60 ] ); \& \& \& # \& # Example 1. Limiting input to an integer in a fixed range. \& # \& $txt = \*(AqEnter an integer between 1 and 10\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 10, \& } \& ); \& \& \& # \& # Example 2. Limiting input to an integer outside a fixed range. \& # \& $txt = \*(AqEnter an integer that is not between 1 and 10 (using cell references)\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqnot between\*(Aq, \& minimum => \*(Aq=E3\*(Aq, \& maximum => \*(Aq=F3\*(Aq, \& } \& ); \& \& \& # \& # Example 3. Limiting input to an integer greater than a fixed value. \& # \& $txt = \*(AqEnter an integer greater than 0\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 0, \& } \& ); \& \& \& # \& # Example 4. Limiting input to an integer less than a fixed value. \& # \& $txt = \*(AqEnter an integer less than 10\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq<\*(Aq, \& value => 10, \& } \& ); \& \& \& # \& # Example 5. Limiting input to a decimal in a fixed range. \& # \& $txt = \*(AqEnter a decimal between 0.1 and 0.5\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqdecimal\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 0.1, \& maximum => 0.5, \& } \& ); \& \& \& # \& # Example 6. Limiting input to a value in a dropdown list. \& # \& $txt = \*(AqSelect a value from a drop down list\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqlist\*(Aq, \& source => [ \*(Aqopen\*(Aq, \*(Aqhigh\*(Aq, \*(Aqclose\*(Aq ], \& } \& ); \& \& \& # \& # Example 6. Limiting input to a value in a dropdown list. \& # \& $txt = \*(AqSelect a value from a drop down list (using a cell range)\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqlist\*(Aq, \& source => \*(Aq=$E$4:$G$4\*(Aq, \& } \& ); \& \& \& # \& # Example 7. Limiting input to a date in a fixed range. \& # \& $txt = \*(AqEnter a date between 1/1/2008 and 12/12/2008\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqdate\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => \*(Aq2008\-01\-01T\*(Aq, \& maximum => \*(Aq2008\-12\-12T\*(Aq, \& } \& ); \& \& \& # \& # Example 8. Limiting input to a time in a fixed range. \& # \& $txt = \*(AqEnter a time between 6:00 and 12:00\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqtime\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => \*(AqT06:00\*(Aq, \& maximum => \*(AqT12:00\*(Aq, \& } \& ); \& \& \& # \& # Example 9. Limiting input to a string greater than a fixed length. \& # \& $txt = \*(AqEnter a string longer than 3 characters\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqlength\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 3, \& } \& ); \& \& \& # \& # Example 10. Limiting input based on a formula. \& # \& $txt = \*(AqEnter a value if the following is true "=AND(F5=50,G5=60)"\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqcustom\*(Aq, \& value => \*(Aq=AND(F5=50,G5=60)\*(Aq, \& } \& ); \& \& \& # \& # Example 11. Displaying and modify data validation messages. \& # \& $txt = \*(AqDisplays a message when you select the cell\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqEnter an integer:\*(Aq, \& input_message => \*(Aqbetween 1 and 100\*(Aq, \& } \& ); \& \& \& # \& # Example 12. Displaying and modify data validation messages. \& # \& $txt = \*(AqDisplay a custom error message when integer isn\e\*(Aqt between 1 and 100\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqEnter an integer:\*(Aq, \& input_message => \*(Aqbetween 1 and 100\*(Aq, \& error_title => \*(AqInput value is not valid!\*(Aq, \& error_message => \*(AqIt should be an integer between 1 and 100\*(Aq, \& } \& ); \& \& \& # \& # Example 13. Displaying and modify data validation messages. \& # \& $txt = \& \*(AqDisplay a custom information message when integer isn\e\*(Aqt between 1 and 100\*(Aq; \& $row += 2; \& \& $worksheet\->write( $row, 0, $txt ); \& $worksheet\->data_validation( \& $row, 1, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqEnter an integer:\*(Aq, \& input_message => \*(Aqbetween 1 and 100\*(Aq, \& error_title => \*(AqInput value is not valid!\*(Aq, \& error_message => \*(AqIt should be an integer between 1 and 100\*(Aq, \& error_type => \*(Aqinformation\*(Aq, \& } \& ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: date_time.pl" .IX Subsection "Example: date_time.pl" Excel::Writer::XLSX example of writing dates and times using the \&\fBwrite_date_time()\fR Worksheet method. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Excel::Writer::XLSX example of writing dates and times using the \& # write_date_time() Worksheet method. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqdate_time.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& \& \& # Expand the first columns so that the date is visible. \& $worksheet\->set_column( "A:B", 30 ); \& \& \& # Write the column headers \& $worksheet\->write( \*(AqA1\*(Aq, \*(AqFormatted date\*(Aq, $bold ); \& $worksheet\->write( \*(AqB1\*(Aq, \*(AqFormat\*(Aq, $bold ); \& \& \& # Examples date and time formats. In the output file compare how changing \& # the format codes change the appearance of the date. \& # \& my @date_formats = ( \& \*(Aqdd/mm/yy\*(Aq, \& \*(Aqmm/dd/yy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqd mm yy\*(Aq, \& \*(Aqdd mm yy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqdd m yy\*(Aq, \& \*(Aqdd mm yy\*(Aq, \& \*(Aqdd mmm yy\*(Aq, \& \*(Aqdd mmmm yy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqdd mm y\*(Aq, \& \*(Aqdd mm yyy\*(Aq, \& \*(Aqdd mm yyyy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqd mmmm yyyy\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqdd/mm/yy\*(Aq, \& \*(Aqdd/mm/yy hh:mm\*(Aq, \& \*(Aqdd/mm/yy hh:mm:ss\*(Aq, \& \*(Aqdd/mm/yy hh:mm:ss.000\*(Aq, \& \*(Aq\*(Aq, \& \*(Aqhh:mm\*(Aq, \& \*(Aqhh:mm:ss\*(Aq, \& \*(Aqhh:mm:ss.000\*(Aq, \& ); \& \& \& # Write the same date and time using each of the above formats. The empty \& # string formats create a blank line to make the example clearer. \& # \& my $row = 0; \& for my $date_format ( @date_formats ) { \& $row++; \& next if $date_format eq \*(Aq\*(Aq; \& \& # Create a format for the date or time. \& my $format = $workbook\->add_format( \& num_format => $date_format, \& align => \*(Aqleft\*(Aq \& ); \& \& # Write the same date using different formats. \& $worksheet\->write_date_time( $row, 0, \*(Aq2004\-08\-01T12:30:45.123\*(Aq, $format ); \& $worksheet\->write( $row, 1, $date_format ); \& } \& \& \& # The following is an example of an invalid date. It is written as a string \& # instead of a number. This is also Excel\*(Aqs default behaviour. \& # \& $row += 2; \& $worksheet\->write_date_time( $row, 0, \*(Aq2004\-13\-01T12:30:45.123\*(Aq ); \& $worksheet\->write( $row, 1, \*(AqInvalid date. Written as string.\*(Aq, $bold ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: defined_name.pl" .IX Subsection "Example: defined_name.pl" Example of how to create defined names in an Excel::Writer::XLSX file. .PP This method is used to define a user friendly name to represent a value, a single cell or a range of cells in a workbook. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # Example of how to create defined names in an Excel::Writer::XLSX file. \& # \& # This method is used to define a user friendly name to represent a value, \& # a single cell or a range of cells in a workbook. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqdefined_name.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& \& # Define some global/workbook names. \& $workbook\->define_name( \*(AqExchange_rate\*(Aq, \*(Aq=0.96\*(Aq ); \& $workbook\->define_name( \*(AqSales\*(Aq, \*(Aq=Sheet1!$G$1:$H$10\*(Aq ); \& \& # Define a local/worksheet name. \& $workbook\->define_name( \*(AqSheet2!Sales\*(Aq, \*(Aq=Sheet2!$G$1:$G$10\*(Aq ); \& \& # Write some text in the file and one of the defined names in a formula. \& for my $worksheet ( $workbook\->sheets() ) { \& $worksheet\->set_column( \*(AqA:A\*(Aq, 45 ); \& $worksheet\->write( \*(AqA1\*(Aq, \*(AqThis worksheet contains some defined names.\*(Aq ); \& $worksheet\->write( \*(AqA2\*(Aq, \*(AqSee Formulas \-> Name Manager above.\*(Aq ); \& $worksheet\->write( \*(AqA3\*(Aq, \*(AqExample formula in cell B3 \->\*(Aq ); \& \& $worksheet\->write( \*(AqB3\*(Aq, \*(Aq=Exchange_rate\*(Aq ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: diag_border.pl" .IX Subsection "Example: diag_border.pl" A simple formatting example that demonstrates how to add a diagonal cell border with Excel::Writer::XLSX .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple formatting example that demonstrates how to add a diagonal cell \& # border with Excel::Writer::XLSX \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqdiag_border.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& my $format1 = $workbook\->add_format( diag_type => 1 ); \& \& my $format2 = $workbook\->add_format( diag_type => 2 ); \& \& my $format3 = $workbook\->add_format( diag_type => 3 ); \& \& my $format4 = $workbook\->add_format( \& diag_type => 3, \& diag_border => 7, \& diag_color => \*(Aqred\*(Aq, \& ); \& \& \& $worksheet\->write( \*(AqB3\*(Aq, \*(AqText\*(Aq, $format1 ); \& $worksheet\->write( \*(AqB6\*(Aq, \*(AqText\*(Aq, $format2 ); \& $worksheet\->write( \*(AqB9\*(Aq, \*(AqText\*(Aq, $format3 ); \& $worksheet\->write( \*(AqB12\*(Aq, \*(AqText\*(Aq, $format4 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: dynamic_arrays.pl" .IX Subsection "Example: dynamic_arrays.pl" An example of how to use the Excel::Writer::XLSX module to write functions that create dynamic arrays. These functions are new to Excel 365. The examples mirror the examples in the Excel documentation on these functions. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # An example of how to use the Excel::Writer::XLSX module to write functions \& # that create dynamic arrays. These functions are new to Excel 365. The \& # examples mirror the examples in the Excel documentation on these functions. \& # \& # Copyright 2000\-2021, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& sub write_worksheet_data; \& \& # Create a new workbook called simple.xls and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new(\*(Aqdynamic_arrays.xlsx\*(Aq); \& my $worksheet1 = $workbook\->add_worksheet(\*(AqFilter\*(Aq); \& my $worksheet2 = $workbook\->add_worksheet(\*(AqUnique\*(Aq); \& my $worksheet3 = $workbook\->add_worksheet(\*(AqSort\*(Aq); \& my $worksheet4 = $workbook\->add_worksheet(\*(AqSortby\*(Aq); \& my $worksheet5 = $workbook\->add_worksheet(\*(AqXlookup\*(Aq); \& my $worksheet6 = $workbook\->add_worksheet(\*(AqXmatch\*(Aq); \& my $worksheet7 = $workbook\->add_worksheet(\*(AqRandarray\*(Aq); \& my $worksheet8 = $workbook\->add_worksheet(\*(AqSequence\*(Aq); \& my $worksheet9 = $workbook\->add_worksheet(\*(AqSpill ranges\*(Aq); \& my $worksheet10 = $workbook\->add_worksheet(\*(AqOlder functions\*(Aq); \& my $header1 = $workbook\->add_format(fg_color => \*(Aq#74AC4C\*(Aq, color => \*(Aq#FFFFFF\*(Aq); \& my $header2 = $workbook\->add_format(fg_color => \*(Aq#528FD3\*(Aq, color => \*(Aq#FFFFFF\*(Aq); \& \& \& # \& # Example of using the FILTER() function. \& # \& $worksheet1\->write(\*(AqF2\*(Aq, \*(Aq=FILTER(A1:D17,C1:C17=K2)\*(Aq); \& \& \& # Write the data the function will work on. \& $worksheet1\->write(\*(AqK1\*(Aq, \*(AqProduct\*(Aq, $header2); \& $worksheet1\->write(\*(AqK2\*(Aq, \*(AqApple\*(Aq); \& $worksheet1\->write(\*(AqF1\*(Aq, \*(AqRegion\*(Aq, $header2); \& $worksheet1\->write(\*(AqG1\*(Aq, \*(AqSales Rep\*(Aq, $header2); \& $worksheet1\->write(\*(AqH1\*(Aq, \*(AqProduct\*(Aq, $header2); \& $worksheet1\->write(\*(AqI1\*(Aq, \*(AqUnits\*(Aq, $header2); \& \& write_worksheet_data($worksheet1, $header1); \& $worksheet1\->set_column_pixels(\*(AqE:E\*(Aq, 20); \& $worksheet1\->set_column_pixels(\*(AqJ:J\*(Aq, 20); \& \& # \& # Example of using the UNIQUE() function. \& # \& $worksheet2\->write(\*(AqF2\*(Aq, \*(Aq=UNIQUE(B2:B17)\*(Aq); \& \& \& # A more complex example combining SORT and UNIQUE. \& $worksheet2\->write(\*(AqH2\*(Aq, \*(Aq=SORT(UNIQUE(B2:B17))\*(Aq); \& \& # Write the data the function will work on. \& $worksheet2\->write(\*(AqF1\*(Aq, \*(AqSales Rep\*(Aq, $header2); \& $worksheet2\->write(\*(AqH1\*(Aq, \*(AqSales Rep\*(Aq, $header2); \& \& write_worksheet_data($worksheet2, $header1); \& $worksheet2\->set_column_pixels(\*(AqE:E\*(Aq, 20); \& $worksheet2\->set_column_pixels(\*(AqG:G\*(Aq, 20); \& \& \& # \& # Example of using the SORT() function. \& # \& $worksheet3\->write(\*(AqF2\*(Aq, \*(Aq=SORT(B2:B17)\*(Aq); \& \& # A more complex example combining SORT and FILTER. \& $worksheet3\->write(\*(AqH2\*(Aq, \*(Aq=SORT(FILTER(C2:D17,D2:D17>5000,""),2,1)\*(Aq); \& \& \& # Write the data the function will work on. \& $worksheet3\->write(\*(AqF1\*(Aq, \*(AqSales Rep\*(Aq, $header2); \& $worksheet3\->write(\*(AqH1\*(Aq, \*(AqProduct\*(Aq, $header2); \& $worksheet3\->write(\*(AqI1\*(Aq, \*(AqUnits\*(Aq, $header2); \& \& write_worksheet_data($worksheet3, $header1); \& $worksheet3\->set_column_pixels(\*(AqE:E\*(Aq, 20); \& $worksheet3\->set_column_pixels(\*(AqG:G\*(Aq, 20); \& \& \& # \& # Example of using the SORTBY() function. \& # \& $worksheet4\->write(\*(AqD2\*(Aq, \*(Aq=SORTBY(A2:B9,B2:B9)\*(Aq); \& \& \& # Write the data the function will work on. \& $worksheet4\->write(\*(AqA1\*(Aq, \*(AqName\*(Aq, $header1); \& $worksheet4\->write(\*(AqB1\*(Aq, \*(AqAge\*(Aq, $header1); \& \& $worksheet4\->write(\*(AqA2\*(Aq, \*(AqTom\*(Aq); \& $worksheet4\->write(\*(AqA3\*(Aq, \*(AqFred\*(Aq); \& $worksheet4\->write(\*(AqA4\*(Aq, \*(AqAmy\*(Aq); \& $worksheet4\->write(\*(AqA5\*(Aq, \*(AqSal\*(Aq); \& $worksheet4\->write(\*(AqA6\*(Aq, \*(AqFritz\*(Aq); \& $worksheet4\->write(\*(AqA7\*(Aq, \*(AqSrivan\*(Aq); \& $worksheet4\->write(\*(AqA8\*(Aq, \*(AqXi\*(Aq); \& $worksheet4\->write(\*(AqA9\*(Aq, \*(AqHector\*(Aq); \& \& $worksheet4\->write(\*(AqB2\*(Aq, 52); \& $worksheet4\->write(\*(AqB3\*(Aq, 65); \& $worksheet4\->write(\*(AqB4\*(Aq, 22); \& $worksheet4\->write(\*(AqB5\*(Aq, 73); \& $worksheet4\->write(\*(AqB6\*(Aq, 19); \& $worksheet4\->write(\*(AqB7\*(Aq, 39); \& $worksheet4\->write(\*(AqB8\*(Aq, 19); \& $worksheet4\->write(\*(AqB9\*(Aq, 66); \& \& $worksheet4\->write(\*(AqD1\*(Aq, \*(AqName\*(Aq, $header2); \& $worksheet4\->write(\*(AqE1\*(Aq, \*(AqAge\*(Aq, $header2); \& \& $worksheet4\->set_column_pixels(\*(AqC:C\*(Aq, 20); \& \& \& # \& # Example of using the XLOOKUP() function. \& # \& $worksheet5\->write(\*(AqF1\*(Aq, \*(Aq=XLOOKUP(E1,A2:A9,C2:C9)\*(Aq); \& \& \& # Write the data the function will work on. \& $worksheet5\->write(\*(AqA1\*(Aq, \*(AqCountry\*(Aq, $header1); \& $worksheet5\->write(\*(AqB1\*(Aq, \*(AqAbr\*(Aq, $header1); \& $worksheet5\->write(\*(AqC1\*(Aq, \*(AqPrefix\*(Aq, $header1); \& \& $worksheet5\->write(\*(AqA2\*(Aq, \*(AqChina\*(Aq); \& $worksheet5\->write(\*(AqA3\*(Aq, \*(AqIndia\*(Aq); \& $worksheet5\->write(\*(AqA4\*(Aq, \*(AqUnited States\*(Aq); \& $worksheet5\->write(\*(AqA5\*(Aq, \*(AqIndonesia\*(Aq); \& $worksheet5\->write(\*(AqA6\*(Aq, \*(AqBrazil\*(Aq); \& $worksheet5\->write(\*(AqA7\*(Aq, \*(AqPakistan\*(Aq); \& $worksheet5\->write(\*(AqA8\*(Aq, \*(AqNigeria\*(Aq); \& $worksheet5\->write(\*(AqA9\*(Aq, \*(AqBangladesh\*(Aq); \& \& $worksheet5\->write(\*(AqB2\*(Aq, \*(AqCN\*(Aq); \& $worksheet5\->write(\*(AqB3\*(Aq, \*(AqIN\*(Aq); \& $worksheet5\->write(\*(AqB4\*(Aq, \*(AqUS\*(Aq); \& $worksheet5\->write(\*(AqB5\*(Aq, \*(AqID\*(Aq); \& $worksheet5\->write(\*(AqB6\*(Aq, \*(AqBR\*(Aq); \& $worksheet5\->write(\*(AqB7\*(Aq, \*(AqPK\*(Aq); \& $worksheet5\->write(\*(AqB8\*(Aq, \*(AqNG\*(Aq); \& $worksheet5\->write(\*(AqB9\*(Aq, \*(AqBD\*(Aq); \& \& $worksheet5\->write(\*(AqC2\*(Aq, 86); \& $worksheet5\->write(\*(AqC3\*(Aq, 91); \& $worksheet5\->write(\*(AqC4\*(Aq, 1); \& $worksheet5\->write(\*(AqC5\*(Aq, 62); \& $worksheet5\->write(\*(AqC6\*(Aq, 55); \& $worksheet5\->write(\*(AqC7\*(Aq, 92); \& $worksheet5\->write(\*(AqC8\*(Aq, 234); \& $worksheet5\->write(\*(AqC9\*(Aq, 880); \& \& $worksheet5\->write(\*(AqE1\*(Aq, \*(AqBrazil\*(Aq, $header2); \& \& $worksheet5\->set_column_pixels(\*(AqA:A\*(Aq, 100); \& $worksheet5\->set_column_pixels(\*(AqD:D\*(Aq, 20); \& \& \& # \& # Example of using the XMATCH() function. \& # \& $worksheet6\->write(\*(AqD2\*(Aq, \*(Aq=XMATCH(C2,A2:A6)\*(Aq); \& \& \& # Write the data the function will work on. \& $worksheet6\->write(\*(AqA1\*(Aq, \*(AqProduct\*(Aq, $header1); \& \& $worksheet6\->write(\*(AqA2\*(Aq, \*(AqApple\*(Aq); \& $worksheet6\->write(\*(AqA3\*(Aq, \*(AqGrape\*(Aq); \& $worksheet6\->write(\*(AqA4\*(Aq, \*(AqPear\*(Aq); \& $worksheet6\->write(\*(AqA5\*(Aq, \*(AqBanana\*(Aq); \& $worksheet6\->write(\*(AqA6\*(Aq, \*(AqCherry\*(Aq); \& \& $worksheet6\->write(\*(AqC1\*(Aq, \*(AqProduct\*(Aq , $header2); \& $worksheet6\->write(\*(AqD1\*(Aq, \*(AqPosition\*(Aq, $header2); \& $worksheet6\->write(\*(AqC2\*(Aq, \*(AqGrape\*(Aq); \& \& $worksheet6\->set_column_pixels(\*(AqB:B\*(Aq, 20); \& \& \& # \& # Example of using the RANDARRAY() function. \& # \& $worksheet7\->write(\*(AqA1\*(Aq, \*(Aq=RANDARRAY(5,3,1,100, TRUE)\*(Aq); \& \& \& # \& # Example of using the SEQUENCE() function. \& # \& $worksheet8\->write(\*(AqA1\*(Aq, \*(Aq=SEQUENCE(4,5)\*(Aq); \& \& \& # \& # Example of using the Spill range operator. \& # \& $worksheet9\->write(\*(AqH2\*(Aq, \*(Aq=ANCHORARRAY(F2)\*(Aq); \& \& $worksheet9\->write(\*(AqJ2\*(Aq, \*(Aq=COUNTA(ANCHORARRAY(F2))\*(Aq); \& \& \& # Write the data the to work on. \& $worksheet9\->write( \*(AqF2\*(Aq, \*(Aq=UNIQUE(B2:B17)\*(Aq); \& $worksheet9\->write(\*(AqF1\*(Aq, \*(AqUnique\*(Aq, $header2); \& $worksheet9\->write(\*(AqH1\*(Aq, \*(AqSpill\*(Aq, $header2); \& $worksheet9\->write(\*(AqJ1\*(Aq, \*(AqSpill\*(Aq, $header2); \& \& write_worksheet_data($worksheet9, $header1); \& $worksheet9\->set_column_pixels(\*(AqE:E\*(Aq, 20); \& $worksheet9\->set_column_pixels(\*(AqG:G\*(Aq, 20); \& $worksheet9\->set_column_pixels(\*(AqI:I\*(Aq, 20); \& \& # \& # Example of using dynamic ranges with older Excel functions. \& # \& $worksheet10\->write_dynamic_array_formula(\*(AqB1:B3\*(Aq, \*(Aq=LEN(A1:A3)\*(Aq); \& \& # Write the data the to work on. \& $worksheet10\->write(\*(AqA1\*(Aq, \*(AqFoo\*(Aq); \& $worksheet10\->write(\*(AqA2\*(Aq, \*(AqFood\*(Aq); \& $worksheet10\->write(\*(AqA3\*(Aq, \*(AqFrood\*(Aq); \& \& \& # Close the workbook. \& $workbook\->close(); \& \& # Utility function to write the data some of the functions work on. \& sub write_worksheet_data { \& my $worksheet = shift; \& my $header = shift; \& \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqRegion\*(Aq, $header); \& $worksheet\->write(\*(AqB1\*(Aq, \*(AqSales Rep\*(Aq, $header); \& $worksheet\->write(\*(AqC1\*(Aq, \*(AqProduct\*(Aq, $header); \& $worksheet\->write(\*(AqD1\*(Aq, \*(AqUnits\*(Aq, $header); \& \& my @data = ( \& [\*(AqEast\*(Aq, \*(AqTom\*(Aq, \*(AqApple\*(Aq, 6380], \& [\*(AqWest\*(Aq, \*(AqFred\*(Aq, \*(AqGrape\*(Aq, 5619], \& [\*(AqNorth\*(Aq, \*(AqAmy\*(Aq, \*(AqPear\*(Aq, 4565], \& [\*(AqSouth\*(Aq, \*(AqSal\*(Aq, \*(AqBanana\*(Aq, 5323], \& [\*(AqEast\*(Aq, \*(AqFritz\*(Aq, \*(AqApple\*(Aq, 4394], \& [\*(AqWest\*(Aq, \*(AqSravan\*(Aq, \*(AqGrape\*(Aq, 7195], \& [\*(AqNorth\*(Aq, \*(AqXi\*(Aq, \*(AqPear\*(Aq, 5231], \& [\*(AqSouth\*(Aq, \*(AqHector\*(Aq, \*(AqBanana\*(Aq, 2427], \& [\*(AqEast\*(Aq, \*(AqTom\*(Aq, \*(AqBanana\*(Aq, 4213], \& [\*(AqWest\*(Aq, \*(AqFred\*(Aq, \*(AqPear\*(Aq, 3239], \& [\*(AqNorth\*(Aq, \*(AqAmy\*(Aq, \*(AqGrape\*(Aq, 6520], \& [\*(AqSouth\*(Aq, \*(AqSal\*(Aq, \*(AqApple\*(Aq, 1310], \& [\*(AqEast\*(Aq, \*(AqFritz\*(Aq, \*(AqBanana\*(Aq, 6274], \& [\*(AqWest\*(Aq, \*(AqSravan\*(Aq, \*(AqPear\*(Aq, 4894], \& [\*(AqNorth\*(Aq, \*(AqXi\*(Aq, \*(AqGrape\*(Aq, 7580], \& [\*(AqSouth\*(Aq, \*(AqHector\*(Aq, \*(AqApple\*(Aq, 9814], \& ); \& \& my $row_num = 1; \& for my $row_data (@data) { \& $worksheet\->write_row($row_num, 0, $row_data); \& $row_num++; \& } \& } \& \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: filehandle.pl" .IX Subsection "Example: filehandle.pl" Example of using Excel::Writer::XLSX to write Excel files to different filehandles. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of using Excel::Writer::XLSX to write Excel files to different \& # filehandles. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& use IO::Scalar; \& \& \& ############################################################################### \& # \& # Example 1. This demonstrates the standard way of creating an Excel file by \& # specifying a file name. \& # \& \& my $workbook1 = Excel::Writer::XLSX\->new( \*(Aqfh_01.xlsx\*(Aq ); \& my $worksheet1 = $workbook1\->add_worksheet(); \& \& $worksheet1\->write( 0, 0, \*(AqHi Excel 1\*(Aq ); \& \& $workbook1\->close(); \& \& \& ############################################################################### \& # \& # Example 2. Write an Excel file to an existing filehandle. \& # \& \& open TEST, \*(Aq>\*(Aq, \*(Aqfh_02.xlsx\*(Aq or die "Couldn\*(Aqt open file: $!"; \& binmode TEST; # Always do this regardless of whether the platform requires it. \& \& my $workbook2 = Excel::Writer::XLSX\->new( \e*TEST ); \& my $worksheet2 = $workbook2\->add_worksheet(); \& \& $worksheet2\->write( 0, 0, \*(AqHi Excel 2\*(Aq ); \& \& $workbook2\->close(); \& \& ############################################################################### \& # \& # Example 3. Write an Excel file to an existing OO style filehandle. \& # \& \& my $fh = FileHandle\->new( \*(Aq> fh_03.xlsx\*(Aq ) or die "Couldn\*(Aqt open file: $!"; \& \& binmode( $fh ); \& \& my $workbook3 = Excel::Writer::XLSX\->new( $fh ); \& my $worksheet3 = $workbook3\->add_worksheet(); \& \& $worksheet3\->write( 0, 0, \*(AqHi Excel 3\*(Aq ); \& \& $workbook3\->close(); \& \& \& ############################################################################### \& # \& # Example 4. Write an Excel file to a string via IO::Scalar. Please refer to \& # the IO::Scalar documentation for further details. \& # \& \& my $xlsx_str; \& \& tie *XLSX, \*(AqIO::Scalar\*(Aq, \e$xlsx_str; \& \& my $workbook4 = Excel::Writer::XLSX\->new( \e*XLSX ); \& my $worksheet4 = $workbook4\->add_worksheet(); \& \& $worksheet4\->write( 0, 0, \*(AqHi Excel 4\*(Aq ); \& $workbook4\->close(); # This is required before we use the scalar \& \& \& # The Excel file is now in $xlsx_str. As a demonstration, print it to a file. \& open TMP, \*(Aq>\*(Aq, \*(Aqfh_04.xlsx\*(Aq or die "Couldn\*(Aqt open file: $!"; \& binmode TMP; \& print TMP $xlsx_str; \& close TMP; \& \& \& ############################################################################### \& # \& # Example 5. Write an Excel file to a string via IO::Scalar\*(Aqs newer interface. \& # Please refer to the IO::Scalar documentation for further details. \& # \& my $xlsx_str2; \& \& my $fh5 = IO::Scalar\->new( \e$xlsx_str2 ); \& \& my $workbook5 = Excel::Writer::XLSX\->new( $fh5 ); \& my $worksheet5 = $workbook5\->add_worksheet(); \& \& $worksheet5\->write( 0, 0, \*(AqHi Excel 5\*(Aq ); \& $workbook5\->close(); # This is required before we use the scalar \& \& # The Excel file is now in $xlsx_str. As a demonstration, print it to a file. \& open TMP, \*(Aq>\*(Aq, \*(Aqfh_05.xlsx\*(Aq or die "Couldn\*(Aqt open file: $!"; \& binmode TMP; \& print TMP $xlsx_str2; \& close TMP; \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: headers.pl" .IX Subsection "Example: headers.pl" This program shows several examples of how to set up headers and footers with Excel::Writer::XLSX. .PP The control characters used in the header/footer strings are: .PP .Vb 5 \& Control Category Description \& ======= ======== =========== \& &L Justification Left \& &C Center \& &R Right \& \& &P Information Page number \& &N Total number of pages \& &D Date \& &T Time \& &F File name \& &A Worksheet name \& \& &fontsize Font Font size \& &"font,style" Font name and style \& &U Single underline \& &E Double underline \& &S Strikethrough \& &X Superscript \& &Y Subscript \& \& &[Picture] Images Image placeholder \& &G Same as &[Picture] \& \& && Miscellaneous Literal ampersand & .Ve .PP See the main Excel::Writer::XLSX documentation for more information. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ###################################################################### \& # \& # This program shows several examples of how to set up headers and \& # footers with Excel::Writer::XLSX. \& # \& # The control characters used in the header/footer strings are: \& # \& # Control Category Description \& # ======= ======== =========== \& # &L Justification Left \& # &C Center \& # &R Right \& # \& # &P Information Page number \& # &N Total number of pages \& # &D Date \& # &T Time \& # &F File name \& # &A Worksheet name \& # \& # &fontsize Font Font size \& # &"font,style" Font name and style \& # &U Single underline \& # &E Double underline \& # &S Strikethrough \& # &X Superscript \& # &Y Subscript \& # \& # &[Picture] Images Image placeholder \& # &G Same as &[Picture] \& # \& # && Miscellaneous Literal ampersand & \& # \& # See the main Excel::Writer::XLSX documentation for more information. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqheaders.xlsx\*(Aq ); \& my $preview = \*(AqSelect Print Preview to see the header and footer\*(Aq; \& \& \& ###################################################################### \& # \& # A simple example to start \& # \& my $worksheet1 = $workbook\->add_worksheet( \*(AqSimple\*(Aq ); \& my $header1 = \*(Aq&CHere is some centred text.\*(Aq; \& my $footer1 = \*(Aq&LHere is some left aligned text.\*(Aq; \& \& $worksheet1\->set_header( $header1 ); \& $worksheet1\->set_footer( $footer1 ); \& \& $worksheet1\->set_column( \*(AqA:A\*(Aq, 50 ); \& $worksheet1\->write( \*(AqA1\*(Aq, $preview ); \& \& \& ###################################################################### \& # \& # A simple example to start \& # \& my $worksheet2 = $workbook\->add_worksheet( \*(AqImage\*(Aq ); \& my $header2 = \*(Aq&L&[Picture]\*(Aq; \& \& # Adjust the page top margin to allow space for the header image. \& $worksheet2\->set_margin_top(1.75); \& \& $worksheet2\->set_header( $header2, 0.3, {image_left => \*(Aqrepublic.png\*(Aq}); \& \& $worksheet2\->set_column( \*(AqA:A\*(Aq, 50 ); \& $worksheet2\->write( \*(AqA1\*(Aq, $preview ); \& \& \& ###################################################################### \& # \& # This is an example of some of the header/footer variables. \& # \& my $worksheet3 = $workbook\->add_worksheet( \*(AqVariables\*(Aq ); \& my $header3 = \*(Aq&LPage &P of &N\*(Aq . \*(Aq&CFilename: &F\*(Aq . \*(Aq&RSheetname: &A\*(Aq; \& my $footer3 = \*(Aq&LCurrent date: &D\*(Aq . \*(Aq&RCurrent time: &T\*(Aq; \& \& $worksheet3\->set_header( $header3 ); \& $worksheet3\->set_footer( $footer3 ); \& \& $worksheet3\->set_column( \*(AqA:A\*(Aq, 50 ); \& $worksheet3\->write( \*(AqA1\*(Aq, $preview ); \& $worksheet3\->write( \*(AqA21\*(Aq, \*(AqNext sheet\*(Aq ); \& $worksheet3\->set_h_pagebreaks( 20 ); \& \& \& ###################################################################### \& # \& # This example shows how to use more than one font \& # \& my $worksheet4 = $workbook\->add_worksheet( \*(AqMixed fonts\*(Aq ); \& my $header4 = q(&C&"Courier New,Bold"Hello &"Arial,Italic"World); \& my $footer4 = q(&C&"Symbol"e&"Arial" = mc&X2); \& \& $worksheet4\->set_header( $header4 ); \& $worksheet4\->set_footer( $footer4 ); \& \& $worksheet4\->set_column( \*(AqA:A\*(Aq, 50 ); \& $worksheet4\->write( \*(AqA1\*(Aq, $preview ); \& \& \& ###################################################################### \& # \& # Example of line wrapping \& # \& my $worksheet5 = $workbook\->add_worksheet( \*(AqWord wrap\*(Aq ); \& my $header5 = "&CHeading 1\enHeading 2"; \& \& $worksheet5\->set_header( $header5 ); \& \& $worksheet5\->set_column( \*(AqA:A\*(Aq, 50 ); \& $worksheet5\->write( \*(AqA1\*(Aq, $preview ); \& \& \& ###################################################################### \& # \& # Example of inserting a literal ampersand & \& # \& my $worksheet6 = $workbook\->add_worksheet( \*(AqAmpersand\*(Aq ); \& my $header6 = \*(Aq&CCuriouser && Curiouser \- Attorneys at Law\*(Aq; \& \& $worksheet6\->set_header( $header6 ); \& \& $worksheet6\->set_column( \*(AqA:A\*(Aq, 50 ); \& $worksheet6\->write( \*(AqA1\*(Aq, $preview ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: hide_row_col.pl" .IX Subsection "Example: hide_row_col.pl" Example of how to hide rows and columns in Excel::Writer::XLSX. In order to hide rows without setting each one, (of approximately 1 million rows), Excel uses an optimisation to hide all rows that don't have data. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to hide rows and columns in Excel::Writer::XLSX. In order to \& # hide rows without setting each one, (of approximately 1 million rows), \& # Excel uses an optimisation to hide all rows that don\*(Aqt have data. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqhide_row_col.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Write some data. \& $worksheet\->write( \*(AqD1\*(Aq, \*(AqSome hidden columns.\*(Aq ); \& $worksheet\->write( \*(AqA8\*(Aq, \*(AqSome hidden rows.\*(Aq ); \& \& # Hide all rows without data. \& $worksheet\->set_default_row( undef, 1 ); \& \& # Set emptys row that we do want to display. All other will be hidden. \& for my $row (1 .. 6) { \& $worksheet\->set_row( $row, 15 ); \& } \& \& # Hide a range of columns. \& $worksheet\->set_column( \*(AqG:XFD\*(Aq, undef, undef, 1); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: hide_sheet.pl" .IX Subsection "Example: hide_sheet.pl" Example of how to hide a worksheet with Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # Example of how to hide a worksheet with Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqhide_sheet.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& \& $worksheet1\->set_column( \*(AqA:A\*(Aq, 30 ); \& $worksheet2\->set_column( \*(AqA:A\*(Aq, 30 ); \& $worksheet3\->set_column( \*(AqA:A\*(Aq, 30 ); \& \& # Sheet2 won\*(Aqt be visible until it is unhidden in Excel. \& $worksheet2\->hide(); \& \& $worksheet1\->write( 0, 0, \*(AqSheet2 is hidden\*(Aq ); \& $worksheet2\->write( 0, 0, "Now it\*(Aqs my turn to find you." ); \& $worksheet3\->write( 0, 0, \*(AqSheet2 is hidden\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: hyperlink1.pl" .IX Subsection "Example: hyperlink1.pl" Example of how to use the Excel::Writer::XLSX module to write hyperlinks .PP See also hyperlink2.pl for worksheet \s-1URL\s0 examples. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX module to write hyperlinks \& # \& # See also hyperlink2.pl for worksheet URL examples. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqhyperlink.xlsx\*(Aq ); \& \& \& my $worksheet = $workbook\->add_worksheet( \*(AqHyperlinks\*(Aq ); \& \& # Format the first column \& $worksheet\->set_column( \*(AqA:A\*(Aq, 30 ); \& $worksheet\->set_selection( \*(AqB1\*(Aq ); \& \& # Add a user defined hyperlink format. \& my $red_format = $workbook\->add_format( \& color => \*(Aqred\*(Aq, \& bold => 1, \& underline => 1, \& size => 12, \& ); \& \& # Add an alternate description string to the URL. \& my $str = \*(AqPerl home.\*(Aq; \& \& # Add a "tool tip" to the URL. \& my $tip = \*(AqGet the latest Perl news here.\*(Aq; \& \& \& # Write some hyperlinks. Unspecified or undefined format paraamters will be \& # replace with the defuault Excel hyperlink style. \& $worksheet\->write( \*(AqA1\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq ); \& $worksheet\->write( \*(AqA3\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq, undef, $str ); \& $worksheet\->write( \*(AqA5\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq, undef, $str, $tip ); \& $worksheet\->write( \*(AqA7\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq, $red_format ); \& $worksheet\->write( \*(AqA9\*(Aq, \*(Aqmailto:jmcnamara@cpan.org\*(Aq, undef, \*(AqMail me\*(Aq ); \& \& # Write a URL that isn\*(Aqt a hyperlink \& $worksheet\->write_string( \*(AqA11\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: hyperlink2.pl" .IX Subsection "Example: hyperlink2.pl" Example of how to use the Excel::Writer::XLSX module to write internal and external hyperlinks. .PP If you wish to run this program and follow the hyperlinks you should create the following directory structure: .PP C:\e \*(-- Temp \-\-+\-\- Europe | \e\*(-- Asia .PP See also hyperlink1.pl for web \s-1URL\s0 examples. .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX module to write internal and \& # external hyperlinks. \& # \& # If you wish to run this program and follow the hyperlinks you should create \& # the following directory structure: \& # \& # C:\e \-\- Temp \-\-+\-\- Europe \& # | \& # \e\-\- Asia \& # \& # \& # See also hyperlink1.pl for web URL examples. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create three workbooks: \& # C:\eTemp\eEurope\eIreland.xlsx \& # C:\eTemp\eEurope\eItaly.xlsx \& # C:\eTemp\eAsia\eChina.xlsx \& # \& \& my $ireland = Excel::Writer::XLSX\->new( \*(AqC:\eTemp\eEurope\eIreland.xlsx\*(Aq ); \& \& my $ire_links = $ireland\->add_worksheet( \*(AqLinks\*(Aq ); \& my $ire_sales = $ireland\->add_worksheet( \*(AqSales\*(Aq ); \& my $ire_data = $ireland\->add_worksheet( \*(AqProduct Data\*(Aq ); \& my $ire_url_format = $ireland\->get_default_url_format(); \& \& \& my $italy = Excel::Writer::XLSX\->new( \*(AqC:\eTemp\eEurope\eItaly.xlsx\*(Aq ); \& \& my $ita_links = $italy\->add_worksheet( \*(AqLinks\*(Aq ); \& my $ita_sales = $italy\->add_worksheet( \*(AqSales\*(Aq ); \& my $ita_data = $italy\->add_worksheet( \*(AqProduct Data\*(Aq ); \& my $ita_url_format = $italy\->get_default_url_format(); \& \& \& my $china = Excel::Writer::XLSX\->new( \*(AqC:\eTemp\eAsia\eChina.xlsx\*(Aq ); \& \& my $cha_links = $china\->add_worksheet( \*(AqLinks\*(Aq ); \& my $cha_sales = $china\->add_worksheet( \*(AqSales\*(Aq ); \& my $cha_data = $china\->add_worksheet( \*(AqProduct Data\*(Aq ); \& my $cha_url_format = $china\->get_default_url_format(); \& \& \& # Add an alternative format \& my $format = $ireland\->add_format( color => \*(Aqgreen\*(Aq, bold => 1 ); \& $ire_links\->set_column( \*(AqA:B\*(Aq, 25 ); \& \& \& ############################################################################### \& # \& # Examples of internal links \& # \& $ire_links\->write( \*(AqA1\*(Aq, \*(AqInternal links\*(Aq, $format ); \& \& # Internal link \& $ire_links\->write_url( \*(AqA2\*(Aq, \*(Aqinternal:Sales!A2\*(Aq, $ire_url_format ); \& \& # Internal link to a range \& $ire_links\->write_url( \*(AqA3\*(Aq, \*(Aqinternal:Sales!A3:D3\*(Aq, $ire_url_format ); \& \& # Internal link with an alternative string \& $ire_links\->write_url( \*(AqA4\*(Aq, \*(Aqinternal:Sales!A4\*(Aq, $ire_url_format, \*(AqLink\*(Aq ); \& \& # Internal link with an alternative format \& $ire_links\->write_url( \*(AqA5\*(Aq, \*(Aqinternal:Sales!A5\*(Aq, $format ); \& \& # Internal link with an alternative string and format \& $ire_links\->write_url( \*(AqA6\*(Aq, \*(Aqinternal:Sales!A6\*(Aq, $ire_url_format, \*(AqLink\*(Aq ); \& \& # Internal link (spaces in worksheet name) \& $ire_links\->write_url( \*(AqA7\*(Aq, q{internal:\*(AqProduct Data\*(Aq!A7}, $ire_url_format ); \& \& \& ############################################################################### \& # \& # Examples of external links \& # \& $ire_links\->write( \*(AqB1\*(Aq, \*(AqExternal links\*(Aq, $format ); \& \& # External link to a local file \& $ire_links\->write_url( \*(AqB2\*(Aq, \*(Aqexternal:Italy.xlsx\*(Aq, $ire_url_format ); \& \& # External link to a local file with worksheet \& $ire_links\->write_url( \*(AqB3\*(Aq, \*(Aqexternal:Italy.xlsx#Sales!B3\*(Aq, $ire_url_format ); \& \& # External link to a local file with worksheet and alternative string \& $ire_links\->write_url( \*(AqB4\*(Aq, \*(Aqexternal:Italy.xlsx#Sales!B4\*(Aq, $ire_url_format, \*(AqLink\*(Aq ); \& \& # External link to a local file with worksheet and format \& $ire_links\->write_url( \*(AqB5\*(Aq, \*(Aqexternal:Italy.xlsx#Sales!B5\*(Aq, $format ); \& \& # External link to a remote file, absolute path \& $ire_links\->write_url( \*(AqB6\*(Aq, \*(Aqexternal:C:/Temp/Asia/China.xlsx\*(Aq, $ire_url_format ); \& \& # External link to a remote file, relative path \& $ire_links\->write_url( \*(AqB7\*(Aq, \*(Aqexternal:../Asia/China.xlsx\*(Aq, $ire_url_format ); \& \& # External link to a remote file with worksheet \& $ire_links\->write_url( \*(AqB8\*(Aq, \*(Aqexternal:C:/Temp/Asia/China.xlsx#Sales!B8\*(Aq, $ire_url_format ); \& \& # External link to a remote file with worksheet (with spaces in the name) \& $ire_links\->write_url( \*(AqB9\*(Aq, q{external:C:/Temp/Asia/China.xlsx#\*(AqProduct Data\*(Aq!B9}, $ire_url_format ); \& \& \& ############################################################################### \& # \& # Some utility links to return to the main sheet \& # \& $ire_sales\->write_url( \*(AqA2\*(Aq, \*(Aqinternal:Links!A2\*(Aq, $ire_url_format, \*(AqBack\*(Aq ); \& $ire_sales\->write_url( \*(AqA3\*(Aq, \*(Aqinternal:Links!A3\*(Aq, $ire_url_format, \*(AqBack\*(Aq ); \& $ire_sales\->write_url( \*(AqA4\*(Aq, \*(Aqinternal:Links!A4\*(Aq, $ire_url_format, \*(AqBack\*(Aq ); \& $ire_sales\->write_url( \*(AqA5\*(Aq, \*(Aqinternal:Links!A5\*(Aq, $ire_url_format, \*(AqBack\*(Aq ); \& $ire_sales\->write_url( \*(AqA6\*(Aq, \*(Aqinternal:Links!A6\*(Aq, $ire_url_format, \*(AqBack\*(Aq ); \& $ire_data\->write_url ( \*(AqA7\*(Aq, \*(Aqinternal:Links!A7\*(Aq, $ire_url_format, \*(AqBack\*(Aq ); \& \& $ita_links\->write_url( \*(AqA1\*(Aq, \*(Aqexternal:Ireland.xlsx#Links!B2\*(Aq, $ita_url_format, \*(AqBack\*(Aq ); \& $ita_sales\->write_url( \*(AqB3\*(Aq, \*(Aqexternal:Ireland.xlsx#Links!B3\*(Aq, $ita_url_format, \*(AqBack\*(Aq ); \& $ita_sales\->write_url( \*(AqB4\*(Aq, \*(Aqexternal:Ireland.xlsx#Links!B4\*(Aq, $ita_url_format, \*(AqBack\*(Aq ); \& $ita_sales\->write_url( \*(AqB5\*(Aq, \*(Aqexternal:Ireland.xlsx#Links!B5\*(Aq, $ita_url_format, \*(AqBack\*(Aq ); \& $cha_links\->write_url( \*(AqA1\*(Aq, \*(Aqexternal:C:/Temp/Europe/Ireland.xlsx#Links!B6\*(Aq, $cha_url_format, \*(AqBack\*(Aq ); \& $cha_sales\->write_url( \*(AqB8\*(Aq, \*(Aqexternal:C:/Temp/Europe/Ireland.xlsx#Links!B8\*(Aq, $cha_url_format, \*(AqBack\*(Aq ); \& $cha_data\->write_url ( \*(AqB9\*(Aq, \*(Aqexternal:C:/Temp/Europe/Ireland.xlsx#Links!B9\*(Aq, $cha_url_format, \*(AqBack\*(Aq ); \& \& $ireland\->close(); \& $italy\->close(); \& $china\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: indent.pl" .IX Subsection "Example: indent.pl" A simple formatting example using Excel::Writer::XLSX. .PP This program demonstrates the indentation cell format. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################## \& # \& # A simple formatting example using Excel::Writer::XLSX. \& # \& # This program demonstrates the indentation cell format. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqindent.xlsx\*(Aq ); \& \& my $worksheet = $workbook\->add_worksheet(); \& my $indent1 = $workbook\->add_format( indent => 1 ); \& my $indent2 = $workbook\->add_format( indent => 2 ); \& \& $worksheet\->set_column( \*(AqA:A\*(Aq, 40 ); \& \& \& $worksheet\->write( \*(AqA1\*(Aq, "This text is indented 1 level", $indent1 ); \& $worksheet\->write( \*(AqA2\*(Aq, "This text is indented 2 levels", $indent2 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: ignore_errors.pl" .IX Subsection "Example: ignore_errors.pl" An example of turning off worksheet cells errors/warnings using the Excel::Writer::XLSX module. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # An example of turning off worksheet cells errors/warnings using the \& # Excel::Writer::XLSX module. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqignore_errors.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Write strings that looks like numbers. This will cause an Excel warning. \& $worksheet\->write_string(\*(AqC2\*(Aq, \*(Aq123\*(Aq); \& $worksheet\->write_string(\*(AqC3\*(Aq, \*(Aq123\*(Aq); \& \& # Write a divide by zero formula. This will also cause an Excel warning. \& $worksheet\->write_formula(\*(AqC5\*(Aq, \*(Aq=1/0\*(Aq); \& $worksheet\->write_formula(\*(AqC6\*(Aq, \*(Aq=1/0\*(Aq); \& \& # Turn off some of the warnings: \& $worksheet\->ignore_errors({number_stored_as_text => \*(AqC3\*(Aq, eval_error => \*(AqC6\*(Aq}); \& \& # Write some descriptions for the cells and make the column wider for clarity. \& $worksheet\->set_column(\*(AqB:B\*(Aq, 16); \& $worksheet\->write(\*(AqB2\*(Aq, \*(AqWarning:\*(Aq); \& $worksheet\->write(\*(AqB3\*(Aq, \*(AqWarning turned off:\*(Aq); \& $worksheet\->write(\*(AqB5\*(Aq, \*(AqWarning:\*(Aq); \& $worksheet\->write(\*(AqB6\*(Aq, \*(AqWarning turned off:\*(Aq); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: lambda.pl" .IX Subsection "Example: lambda.pl" An example of using the new Excel \s-1\fBLAMBDA\s0()\fR function with the Excel::Writer::XLSX module. Note, this function is only currently available if you are subscribed to the Microsoft Office Beta program. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # An example of using the new Excel LAMBDA() function with the \& # Excel::Writer::XLSX module. Note, this function is only currently available \& # if you are subscribed to the Microsoft Office Beta program. \& # \& # Copyright 2000\-2021, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqlambda.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqNote: Lambda functions currently only work with the Beta Channel versions of Excel 365\*(Aq); \& \& # Write a Lambda function to convert Fahrenheit to Celsius to a cell. \& # \& # Note that the lambda function parameters must be prefixed with \& # "_xlpm.". These prefixes won\*(Aqt show up in Excel. \& $worksheet\->write(\*(AqA2\*(Aq, \*(Aq=LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp\-32))(32)\*(Aq); \& \& # Create the same formula (without an argument) as a defined name and use that \& # to calculate a value. \& # \& # Note that the formula name is prefixed with "_xlfn." (this is normally \& # converted automatically by write_formula() but isn\*(Aqt for defined names) \& # and note that the lambda function parameters are prefixed with \& # "_xlpm.". These prefixes won\*(Aqt show up in Excel. \& $workbook\->define_name(\*(AqToCelsius\*(Aq, \& \*(Aq=_xlfn.LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp\-32))\*(Aq); \& $worksheet\->write_dynamic_array_formula( \*(AqA3\*(Aq, \*(Aq=ToCelsius(212)\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: macros.pl" .IX Subsection "Example: macros.pl" An example of adding macros to an Excel::Writer::XLSX file using a \s-1VBA\s0 project file extracted from an existing Excel xlsm file. .PP The \f(CW\*(C`extract_vba\*(C'\fR utility supplied with Excel::Writer::XLSX can be used to extract the vbaProject.bin file. .PP An embedded macro is connected to a form button on the worksheet. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # An example of adding macros to an Excel::Writer::XLSX file using \& # a VBA project file extracted from an existing Excel xlsm file. \& # \& # The C utility supplied with Excel::Writer::XLSX can be \& # used to extract the vbaProject.bin file. \& # \& # An embedded macro is connected to a form button on the worksheet. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Note the file extension should be .xlsm. \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqmacros.xlsm\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->set_column( \*(AqA:A\*(Aq, 30 ); \& \& # Add the VBA project binary. \& $workbook\->add_vba_project( \*(Aq./vbaProject.bin\*(Aq ); \& \& # Show text for the end user. \& $worksheet\->write( \*(AqA3\*(Aq, \*(AqPress the button to say hello.\*(Aq ); \& \& # Add a button tied to a macro in the VBA project. \& $worksheet\->insert_button( \& \*(AqB3\*(Aq, \& { \& macro => \*(Aqsay_hello\*(Aq, \& caption => \*(AqPress Me\*(Aq, \& width => 80, \& height => 30 \& } \& ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: merge1.pl" .IX Subsection "Example: merge1.pl" Simple example of merging cells using the Excel::Writer::XLSX module. .PP This example merges three cells using the \*(L"Centre Across Selection\*(R" alignment which was the Excel 5 method of achieving a merge. For a more modern approach use the \fBmerge_range()\fR worksheet method instead. See the merge3.pl \- merge6.pl programs. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Simple example of merging cells using the Excel::Writer::XLSX module. \& # \& # This example merges three cells using the "Centre Across Selection" \& # alignment which was the Excel 5 method of achieving a merge. For a more \& # modern approach use the merge_range() worksheet method instead. \& # See the merge3.pl \- merge6.pl programs. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqmerge1.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_column( \*(AqB:D\*(Aq, 20 ); \& $worksheet\->set_row( 2, 30 ); \& \& \& # Create a merge format \& my $format = $workbook\->add_format( center_across => 1 ); \& \& \& # Only one cell should contain text, the others should be blank. \& $worksheet\->write( 2, 1, "Center across selection", $format ); \& $worksheet\->write_blank( 2, 2, $format ); \& $worksheet\->write_blank( 2, 3, $format ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: merge2.pl" .IX Subsection "Example: merge2.pl" Simple example of merging cells using the Excel::Writer::XLSX module .PP This example merges three cells using the \*(L"Centre Across Selection\*(R" alignment which was the Excel 5 method of achieving a merge. For a more modern approach use the \fBmerge_range()\fR worksheet method instead. See the merge3.pl \- merge6.pl programs. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Simple example of merging cells using the Excel::Writer::XLSX module \& # \& # This example merges three cells using the "Centre Across Selection" \& # alignment which was the Excel 5 method of achieving a merge. For a more \& # modern approach use the merge_range() worksheet method instead. \& # See the merge3.pl \- merge6.pl programs. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqmerge2.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_column( 1, 2, 30 ); \& $worksheet\->set_row( 2, 40 ); \& \& \& # Create a merged format \& my $format = $workbook\->add_format( \& center_across => 1, \& bold => 1, \& size => 15, \& pattern => 1, \& border => 6, \& color => \*(Aqwhite\*(Aq, \& fg_color => \*(Aqgreen\*(Aq, \& border_color => \*(Aqyellow\*(Aq, \& align => \*(Aqvcenter\*(Aq, \& ); \& \& \& # Only one cell should contain text, the others should be blank. \& $worksheet\->write( 2, 1, "Center across selection", $format ); \& $worksheet\->write_blank( 2, 2, $format ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: merge3.pl" .IX Subsection "Example: merge3.pl" Example of how to use Excel::Writer::XLSX to write a hyperlink in a merged cell. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to use Excel::Writer::XLSX to write a hyperlink in a \& # merged cell. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqmerge3.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_row( $_, 30 ) for ( 3, 6, 7 ); \& $worksheet\->set_column( \*(AqB:D\*(Aq, 20 ); \& \& \& ############################################################################### \& # \& # Example: Merge cells containing a hyperlink using merge_range(). \& # \& my $format = $workbook\->add_format( \& border => 1, \& underline => 1, \& color => \*(Aqblue\*(Aq, \& align => \*(Aqcenter\*(Aq, \& valign => \*(Aqvcenter\*(Aq, \& ); \& \& # Merge 3 cells \& $worksheet\->merge_range( \*(AqB4:D4\*(Aq, \*(Aqhttp://www.perl.com\*(Aq, $format ); \& \& \& # Merge 3 cells over two rows \& $worksheet\->merge_range( \*(AqB7:D8\*(Aq, \*(Aqhttp://www.perl.com\*(Aq, $format ); \& \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: merge4.pl" .IX Subsection "Example: merge4.pl" Example of how to use the Excel::Writer::XLSX \fBmerge_range()\fR workbook method with complex formatting. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX merge_range() workbook \& # method with complex formatting. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqmerge4.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_row( $_, 30 ) for ( 1 .. 11 ); \& $worksheet\->set_column( \*(AqB:D\*(Aq, 20 ); \& \& \& ############################################################################### \& # \& # Example 1: Text centered vertically and horizontally \& # \& my $format1 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqvcenter\*(Aq, \& align => \*(Aqcenter\*(Aq, \& ); \& \& \& $worksheet\->merge_range( \*(AqB2:D3\*(Aq, \*(AqVertical and horizontal\*(Aq, $format1 ); \& \& \& ############################################################################### \& # \& # Example 2: Text aligned to the top and left \& # \& my $format2 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqtop\*(Aq, \& align => \*(Aqleft\*(Aq, \& ); \& \& \& $worksheet\->merge_range( \*(AqB5:D6\*(Aq, \*(AqAligned to the top and left\*(Aq, $format2 ); \& \& \& ############################################################################### \& # \& # Example 3: Text aligned to the bottom and right \& # \& my $format3 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqbottom\*(Aq, \& align => \*(Aqright\*(Aq, \& ); \& \& \& $worksheet\->merge_range( \*(AqB8:D9\*(Aq, \*(AqAligned to the bottom and right\*(Aq, $format3 ); \& \& \& ############################################################################### \& # \& # Example 4: Text justified (i.e. wrapped) in the cell \& # \& my $format4 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqtop\*(Aq, \& align => \*(Aqjustify\*(Aq, \& ); \& \& \& $worksheet\->merge_range( \*(AqB11:D12\*(Aq, \*(AqJustified: \*(Aq . \*(Aqso on and \*(Aq x 18, \& $format4 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: merge5.pl" .IX Subsection "Example: merge5.pl" Example of how to use the Excel::Writer::XLSX \fBmerge_cells()\fR workbook method with complex formatting and rotation. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX merge_cells() workbook \& # method with complex formatting and rotation. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqmerge5.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_row( $_, 36 ) for ( 3 .. 8 ); \& $worksheet\->set_column( $_, $_, 15 ) for ( 1, 3, 5 ); \& \& \& ############################################################################### \& # \& # Rotation 1, letters run from top to bottom \& # \& my $format1 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqvcentre\*(Aq, \& align => \*(Aqcentre\*(Aq, \& rotation => 270, \& ); \& \& \& $worksheet\->merge_range( \*(AqB4:B9\*(Aq, \*(AqRotation 270\*(Aq, $format1 ); \& \& \& ############################################################################### \& # \& # Rotation 2, 90° anticlockwise \& # \& my $format2 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqvcentre\*(Aq, \& align => \*(Aqcentre\*(Aq, \& rotation => 90, \& ); \& \& \& $worksheet\->merge_range( \*(AqD4:D9\*(Aq, \*(AqRotation 90°\*(Aq, $format2 ); \& \& \& ############################################################################### \& # \& # Rotation 3, 90° clockwise \& # \& my $format3 = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& valign => \*(Aqvcentre\*(Aq, \& align => \*(Aqcentre\*(Aq, \& rotation => \-90, \& ); \& \& \& $worksheet\->merge_range( \*(AqF4:F9\*(Aq, \*(AqRotation \-90°\*(Aq, $format3 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: merge6.pl" .IX Subsection "Example: merge6.pl" Example of how to use the Excel::Writer::XLSX \fBmerge_cells()\fR workbook method with Unicode strings. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX merge_cells() workbook \& # method with Unicode strings. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqmerge6.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Increase the cell size of the merged cells to highlight the formatting. \& $worksheet\->set_row( $_, 36 ) for 2 .. 9; \& $worksheet\->set_column( \*(AqB:D\*(Aq, 25 ); \& \& \& # Format for the merged cells. \& my $format = $workbook\->add_format( \& border => 6, \& bold => 1, \& color => \*(Aqred\*(Aq, \& size => 20, \& valign => \*(Aqvcentre\*(Aq, \& align => \*(Aqleft\*(Aq, \& indent => 1, \& ); \& \& \& ############################################################################### \& # \& # Write an Ascii string. \& # \& $worksheet\->merge_range( \*(AqB3:D4\*(Aq, \*(AqASCII: A simple string\*(Aq, $format ); \& \& \& ############################################################################### \& # \& # Write a UTF\-8 Unicode string. \& # \& my $smiley = chr 0x263a; \& $worksheet\->merge_range( \*(AqB6:D7\*(Aq, "UTF\-8: A Unicode smiley $smiley", $format ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: mod_perl1.pl" .IX Subsection "Example: mod_perl1.pl" Example of how to use the Excel::Writer::XLSX module to send an Excel file to a browser using mod_perl 1 and Apache .PP This module ties *XLSX directly to Apache, and with the correct content\-disposition/types it will prompt the user to save the file, or open it at this location. .PP This script is a modification of the Excel::Writer::XLSX cgi.pl example. .PP Change the name of this file to Cgi.pm. Change the package location to wherever you locate this package. In the example below it is located in the Excel::Writer::XLSX directory. .PP Your httpd.conf entry for this module, should you choose to use it as a stand alone app, should look similar to the following: .PP .Vb 5 \& \& SetHandler perl\-script \& PerlHandler Excel::Writer::XLSX::Cgi \& PerlSendHeader On \& .Ve .PP The PerlHandler name above and the package name below *have* to match. .PP .Vb 10 \& ############################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX module to send an Excel \& # file to a browser using mod_perl 1 and Apache \& # \& # This module ties *XLSX directly to Apache, and with the correct \& # content\-disposition/types it will prompt the user to save \& # the file, or open it at this location. \& # \& # This script is a modification of the Excel::Writer::XLSX cgi.pl example. \& # \& # Change the name of this file to Cgi.pm. \& # Change the package location to wherever you locate this package. \& # In the example below it is located in the Excel::Writer::XLSX directory. \& # \& # Your httpd.conf entry for this module, should you choose to use it \& # as a stand alone app, should look similar to the following: \& # \& # \& # SetHandler perl\-script \& # PerlHandler Excel::Writer::XLSX::Cgi \& # PerlSendHeader On \& # \& # \& # The PerlHandler name above and the package name below *have* to match. \& \& # Apr 2001, Thomas Sullivan, webmaster@860.org \& # Feb 2001, John McNamara, jmcnamara@cpan.org \& \& package Excel::Writer::XLSX::Cgi; \& \& ########################################## \& # Pragma Definitions \& ########################################## \& use strict; \& \& ########################################## \& # Required Modules \& ########################################## \& use Apache::Constants qw(:common); \& use Apache::Request; \& use Apache::URI; # This may not be needed \& use Excel::Writer::XLSX; \& \& ########################################## \& # Main App Body \& ########################################## \& sub handler { \& \& # New apache object \& # Should you decide to use it. \& my $r = Apache::Request\->new( shift ); \& \& # Set the filename and send the content type \& # This will appear when they save the spreadsheet \& my $filename = "cgitest.xlsx"; \& \& #################################################### \& ## Send the content type headers \& #################################################### \& print "Content\-disposition: attachment;filename=$filename\en"; \& print "Content\-type: application/vnd.openxmlformats\-officedocument.spreadsheetml.sheet\en\en"; \& \& #################################################### \& # Tie a filehandle to Apache\*(Aqs STDOUT. \& # Create a new workbook and add a worksheet. \& #################################################### \& tie *XLSX => \*(AqApache\*(Aq; \& binmode( *XLSX ); \& \& my $workbook = Excel::Writer::XLSX\->new( \e*XLSX ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Set the column width for column 1 \& $worksheet\->set_column( 0, 0, 20 ); \& \& \& # Create a format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_size( 15 ); \& $format\->set_color( \*(Aqblue\*(Aq ); \& \& \& # Write to the workbook \& $worksheet\->write( 0, 0, "Hi Excel!", $format ); \& \& # You must close the workbook for Content\-disposition \& $workbook\->close(); \& } \& \& 1; .Ve .PP Download this example: .SS "Example: mod_perl2.pl" .IX Subsection "Example: mod_perl2.pl" Example of how to use the Excel::Writer::XLSX module to send an Excel file to a browser using mod_perl 2 and Apache. .PP This module ties *XLSX directly to Apache, and with the correct content\-disposition/types it will prompt the user to save the file, or open it at this location. .PP This script is a modification of the Excel::Writer::XLSX cgi.pl example. .PP Change the name of this file to MP2Test.pm. Change the package location to wherever you locate this package. In the example below it is located in the Excel::Writer::XLSX directory. .PP Your httpd.conf entry for this module, should you choose to use it as a stand alone app, should look similar to the following: .PP .Vb 3 \& PerlModule Apache2::RequestRec \& PerlModule APR::Table \& PerlModule Apache2::RequestIO \& \& \& SetHandler perl\-script \& PerlResponseHandler Excel::Writer::XLSX::MP2Test \& .Ve .PP The PerlResponseHandler must match the package name below. .PP .Vb 10 \& ############################################################################### \& # \& # Example of how to use the Excel::Writer::XLSX module to send an Excel \& # file to a browser using mod_perl 2 and Apache. \& # \& # This module ties *XLSX directly to Apache, and with the correct \& # content\-disposition/types it will prompt the user to save \& # the file, or open it at this location. \& # \& # This script is a modification of the Excel::Writer::XLSX cgi.pl example. \& # \& # Change the name of this file to MP2Test.pm. \& # Change the package location to wherever you locate this package. \& # In the example below it is located in the Excel::Writer::XLSX directory. \& # \& # Your httpd.conf entry for this module, should you choose to use it \& # as a stand alone app, should look similar to the following: \& # \& # PerlModule Apache2::RequestRec \& # PerlModule APR::Table \& # PerlModule Apache2::RequestIO \& # \& # \& # SetHandler perl\-script \& # PerlResponseHandler Excel::Writer::XLSX::MP2Test \& # \& # \& # The PerlResponseHandler must match the package name below. \& \& # Jun 2004, Matisse Enzer, matisse@matisse.net (mod_perl 2 version) \& # Apr 2001, Thomas Sullivan, webmaster@860.org \& # Feb 2001, John McNamara, jmcnamara@cpan.org \& \& package Excel::Writer::XLSX::MP2Test; \& \& ########################################## \& # Pragma Definitions \& ########################################## \& use strict; \& \& ########################################## \& # Required Modules \& ########################################## \& use Apache2::Const \-compile => qw( :common ); \& use Excel::Writer::XLSX; \& \& ########################################## \& # Main App Body \& ########################################## \& sub handler { \& my ( $r ) = @_; # Apache request object is passed to handler in mod_perl 2 \& \& # Set the filename and send the content type \& # This will appear when they save the spreadsheet \& my $filename = "mod_perl2_test.xlsx"; \& \& #################################################### \& ## Send the content type headers the mod_perl 2 way \& #################################################### \& $r\->headers_out\->{\*(AqContent\-Disposition\*(Aq} = "attachment;filename=$filename"; \& $r\->content_type( \*(Aqapplication/vnd.openxmlformats\-officedocument.spreadsheetml.sheet\*(Aq ); \& \& #################################################### \& # Tie a filehandle to Apache\*(Aqs STDOUT. \& # Create a new workbook and add a worksheet. \& #################################################### \& tie *XLSX => $r; # The mod_perl 2 way. Tie to the Apache::RequestRec object \& binmode( *XLSX ); \& \& my $workbook = Excel::Writer::XLSX\->new( \e*XLSX ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& # Set the column width for column 1 \& $worksheet\->set_column( 0, 0, 20 ); \& \& \& # Create a format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_size( 15 ); \& $format\->set_color( \*(Aqblue\*(Aq ); \& \& \& # Write to the workbook \& $worksheet\->write( 0, 0, \*(AqHi Excel! from \*(Aq . $r\->hostname, $format ); \& \& # You must close the workbook for Content\-disposition \& $workbook\->close(); \& return Apache2::Const::OK; \& } \& \& 1; .Ve .PP Download this example: .SS "Example: outline.pl" .IX Subsection "Example: outline.pl" Example of how use Excel::Writer::XLSX to generate Excel outlines and grouping. .PP Excel allows you to group rows or columns so that they can be hidden or displayed with a single mouse click. This feature is referred to as outlines. .PP Outlines can reduce complex data down to a few salient sub-totals or summaries. .PP This feature is best viewed in Excel but the following is an \s-1ASCII\s0 representation of what a worksheet with three outlines might look like. Rows 3\-4 and rows 7\-8 are grouped at level 2. Rows 2\-9 are grouped at level 1. The lines at the left hand side are called outline level bars. .PP .Vb 10 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& _ | 1 | A | | | | ... \& | _ | 2 | B | | | | ... \& | | | 3 | (C) | | | | ... \& | | | 4 | (D) | | | | ... \& | \- | 5 | E | | | | ... \& | _ | 6 | F | | | | ... \& | | | 7 | (G) | | | | ... \& | | | 8 | (H) | | | | ... \& | \- | 9 | I | | | | ... \& \- | . | ... | ... | ... | ... | ... .Ve .PP Clicking the minus sign on each of the level 2 outlines will collapse and hide the data as shown in the next figure. The minus sign changes to a plus sign to indicate that the data in the outline is hidden. .PP .Vb 9 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& _ | 1 | A | | | | ... \& | | 2 | B | | | | ... \& | + | 5 | E | | | | ... \& | | 6 | F | | | | ... \& | + | 9 | I | | | | ... \& \- | . | ... | ... | ... | ... | ... .Ve .PP Clicking on the minus sign on the level 1 outline will collapse the remaining rows as follows: .PP .Vb 5 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 1 | A | | | | ... \& + | . | ... | ... | ... | ... | ... .Ve .PP See the main Excel::Writer::XLSX documentation for more information. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how use Excel::Writer::XLSX to generate Excel outlines and \& # grouping. \& # \& # \& # Excel allows you to group rows or columns so that they can be hidden or \& # displayed with a single mouse click. This feature is referred to as outlines. \& # \& # Outlines can reduce complex data down to a few salient sub\-totals or \& # summaries. \& # \& # This feature is best viewed in Excel but the following is an ASCII \& # representation of what a worksheet with three outlines might look like. \& # Rows 3\-4 and rows 7\-8 are grouped at level 2. Rows 2\-9 are grouped at \& # level 1. The lines at the left hand side are called outline level bars. \& # \& # \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # 1 2 3 | | A | B | C | D | ... \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # _ | 1 | A | | | | ... \& # | _ | 2 | B | | | | ... \& # | | | 3 | (C) | | | | ... \& # | | | 4 | (D) | | | | ... \& # | \- | 5 | E | | | | ... \& # | _ | 6 | F | | | | ... \& # | | | 7 | (G) | | | | ... \& # | | | 8 | (H) | | | | ... \& # | \- | 9 | I | | | | ... \& # \- | . | ... | ... | ... | ... | ... \& # \& # \& # Clicking the minus sign on each of the level 2 outlines will collapse and \& # hide the data as shown in the next figure. The minus sign changes to a plus \& # sign to indicate that the data in the outline is hidden. \& # \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # 1 2 3 | | A | B | C | D | ... \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # _ | 1 | A | | | | ... \& # | | 2 | B | | | | ... \& # | + | 5 | E | | | | ... \& # | | 6 | F | | | | ... \& # | + | 9 | I | | | | ... \& # \- | . | ... | ... | ... | ... | ... \& # \& # \& # Clicking on the minus sign on the level 1 outline will collapse the remaining \& # rows as follows: \& # \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # 1 2 3 | | A | B | C | D | ... \& # \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& # | 1 | A | | | | ... \& # + | . | ... | ... | ... | ... | ... \& # \& # See the main Excel::Writer::XLSX documentation for more information. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add some worksheets \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqoutline.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet( \*(AqOutlined Rows\*(Aq ); \& my $worksheet2 = $workbook\->add_worksheet( \*(AqCollapsed Rows\*(Aq ); \& my $worksheet3 = $workbook\->add_worksheet( \*(AqOutline Columns\*(Aq ); \& my $worksheet4 = $workbook\->add_worksheet( \*(AqOutline levels\*(Aq ); \& \& # Add a general format \& my $bold = $workbook\->add_format( bold => 1 ); \& \& \& ############################################################################### \& # \& # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL() \& # functions so that it looks like the type of automatic outlines that are \& # generated when you use the Excel Data\->SubTotals menu item. \& # \& \& \& # For outlines the important parameters are $hidden and $level. Rows with the \& # same $level are grouped together. The group will be collapsed if $hidden is \& # non\-zero. $height and $XF are assigned default values if they are undef. \& # \& # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) \& # \& $worksheet1\->set_row( 1, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 2, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 3, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 4, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 5, undef, undef, 0, 1 ); \& \& $worksheet1\->set_row( 6, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 7, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 8, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 9, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 10, undef, undef, 0, 1 ); \& \& \& # Add a column format for clarity \& $worksheet1\->set_column( \*(AqA:A\*(Aq, 20 ); \& \& # Add the data, labels and formulas \& $worksheet1\->write( \*(AqA1\*(Aq, \*(AqRegion\*(Aq, $bold ); \& $worksheet1\->write( \*(AqA2\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet1\->write( \*(AqA3\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet1\->write( \*(AqA4\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet1\->write( \*(AqA5\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet1\->write( \*(AqA6\*(Aq, \*(AqNorth Total\*(Aq, $bold ); \& \& $worksheet1\->write( \*(AqB1\*(Aq, \*(AqSales\*(Aq, $bold ); \& $worksheet1\->write( \*(AqB2\*(Aq, 1000 ); \& $worksheet1\->write( \*(AqB3\*(Aq, 1200 ); \& $worksheet1\->write( \*(AqB4\*(Aq, 900 ); \& $worksheet1\->write( \*(AqB5\*(Aq, 1200 ); \& $worksheet1\->write( \*(AqB6\*(Aq, \*(Aq=SUBTOTAL(9,B2:B5)\*(Aq, $bold ); \& \& $worksheet1\->write( \*(AqA7\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet1\->write( \*(AqA8\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet1\->write( \*(AqA9\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet1\->write( \*(AqA10\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet1\->write( \*(AqA11\*(Aq, \*(AqSouth Total\*(Aq, $bold ); \& \& $worksheet1\->write( \*(AqB7\*(Aq, 400 ); \& $worksheet1\->write( \*(AqB8\*(Aq, 600 ); \& $worksheet1\->write( \*(AqB9\*(Aq, 500 ); \& $worksheet1\->write( \*(AqB10\*(Aq, 600 ); \& $worksheet1\->write( \*(AqB11\*(Aq, \*(Aq=SUBTOTAL(9,B7:B10)\*(Aq, $bold ); \& \& $worksheet1\->write( \*(AqA12\*(Aq, \*(AqGrand Total\*(Aq, $bold ); \& $worksheet1\->write( \*(AqB12\*(Aq, \*(Aq=SUBTOTAL(9,B2:B10)\*(Aq, $bold ); \& \& \& ############################################################################### \& # \& # Example 2: Create a worksheet with outlined rows. This is the same as the \& # previous example except that the rows are collapsed. \& # Note: We need to indicate the row that contains the collapsed symbol \*(Aq+\*(Aq \& # with the optional parameter, $collapsed. \& \& # The group will be collapsed if $hidden is non\-zero. \& # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) \& # \& $worksheet2\->set_row( 1, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 2, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 3, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 4, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 5, undef, undef, 1, 1 ); \& \& $worksheet2\->set_row( 6, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 7, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 8, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 9, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 10, undef, undef, 1, 1 ); \& $worksheet2\->set_row( 11, undef, undef, 0, 0, 1 ); \& \& \& # Add a column format for clarity \& $worksheet2\->set_column( \*(AqA:A\*(Aq, 20 ); \& \& # Add the data, labels and formulas \& $worksheet2\->write( \*(AqA1\*(Aq, \*(AqRegion\*(Aq, $bold ); \& $worksheet2\->write( \*(AqA2\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet2\->write( \*(AqA3\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet2\->write( \*(AqA4\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet2\->write( \*(AqA5\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet2\->write( \*(AqA6\*(Aq, \*(AqNorth Total\*(Aq, $bold ); \& \& $worksheet2\->write( \*(AqB1\*(Aq, \*(AqSales\*(Aq, $bold ); \& $worksheet2\->write( \*(AqB2\*(Aq, 1000 ); \& $worksheet2\->write( \*(AqB3\*(Aq, 1200 ); \& $worksheet2\->write( \*(AqB4\*(Aq, 900 ); \& $worksheet2\->write( \*(AqB5\*(Aq, 1200 ); \& $worksheet2\->write( \*(AqB6\*(Aq, \*(Aq=SUBTOTAL(9,B2:B5)\*(Aq, $bold ); \& \& $worksheet2\->write( \*(AqA7\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet2\->write( \*(AqA8\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet2\->write( \*(AqA9\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet2\->write( \*(AqA10\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet2\->write( \*(AqA11\*(Aq, \*(AqSouth Total\*(Aq, $bold ); \& \& $worksheet2\->write( \*(AqB7\*(Aq, 400 ); \& $worksheet2\->write( \*(AqB8\*(Aq, 600 ); \& $worksheet2\->write( \*(AqB9\*(Aq, 500 ); \& $worksheet2\->write( \*(AqB10\*(Aq, 600 ); \& $worksheet2\->write( \*(AqB11\*(Aq, \*(Aq=SUBTOTAL(9,B7:B10)\*(Aq, $bold ); \& \& $worksheet2\->write( \*(AqA12\*(Aq, \*(AqGrand Total\*(Aq, $bold ); \& $worksheet2\->write( \*(AqB12\*(Aq, \*(Aq=SUBTOTAL(9,B2:B10)\*(Aq, $bold ); \& \& \& ############################################################################### \& # \& # Example 3: Create a worksheet with outlined columns. \& # \& my $data = [ \& [ \*(AqMonth\*(Aq, \*(AqJan\*(Aq, \*(AqFeb\*(Aq, \*(AqMar\*(Aq, \*(AqApr\*(Aq, \*(AqMay\*(Aq, \*(AqJun\*(Aq, \*(Aq Total\*(Aq ], \& [ \*(AqNorth\*(Aq, 50, 20, 15, 25, 65, 80, \*(Aq=SUM(B2:G2)\*(Aq ], \& [ \*(AqSouth\*(Aq, 10, 20, 30, 50, 50, 50, \*(Aq=SUM(B3:G3)\*(Aq ], \& [ \*(AqEast\*(Aq, 45, 75, 50, 15, 75, 100, \*(Aq=SUM(B4:G4)\*(Aq ], \& [ \*(AqWest\*(Aq, 15, 15, 55, 35, 20, 50, \*(Aq=SUM(B5:G5)\*(Aq ], \& ]; \& \& # Add bold format to the first row \& $worksheet3\->set_row( 0, undef, $bold ); \& \& # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) \& $worksheet3\->set_column( \*(AqA:A\*(Aq, 10, $bold ); \& $worksheet3\->set_column( \*(AqB:G\*(Aq, 5, undef, 0, 1 ); \& $worksheet3\->set_column( \*(AqH:H\*(Aq, 10 ); \& \& # Write the data and a formula \& $worksheet3\->write_col( \*(AqA1\*(Aq, $data ); \& $worksheet3\->write( \*(AqH6\*(Aq, \*(Aq=SUM(H2:H5)\*(Aq, $bold ); \& \& \& ############################################################################### \& # \& # Example 4: Show all possible outline levels. \& # \& my $levels = [ \& "Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", \& "Level 7", "Level 6", "Level 5", "Level 4", "Level 3", "Level 2", \& "Level 1" \& ]; \& \& \& $worksheet4\->write_col( \*(AqA1\*(Aq, $levels ); \& \& $worksheet4\->set_row( 0, undef, undef, undef, 1 ); \& $worksheet4\->set_row( 1, undef, undef, undef, 2 ); \& $worksheet4\->set_row( 2, undef, undef, undef, 3 ); \& $worksheet4\->set_row( 3, undef, undef, undef, 4 ); \& $worksheet4\->set_row( 4, undef, undef, undef, 5 ); \& $worksheet4\->set_row( 5, undef, undef, undef, 6 ); \& $worksheet4\->set_row( 6, undef, undef, undef, 7 ); \& $worksheet4\->set_row( 7, undef, undef, undef, 6 ); \& $worksheet4\->set_row( 8, undef, undef, undef, 5 ); \& $worksheet4\->set_row( 9, undef, undef, undef, 4 ); \& $worksheet4\->set_row( 10, undef, undef, undef, 3 ); \& $worksheet4\->set_row( 11, undef, undef, undef, 2 ); \& $worksheet4\->set_row( 12, undef, undef, undef, 1 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: outline_collapsed.pl" .IX Subsection "Example: outline_collapsed.pl" Example of how to use Excel::Writer::XLSX to generate Excel outlines and grouping. .PP These examples focus mainly on collapsed outlines. See also the outlines.pl example program for more general examples. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to use Excel::Writer::XLSX to generate Excel outlines and \& # grouping. \& # \& # These examples focus mainly on collapsed outlines. See also the \& # outlines.pl example program for more general examples. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add some worksheets \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqoutline_collapsed.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet( \*(AqOutlined Rows\*(Aq ); \& my $worksheet2 = $workbook\->add_worksheet( \*(AqCollapsed Rows 1\*(Aq ); \& my $worksheet3 = $workbook\->add_worksheet( \*(AqCollapsed Rows 2\*(Aq ); \& my $worksheet4 = $workbook\->add_worksheet( \*(AqCollapsed Rows 3\*(Aq ); \& my $worksheet5 = $workbook\->add_worksheet( \*(AqOutline Columns\*(Aq ); \& my $worksheet6 = $workbook\->add_worksheet( \*(AqCollapsed Columns\*(Aq ); \& \& \& # Add a general format \& my $bold = $workbook\->add_format( bold => 1 ); \& \& \& # \& # This function will generate the same data and sub\-totals on each worksheet. \& # \& sub create_sub_totals { \& \& my $worksheet = $_[0]; \& \& # Add a column format for clarity \& $worksheet\->set_column( \*(AqA:A\*(Aq, 20 ); \& \& # Add the data, labels and formulas \& $worksheet\->write( \*(AqA1\*(Aq, \*(AqRegion\*(Aq, $bold ); \& $worksheet\->write( \*(AqA2\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet\->write( \*(AqA3\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet\->write( \*(AqA4\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet\->write( \*(AqA5\*(Aq, \*(AqNorth\*(Aq ); \& $worksheet\->write( \*(AqA6\*(Aq, \*(AqNorth Total\*(Aq, $bold ); \& \& $worksheet\->write( \*(AqB1\*(Aq, \*(AqSales\*(Aq, $bold ); \& $worksheet\->write( \*(AqB2\*(Aq, 1000 ); \& $worksheet\->write( \*(AqB3\*(Aq, 1200 ); \& $worksheet\->write( \*(AqB4\*(Aq, 900 ); \& $worksheet\->write( \*(AqB5\*(Aq, 1200 ); \& $worksheet\->write( \*(AqB6\*(Aq, \*(Aq=SUBTOTAL(9,B2:B5)\*(Aq, $bold ); \& \& $worksheet\->write( \*(AqA7\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet\->write( \*(AqA8\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet\->write( \*(AqA9\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet\->write( \*(AqA10\*(Aq, \*(AqSouth\*(Aq ); \& $worksheet\->write( \*(AqA11\*(Aq, \*(AqSouth Total\*(Aq, $bold ); \& \& $worksheet\->write( \*(AqB7\*(Aq, 400 ); \& $worksheet\->write( \*(AqB8\*(Aq, 600 ); \& $worksheet\->write( \*(AqB9\*(Aq, 500 ); \& $worksheet\->write( \*(AqB10\*(Aq, 600 ); \& $worksheet\->write( \*(AqB11\*(Aq, \*(Aq=SUBTOTAL(9,B7:B10)\*(Aq, $bold ); \& \& $worksheet\->write( \*(AqA12\*(Aq, \*(AqGrand Total\*(Aq, $bold ); \& $worksheet\->write( \*(AqB12\*(Aq, \*(Aq=SUBTOTAL(9,B2:B10)\*(Aq, $bold ); \& \& } \& \& \& ############################################################################### \& # \& # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL() \& # functions so that it looks like the type of automatic outlines that are \& # generated when you use the Excel Data\->SubTotals menu item. \& # \& \& # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed) \& $worksheet1\->set_row( 1, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 2, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 3, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 4, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 5, undef, undef, 0, 1 ); \& \& $worksheet1\->set_row( 6, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 7, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 8, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 9, undef, undef, 0, 2 ); \& $worksheet1\->set_row( 10, undef, undef, 0, 1 ); \& \& # Write the sub\-total data that is common to the row examples. \& create_sub_totals( $worksheet1 ); \& \& \& ############################################################################### \& # \& # Example 2: Create a worksheet with collapsed outlined rows. \& # This is the same as the example 1 except that the all rows are collapsed. \& # Note: We need to indicate the row that contains the collapsed symbol \*(Aq+\*(Aq with \& # the optional parameter, $collapsed. \& \& $worksheet2\->set_row( 1, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 2, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 3, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 4, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 5, undef, undef, 1, 1 ); \& \& $worksheet2\->set_row( 6, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 7, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 8, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 9, undef, undef, 1, 2 ); \& $worksheet2\->set_row( 10, undef, undef, 1, 1 ); \& \& $worksheet2\->set_row( 11, undef, undef, 0, 0, 1 ); \& \& # Write the sub\-total data that is common to the row examples. \& create_sub_totals( $worksheet2 ); \& \& \& ############################################################################### \& # \& # Example 3: Create a worksheet with collapsed outlined rows. \& # Same as the example 1 except that the two sub\-totals are collapsed. \& \& $worksheet3\->set_row( 1, undef, undef, 1, 2 ); \& $worksheet3\->set_row( 2, undef, undef, 1, 2 ); \& $worksheet3\->set_row( 3, undef, undef, 1, 2 ); \& $worksheet3\->set_row( 4, undef, undef, 1, 2 ); \& $worksheet3\->set_row( 5, undef, undef, 0, 1, 1 ); \& \& $worksheet3\->set_row( 6, undef, undef, 1, 2 ); \& $worksheet3\->set_row( 7, undef, undef, 1, 2 ); \& $worksheet3\->set_row( 8, undef, undef, 1, 2 ); \& $worksheet3\->set_row( 9, undef, undef, 1, 2 ); \& $worksheet3\->set_row( 10, undef, undef, 0, 1, 1 ); \& \& \& # Write the sub\-total data that is common to the row examples. \& create_sub_totals( $worksheet3 ); \& \& \& ############################################################################### \& # \& # Example 4: Create a worksheet with outlined rows. \& # Same as the example 1 except that the two sub\-totals are collapsed. \& \& $worksheet4\->set_row( 1, undef, undef, 1, 2 ); \& $worksheet4\->set_row( 2, undef, undef, 1, 2 ); \& $worksheet4\->set_row( 3, undef, undef, 1, 2 ); \& $worksheet4\->set_row( 4, undef, undef, 1, 2 ); \& $worksheet4\->set_row( 5, undef, undef, 1, 1, 1 ); \& \& $worksheet4\->set_row( 6, undef, undef, 1, 2 ); \& $worksheet4\->set_row( 7, undef, undef, 1, 2 ); \& $worksheet4\->set_row( 8, undef, undef, 1, 2 ); \& $worksheet4\->set_row( 9, undef, undef, 1, 2 ); \& $worksheet4\->set_row( 10, undef, undef, 1, 1, 1 ); \& \& $worksheet4\->set_row( 11, undef, undef, 0, 0, 1 ); \& \& # Write the sub\-total data that is common to the row examples. \& create_sub_totals( $worksheet4 ); \& \& \& ############################################################################### \& # \& # Example 5: Create a worksheet with outlined columns. \& # \& my $data = [ \& [ \*(AqMonth\*(Aq, \*(AqJan\*(Aq, \*(AqFeb\*(Aq, \*(AqMar\*(Aq, \*(AqApr\*(Aq, \*(AqMay\*(Aq, \*(AqJun\*(Aq, \*(AqTotal\*(Aq ], \& [ \*(AqNorth\*(Aq, 50, 20, 15, 25, 65, 80,, \*(Aq=SUM(B2:G2)\*(Aq ], \& [ \*(AqSouth\*(Aq, 10, 20, 30, 50, 50, 50,, \*(Aq=SUM(B3:G3)\*(Aq ], \& [ \*(AqEast\*(Aq, 45, 75, 50, 15, 75, 100,, \*(Aq=SUM(B4:G4)\*(Aq ], \& [ \*(AqWest\*(Aq, 15, 15, 55, 35, 20, 50,, \*(Aq=SUM(B5:G6)\*(Aq ], \& ]; \& \& # Add bold format to the first row \& $worksheet5\->set_row( 0, undef, $bold ); \& \& # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) \& $worksheet5\->set_column( \*(AqA:A\*(Aq, 10, $bold ); \& $worksheet5\->set_column( \*(AqB:G\*(Aq, 5, undef, 0, 1 ); \& $worksheet5\->set_column( \*(AqH:H\*(Aq, 10 ); \& \& # Write the data and a formula \& $worksheet5\->write_col( \*(AqA1\*(Aq, $data ); \& $worksheet5\->write( \*(AqH6\*(Aq, \*(Aq=SUM(H2:H5)\*(Aq, $bold ); \& \& \& ############################################################################### \& # \& # Example 6: Create a worksheet with collapsed outlined columns. \& # This is the same as the previous example except collapsed columns. \& \& # Add bold format to the first row \& $worksheet6\->set_row( 0, undef, $bold ); \& \& # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed) \& $worksheet6\->set_column( \*(AqA:A\*(Aq, 10, $bold ); \& $worksheet6\->set_column( \*(AqB:G\*(Aq, 5, undef, 1, 1 ); \& $worksheet6\->set_column( \*(AqH:H\*(Aq, 10, undef, 0, 0, 1 ); \& \& # Write the data and a formula \& $worksheet6\->write_col( \*(AqA1\*(Aq, $data ); \& $worksheet6\->write( \*(AqH6\*(Aq, \*(Aq=SUM(H2:H5)\*(Aq, $bold ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: panes.pl" .IX Subsection "Example: panes.pl" Example of using the Excel::Writer::XLSX module to create worksheet panes. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # Example of using the Excel::Writer::XLSX module to create worksheet panes. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqpanes.xlsx\*(Aq ); \& \& my $worksheet1 = $workbook\->add_worksheet( \*(AqPanes 1\*(Aq ); \& my $worksheet2 = $workbook\->add_worksheet( \*(AqPanes 2\*(Aq ); \& my $worksheet3 = $workbook\->add_worksheet( \*(AqPanes 3\*(Aq ); \& my $worksheet4 = $workbook\->add_worksheet( \*(AqPanes 4\*(Aq ); \& \& # Freeze panes \& $worksheet1\->freeze_panes( 1, 0 ); # 1 row \& \& $worksheet2\->freeze_panes( 0, 1 ); # 1 column \& $worksheet3\->freeze_panes( 1, 1 ); # 1 row and column \& \& # Split panes. \& # The divisions must be specified in terms of row and column dimensions. \& # The default row height is 15 and the default column width is 8.43 \& # \& $worksheet4\->split_panes( 15, 8.43 ); # 1 row and column \& \& \& ####################################################################### \& # \& # Set up some formatting and text to highlight the panes \& # \& \& my $header = $workbook\->add_format( \& align => \*(Aqcenter\*(Aq, \& valign => \*(Aqvcenter\*(Aq, \& fg_color => \*(Aq#C3FFC0\*(Aq, \& ); \& \& my $center = $workbook\->add_format( align => \*(Aqcenter\*(Aq ); \& \& \& ####################################################################### \& # \& # Sheet 1 \& # \& \& $worksheet1\->set_column( \*(AqA:I\*(Aq, 16 ); \& $worksheet1\->set_row( 0, 20 ); \& $worksheet1\->set_selection( \*(AqC3\*(Aq ); \& \& for my $i ( 0 .. 8 ) { \& $worksheet1\->write( 0, $i, \*(AqScroll down\*(Aq, $header ); \& } \& \& for my $i ( 1 .. 100 ) { \& for my $j ( 0 .. 8 ) { \& $worksheet1\->write( $i, $j, $i + 1, $center ); \& } \& } \& \& \& ####################################################################### \& # \& # Sheet 2 \& # \& \& $worksheet2\->set_column( \*(AqA:A\*(Aq, 16 ); \& $worksheet2\->set_selection( \*(AqC3\*(Aq ); \& \& for my $i ( 0 .. 49 ) { \& $worksheet2\->set_row( $i, 15 ); \& $worksheet2\->write( $i, 0, \*(AqScroll right\*(Aq, $header ); \& } \& \& for my $i ( 0 .. 49 ) { \& for my $j ( 1 .. 25 ) { \& $worksheet2\->write( $i, $j, $j, $center ); \& } \& } \& \& \& ####################################################################### \& # \& # Sheet 3 \& # \& \& $worksheet3\->set_column( \*(AqA:Z\*(Aq, 16 ); \& $worksheet3\->set_selection( \*(AqC3\*(Aq ); \& \& $worksheet3\->write( 0, 0, \*(Aq\*(Aq, $header ); \& \& for my $i ( 1 .. 25 ) { \& $worksheet3\->write( 0, $i, \*(AqScroll down\*(Aq, $header ); \& } \& \& for my $i ( 1 .. 49 ) { \& $worksheet3\->write( $i, 0, \*(AqScroll right\*(Aq, $header ); \& } \& \& for my $i ( 1 .. 49 ) { \& for my $j ( 1 .. 25 ) { \& $worksheet3\->write( $i, $j, $j, $center ); \& } \& } \& \& \& ####################################################################### \& # \& # Sheet 4 \& # \& \& $worksheet4\->set_selection( \*(AqC3\*(Aq ); \& \& for my $i ( 1 .. 25 ) { \& $worksheet4\->write( 0, $i, \*(AqScroll\*(Aq, $center ); \& } \& \& for my $i ( 1 .. 49 ) { \& $worksheet4\->write( $i, 0, \*(AqScroll\*(Aq, $center ); \& } \& \& for my $i ( 1 .. 49 ) { \& for my $j ( 1 .. 25 ) { \& $worksheet4\->write( $i, $j, $j, $center ); \& } \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: properties.pl" .IX Subsection "Example: properties.pl" An example of adding document properties to a Excel::Writer::XLSX file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # An example of adding document properties to a Excel::Writer::XLSX file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqproperties.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& $workbook\->set_properties( \& title => \*(AqThis is an example spreadsheet\*(Aq, \& subject => \*(AqWith document properties\*(Aq, \& author => \*(AqJohn McNamara\*(Aq, \& manager => \*(AqDr. Heinz Doofenshmirtz\*(Aq, \& company => \*(Aqof Wolves\*(Aq, \& category => \*(AqExample spreadsheets\*(Aq, \& keywords => \*(AqSample, Example, Properties\*(Aq, \& comments => \*(AqCreated with Perl and Excel::Writer::XLSX\*(Aq, \& status => \*(AqQuo\*(Aq, \& ); \& \& \& $worksheet\->set_column( \*(AqA:A\*(Aq, 70 ); \& $worksheet\->write( \*(AqA1\*(Aq, qq{Select \*(AqOffice Button \-> Prepare \-> Properties\*(Aq to see the file properties.} ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: protection.pl" .IX Subsection "Example: protection.pl" Example of cell locking and formula hiding in an Excel worksheet via the Excel::Writer::XLSX module. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ######################################################################## \& # \& # Example of cell locking and formula hiding in an Excel worksheet via \& # the Excel::Writer::XLSX module. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqprotection.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Create some format objects \& my $unlocked = $workbook\->add_format( locked => 0 ); \& my $hidden = $workbook\->add_format( hidden => 1 ); \& \& # Format the columns \& $worksheet\->set_column( \*(AqA:A\*(Aq, 45 ); \& $worksheet\->set_selection( \*(AqB3\*(Aq ); \& \& # Protect the worksheet \& $worksheet\->protect(); \& \& # Examples of cell locking and hiding. \& $worksheet\->write( \*(AqA1\*(Aq, \*(AqCell B1 is locked. It cannot be edited.\*(Aq ); \& $worksheet\->write_formula( \*(AqB1\*(Aq, \*(Aq=1+2\*(Aq, undef, 3 ); # Locked by default. \& \& $worksheet\->write( \*(AqA2\*(Aq, \*(AqCell B2 is unlocked. It can be edited.\*(Aq ); \& $worksheet\->write_formula( \*(AqB2\*(Aq, \*(Aq=1+2\*(Aq, $unlocked, 3 ); \& \& $worksheet\->write( \*(AqA3\*(Aq, "Cell B3 is hidden. The formula isn\*(Aqt visible." ); \& $worksheet\->write_formula( \*(AqB3\*(Aq, \*(Aq=1+2\*(Aq, $hidden, 3 ); \& \& $worksheet\->write( \*(AqA5\*(Aq, \*(AqUse Menu\->Tools\->Protection\->Unprotect Sheet\*(Aq ); \& $worksheet\->write( \*(AqA6\*(Aq, \*(Aqto remove the worksheet protection.\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: rich_strings.pl" .IX Subsection "Example: rich_strings.pl" An Excel::Writer::XLSX example showing how to use \*(L"rich strings\*(R", i.e., strings with multiple formatting. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # An Excel::Writer::XLSX example showing how to use "rich strings", i.e., \& # strings with multiple formatting. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqrich_strings.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->set_column( \*(AqA:A\*(Aq, 30 ); \& \& # Set some formats to use. \& my $bold = $workbook\->add_format( bold => 1 ); \& my $italic = $workbook\->add_format( italic => 1 ); \& my $red = $workbook\->add_format( color => \*(Aqred\*(Aq ); \& my $blue = $workbook\->add_format( color => \*(Aqblue\*(Aq ); \& my $center = $workbook\->add_format( align => \*(Aqcenter\*(Aq ); \& my $super = $workbook\->add_format( font_script => 1 ); \& \& \& # Write some strings with multiple formats. \& $worksheet\->write_rich_string( \*(AqA1\*(Aq, \& \*(AqThis is \*(Aq, $bold, \*(Aqbold\*(Aq, \*(Aq and this is \*(Aq, $italic, \*(Aqitalic\*(Aq ); \& \& $worksheet\->write_rich_string( \*(AqA3\*(Aq, \& \*(AqThis is \*(Aq, $red, \*(Aqred\*(Aq, \*(Aq and this is \*(Aq, $blue, \*(Aqblue\*(Aq ); \& \& $worksheet\->write_rich_string( \*(AqA5\*(Aq, \& \*(AqSome \*(Aq, $bold, \*(Aqbold text\*(Aq, \*(Aq centered\*(Aq, $center ); \& \& $worksheet\->write_rich_string( \*(AqA7\*(Aq, \& $italic, \*(Aqj = k\*(Aq, $super, \*(Aq(n\-1)\*(Aq, $center ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: right_to_left.pl" .IX Subsection "Example: right_to_left.pl" Example of how to change the default worksheet direction from left-to-right to right-to-left as required by some eastern verions of Excel. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # Example of how to change the default worksheet direction from \& # left\-to\-right to right\-to\-left as required by some eastern verions \& # of Excel. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqright_to_left.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& \& $worksheet2\->right_to_left(); \& \& $worksheet1\->write( 0, 0, \*(AqHello\*(Aq ); # A1, B1, C1, ... \& $worksheet2\->write( 0, 0, \*(AqHello\*(Aq ); # ..., C1, B1, A1 \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: sales.pl" .IX Subsection "Example: sales.pl" Example of a sales worksheet to demonstrate several different features. Also uses functions from the Excel::Writer::XLSX::Utility module. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of a sales worksheet to demonstrate several different features. \& # Also uses functions from the L module. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& use Excel::Writer::XLSX::Utility; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqsales.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet( \*(AqMay Sales\*(Aq ); \& \& \& # Set up some formats \& my %heading = ( \& bold => 1, \& pattern => 1, \& fg_color => \*(Aq#C3FFC0\*(Aq, \& border => 1, \& align => \*(Aqcenter\*(Aq, \& ); \& \& my %total = ( \& bold => 1, \& top => 1, \& num_format => \*(Aq$#,##0.00\*(Aq \& ); \& \& my $heading = $workbook\->add_format( %heading ); \& my $total_format = $workbook\->add_format( %total ); \& my $price_format = $workbook\->add_format( num_format => \*(Aq$#,##0.00\*(Aq ); \& my $date_format = $workbook\->add_format( num_format => \*(Aqmmm d yyy\*(Aq ); \& \& \& # Write the main headings \& $worksheet\->freeze_panes( 1 ); # Freeze the first row \& $worksheet\->write( \*(AqA1\*(Aq, \*(AqItem\*(Aq, $heading ); \& $worksheet\->write( \*(AqB1\*(Aq, \*(AqQuantity\*(Aq, $heading ); \& $worksheet\->write( \*(AqC1\*(Aq, \*(AqPrice\*(Aq, $heading ); \& $worksheet\->write( \*(AqD1\*(Aq, \*(AqTotal\*(Aq, $heading ); \& $worksheet\->write( \*(AqE1\*(Aq, \*(AqDate\*(Aq, $heading ); \& \& # Set the column widths \& $worksheet\->set_column( \*(AqA:A\*(Aq, 25 ); \& $worksheet\->set_column( \*(AqB:B\*(Aq, 10 ); \& $worksheet\->set_column( \*(AqC:E\*(Aq, 16 ); \& \& \& # Extract the sales data from the _\|_DATA_\|_ section at the end of the file. \& # In reality this information would probably come from a database \& my @sales; \& \& foreach my $line ( ) { \& chomp $line; \& next if $line eq \*(Aq\*(Aq; \& \& # Simple\-minded processing of CSV data. Refer to the Text::CSV_XS \& # and Text::xSV modules for a more complete CSV handling. \& my @items = split /,/, $line; \& push @sales, \e@items; \& } \& \& \& # Write out the items from each row \& my $row = 1; \& foreach my $sale ( @sales ) { \& \& $worksheet\->write( $row, 0, @$sale[0] ); \& $worksheet\->write( $row, 1, @$sale[1] ); \& $worksheet\->write( $row, 2, @$sale[2], $price_format ); \& \& # Create a formula like \*(Aq=B2*C2\*(Aq \& my $formula = \& \*(Aq=\*(Aq . xl_rowcol_to_cell( $row, 1 ) . "*" . xl_rowcol_to_cell( $row, 2 ); \& \& $worksheet\->write( $row, 3, $formula, $price_format ); \& \& # Parse the date \& my $date = xl_decode_date_US( @$sale[3] ); \& $worksheet\->write( $row, 4, $date, $date_format ); \& $row++; \& } \& \& # Create a formula to sum the totals, like \*(Aq=SUM(D2:D6)\*(Aq \& my $total = \*(Aq=SUM(D2:\*(Aq . xl_rowcol_to_cell( $row \- 1, 3 ) . ")"; \& \& $worksheet\->write( $row, 3, $total, $total_format ); \& \& $workbook\->close(); \& \& _\|_DATA_\|_ \& 586 card,20,125.50,5/12/01 \& Flat Screen Monitor,1,1300.00,5/12/01 \& 64 MB dimms,45,49.99,5/13/01 \& 15 GB HD,12,300.00,5/13/01 \& Speakers (pair),5,15.50,5/14/01 .Ve .PP Download this example: .SS "Example: shape1.pl" .IX Subsection "Example: shape1.pl" A simple example of how to use the Excel::Writer::XLSX module to add shapes to an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # add shapes to an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape1.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Add a circle, with centered text. \& my $ellipse = $workbook\->add_shape( \& type => \*(Aqellipse\*(Aq, \& text => "Hello\enWorld", \& width => 60, \& height => 60 \& ); \& \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $ellipse, 50, 50 ); \& \& # Add a plus sign. \& my $plus = $workbook\->add_shape( type => \*(Aqplus\*(Aq, width => 20, height => 20 ); \& $worksheet\->insert_shape( \*(AqD8\*(Aq, $plus ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: shape2.pl" .IX Subsection "Example: shape2.pl" A simple example of how to use the Excel::Writer::XLSX module to modify shape properties in an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # modify shape properties in an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape2.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->hide_gridlines( 2 ); \& \& my $plain = $workbook\->add_shape( \& type => \*(AqsmileyFace\*(Aq, \& text => "Plain", \& width => 100, \& height => 100, \& ); \& \& my $bbformat = $workbook\->add_format( \& color => \*(Aqred\*(Aq, \& font => \*(AqLucida Calligraphy\*(Aq, \& ); \& \& $bbformat\->set_bold(); \& $bbformat\->set_underline(); \& $bbformat\->set_italic(); \& \& my $decor = $workbook\->add_shape( \& type => \*(AqsmileyFace\*(Aq, \& text => "Decorated", \& rotation => 45, \& width => 200, \& height => 100, \& format => $bbformat, \& line_type => \*(AqsysDot\*(Aq, \& line_weight => 3, \& fill => \*(AqFFFF00\*(Aq, \& line => \*(Aq3366FF\*(Aq, \& ); \& \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $plain, 50, 50 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $decor, 250, 50 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: shape3.pl" .IX Subsection "Example: shape3.pl" A simple example of how to use the Excel::Writer::XLSX module to scale shapes in an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # scale shapes in an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape3.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& my $normal = $workbook\->add_shape( \& name => \*(Aqchip\*(Aq, \& type => \*(Aqdiamond\*(Aq, \& text => "Normal", \& width => 100, \& height => 100, \& ); \& \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $normal, 50, 50 ); \& $normal\->set_text( \*(AqScaled 3w x 2h\*(Aq ); \& $normal\->set_name( \*(AqHope\*(Aq ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $normal, 250, 50, 3, 2 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: shape4.pl" .IX Subsection "Example: shape4.pl" A simple example of how to use the Excel::Writer::XLSX module to demonstrate stenciling in an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # demonstrate stenciling in an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape4.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->hide_gridlines( 2 ); \& \& my $type = \*(Aqrect\*(Aq; \& my $shape = $workbook\->add_shape( \& type => $type, \& width => 90, \& height => 90, \& ); \& \& for my $n ( 1 .. 10 ) { \& \& # Change the last 5 rectangles to stars. Previously inserted shapes stay \& # as rectangles. \& $type = \*(Aqstar5\*(Aq if $n == 6; \& $shape\->set_type( $type ); \& $shape\->set_text( "$type $n" ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $shape, $n * 100, 50 ); \& } \& \& \& my $stencil = $workbook\->add_shape( \& stencil => 1, # The default. \& width => 90, \& height => 90, \& text => \*(Aqstarted as a box\*(Aq, \& ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $stencil, 100, 150 ); \& \& $stencil\->set_stencil( 0 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $stencil, 200, 150 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $stencil, 300, 150 ); \& \& # Ooops! Changed my mind. Change the rectangle to an ellipse (circle), \& # for the last two shapes. \& $stencil\->set_type( \*(Aqellipse\*(Aq ); \& $stencil\->set_text( \*(AqNow its a circle\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: shape5.pl" .IX Subsection "Example: shape5.pl" A simple example of how to use the Excel::Writer::XLSX module to add shapes (objects and top/bottom connectors) to an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # add shapes (objects and top/bottom connectors) to an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape5.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& my $s1 = $workbook\->add_shape( type => \*(Aqellipse\*(Aq, width => 60, height => 60 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $s1, 50, 50 ); \& \& my $s2 = $workbook\->add_shape( type => \*(Aqplus\*(Aq, width => 20, height => 20 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $s2, 250, 200 ); \& \& # Create a connector to link the two shapes. \& my $cxn_shape = $workbook\->add_shape( type => \*(AqbentConnector3\*(Aq ); \& \& # Link the start of the connector to the right side. \& $cxn_shape\->set_start( $s1\->get_id() ); \& $cxn_shape\->set_start_index( 4 ); # 4th connection pt, clockwise from top(0). \& $cxn_shape\->set_start_side( \*(Aqb\*(Aq ); # r)ight or b)ottom. \& \& # Link the end of the connector to the left side. \& $cxn_shape\->set_end( $s2\->get_id() ); \& $cxn_shape\->set_end_index( 0 ); # clockwise from top(0). \& $cxn_shape\->set_end_side( \*(Aqt\*(Aq ); # t)top. \& \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: shape6.pl" .IX Subsection "Example: shape6.pl" A simple example of how to use the Excel::Writer::XLSX module to add shapes (objects and right/left connectors) to an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # add shapes (objects and right/left connectors) to an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape6.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& my $s1 = $workbook\->add_shape( type => \*(Aqchevron\*(Aq, width => 60, height => 60 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $s1, 50, 50 ); \& \& my $s2 = $workbook\->add_shape( type => \*(Aqpentagon\*(Aq, width => 20, height => 20 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $s2, 250, 200 ); \& \& # Create a connector to link the two shapes. \& my $cxn_shape = $workbook\->add_shape( type => \*(AqcurvedConnector3\*(Aq ); \& \& # Link the start of the connector to the right side. \& $cxn_shape\->set_start( $s1\->get_id() ); \& $cxn_shape\->set_start_index( 2 ); # 2nd connection pt, clockwise from top(0). \& $cxn_shape\->set_start_side( \*(Aqr\*(Aq ); # r)ight or b)ottom. \& \& # Link the end of the connector to the left side. \& $cxn_shape\->set_end( $s2\->get_id() ); \& $cxn_shape\->set_end_index( 4 ); # 4th connection pt, clockwise from top(0). \& $cxn_shape\->set_end_side( \*(Aql\*(Aq ); # l)eft or t)op. \& \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: shape7.pl" .IX Subsection "Example: shape7.pl" A simple example of how to use the Excel::Writer::XLSX module to add shapes and one-to-many connectors to an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # add shapes and one\-to\-many connectors to an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape7.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Add a circle, with centered text. c is for circle, not center. \& my $cw = 60; \& my $ch = 60; \& my $cx = 210; \& my $cy = 190; \& \& my $ellipse = $workbook\->add_shape( \& type => \*(Aqellipse\*(Aq, \& id => 2, \& text => "Hello\enWorld", \& width => $cw, \& height => $ch \& ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $ellipse, $cx, $cy ); \& \& # Add a plus sign at 4 different positions around the circle. \& my $pw = 20; \& my $ph = 20; \& my $px = 120; \& my $py = 250; \& my $plus = \& $workbook\->add_shape( type => \*(Aqplus\*(Aq, id => 3, width => $pw, height => $ph ); \& my $p1 = $worksheet\->insert_shape( \*(AqA1\*(Aq, $plus, 350, 350 ); \& my $p2 = $worksheet\->insert_shape( \*(AqA1\*(Aq, $plus, 150, 350 ); \& my $p3 = $worksheet\->insert_shape( \*(AqA1\*(Aq, $plus, 350, 150 ); \& $plus\->set_adjustments( 35 ); # change shape of plus symbol. \& my $p4 = $worksheet\->insert_shape( \*(AqA1\*(Aq, $plus, 150, 150 ); \& \& my $cxn_shape = $workbook\->add_shape( type => \*(AqbentConnector3\*(Aq, fill => 0 ); \& \& $cxn_shape\->set_start( $ellipse\->get_id() ); \& $cxn_shape\->set_start_index( 4 ); # 4nd connection pt, clockwise from top(0). \& $cxn_shape\->set_start_side( \*(Aqb\*(Aq ); # r)ight or b)ottom. \& \& $cxn_shape\->set_end( $p1\->get_id() ); \& $cxn_shape\->set_end_index( 0 ); \& $cxn_shape\->set_end_side( \*(Aqt\*(Aq ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $cxn_shape\->set_end( $p2\->get_id() ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $cxn_shape\->set_end( $p3\->get_id() ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $cxn_shape\->set_end( $p4\->get_id() ); \& $cxn_shape\->set_adjustments( \-50, 45, 120 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: shape8.pl" .IX Subsection "Example: shape8.pl" A simple example of how to use the Excel::Writer::XLSX module to add shapes and one-to-many connectors to an Excel xlsx file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # add shapes and one\-to\-many connectors to an Excel xlsx file. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape8.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Add a circle, with centered text. c is for circle, not center. \& my $cw = 60; \& my $ch = 60; \& my $cx = 210; \& my $cy = 190; \& \& my $ellipse = $workbook\->add_shape( \& type => \*(Aqellipse\*(Aq, \& id => 2, \& text => "Hello\enWorld", \& width => $cw, \& height => $ch \& ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $ellipse, $cx, $cy ); \& \& # Add a plus sign at 4 different positions around the circle. \& my $pw = 20; \& my $ph = 20; \& my $px = 120; \& my $py = 250; \& my $plus = \& $workbook\->add_shape( type => \*(Aqplus\*(Aq, id => 3, width => $pw, height => $ph ); \& my $p1 = $worksheet\->insert_shape( \*(AqA1\*(Aq, $plus, 350, 150 ); # 2:00 \& my $p2 = $worksheet\->insert_shape( \*(AqA1\*(Aq, $plus, 350, 350 ); # 4:00 \& my $p3 = $worksheet\->insert_shape( \*(AqA1\*(Aq, $plus, 150, 350 ); # 8:00 \& my $p4 = $worksheet\->insert_shape( \*(AqA1\*(Aq, $plus, 150, 150 ); # 10:00 \& \& my $cxn_shape = $workbook\->add_shape( type => \*(AqbentConnector3\*(Aq, fill => 0 ); \& \& $cxn_shape\->set_start( $ellipse\->get_id() ); \& $cxn_shape\->set_start_index( 2 ); # 2nd connection pt, clockwise from top(0). \& $cxn_shape\->set_start_side( \*(Aqr\*(Aq ); # r)ight or b)ottom. \& \& $cxn_shape\->set_end( $p1\->get_id() ); \& $cxn_shape\->set_end_index( 3 ); # 3rd connection point on plus, right side \& $cxn_shape\->set_end_side( \*(Aql\*(Aq ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $cxn_shape\->set_end( $p2\->get_id() ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $cxn_shape\->set_end( $p3\->get_id() ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $cxn_shape\->set_end( $p4\->get_id() ); \& $cxn_shape\->set_adjustments( \-50, 45, 120 ); \& $worksheet\->insert_shape( \*(AqA1\*(Aq, $cxn_shape, 0, 0 ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: shape_all.pl" .IX Subsection "Example: shape_all.pl" A simple example of how to use the Excel::Writer::XLSX module to add all shapes (as currently implemented) to an Excel xlsx file. .PP The list at the end consists of all the shape types defined as ST_ShapeType in \s-1ECMA\-376,\s0 Office Open \s-1XML\s0 File Formats Part 4. .PP The grouping by worksheet name is for illustration only. It isn't part of the \s-1ECMA\-376\s0 standard. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # A simple example of how to use the Excel::Writer::XLSX module to \& # add all shapes (as currently implemented) to an Excel xlsx file. \& # \& # The list at the end consists of all the shape types defined as \& # ST_ShapeType in ECMA\-376, Office Open XML File Formats Part 4. \& # \& # The grouping by worksheet name is for illustration only. It isn\*(Aqt \& # part of the ECMA\-376 standard. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqshape_all.xlsx\*(Aq ); \& \& my ( $worksheet, $last_sheet, $shape, $r ) = ( 0, \*(Aq\*(Aq, \*(Aq\*(Aq, undef, 0 ); \& \& while ( ) { \& chomp; \& next unless m/^\ew/; # Skip blank lines and comments. \& \& my ( $sheet, $name ) = split( /\et/, $_ ); \& if ( $last_sheet ne $sheet ) { \& $worksheet = $workbook\->add_worksheet( $sheet ); \& $r = 2; \& } \& $last_sheet = $sheet; \& $shape = $workbook\->add_shape( \& type => $name, \& text => $name, \& width => 90, \& height => 90 \& ); \& \& # Connectors can not have labels, so write the connector name in the cell \& # to the left. \& $worksheet\->write( $r, 0, $name ) if $sheet eq \*(AqConnector\*(Aq; \& $worksheet\->insert_shape( $r, 2, $shape, 0, 0 ); \& $r += 5; \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ \& Action actionButtonBackPrevious \& Action actionButtonBeginning \& Action actionButtonBlank \& Action actionButtonDocument \& Action actionButtonEnd \& Action actionButtonForwardNext \& Action actionButtonHelp \& Action actionButtonHome \& Action actionButtonInformation \& Action actionButtonMovie \& Action actionButtonReturn \& Action actionButtonSound \& Arrow bentArrow \& Arrow bentUpArrow \& Arrow circularArrow \& Arrow curvedDownArrow \& Arrow curvedLeftArrow \& Arrow curvedRightArrow \& Arrow curvedUpArrow \& Arrow downArrow \& Arrow leftArrow \& Arrow leftCircularArrow \& Arrow leftRightArrow \& Arrow leftRightCircularArrow \& Arrow leftRightUpArrow \& Arrow leftUpArrow \& Arrow notchedRightArrow \& Arrow quadArrow \& Arrow rightArrow \& Arrow stripedRightArrow \& Arrow swooshArrow \& Arrow upArrow \& Arrow upDownArrow \& Arrow uturnArrow \& Basic blockArc \& Basic can \& Basic chevron \& Basic cube \& Basic decagon \& Basic diamond \& Basic dodecagon \& Basic donut \& Basic ellipse \& Basic funnel \& Basic gear6 \& Basic gear9 \& Basic heart \& Basic heptagon \& Basic hexagon \& Basic homePlate \& Basic lightningBolt \& Basic line \& Basic lineInv \& Basic moon \& Basic nonIsoscelesTrapezoid \& Basic noSmoking \& Basic octagon \& Basic parallelogram \& Basic pentagon \& Basic pie \& Basic pieWedge \& Basic plaque \& Basic rect \& Basic round1Rect \& Basic round2DiagRect \& Basic round2SameRect \& Basic roundRect \& Basic rtTriangle \& Basic smileyFace \& Basic snip1Rect \& Basic snip2DiagRect \& Basic snip2SameRect \& Basic snipRoundRect \& Basic star10 \& Basic star12 \& Basic star16 \& Basic star24 \& Basic star32 \& Basic star4 \& Basic star5 \& Basic star6 \& Basic star7 \& Basic star8 \& Basic sun \& Basic teardrop \& Basic trapezoid \& Basic triangle \& Callout accentBorderCallout1 \& Callout accentBorderCallout2 \& Callout accentBorderCallout3 \& Callout accentCallout1 \& Callout accentCallout2 \& Callout accentCallout3 \& Callout borderCallout1 \& Callout borderCallout2 \& Callout borderCallout3 \& Callout callout1 \& Callout callout2 \& Callout callout3 \& Callout cloudCallout \& Callout downArrowCallout \& Callout leftArrowCallout \& Callout leftRightArrowCallout \& Callout quadArrowCallout \& Callout rightArrowCallout \& Callout upArrowCallout \& Callout upDownArrowCallout \& Callout wedgeEllipseCallout \& Callout wedgeRectCallout \& Callout wedgeRoundRectCallout \& Chart chartPlus \& Chart chartStar \& Chart chartX \& Connector bentConnector2 \& Connector bentConnector3 \& Connector bentConnector4 \& Connector bentConnector5 \& Connector curvedConnector2 \& Connector curvedConnector3 \& Connector curvedConnector4 \& Connector curvedConnector5 \& Connector straightConnector1 \& FlowChart flowChartAlternateProcess \& FlowChart flowChartCollate \& FlowChart flowChartConnector \& FlowChart flowChartDecision \& FlowChart flowChartDelay \& FlowChart flowChartDisplay \& FlowChart flowChartDocument \& FlowChart flowChartExtract \& FlowChart flowChartInputOutput \& FlowChart flowChartInternalStorage \& FlowChart flowChartMagneticDisk \& FlowChart flowChartMagneticDrum \& FlowChart flowChartMagneticTape \& FlowChart flowChartManualInput \& FlowChart flowChartManualOperation \& FlowChart flowChartMerge \& FlowChart flowChartMultidocument \& FlowChart flowChartOfflineStorage \& FlowChart flowChartOffpageConnector \& FlowChart flowChartOnlineStorage \& FlowChart flowChartOr \& FlowChart flowChartPredefinedProcess \& FlowChart flowChartPreparation \& FlowChart flowChartProcess \& FlowChart flowChartPunchedCard \& FlowChart flowChartPunchedTape \& FlowChart flowChartSort \& FlowChart flowChartSummingJunction \& FlowChart flowChartTerminator \& Math mathDivide \& Math mathEqual \& Math mathMinus \& Math mathMultiply \& Math mathNotEqual \& Math mathPlus \& Star_Banner arc \& Star_Banner bevel \& Star_Banner bracePair \& Star_Banner bracketPair \& Star_Banner chord \& Star_Banner cloud \& Star_Banner corner \& Star_Banner diagStripe \& Star_Banner doubleWave \& Star_Banner ellipseRibbon \& Star_Banner ellipseRibbon2 \& Star_Banner foldedCorner \& Star_Banner frame \& Star_Banner halfFrame \& Star_Banner horizontalScroll \& Star_Banner irregularSeal1 \& Star_Banner irregularSeal2 \& Star_Banner leftBrace \& Star_Banner leftBracket \& Star_Banner leftRightRibbon \& Star_Banner plus \& Star_Banner ribbon \& Star_Banner ribbon2 \& Star_Banner rightBrace \& Star_Banner rightBracket \& Star_Banner verticalScroll \& Star_Banner wave \& Tabs cornerTabs \& Tabs plaqueTabs \& Tabs squareTabs .Ve .PP Download this example: .SS "Example: sparklines1.pl" .IX Subsection "Example: sparklines1.pl" Example of how to add sparklines to an Excel::Writer::XLSX file. .PP Sparklines are small charts that fit in a single cell and are used to show trends in data. See sparklines2.pl for examples of more complex sparkline formatting. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to add sparklines to an Excel::Writer::XLSX file. \& # \& # Sparklines are small charts that fit in a single cell and are \& # used to show trends in data. See sparklines2.pl for examples \& # of more complex sparkline formatting. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqsparklines1.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Some sample data to plot. \& my $data = [ \& \& [ \-2, 2, 3, \-1, 0 ], \& [ 30, 20, 33, 20, 15 ], \& [ 1, \-1, \-1, 1, \-1 ], \& \& ]; \& \& # Write the sample data to the worksheet. \& $worksheet\->write_col( \*(AqA1\*(Aq, $data ); \& \& \& # Add a line sparkline (the default) with markers. \& $worksheet\->add_sparkline( \& { \& location => \*(AqF1\*(Aq, \& range => \*(AqSheet1!A1:E1\*(Aq, \& markers => 1, \& } \& ); \& \& # Add a column sparkline with non\-default style. \& $worksheet\->add_sparkline( \& { \& location => \*(AqF2\*(Aq, \& range => \*(AqSheet1!A2:E2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 12, \& } \& ); \& \& # Add a win/loss sparkline with negative values highlighted. \& $worksheet\->add_sparkline( \& { \& location => \*(AqF3\*(Aq, \& range => \*(AqSheet1!A3:E3\*(Aq, \& type => \*(Aqwin_loss\*(Aq, \& negative_points => 1, \& } \& ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: sparklines2.pl" .IX Subsection "Example: sparklines2.pl" Example of how to add sparklines to an Excel::Writer::XLSX file. .PP Sparklines are small charts that fit in a single cell and are used to show trends in data. This example shows the majority of options that can be applied to sparklines. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to add sparklines to an Excel::Writer::XLSX file. \& # \& # Sparklines are small charts that fit in a single cell and are \& # used to show trends in data. This example shows the majority of \& # options that can be applied to sparklines. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqsparklines2.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $bold = $workbook\->add_format( bold => 1 ); \& my $str; \& my $row = 1; \& \& # Set the columns widths to make the output clearer. \& $worksheet1\->set_column( \*(AqA:A\*(Aq, 14 ); \& $worksheet1\->set_column( \*(AqB:B\*(Aq, 50 ); \& $worksheet1\->set_zoom( 150 ); \& \& # Headings. \& $worksheet1\->write( \*(AqA1\*(Aq, \*(AqSparkline\*(Aq, $bold ); \& $worksheet1\->write( \*(AqB1\*(Aq, \*(AqDescription\*(Aq, $bold ); \& \& \& ############################################################################### \& # \& $str = \*(AqA default "line" sparkline.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA2\*(Aq, \& range => \*(AqSheet2!A1:J1\*(Aq, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqA default "column" sparkline.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA3\*(Aq, \& range => \*(AqSheet2!A2:J2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqA default "win/loss" sparkline.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA4\*(Aq, \& range => \*(AqSheet2!A3:J3\*(Aq, \& type => \*(Aqwin_loss\*(Aq, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& $row++; \& \& \& ############################################################################### \& # \& $str = \*(AqLine with markers.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA6\*(Aq, \& range => \*(AqSheet2!A1:J1\*(Aq, \& markers => 1, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqLine with high and low points.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA7\*(Aq, \& range => \*(AqSheet2!A1:J1\*(Aq, \& high_point => 1, \& low_point => 1, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqLine with first and last point markers.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA8\*(Aq, \& range => \*(AqSheet2!A1:J1\*(Aq, \& first_point => 1, \& last_point => 1, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqLine with negative point markers.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA9\*(Aq, \& range => \*(AqSheet2!A1:J1\*(Aq, \& negative_points => 1, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqLine with axis.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA10\*(Aq, \& range => \*(AqSheet2!A1:J1\*(Aq, \& axis => 1, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& $row++; \& \& \& ############################################################################### \& # \& $str = \*(AqColumn with default style (1).\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA12\*(Aq, \& range => \*(AqSheet2!A2:J2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqColumn with style 2.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA13\*(Aq, \& range => \*(AqSheet2!A2:J2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 2, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqColumn with style 3.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA14\*(Aq, \& range => \*(AqSheet2!A2:J2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 3, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqColumn with style 4.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA15\*(Aq, \& range => \*(AqSheet2!A2:J2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 4, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqColumn with style 5.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA16\*(Aq, \& range => \*(AqSheet2!A2:J2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 5, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqColumn with style 6.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA17\*(Aq, \& range => \*(AqSheet2!A2:J2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 6, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqColumn with a user defined colour.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA18\*(Aq, \& range => \*(AqSheet2!A2:J2\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& series_color => \*(Aq#E965E0\*(Aq, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& $row++; \& \& \& ############################################################################### \& # \& $str = \*(AqA win/loss sparkline.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA20\*(Aq, \& range => \*(AqSheet2!A3:J3\*(Aq, \& type => \*(Aqwin_loss\*(Aq, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqA win/loss sparkline with negative points highlighted.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA21\*(Aq, \& range => \*(AqSheet2!A3:J3\*(Aq, \& type => \*(Aqwin_loss\*(Aq, \& negative_points => 1, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& $row++; \& \& \& ############################################################################### \& # \& $str = \*(AqA left to right column (the default).\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA23\*(Aq, \& range => \*(AqSheet2!A4:J4\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 20, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqA right to left column.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA24\*(Aq, \& range => \*(AqSheet2!A4:J4\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 20, \& reverse => 1, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& ############################################################################### \& # \& $str = \*(AqSparkline and text in one cell.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => \*(AqA25\*(Aq, \& range => \*(AqSheet2!A4:J4\*(Aq, \& type => \*(Aqcolumn\*(Aq, \& style => 20, \& } \& ); \& \& $worksheet1\->write( $row, 0, \*(AqGrowth\*(Aq ); \& $worksheet1\->write( $row++, 1, $str ); \& $row++; \& \& \& ############################################################################### \& # \& $str = \*(AqA grouped sparkline. Changes are applied to all three.\*(Aq; \& \& $worksheet1\->add_sparkline( \& { \& location => [ \*(AqA27\*(Aq, \*(AqA28\*(Aq, \*(AqA29\*(Aq ], \& range => [ \*(AqSheet2!A5:J5\*(Aq, \*(AqSheet2!A6:J6\*(Aq, \*(AqSheet2!A7:J7\*(Aq ], \& markers => 1, \& } \& ); \& \& $worksheet1\->write( $row++, 1, $str ); \& \& \& \& \& ############################################################################### \& # \& # Create a second worksheet with data to plot. \& # \& \& $worksheet2\->set_column( \*(AqA:J\*(Aq, 11 ); \& \& my $data = [ \& \& # Simple line data. \& [ \-2, 2, 3, \-1, 0, \-2, 3, 2, 1, 0 ], \& \& # Simple column data. \& [ 30, 20, 33, 20, 15, 5, 5, 15, 10, 15 ], \& \& # Simple win/loss data. \& [ 1, 1, \-1, \-1, 1, \-1, 1, 1, 1, \-1 ], \& \& # Unbalanced histogram. \& [ 5, 6, 7, 10, 15, 20, 30, 50, 70, 100 ], \& \& # Data for the grouped sparkline example. \& [ \-2, 2, 3, \-1, 0, \-2, 3, 2, 1, 0 ], \& [ 3, \-1, 0, \-2, 3, 2, 1, 0, 2, 1 ], \& [ 0, \-2, 3, 2, 1, 0, 1, 2, 3, 1 ], \& \& \& ]; \& \& # Write the sample data to the worksheet. \& $worksheet2\->write_col( \*(AqA1\*(Aq, $data ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: stats_ext.pl" .IX Subsection "Example: stats_ext.pl" Example of formatting using the Excel::Writer::XLSX module .PP This is a simple example of how to use functions that reference cells in other worksheets within the same workbook. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of formatting using the Excel::Writer::XLSX module \& # \& # This is a simple example of how to use functions that reference cells in \& # other worksheets within the same workbook. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqstats_ext.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet( \*(AqTest results\*(Aq ); \& my $worksheet2 = $workbook\->add_worksheet( \*(AqData\*(Aq ); \& \& # Set the column width for columns 1 \& $worksheet1\->set_column( \*(AqA:A\*(Aq, 20 ); \& \& \& # Create a format for the headings \& my $heading = $workbook\->add_format(); \& $heading\->set_bold(); \& \& # Create a numerical format \& my $numformat = $workbook\->add_format(); \& $numformat\->set_num_format( \*(Aq0.00\*(Aq ); \& \& \& # Write some statistical functions \& $worksheet1\->write( \*(AqA1\*(Aq, \*(AqCount\*(Aq, $heading ); \& $worksheet1\->write( \*(AqB1\*(Aq, \*(Aq=COUNT(Data!B2:B9)\*(Aq ); \& \& $worksheet1\->write( \*(AqA2\*(Aq, \*(AqSum\*(Aq, $heading ); \& $worksheet1\->write( \*(AqB2\*(Aq, \*(Aq=SUM(Data!B2:B9)\*(Aq ); \& \& $worksheet1\->write( \*(AqA3\*(Aq, \*(AqAverage\*(Aq, $heading ); \& $worksheet1\->write( \*(AqB3\*(Aq, \*(Aq=AVERAGE(Data!B2:B9)\*(Aq ); \& \& $worksheet1\->write( \*(AqA4\*(Aq, \*(AqMin\*(Aq, $heading ); \& $worksheet1\->write( \*(AqB4\*(Aq, \*(Aq=MIN(Data!B2:B9)\*(Aq ); \& \& $worksheet1\->write( \*(AqA5\*(Aq, \*(AqMax\*(Aq, $heading ); \& $worksheet1\->write( \*(AqB5\*(Aq, \*(Aq=MAX(Data!B2:B9)\*(Aq ); \& \& $worksheet1\->write( \*(AqA6\*(Aq, \*(AqStandard Deviation\*(Aq, $heading ); \& $worksheet1\->write( \*(AqB6\*(Aq, \*(Aq=STDEV(Data!B2:B9)\*(Aq ); \& \& $worksheet1\->write( \*(AqA7\*(Aq, \*(AqKurtosis\*(Aq, $heading ); \& $worksheet1\->write( \*(AqB7\*(Aq, \*(Aq=KURT(Data!B2:B9)\*(Aq ); \& \& \& # Write the sample data \& $worksheet2\->write( \*(AqA1\*(Aq, \*(AqSample\*(Aq, $heading ); \& $worksheet2\->write( \*(AqA2\*(Aq, 1 ); \& $worksheet2\->write( \*(AqA3\*(Aq, 2 ); \& $worksheet2\->write( \*(AqA4\*(Aq, 3 ); \& $worksheet2\->write( \*(AqA5\*(Aq, 4 ); \& $worksheet2\->write( \*(AqA6\*(Aq, 5 ); \& $worksheet2\->write( \*(AqA7\*(Aq, 6 ); \& $worksheet2\->write( \*(AqA8\*(Aq, 7 ); \& $worksheet2\->write( \*(AqA9\*(Aq, 8 ); \& \& $worksheet2\->write( \*(AqB1\*(Aq, \*(AqLength\*(Aq, $heading ); \& $worksheet2\->write( \*(AqB2\*(Aq, 25.4, $numformat ); \& $worksheet2\->write( \*(AqB3\*(Aq, 25.4, $numformat ); \& $worksheet2\->write( \*(AqB4\*(Aq, 24.8, $numformat ); \& $worksheet2\->write( \*(AqB5\*(Aq, 25.0, $numformat ); \& $worksheet2\->write( \*(AqB6\*(Aq, 25.3, $numformat ); \& $worksheet2\->write( \*(AqB7\*(Aq, 24.9, $numformat ); \& $worksheet2\->write( \*(AqB8\*(Aq, 25.2, $numformat ); \& $worksheet2\->write( \*(AqB9\*(Aq, 24.8, $numformat ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: stocks.pl" .IX Subsection "Example: stocks.pl" Example of formatting using the Excel::Writer::XLSX module .PP This example shows how to use a conditional numerical format with colours to indicate if a share price has gone up or down. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of formatting using the Excel::Writer::XLSX module \& # \& # This example shows how to use a conditional numerical format \& # with colours to indicate if a share price has gone up or down. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& # Create a new workbook and add a worksheet \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqstocks.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Set the column width for columns 1, 2, 3 and 4 \& $worksheet\->set_column( 0, 3, 15 ); \& \& \& # Create a format for the column headings \& my $header = $workbook\->add_format(); \& $header\->set_bold(); \& $header\->set_size( 12 ); \& $header\->set_color( \*(Aqblue\*(Aq ); \& \& \& # Create a format for the stock price \& my $f_price = $workbook\->add_format(); \& $f_price\->set_align( \*(Aqleft\*(Aq ); \& $f_price\->set_num_format( \*(Aq$0.00\*(Aq ); \& \& \& # Create a format for the stock volume \& my $f_volume = $workbook\->add_format(); \& $f_volume\->set_align( \*(Aqleft\*(Aq ); \& $f_volume\->set_num_format( \*(Aq#,##0\*(Aq ); \& \& \& # Create a format for the price change. This is an example of a conditional \& # format. The number is formatted as a percentage. If it is positive it is \& # formatted in green, if it is negative it is formatted in red and if it is \& # zero it is formatted as the default font colour (in this case black). \& # Note: the [Green] format produces an unappealing lime green. Try \& # [Color 10] instead for a dark green. \& # \& my $f_change = $workbook\->add_format(); \& $f_change\->set_align( \*(Aqleft\*(Aq ); \& $f_change\->set_num_format( \*(Aq[Green]0.0%;[Red]\-0.0%;0.0%\*(Aq ); \& \& \& # Write out the data \& $worksheet\->write( 0, 0, \*(AqCompany\*(Aq, $header ); \& $worksheet\->write( 0, 1, \*(AqPrice\*(Aq, $header ); \& $worksheet\->write( 0, 2, \*(AqVolume\*(Aq, $header ); \& $worksheet\->write( 0, 3, \*(AqChange\*(Aq, $header ); \& \& $worksheet\->write( 1, 0, \*(AqDamage Inc.\*(Aq ); \& $worksheet\->write( 1, 1, 30.25, $f_price ); # $30.25 \& $worksheet\->write( 1, 2, 1234567, $f_volume ); # 1,234,567 \& $worksheet\->write( 1, 3, 0.085, $f_change ); # 8.5% in green \& \& $worksheet\->write( 2, 0, \*(AqDump Corp.\*(Aq ); \& $worksheet\->write( 2, 1, 1.56, $f_price ); # $1.56 \& $worksheet\->write( 2, 2, 7564, $f_volume ); # 7,564 \& $worksheet\->write( 2, 3, \-0.015, $f_change ); # \-1.5% in red \& \& $worksheet\->write( 3, 0, \*(AqRev Ltd.\*(Aq ); \& $worksheet\->write( 3, 1, 0.13, $f_price ); # $0.13 \& $worksheet\->write( 3, 2, 321, $f_volume ); # 321 \& $worksheet\->write( 3, 3, 0, $f_change ); # 0 in the font color (black) \& \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: watermark.pl" .IX Subsection "Example: watermark.pl" An example of adding a worksheet watermark image using the Excel::Writer::XLSX module. This is based on the method of putting an image in the worksheet header as suggested in the Microsoft documentation: https://support.microsoft.com/en\-us/office/add\-a\-watermark\-in\-excel\-a372182a\-d733\-484e\-825c\-18ddf3edf009 .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # An example of adding a worksheet watermark image using the Excel::Writer::XLSX \& # module. This is based on the method of putting an image in the worksheet \& # header as suggested in the Microsoft documentation: \& # https://support.microsoft.com/en\-us/office/add\-a\-watermark\-in\-excel\-a372182a\-d733\-484e\-825c\-18ddf3edf009 \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqwatermark.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Set a worksheet header with the watermark image. \& $worksheet\->set_header( \*(Aq&C&C&[Picture]\*(Aq, undef, { image_center => \*(Aqwatermark.png\*(Aq } ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: background.pl" .IX Subsection "Example: background.pl" An example of setting a worksheet background image with Excel::Writer::XLSX. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ####################################################################### \& # \& # An example of setting a worksheet background image with Excel::Writer::XLSX. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqbackground.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->set_background( \*(Aqrepublic.png\*(Aq ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: tab_colors.pl" .IX Subsection "Example: tab_colors.pl" Example of how to set Excel worksheet tab colours. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ####################################################################### \& # \& # Example of how to set Excel worksheet tab colours. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqtab_colors.xlsx\*(Aq ); \& \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& my $worksheet4 = $workbook\->add_worksheet(); \& \& # Worksheet1 will have the default tab colour. \& $worksheet2\->set_tab_color( \*(Aqred\*(Aq ); \& $worksheet3\->set_tab_color( \*(Aqgreen\*(Aq ); \& $worksheet4\->set_tab_color( \*(Aq#FF6600\*(Aq); # Orange \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: tables.pl" .IX Subsection "Example: tables.pl" Example of how to add tables to an Excel::Writer::XLSX worksheet. .PP Tables in Excel are used to group rows and columns of data into a single structure that can be referenced in a formula or formatted collectively. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################### \& # \& # Example of how to add tables to an Excel::Writer::XLSX worksheet. \& # \& # Tables in Excel are used to group rows and columns of data into a single \& # structure that can be referenced in a formula or formatted collectively. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqtables.xlsx\*(Aq ); \& my $worksheet1 = $workbook\->add_worksheet(); \& my $worksheet2 = $workbook\->add_worksheet(); \& my $worksheet3 = $workbook\->add_worksheet(); \& my $worksheet4 = $workbook\->add_worksheet(); \& my $worksheet5 = $workbook\->add_worksheet(); \& my $worksheet6 = $workbook\->add_worksheet(); \& my $worksheet7 = $workbook\->add_worksheet(); \& my $worksheet8 = $workbook\->add_worksheet(); \& my $worksheet9 = $workbook\->add_worksheet(); \& my $worksheet10 = $workbook\->add_worksheet(); \& my $worksheet11 = $workbook\->add_worksheet(); \& my $worksheet12 = $workbook\->add_worksheet(); \& my $worksheet13 = $workbook\->add_worksheet(); \& \& my $currency_format = $workbook\->add_format( num_format => \*(Aq$#,##0\*(Aq ); \& \& \& # Some sample data for the table. \& my $data = [ \& [ \*(AqApples\*(Aq, 10000, 5000, 8000, 6000 ], \& [ \*(AqPears\*(Aq, 2000, 3000, 4000, 5000 ], \& [ \*(AqBananas\*(Aq, 6000, 6000, 6500, 6000 ], \& [ \*(AqOranges\*(Aq, 500, 300, 200, 700 ], \& \& ]; \& \& \& ############################################################################### \& # \& # Example 1. \& # \& my $caption = \*(AqDefault table with no data.\*(Aq; \& \& # Set the columns widths. \& $worksheet1\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet1\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet1\->add_table( \*(AqB3:F7\*(Aq ); \& \& \& ############################################################################### \& # \& # Example 2. \& # \& $caption = \*(AqDefault table with data.\*(Aq; \& \& # Set the columns widths. \& $worksheet2\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet2\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet2\->add_table( \*(AqB3:F7\*(Aq, { data => $data } ); \& \& \& ############################################################################### \& # \& # Example 3. \& # \& $caption = \*(AqTable without default autofilter.\*(Aq; \& \& # Set the columns widths. \& $worksheet3\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet3\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet3\->add_table( \*(AqB3:F7\*(Aq, { autofilter => 0 } ); \& \& # Table data can also be written separately, as an array or individual cells. \& $worksheet3\->write_col( \*(AqB4\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 4. \& # \& $caption = \*(AqTable without default header row.\*(Aq; \& \& # Set the columns widths. \& $worksheet4\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet4\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet4\->add_table( \*(AqB4:F7\*(Aq, { header_row => 0 } ); \& \& # Table data can also be written separately, as an array or individual cells. \& $worksheet4\->write_col( \*(AqB4\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 5. \& # \& $caption = \*(AqDefault table with "First Column" and "Last Column" options.\*(Aq; \& \& # Set the columns widths. \& $worksheet5\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet5\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet5\->add_table( \*(AqB3:F7\*(Aq, { first_column => 1, last_column => 1 } ); \& \& # Table data can also be written separately, as an array or individual cells. \& $worksheet5\->write_col( \*(AqB4\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 6. \& # \& $caption = \*(AqTable with banded columns but without default banded rows.\*(Aq; \& \& # Set the columns widths. \& $worksheet6\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet6\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet6\->add_table( \*(AqB3:F7\*(Aq, { banded_rows => 0, banded_columns => 1 } ); \& \& # Table data can also be written separately, as an array or individual cells. \& $worksheet6\->write_col( \*(AqB4\*(Aq, $data ); \& \& \& ############################################################################### \& # \& # Example 7. \& # \& $caption = \*(AqTable with user defined column headers\*(Aq; \& \& # Set the columns widths. \& $worksheet7\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet7\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet7\->add_table( \& \*(AqB3:F7\*(Aq, \& { \& data => $data, \& columns => [ \& { header => \*(AqProduct\*(Aq }, \& { header => \*(AqQuarter 1\*(Aq }, \& { header => \*(AqQuarter 2\*(Aq }, \& { header => \*(AqQuarter 3\*(Aq }, \& { header => \*(AqQuarter 4\*(Aq }, \& ] \& } \& ); \& \& \& ############################################################################### \& # \& # Example 8. \& # \& $caption = \*(AqTable with user defined column headers\*(Aq; \& \& # Set the columns widths. \& $worksheet8\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet8\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet8\->add_table( \& \*(AqB3:G7\*(Aq, \& { \& data => $data, \& columns => [ \& { header => \*(AqProduct\*(Aq }, \& { header => \*(AqQuarter 1\*(Aq }, \& { header => \*(AqQuarter 2\*(Aq }, \& { header => \*(AqQuarter 3\*(Aq }, \& { header => \*(AqQuarter 4\*(Aq }, \& { \& header => \*(AqYear\*(Aq, \& formula => \*(Aq=SUM(Table8[@[Quarter 1]:[Quarter 4]])\*(Aq \& }, \& ] \& } \& ); \& \& \& ############################################################################### \& # \& # Example 9. \& # \& $caption = \*(AqTable with totals row (but no caption or totals).\*(Aq; \& \& # Set the columns widths. \& $worksheet9\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet9\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet9\->add_table( \& \*(AqB3:G8\*(Aq, \& { \& data => $data, \& total_row => 1, \& columns => [ \& { header => \*(AqProduct\*(Aq }, \& { header => \*(AqQuarter 1\*(Aq }, \& { header => \*(AqQuarter 2\*(Aq }, \& { header => \*(AqQuarter 3\*(Aq }, \& { header => \*(AqQuarter 4\*(Aq }, \& { \& header => \*(AqYear\*(Aq, \& formula => \*(Aq=SUM(Table9[@[Quarter 1]:[Quarter 4]])\*(Aq \& }, \& ] \& } \& ); \& \& \& ############################################################################### \& # \& # Example 10. \& # \& $caption = \*(AqTable with totals row with user captions and functions.\*(Aq; \& \& # Set the columns widths. \& $worksheet10\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet10\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet10\->add_table( \& \*(AqB3:G8\*(Aq, \& { \& data => $data, \& total_row => 1, \& columns => [ \& { header => \*(AqProduct\*(Aq, total_string => \*(AqTotals\*(Aq }, \& { header => \*(AqQuarter 1\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 2\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 3\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 4\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { \& header => \*(AqYear\*(Aq, \& formula => \*(Aq=SUM(Table10[@[Quarter 1]:[Quarter 4]])\*(Aq, \& total_function => \*(Aqsum\*(Aq \& }, \& ] \& } \& ); \& \& \& ############################################################################### \& # \& # Example 11. \& # \& $caption = \*(AqTable with alternative Excel style.\*(Aq; \& \& # Set the columns widths. \& $worksheet11\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet11\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet11\->add_table( \& \*(AqB3:G8\*(Aq, \& { \& data => $data, \& style => \*(AqTable Style Light 11\*(Aq, \& total_row => 1, \& columns => [ \& { header => \*(AqProduct\*(Aq, total_string => \*(AqTotals\*(Aq }, \& { header => \*(AqQuarter 1\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 2\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 3\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 4\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { \& header => \*(AqYear\*(Aq, \& formula => \*(Aq=SUM(Table11[@[Quarter 1]:[Quarter 4]])\*(Aq, \& total_function => \*(Aqsum\*(Aq \& }, \& ] \& } \& ); \& \& \& ############################################################################### \& # \& # Example 12. \& # \& $caption = \*(AqTable with no Excel style.\*(Aq; \& \& # Set the columns widths. \& $worksheet12\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet12\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet12\->add_table( \& \*(AqB3:G8\*(Aq, \& { \& data => $data, \& style => \*(AqNone\*(Aq, \& total_row => 1, \& columns => [ \& { header => \*(AqProduct\*(Aq, total_string => \*(AqTotals\*(Aq }, \& { header => \*(AqQuarter 1\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 2\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 3\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { header => \*(AqQuarter 4\*(Aq, total_function => \*(Aqsum\*(Aq }, \& { \& header => \*(AqYear\*(Aq, \& formula => \*(Aq=SUM(Table12[@[Quarter 1]:[Quarter 4]])\*(Aq, \& total_function => \*(Aqsum\*(Aq \& }, \& ] \& } \& ); \& \& \& ############################################################################### \& # \& # Example 13. \& # \& $caption = \*(AqTable with column formats.\*(Aq; \& \& # Set the columns widths. \& $worksheet13\->set_column( \*(AqB:G\*(Aq, 12 ); \& \& # Write the caption. \& $worksheet13\->write( \*(AqB1\*(Aq, $caption ); \& \& # Add a table to the worksheet. \& $worksheet13\->add_table( \& \*(AqB3:G8\*(Aq, \& { \& data => $data, \& total_row => 1, \& columns => [ \& { header => \*(AqProduct\*(Aq, total_string => \*(AqTotals\*(Aq }, \& { \& header => \*(AqQuarter 1\*(Aq, \& total_function => \*(Aqsum\*(Aq, \& format => $currency_format, \& }, \& { \& header => \*(AqQuarter 2\*(Aq, \& total_function => \*(Aqsum\*(Aq, \& format => $currency_format, \& }, \& { \& header => \*(AqQuarter 3\*(Aq, \& total_function => \*(Aqsum\*(Aq, \& format => $currency_format, \& }, \& { \& header => \*(AqQuarter 4\*(Aq, \& total_function => \*(Aqsum\*(Aq, \& format => $currency_format, \& }, \& { \& header => \*(AqYear\*(Aq, \& formula => \*(Aq=SUM(Table13[@[Quarter 1]:[Quarter 4]])\*(Aq, \& total_function => \*(Aqsum\*(Aq, \& format => $currency_format, \& }, \& ] \& } \& ); \& \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_handler1.pl" .IX Subsection "Example: write_handler1.pl" Example of how to add a user defined data handler to the Excel::Writer::XLSX \fBwrite()\fR method. .PP The following example shows how to add a handler for a 7 digit \s-1ID\s0 number. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add a user defined data handler to the \& # Excel::Writer::XLSX write() method. \& # \& # The following example shows how to add a handler for a 7 digit ID number. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqwrite_handler1.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& ############################################################################### \& # \& # Add a handler for 7 digit id numbers. This is useful when you want a string \& # such as 0000001 written as a string instead of a number and thus preserve \& # the leading zeroes. \& # \& # Note: you can get the same effect using the keep_leading_zeros() method but \& # this serves as a simple example. \& # \& $worksheet\->add_write_handler( qr[^\ed{7}$], \e&write_my_id ); \& \& \& ############################################################################### \& # \& # The following function processes the data when a match is found. \& # \& sub write_my_id { \& \& my $worksheet = shift; \& \& return $worksheet\->write_string( @_ ); \& } \& \& \& # This format maintains the cell as text even if it is edited. \& my $id_format = $workbook\->add_format( num_format => \*(Aq@\*(Aq ); \& \& \& # Write some numbers in the user defined format \& $worksheet\->write( \*(AqA1\*(Aq, \*(Aq0000000\*(Aq, $id_format ); \& $worksheet\->write( \*(AqA2\*(Aq, \*(Aq0000001\*(Aq, $id_format ); \& $worksheet\->write( \*(AqA3\*(Aq, \*(Aq0004000\*(Aq, $id_format ); \& $worksheet\->write( \*(AqA4\*(Aq, \*(Aq1234567\*(Aq, $id_format ); \& \& # Write some numbers that don\*(Aqt match the defined format \& $worksheet\->write( \*(AqA6\*(Aq, \*(Aq000000\*(Aq, $id_format ); \& $worksheet\->write( \*(AqA7\*(Aq, \*(Aq000001\*(Aq, $id_format ); \& $worksheet\->write( \*(AqA8\*(Aq, \*(Aq004000\*(Aq, $id_format ); \& $worksheet\->write( \*(AqA9\*(Aq, \*(Aq123456\*(Aq, $id_format ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_handler2.pl" .IX Subsection "Example: write_handler2.pl" Example of how to add a user defined data handler to the Excel::Writer::XLSX \fBwrite()\fR method. .PP The following example shows how to add a handler for a 7 digit \s-1ID\s0 number. It adds an additional constraint to the write_handler1.pl in that it only filters data that isn't in the third column. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add a user defined data handler to the \& # Excel::Writer::XLSX write() method. \& # \& # The following example shows how to add a handler for a 7 digit ID number. \& # It adds an additional constraint to the write_handler1.pl in that it only \& # filters data that isn\*(Aqt in the third column. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqwrite_handler2.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& \& \& ############################################################################### \& # \& # Add a handler for 7 digit id numbers. This is useful when you want a string \& # such as 0000001 written as a string instead of a number and thus preserve \& # the leading zeroes. \& # \& # Note: you can get the same effect using the keep_leading_zeros() method but \& # this serves as a simple example. \& # \& $worksheet\->add_write_handler( qr[^\ed{7}$], \e&write_my_id ); \& \& \& ############################################################################### \& # \& # The following function processes the data when a match is found. The handler \& # is set up so that it only filters data if it is in the third column. \& # \& sub write_my_id { \& \& my $worksheet = shift; \& my $col = $_[1]; \& \& # col is zero based \& if ( $col != 2 ) { \& return $worksheet\->write_string( @_ ); \& } \& else { \& \& # Reject the match and return control to write() \& return undef; \& } \& \& } \& \& \& # This format maintains the cell as text even if it is edited. \& my $id_format = $workbook\->add_format( num_format => \*(Aq@\*(Aq ); \& \& \& # Write some numbers in the user defined format \& $worksheet\->write( \*(AqA1\*(Aq, \*(Aq0000000\*(Aq, $id_format ); \& $worksheet\->write( \*(AqB1\*(Aq, \*(Aq0000001\*(Aq, $id_format ); \& $worksheet\->write( \*(AqC1\*(Aq, \*(Aq0000002\*(Aq, $id_format ); \& $worksheet\->write( \*(AqD1\*(Aq, \*(Aq0000003\*(Aq, $id_format ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_handler3.pl" .IX Subsection "Example: write_handler3.pl" Example of how to add a user defined data handler to the Excel::Writer::XLSX \fBwrite()\fR method. .PP The following example shows how to add a handler for dates in a specific format. .PP See write_handler4.pl for a more rigorous example with error handling. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add a user defined data handler to the \& # Excel::Writer::XLSX write() method. \& # \& # The following example shows how to add a handler for dates in a specific \& # format. \& # \& # See write_handler4.pl for a more rigorous example with error handling. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqwrite_handler3.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $date_format = $workbook\->add_format( num_format => \*(Aqdd/mm/yy\*(Aq ); \& \& \& ############################################################################### \& # \& # Add a handler to match dates in the following format: d/m/yyyy \& # \& # The day and month can be single or double digits. \& # \& $worksheet\->add_write_handler( qr[^\ed{1,2}/\ed{1,2}/\ed{4}$], \e&write_my_date ); \& \& \& ############################################################################### \& # \& # The following function processes the data when a match is found. \& # See write_handler4.pl for a more rigorous example with error handling. \& # \& sub write_my_date { \& \& my $worksheet = shift; \& my @args = @_; \& \& my $token = $args[2]; \& $token =~ qr[^(\ed{1,2})/(\ed{1,2})/(\ed{4})$]; \& \& # Change to the date format required by write_date_time(). \& my $date = sprintf "%4d\-%02d\-%02dT", $3, $2, $1; \& \& $args[2] = $date; \& \& return $worksheet\->write_date_time( @args ); \& } \& \& \& # Write some dates in the user defined format \& $worksheet\->write( \*(AqA1\*(Aq, \*(Aq22/12/2004\*(Aq, $date_format ); \& $worksheet\->write( \*(AqA2\*(Aq, \*(Aq1/1/1995\*(Aq, $date_format ); \& $worksheet\->write( \*(AqA3\*(Aq, \*(Aq01/01/1995\*(Aq, $date_format ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_handler4.pl" .IX Subsection "Example: write_handler4.pl" Example of how to add a user defined data handler to the Excel::Writer::XLSX \fBwrite()\fR method. .PP The following example shows how to add a handler for dates in a specific format. .PP This is a more rigorous version of write_handler3.pl. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& ############################################################################### \& # \& # Example of how to add a user defined data handler to the \& # Excel::Writer::XLSX write() method. \& # \& # The following example shows how to add a handler for dates in a specific \& # format. \& # \& # This is a more rigorous version of write_handler3.pl. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqwrite_handler4.xlsx\*(Aq ); \& my $worksheet = $workbook\->add_worksheet(); \& my $date_format = $workbook\->add_format( num_format => \*(Aqdd/mm/yy\*(Aq ); \& \& \& ############################################################################### \& # \& # Add a handler to match dates in the following formats: d/m/yy, d/m/yyyy \& # \& # The day and month can be single or double digits and the year can be 2 or 4 \& # digits. \& # \& $worksheet\->add_write_handler( qr[^\ed{1,2}/\ed{1,2}/\ed{2,4}$], \e&write_my_date ); \& \& \& ############################################################################### \& # \& # The following function processes the data when a match is found. \& # \& sub write_my_date { \& \& my $worksheet = shift; \& my @args = @_; \& \& my $token = $args[2]; \& \& if ( $token =~ qr[^(\ed{1,2})/(\ed{1,2})/(\ed{2,4})$] ) { \& \& my $day = $1; \& my $mon = $2; \& my $year = $3; \& \& # Use a window for 2 digit dates. This will keep some ragged Perl \& # programmer employed in thirty years time. :\-) \& if ( length $year == 2 ) { \& if ( $year < 50 ) { \& $year += 2000; \& } \& else { \& $year += 1900; \& } \& } \& \& my $date = sprintf "%4d\-%02d\-%02dT", $year, $mon, $day; \& \& # Convert the ISO ISO8601 style string to an Excel date \& $date = $worksheet\->convert_date_time( $date ); \& \& if ( defined $date ) { \& \& # Date was valid \& $args[2] = $date; \& return $worksheet\->write_number( @args ); \& } \& else { \& \& # Not a valid date therefore write as a string \& return $worksheet\->write_string( @args ); \& } \& } \& else { \& \& # Shouldn\*(Aqt happen if the same match is used in the re and sub. \& return undef; \& } \& } \& \& \& # Write some dates in the user defined format \& $worksheet\->write( \*(AqA1\*(Aq, \*(Aq22/12/2004\*(Aq, $date_format ); \& $worksheet\->write( \*(AqA2\*(Aq, \*(Aq22/12/04\*(Aq, $date_format ); \& $worksheet\->write( \*(AqA3\*(Aq, \*(Aq2/12/04\*(Aq, $date_format ); \& $worksheet\->write( \*(AqA4\*(Aq, \*(Aq2/5/04\*(Aq, $date_format ); \& $worksheet\->write( \*(AqA5\*(Aq, \*(Aq2/5/95\*(Aq, $date_format ); \& $worksheet\->write( \*(AqA6\*(Aq, \*(Aq2/5/1995\*(Aq, $date_format ); \& \& # Some erroneous dates \& $worksheet\->write( \*(AqA8\*(Aq, \*(Aq2/5/1895\*(Aq, $date_format ); # Date out of Excel range \& $worksheet\->write( \*(AqA9\*(Aq, \*(Aq29/2/2003\*(Aq, $date_format ); # Invalid leap day \& $worksheet\->write( \*(AqA10\*(Aq, \*(Aq50/50/50\*(Aq, $date_format ); # Matches but isn\*(Aqt a date \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: write_to_scalar.pl" .IX Subsection "Example: write_to_scalar.pl" An example of writing an Excel::Writer::XLSX file to a perl scalar. .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # An example of writing an Excel::Writer::XLSX file to a perl scalar. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& # Use a scalar as a filehandle. \& open my $fh, \*(Aq>\*(Aq, \emy $str or die "Failed to open filehandle: $!"; \& \& \& # Spreadsheet::WriteExce accepts filehandle as well as file names. \& my $workbook = Excel::Writer::XLSX\->new( $fh ); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->write( 0, 0, \*(AqHi Excel!\*(Aq ); \& \& $workbook\->close(); \& \& \& # The Excel file in now in $str. Remember to binmode() the output \& # filehandle before printing it. \& open my $out_fh, \*(Aq>\*(Aq, \*(Aqwrite_to_scalar.xlsx\*(Aq \& or die "Failed to open out filehandle: $!"; \& \& binmode $out_fh; \& print $out_fh $str; \& close $out_fh; \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_2022_jp.pl" .IX Subsection "Example: unicode_2022_jp.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Japanese from a file with \s-1ISO\-2022\-JP\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Japanese from a file with ISO\-2022\-JP \& # encoded text. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_2022_jp.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 50 ); \& \& \& my $file = \*(Aqunicode_2022_jp.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(iso\-2022\-jp)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_8859_11.pl" .IX Subsection "Example: unicode_8859_11.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Thai from a file with \s-1ISO\-8859\-11\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Thai from a file with ISO\-8859\-11 encoded text. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_8859_11.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 50 ); \& \& \& my $file = \*(Aqunicode_8859_11.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(iso\-8859\-11)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_8859_7.pl" .IX Subsection "Example: unicode_8859_7.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Greek from a file with \s-1ISO\-8859\-7\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Greek from a file with ISO\-8859\-7 encoded text. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_8859_7.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 50 ); \& \& \& my $file = \*(Aqunicode_8859_7.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(iso\-8859\-7)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_big5.pl" .IX Subsection "Example: unicode_big5.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Chinese from a file with \s-1BIG5\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Chinese from a file with BIG5 encoded text. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_big5.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 80 ); \& \& \& my $file = \*(Aqunicode_big5.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(big5)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_cp1251.pl" .IX Subsection "Example: unicode_cp1251.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Russian from a file with \s-1CP1251\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Russian from a file with CP1251 encoded text. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_cp1251.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 50 ); \& \& \& my $file = \*(Aqunicode_cp1251.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(cp1251)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_cp1256.pl" .IX Subsection "Example: unicode_cp1256.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Arabic text from a \s-1CP\-1256\s0 encoded file. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Arabic text from a CP\-1256 encoded file. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_cp1256.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 50 ); \& \& \& my $file = \*(Aqunicode_cp1256.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(cp1256)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_cyrillic.pl" .IX Subsection "Example: unicode_cyrillic.pl" A simple example of writing some Russian cyrillic text using Excel::Writer::XLSX. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of writing some Russian cyrillic text using \& # Excel::Writer::XLSX. \& # \& # \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& # In this example we generate utf8 strings from character data but in a \& # real application we would expect them to come from an external source. \& # \& \& \& # Create a Russian worksheet name in utf8. \& my $sheet = pack "U*", 0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438, \& 0x0446, 0x0430; \& \& \& # Create a Russian string. \& my $str = pack "U*", 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, \& 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C, \& 0x0438, 0x0440, 0x0021; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_cyrillic.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet( $sheet . \*(Aq1\*(Aq ); \& \& $worksheet\->set_column( \*(AqA:A\*(Aq, 18 ); \& $worksheet\->write( \*(AqA1\*(Aq, $str ); \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_koi8r.pl" .IX Subsection "Example: unicode_koi8r.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Russian from a file with \s-1KOI8\-R\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Russian from a file with KOI8\-R encoded text. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_koi8r.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 50 ); \& \& \& my $file = \*(Aqunicode_koi8r.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(koi8\-r)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_polish_utf8.pl" .IX Subsection "Example: unicode_polish_utf8.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Polish from a file with \s-1UTF8\s0 encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Polish from a file with UTF8 encoded text. \& # \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_polish_utf8.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 50 ); \& \& \& my $file = \*(Aqunicode_polish_utf8.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(utf8)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SS "Example: unicode_shift_jis.pl" .IX Subsection "Example: unicode_shift_jis.pl" A simple example of converting some Unicode text to an Excel file using Excel::Writer::XLSX. .PP This example generates some Japenese text from a file with Shift-JIS encoded text. .PP Source code for this example: .PP .Vb 1 \& #!/usr/bin/perl \& \& ############################################################################## \& # \& # A simple example of converting some Unicode text to an Excel file using \& # Excel::Writer::XLSX. \& # \& # This example generates some Japenese text from a file with Shift\-JIS \& # encoded text. \& # \& # Copyright 2000\-2023, John McNamara, jmcnamara@cpan.org \& # \& \& use strict; \& use warnings; \& use Excel::Writer::XLSX; \& \& \& my $workbook = Excel::Writer::XLSX\->new( \*(Aqunicode_shift_jis.xlsx\*(Aq ); \& \& die "Couldn\*(Aqt create new Excel file: $!.\en" unless defined $workbook; \& \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->set_column( \*(AqA:A\*(Aq, 50 ); \& \& \& my $file = \*(Aqunicode_shift_jis.txt\*(Aq; \& \& open FH, \*(Aq<:encoding(shiftjis)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& \& while ( ) { \& next if /^#/; # Ignore the comments in the sample file. \& chomp; \& $worksheet\->write( $row++, 0, $_ ); \& } \& \& $workbook\->close(); \& \& _\|_END_\|_ .Ve .PP Download this example: .SH "AUTHOR" .IX Header "AUTHOR" John McNamara jmcnamara@cpan.org .PP Contributed examples contain the original author's name. .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2000\-2023, John McNamara. .PP All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.