1Excel::Writer::XLSX(3)User Contributed Perl DocumentationExcel::Writer::XLSX(3)
2
3
4
6 Excel::Writer::XLSX - Create a new file in the Excel 2007+ XLSX format.
7
9 To write a string, a formatted string, a number and a formula to the
10 first worksheet in an Excel workbook called perl.xlsx:
11
12 use Excel::Writer::XLSX;
13
14 # Create a new Excel workbook
15 my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );
16
17 # Add a worksheet
18 $worksheet = $workbook->add_worksheet();
19
20 # Add and define a format
21 $format = $workbook->add_format();
22 $format->set_bold();
23 $format->set_color( 'red' );
24 $format->set_align( 'center' );
25
26 # Write a formatted and unformatted string, row and column notation.
27 $col = $row = 0;
28 $worksheet->write( $row, $col, 'Hi Excel!', $format );
29 $worksheet->write( 1, $col, 'Hi Excel!' );
30
31 # Write a number and a formula using A1 notation
32 $worksheet->write( 'A3', 1.2345 );
33 $worksheet->write( 'A4', '=SIN(PI()/4)' );
34
35 $workbook->close();
36
38 The "Excel::Writer::XLSX" module can be used to create an Excel file in
39 the 2007+ XLSX format.
40
41 Multiple worksheets can be added to a workbook and formatting can be
42 applied to cells. Text, numbers, and formulas can be written to the
43 cells.
44
46 "Excel::Writer::XLSX" uses the same interface as the
47 Spreadsheet::WriteExcel module which produces an Excel file in binary
48 XLS format.
49
51 Excel::Writer::XLSX tries to provide an interface to as many of Excel's
52 features as possible. As a result there is a lot of documentation to
53 accompany the interface and it can be difficult at first glance to see
54 what it important and what is not. So for those of you who prefer to
55 assemble Ikea furniture first and then read the instructions, here are
56 four easy steps:
57
58 1. Create a new Excel workbook (i.e. file) using "new()".
59
60 2. Add a worksheet to the new workbook using "add_worksheet()".
61
62 3. Write to the worksheet using "write()".
63
64 4. "close()" the file.
65
66 Like this:
67
68 use Excel::Writer::XLSX; # Step 0
69
70 my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' ); # Step 1
71 $worksheet = $workbook->add_worksheet(); # Step 2
72 $worksheet->write( 'A1', 'Hi Excel!' ); # Step 3
73
74 $workbook->close(); # Step 4
75
76 This will create an Excel file called "perl.xlsx" with a single
77 worksheet and the text 'Hi Excel!' in the relevant cell. And that's it.
78 Okay, so there is actually a zeroth step as well, but "use module" goes
79 without saying. There are many examples that come with the distribution
80 and which you can use to get you started. See "EXAMPLES".
81
82 Those of you who read the instructions first and assemble the furniture
83 afterwards will know how to proceed. ;-)
84
86 The Excel::Writer::XLSX module provides an object oriented interface to
87 a new Excel workbook. The following methods are available through a new
88 workbook.
89
90 new()
91 add_worksheet()
92 add_format()
93 add_chart()
94 add_shape()
95 add_vba_project()
96 set_vba_name()
97 close()
98 set_properties()
99 set_custom_property()
100 define_name()
101 set_tempdir()
102 set_custom_color()
103 sheets()
104 get_worksheet_by_name()
105 set_1904()
106 set_optimization()
107 set_calc_mode()
108 get_default_url_format()
109 read_only_recommended()
110
111 If you are unfamiliar with object oriented interfaces or the way that
112 they are implemented in Perl have a look at "perlobj" and "perltoot" in
113 the main Perl documentation.
114
115 new()
116 A new Excel workbook is created using the "new()" constructor which
117 accepts either a filename or a filehandle as a parameter. The following
118 example creates a new Excel file based on a filename:
119
120 my $workbook = Excel::Writer::XLSX->new( 'filename.xlsx' );
121 my $worksheet = $workbook->add_worksheet();
122 $worksheet->write( 0, 0, 'Hi Excel!' );
123 $workbook->close();
124
125 Here are some other examples of using "new()" with filenames:
126
127 my $workbook1 = Excel::Writer::XLSX->new( $filename );
128 my $workbook2 = Excel::Writer::XLSX->new( '/tmp/filename.xlsx' );
129 my $workbook3 = Excel::Writer::XLSX->new( "c:\\tmp\\filename.xlsx" );
130 my $workbook4 = Excel::Writer::XLSX->new( 'c:\tmp\filename.xlsx' );
131
132 The last two examples demonstrates how to create a file on DOS or
133 Windows where it is necessary to either escape the directory separator
134 "\" or to use single quotes to ensure that it isn't interpolated. For
135 more information see "perlfaq5: Why can't I use "C:\temp\foo" in DOS
136 paths?".
137
138 It is recommended that the filename uses the extension ".xlsx" rather
139 than ".xls" since the latter causes an Excel warning when used with the
140 XLSX format.
141
142 The "new()" constructor returns a Excel::Writer::XLSX object that you
143 can use to add worksheets and store data. It should be noted that
144 although "my" is not specifically required it defines the scope of the
145 new workbook variable and, in the majority of cases, ensures that the
146 workbook is closed properly without explicitly calling the "close()"
147 method.
148
149 If the file cannot be created, due to file permissions or some other
150 reason, "new" will return "undef". Therefore, it is good practice to
151 check the return value of "new" before proceeding. As usual the Perl
152 variable $! will be set if there is a file creation error. You will
153 also see one of the warning messages detailed in "DIAGNOSTICS":
154
155 my $workbook = Excel::Writer::XLSX->new( 'protected.xlsx' );
156 die "Problems creating new Excel file: $!" unless defined $workbook;
157
158 You can also pass a valid filehandle to the "new()" constructor. For
159 example in a CGI program you could do something like this:
160
161 binmode( STDOUT );
162 my $workbook = Excel::Writer::XLSX->new( \*STDOUT );
163
164 The requirement for "binmode()" is explained below.
165
166 See also, the "cgi.pl" program in the "examples" directory of the
167 distro.
168
169 In "mod_perl" programs where you will have to do something like the
170 following:
171
172 # mod_perl 1
173 ...
174 tie *XLSX, 'Apache';
175 binmode( XLSX );
176 my $workbook = Excel::Writer::XLSX->new( \*XLSX );
177 ...
178
179 # mod_perl 2
180 ...
181 tie *XLSX => $r; # Tie to the Apache::RequestRec object
182 binmode( *XLSX );
183 my $workbook = Excel::Writer::XLSX->new( \*XLSX );
184 ...
185
186 See also, the "mod_perl1.pl" and "mod_perl2.pl" programs in the
187 "examples" directory of the distro.
188
189 Filehandles can also be useful if you want to stream an Excel file over
190 a socket or if you want to store an Excel file in a scalar.
191
192 For example here is a way to write an Excel file to a scalar:
193
194 #!/usr/bin/perl -w
195
196 use strict;
197 use Excel::Writer::XLSX;
198
199 open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
200
201 my $workbook = Excel::Writer::XLSX->new( $fh );
202 my $worksheet = $workbook->add_worksheet();
203
204 $worksheet->write( 0, 0, 'Hi Excel!' );
205
206 $workbook->close();
207
208 # The Excel file in now in $str. Remember to binmode() the output
209 # filehandle before printing it.
210 binmode STDOUT;
211 print $str;
212
213 See also the "write_to_scalar.pl" and "filehandle.pl" programs in the
214 "examples" directory of the distro.
215
216 Note about the requirement for "binmode()". An Excel file is comprised
217 of binary data. Therefore, if you are using a filehandle you should
218 ensure that you "binmode()" it prior to passing it to "new()".You
219 should do this regardless of whether you are on a Windows platform or
220 not.
221
222 You don't have to worry about "binmode()" if you are using filenames
223 instead of filehandles. Excel::Writer::XLSX performs the "binmode()"
224 internally when it converts the filename to a filehandle. For more
225 information about "binmode()" see "perlfunc" and "perlopentut" in the
226 main Perl documentation.
227
228 add_worksheet( $sheetname )
229 At least one worksheet should be added to a new workbook. A worksheet
230 is used to write data into cells:
231
232 $worksheet1 = $workbook->add_worksheet(); # Sheet1
233 $worksheet2 = $workbook->add_worksheet( 'Foglio2' ); # Foglio2
234 $worksheet3 = $workbook->add_worksheet( 'Data' ); # Data
235 $worksheet4 = $workbook->add_worksheet(); # Sheet4
236
237 If $sheetname is not specified the default Excel convention will be
238 followed, i.e. Sheet1, Sheet2, etc.
239
240 The worksheet name must be a valid Excel worksheet name, i.e:
241
242 • It must be less than 32 characters.
243
244 • It cannot contain any of the following characters: "[ ] : * ? / \"
245
246 • It cannot start or end with an apostrophe.
247
248 • It cannot be the same as an existing worksheet name (or a case
249 insensitive variant).
250
251 Note, the sheetname should not be "History" (case insensitive) which is
252 reserved in English language versions of Excel. Non-English versions
253 may have restrictions on the equivalent word.
254
255 See the Excel worksheet naming rules at
256 <https://support.office.com/en-ie/article/rename-a-worksheet-3f1f7148-ee83-404d-8ef0-9ff99fbad1f9>.
257
258 add_format( %properties )
259 The "add_format()" method can be used to create new Format objects
260 which are used to apply formatting to a cell. You can either define the
261 properties at creation time via a hash of property values or later via
262 method calls.
263
264 $format1 = $workbook->add_format( %props ); # Set properties at creation
265 $format2 = $workbook->add_format(); # Set properties later
266
267 See the "CELL FORMATTING" section for more details about Format
268 properties and how to set them.
269
270 add_chart( %properties )
271 This method is use to create a new chart either as a standalone
272 worksheet (the default) or as an embeddable object that can be inserted
273 into a worksheet via the "insert_chart()" Worksheet method.
274
275 my $chart = $workbook->add_chart( type => 'column' );
276
277 The properties that can be set are:
278
279 type (required)
280 subtype (optional)
281 name (optional)
282 embedded (optional)
283
284 • "type"
285
286 This is a required parameter. It defines the type of chart that
287 will be created.
288
289 my $chart = $workbook->add_chart( type => 'line' );
290
291 The available types are:
292
293 area
294 bar
295 column
296 line
297 pie
298 doughnut
299 scatter
300 stock
301
302 • "subtype"
303
304 Used to define a chart subtype where available.
305
306 my $chart = $workbook->add_chart( type => 'bar', subtype => 'stacked' );
307
308 See the Excel::Writer::XLSX::Chart documentation for a list of
309 available chart subtypes.
310
311 • "name"
312
313 Set the name for the chart sheet. The name property is optional and
314 if it isn't supplied will default to "Chart1 .. n". The name must
315 be a valid Excel worksheet name. See "add_worksheet()" for more
316 details on valid sheet names. The "name" property can be omitted
317 for embedded charts.
318
319 my $chart = $workbook->add_chart( type => 'line', name => 'Results Chart' );
320
321 • "embedded"
322
323 Specifies that the Chart object will be inserted in a worksheet via
324 the "insert_chart()" Worksheet method. It is an error to try insert
325 a Chart that doesn't have this flag set.
326
327 my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
328
329 # Configure the chart.
330 ...
331
332 # Insert the chart into the a worksheet.
333 $worksheet->insert_chart( 'E2', $chart );
334
335 See Excel::Writer::XLSX::Chart for details on how to configure the
336 chart object once it is created. See also the "chart_*.pl" programs in
337 the examples directory of the distro.
338
339 add_shape( %properties )
340 The "add_shape()" method can be used to create new shapes that may be
341 inserted into a worksheet.
342
343 You can either define the properties at creation time via a hash of
344 property values or later via method calls.
345
346 # Set properties at creation.
347 $plus = $workbook->add_shape(
348 type => 'plus',
349 id => 3,
350 width => $pw,
351 height => $ph
352 );
353
354
355 # Default rectangle shape. Set properties later.
356 $rect = $workbook->add_shape();
357
358 See Excel::Writer::XLSX::Shape for details on how to configure the
359 shape object once it is created.
360
361 See also the "shape*.pl" programs in the examples directory of the
362 distro.
363
364 add_vba_project( 'vbaProject.bin' )
365 The "add_vba_project()" method can be used to add macros or functions
366 to an Excel::Writer::XLSX file using a binary VBA project file that has
367 been extracted from an existing Excel "xlsm" file.
368
369 my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' );
370
371 $workbook->add_vba_project( './vbaProject.bin' );
372
373 The supplied "extract_vba" utility can be used to extract the required
374 "vbaProject.bin" file from an existing Excel file:
375
376 $ extract_vba file.xlsm
377 Extracted 'vbaProject.bin' successfully
378
379 Macros can be tied to buttons using the worksheet "insert_button()"
380 method (see the "WORKSHEET METHODS" section for details):
381
382 $worksheet->insert_button( 'C2', { macro => 'my_macro' } );
383
384 Note, Excel uses the file extension "xlsm" instead of "xlsx" for files
385 that contain macros. It is advisable to follow the same convention.
386
387 See also the "macros.pl" example file and the "WORKING WITH VBA
388 MACROS".
389
390 set_vba_name()
391 The "set_vba_name()" method can be used to set the VBA codename for the
392 workbook. This is sometimes required when a "vbaProject macro" included
393 via "add_vba_project()" refers to the workbook. The default Excel VBA
394 name of "ThisWorkbook" is used if a user defined name isn't specified.
395 See also "WORKING WITH VBA MACROS".
396
397 close()
398 In general your Excel file will be closed automatically when your
399 program ends or when the Workbook object goes out of scope. However it
400 is recommended to explicitly call the "close()" method close the Excel
401 file and avoid the potential issues outlined below. The "close()"
402 method is called like this:
403
404 $workbook->close();
405
406 The return value of "close()" is the same as that returned by perl when
407 it closes the file created by "new()". This allows you to handle error
408 conditions in the usual way:
409
410 $workbook->close() or die "Error closing file: $!";
411
412 An explicit "close()" is required if the file must be closed prior to
413 performing some external action on it such as copying it, reading its
414 size or attaching it to an email.
415
416 In addition, "close()" may be required to prevent perl's garbage
417 collector from disposing of the Workbook, Worksheet and Format objects
418 in the wrong order. Situations where this can occur are:
419
420 • If "my()" was not used to declare the scope of a workbook variable
421 created using "new()".
422
423 • If the "new()", "add_worksheet()" or "add_format()" methods are
424 called in subroutines.
425
426 The reason for this is that Excel::Writer::XLSX relies on Perl's
427 "DESTROY" mechanism to trigger destructor methods in a specific
428 sequence. This may not happen in cases where the Workbook, Worksheet
429 and Format variables are not lexically scoped or where they have
430 different lexical scopes.
431
432 To avoid these issues it is recommended that you always close the
433 Excel::Writer::XLSX filehandle using "close()".
434
435 set_size( $width, $height )
436 The "set_size()" method can be used to set the size of a workbook
437 window.
438
439 $workbook->set_size(1200, 800);
440
441 The Excel window size was used in Excel 2007 to define the width and
442 height of a workbook window within the Multiple Document Interface
443 (MDI). In later versions of Excel for Windows this interface was
444 dropped. This method is currently only useful when setting the window
445 size in Excel for Mac 2011. The units are pixels and the default size
446 is 1073 x 644.
447
448 Note, this doesn't equate exactly to the Excel for Mac pixel size since
449 it is based on the original Excel 2007 for Windows sizing.
450
451 set_tab_ratio( $tab_ratio )
452 The "set_tab_ratio()" method can be used to set the ratio between
453 worksheet tabs and the horizontal slider at the bottom of a workbook.
454 This can be increased to give more room to the tabs or reduced to
455 increase the size of the horizontal slider:
456
457 $workbook->set_tab_ratio(75);
458
459 The default value in Excel is 60.
460
461 set_properties()
462 The "set_properties" method can be used to set the document properties
463 of the Excel file created by "Excel::Writer::XLSX". These properties
464 are visible when you use the "Office Button -> Prepare -> Properties"
465 option in Excel and are also available to external applications that
466 read or index Windows files.
467
468 The properties should be passed in hash format as follows:
469
470 $workbook->set_properties(
471 title => 'This is an example spreadsheet',
472 author => 'John McNamara',
473 comments => 'Created with Perl and Excel::Writer::XLSX',
474 );
475
476 The properties that can be set are:
477
478 title
479 subject
480 author
481 manager
482 company
483 category
484 keywords
485 comments
486 status
487 hyperlink_base
488 created - File create date. Such be an aref of gmtime() values.
489
490 See also the "properties.pl" program in the examples directory of the
491 distro.
492
493 set_custom_property( $name, $value, $type)
494 The "set_custom_property" method can be used to set one of more custom
495 document properties not covered by the "set_properties()" method above.
496 These properties are visible when you use the "Office Button -> Prepare
497 -> Properties -> Advanced Properties -> Custom" option in Excel and are
498 also available to external applications that read or index Windows
499 files.
500
501 The "set_custom_property" method takes 3 parameters:
502
503 $workbook-> set_custom_property( $name, $value, $type);
504
505 Where the available types are:
506
507 text
508 date
509 number
510 bool
511
512 For example:
513
514 $workbook->set_custom_property( 'Checked by', 'Eve', 'text' );
515 $workbook->set_custom_property( 'Date completed', '2016-12-12T23:00:00Z', 'date' );
516 $workbook->set_custom_property( 'Document number', '12345' , 'number' );
517 $workbook->set_custom_property( 'Reference', '1.2345', 'number' );
518 $workbook->set_custom_property( 'Has review', 1, 'bool' );
519 $workbook->set_custom_property( 'Signed off', 0, 'bool' );
520 $workbook->set_custom_property( 'Department', $some_string, 'text' );
521 $workbook->set_custom_property( 'Scale', '1.2345678901234', 'number' );
522
523 Dates should by in ISO8601 "yyyy-mm-ddThh:mm:ss.sssZ" date format in
524 Zulu time, as shown above.
525
526 The "text" and "number" types are optional since they can usually be
527 inferred from the data:
528
529 $workbook->set_custom_property( 'Checked by', 'Eve' );
530 $workbook->set_custom_property( 'Reference', '1.2345' );
531
532 The $name and $value parameters are limited to 255 characters by Excel.
533
534 define_name()
535 This method is used to defined a name that can be used to represent a
536 value, a single cell or a range of cells in a workbook.
537
538 For example to set a global/workbook name:
539
540 # Global/workbook names.
541 $workbook->define_name( 'Exchange_rate', '=0.96' );
542 $workbook->define_name( 'Sales', '=Sheet1!$G$1:$H$10' );
543
544 It is also possible to define a local/worksheet name by prefixing the
545 name with the sheet name using the syntax "sheetname!definedname":
546
547 # Local/worksheet name.
548 $workbook->define_name( 'Sheet2!Sales', '=Sheet2!$G$1:$G$10' );
549
550 If the sheet name contains spaces or special characters you must
551 enclose it in single quotes like in Excel:
552
553 $workbook->define_name( "'New Data'!Sales", '=Sheet2!$G$1:$G$10' );
554
555 See the defined_name.pl program in the examples dir of the distro.
556
557 Refer to the following to see Excel's syntax rules for defined names:
558 <http://office.microsoft.com/en-001/excel-help/define-and-use-names-in-formulas-HA010147120.aspx#BMsyntax_rules_for_names>
559
560 set_tempdir()
561 "Excel::Writer::XLSX" stores worksheet data in temporary files prior to
562 assembling the final workbook.
563
564 The "File::Temp" module is used to create these temporary files.
565 File::Temp uses "File::Spec" to determine an appropriate location for
566 these files such as "/tmp" or "c:\windows\temp". You can find out which
567 directory is used on your system as follows:
568
569 perl -MFile::Spec -le "print File::Spec->tmpdir()"
570
571 If the default temporary file directory isn't accessible to your
572 application, or doesn't contain enough space, you can specify an
573 alternative location using the "set_tempdir()" method:
574
575 $workbook->set_tempdir( '/tmp/writeexcel' );
576 $workbook->set_tempdir( 'c:\windows\temp\writeexcel' );
577
578 The directory for the temporary file must exist, "set_tempdir()" will
579 not create a new directory.
580
581 set_custom_color( $index, $red, $green, $blue )
582 The method is maintained for backward compatibility with
583 Spreadsheet::WriteExcel. Excel::Writer::XLSX programs don't require
584 this method and colours can be specified using a Html style "#RRGGBB"
585 value, see "WORKING WITH COLOURS".
586
587 sheets( 0, 1, ... )
588 The "sheets()" method returns a list, or a sliced list, of the
589 worksheets in a workbook.
590
591 If no arguments are passed the method returns a list of all the
592 worksheets in the workbook. This is useful if you want to repeat an
593 operation on each worksheet:
594
595 for $worksheet ( $workbook->sheets() ) {
596 print $worksheet->get_name();
597 }
598
599 You can also specify a slice list to return one or more worksheet
600 objects:
601
602 $worksheet = $workbook->sheets( 0 );
603 $worksheet->write( 'A1', 'Hello' );
604
605 Or since the return value from "sheets()" is a reference to a worksheet
606 object you can write the above example as:
607
608 $workbook->sheets( 0 )->write( 'A1', 'Hello' );
609
610 The following example returns the first and last worksheet in a
611 workbook:
612
613 for $worksheet ( $workbook->sheets( 0, -1 ) ) {
614 # Do something
615 }
616
617 Array slices are explained in the "perldata" manpage.
618
619 get_worksheet_by_name()
620 The "get_worksheet_by_name()" function return a worksheet or chartsheet
621 object in the workbook using the sheetname:
622
623 $worksheet = $workbook->get_worksheet_by_name('Sheet1');
624
625 set_1904()
626 Excel stores dates as real numbers where the integer part stores the
627 number of days since the epoch and the fractional part stores the
628 percentage of the day. The epoch can be either 1900 or 1904. Excel for
629 Windows uses 1900 and Excel for Macintosh uses 1904. However, Excel on
630 either platform will convert automatically between one system and the
631 other.
632
633 Excel::Writer::XLSX stores dates in the 1900 format by default. If you
634 wish to change this you can call the "set_1904()" workbook method. You
635 can query the current value by calling the "get_1904()" workbook
636 method. This returns 0 for 1900 and 1 for 1904.
637
638 See also "DATES AND TIME IN EXCEL" for more information about working
639 with Excel's date system.
640
641 In general you probably won't need to use "set_1904()".
642
643 set_optimization()
644 The "set_optimization()" method is used to turn on optimizations in the
645 Excel::Writer::XLSX module. Currently there is only one optimization
646 available and that is to reduce memory usage.
647
648 $workbook->set_optimization();
649
650 See "SPEED AND MEMORY USAGE" for more background information.
651
652 Note, that with this optimization turned on a row of data is written
653 and then discarded when a cell in a new row is added via one of the
654 Worksheet "write_*()" methods. As such data should be written in
655 sequential row order once the optimization is turned on.
656
657 This method must be called before any calls to "add_worksheet()".
658
659 set_calc_mode( $mode )
660 Set the calculation mode for formulas in the workbook. This is mainly
661 of use for workbooks with slow formulas where you want to allow the
662 user to calculate them manually.
663
664 The mode parameter can be one of the following strings:
665
666 "auto"
667 The default. Excel will re-calculate formulas when a formula or a
668 value affecting the formula changes.
669
670 "manual"
671 Only re-calculate formulas when the user requires it. Generally by
672 pressing F9.
673
674 "auto_except_tables"
675 Excel will automatically re-calculate formulas except for tables.
676
677 get_default_url_format()
678 The "get_default_url_format()" method gets a copy of the default url
679 format used when a user defined format isn't specified with the
680 worksheet "write_url()" method. The format is the hyperlink style
681 defined by Excel for the default theme:
682
683 my $url_format = $workbook->get_default_url_format();
684
685 read_only_recommended()
686 The "read_only_recommended()" method can be used to set the Excel
687 "Read-only Recommended" option that is available when saving a file.
688 This presents the user of the file with an option to open it in "read-
689 only" mode. This means that any changes to the file can't be saved back
690 to the same file and must be saved to a new file. It can be set as
691 follows:
692
693 $workbook->read_only_recommended();
694
696 A new worksheet is created by calling the "add_worksheet()" method from
697 a workbook object:
698
699 $worksheet1 = $workbook->add_worksheet();
700 $worksheet2 = $workbook->add_worksheet();
701
702 The following methods are available through a new worksheet:
703
704 write()
705 write_number()
706 write_string()
707 write_rich_string()
708 keep_leading_zeros()
709 write_blank()
710 write_row()
711 write_col()
712 write_date_time()
713 write_url()
714 write_url_range()
715 write_formula()
716 write_boolean()
717 write_comment()
718 show_comments()
719 set_comments_author()
720 add_write_handler()
721 insert_image()
722 insert_chart()
723 insert_shape()
724 insert_button()
725 data_validation()
726 conditional_formatting()
727 add_sparkline()
728 add_table()
729 get_name()
730 activate()
731 select()
732 hide()
733 set_first_sheet()
734 protect()
735 unprotect_range()
736 set_selection()
737 set_row()
738 set_row_pixels()
739 set_default_row()
740 set_column()
741 set_column_pixels()
742 outline_settings()
743 freeze_panes()
744 split_panes()
745 merge_range()
746 merge_range_type()
747 set_zoom()
748 right_to_left()
749 hide_zero()
750 set_background()
751 set_tab_color()
752 autofilter()
753 filter_column()
754 filter_column_list()
755 set_vba_name()
756 ignore_errors()
757
758 Cell notation
759 Excel::Writer::XLSX supports two forms of notation to designate the
760 position of cells: Row-column notation and A1 notation.
761
762 Row-column notation uses a zero based index for both row and column
763 while A1 notation uses the standard Excel alphanumeric sequence of
764 column letter and 1-based row. For example:
765
766 (0, 0) # The top left cell in row-column notation.
767 ('A1') # The top left cell in A1 notation.
768
769 (1999, 29) # Row-column notation.
770 ('AD2000') # The same cell in A1 notation.
771
772 Row-column notation is useful if you are referring to cells
773 programmatically:
774
775 for my $i ( 0 .. 9 ) {
776 $worksheet->write( $i, 0, 'Hello' ); # Cells A1 to A10
777 }
778
779 A1 notation is useful for setting up a worksheet manually and for
780 working with formulas:
781
782 $worksheet->write( 'H1', 200 );
783 $worksheet->write( 'H2', '=H1+1' );
784
785 In formulas and applicable methods you can also use the "A:A" column
786 notation:
787
788 $worksheet->write( 'A1', '=SUM(B:B)' );
789
790 The "Excel::Writer::XLSX::Utility" module that is included in the
791 distro contains helper functions for dealing with A1 notation, for
792 example:
793
794 use Excel::Writer::XLSX::Utility;
795
796 ( $row, $col ) = xl_cell_to_rowcol( 'C2' ); # (1, 2)
797 $str = xl_rowcol_to_cell( 1, 2 ); # C2
798
799 For simplicity, the parameter lists for the worksheet method calls in
800 the following sections are given in terms of row-column notation. In
801 all cases it is also possible to use A1 notation.
802
803 Note: in Excel it is also possible to use a R1C1 notation. This is not
804 supported by Excel::Writer::XLSX.
805
806 write( $row, $column, $token, $format )
807 Excel makes a distinction between data types such as strings, numbers,
808 blanks, formulas and hyperlinks. To simplify the process of writing
809 data the "write()" method acts as a general alias for several more
810 specific methods:
811
812 write_string()
813 write_number()
814 write_blank()
815 write_formula()
816 write_url()
817 write_row()
818 write_col()
819
820 The general rule is that if the data looks like a something then a
821 something is written. Here are some examples in both row-column and A1
822 notation:
823
824 # Same as:
825 $worksheet->write( 0, 0, 'Hello' ); # write_string()
826 $worksheet->write( 1, 0, 'One' ); # write_string()
827 $worksheet->write( 2, 0, 2 ); # write_number()
828 $worksheet->write( 3, 0, 3.00001 ); # write_number()
829 $worksheet->write( 4, 0, "" ); # write_blank()
830 $worksheet->write( 5, 0, '' ); # write_blank()
831 $worksheet->write( 6, 0, undef ); # write_blank()
832 $worksheet->write( 7, 0 ); # write_blank()
833 $worksheet->write( 8, 0, 'http://www.perl.com/' ); # write_url()
834 $worksheet->write( 'A9', 'ftp://ftp.cpan.org/' ); # write_url()
835 $worksheet->write( 'A10', 'internal:Sheet1!A1' ); # write_url()
836 $worksheet->write( 'A11', 'external:c:\foo.xlsx' ); # write_url()
837 $worksheet->write( 'A12', '=A3 + 3*A4' ); # write_formula()
838 $worksheet->write( 'A13', '=SIN(PI()/4)' ); # write_formula()
839 $worksheet->write( 'A14', \@array ); # write_row()
840 $worksheet->write( 'A15', [\@array] ); # write_col()
841
842 # And if the keep_leading_zeros property is set:
843 $worksheet->write( 'A16', '2' ); # write_number()
844 $worksheet->write( 'A17', '02' ); # write_string()
845 $worksheet->write( 'A18', '00002' ); # write_string()
846
847 # Write an array formula. Not available in Spreadsheet::WriteExcel.
848 $worksheet->write( 'A19', '{=SUM(A1:B1*A2:B2)}' ); # write_formula()
849
850 The "looks like" rule is defined by regular expressions:
851
852 "write_number()" if $token is a number based on the following regex:
853 "$token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/".
854
855 "write_string()" if "keep_leading_zeros()" is set and $token is an
856 integer with leading zeros based on the following regex: "$token =~
857 /^0\d+$/".
858
859 "write_blank()" if $token is undef or a blank string: "undef", "" or
860 ''.
861
862 "write_url()" if $token is a http, https, ftp or mailto URL based on
863 the following regexes: "$token =~ m|^[fh]tt?ps?://|" or "$token =~
864 m|^mailto:|".
865
866 "write_url()" if $token is an internal or external sheet reference
867 based on the following regex: "$token =~ m[^(in|ex)ternal:]".
868
869 "write_formula()" if the first character of $token is "=".
870
871 "write_array_formula()" if the $token matches "/^{=.*}$/".
872
873 "write_row()" if $token is an array ref.
874
875 "write_col()" if $token is an array ref of array refs.
876
877 "write_string()" if none of the previous conditions apply.
878
879 The $format parameter is optional. It should be a valid Format object,
880 see "CELL FORMATTING":
881
882 my $format = $workbook->add_format();
883 $format->set_bold();
884 $format->set_color( 'red' );
885 $format->set_align( 'center' );
886
887 $worksheet->write( 4, 0, 'Hello', $format ); # Formatted string
888
889 The write() method will ignore empty strings or "undef" tokens unless a
890 format is also supplied. As such you needn't worry about special
891 handling for empty or "undef" values in your data. See also the
892 "write_blank()" method.
893
894 One problem with the "write()" method is that occasionally data looks
895 like a number but you don't want it treated as a number. For example,
896 zip codes or ID numbers often start with a leading zero. If you write
897 this data as a number then the leading zero(s) will be stripped. You
898 can change this default behaviour by using the "keep_leading_zeros()"
899 method. While this property is in place any integers with leading zeros
900 will be treated as strings and the zeros will be preserved. See the
901 "keep_leading_zeros()" section for a full discussion of this issue.
902
903 You can also add your own data handlers to the "write()" method using
904 "add_write_handler()".
905
906 The "write()" method will also handle Unicode strings in "UTF-8"
907 format.
908
909 The "write" methods return:
910
911 0 for success.
912 -1 for insufficient number of arguments.
913 -2 for row or column out of bounds.
914 -3 for string too long.
915
916 write_number( $row, $column, $number, $format )
917 Write an integer or a float to the cell specified by $row and $column:
918
919 $worksheet->write_number( 0, 0, 123456 );
920 $worksheet->write_number( 'A2', 2.3451 );
921
922 See the note about "Cell notation". The $format parameter is optional.
923
924 In general it is sufficient to use the "write()" method.
925
926 Note: some versions of Excel 2007 do not display the calculated values
927 of formulas written by Excel::Writer::XLSX. Applying all available
928 Service Packs to Excel should fix this.
929
930 write_string( $row, $column, $string, $format )
931 Write a string to the cell specified by $row and $column:
932
933 $worksheet->write_string( 0, 0, 'Your text here' );
934 $worksheet->write_string( 'A2', 'or here' );
935
936 The maximum string size is 32767 characters. However the maximum string
937 segment that Excel can display in a cell is 1000. All 32767 characters
938 can be displayed in the formula bar.
939
940 The $format parameter is optional.
941
942 The "write()" method will also handle strings in "UTF-8" format. See
943 also the "unicode_*.pl" programs in the examples directory of the
944 distro.
945
946 In general it is sufficient to use the "write()" method. However, you
947 may sometimes wish to use the "write_string()" method to write data
948 that looks like a number but that you don't want treated as a number.
949 For example, zip codes or phone numbers:
950
951 # Write as a plain string
952 $worksheet->write_string( 'A1', '01209' );
953
954 However, if the user edits this string Excel may convert it back to a
955 number. To get around this you can use the Excel text format "@":
956
957 # Format as a string. Doesn't change to a number when edited
958 my $format1 = $workbook->add_format( num_format => '@' );
959 $worksheet->write_string( 'A2', '01209', $format1 );
960
961 See also the note about "Cell notation".
962
963 write_rich_string( $row, $column, $format, $string, ..., $cell_format )
964 The "write_rich_string()" method is used to write strings with multiple
965 formats. For example to write the string "This is bold and this is
966 italic" you would use the following:
967
968 my $bold = $workbook->add_format( bold => 1 );
969 my $italic = $workbook->add_format( italic => 1 );
970
971 $worksheet->write_rich_string( 'A1',
972 'This is ', $bold, 'bold', ' and this is ', $italic, 'italic' );
973
974 The basic rule is to break the string into fragments and put a $format
975 object before the fragment that you want to format. For example:
976
977 # Unformatted string.
978 'This is an example string'
979
980 # Break it into fragments.
981 'This is an ', 'example', ' string'
982
983 # Add formatting before the fragments you want formatted.
984 'This is an ', $format, 'example', ' string'
985
986 # In Excel::Writer::XLSX.
987 $worksheet->write_rich_string( 'A1',
988 'This is an ', $format, 'example', ' string' );
989
990 String fragments that don't have a format are given a default format.
991 So for example when writing the string "Some bold text" you would use
992 the first example below but it would be equivalent to the second:
993
994 # With default formatting:
995 my $bold = $workbook->add_format( bold => 1 );
996
997 $worksheet->write_rich_string( 'A1',
998 'Some ', $bold, 'bold', ' text' );
999
1000 # Or more explicitly:
1001 my $bold = $workbook->add_format( bold => 1 );
1002 my $default = $workbook->add_format();
1003
1004 $worksheet->write_rich_string( 'A1',
1005 $default, 'Some ', $bold, 'bold', $default, ' text' );
1006
1007 As with Excel, only the font properties of the format such as font
1008 name, style, size, underline, color and effects are applied to the
1009 string fragments. Other features such as border, background, text wrap
1010 and alignment must be applied to the cell.
1011
1012 The "write_rich_string()" method allows you to do this by using the
1013 last argument as a cell format (if it is a format object). The
1014 following example centers a rich string in the cell:
1015
1016 my $bold = $workbook->add_format( bold => 1 );
1017 my $center = $workbook->add_format( align => 'center' );
1018
1019 $worksheet->write_rich_string( 'A5',
1020 'Some ', $bold, 'bold text', ' centered', $center );
1021
1022 See the "rich_strings.pl" example in the distro for more examples.
1023
1024 my $bold = $workbook->add_format( bold => 1 );
1025 my $italic = $workbook->add_format( italic => 1 );
1026 my $red = $workbook->add_format( color => 'red' );
1027 my $blue = $workbook->add_format( color => 'blue' );
1028 my $center = $workbook->add_format( align => 'center' );
1029 my $super = $workbook->add_format( font_script => 1 );
1030
1031
1032 # Write some strings with multiple formats.
1033 $worksheet->write_rich_string( 'A1',
1034 'This is ', $bold, 'bold', ' and this is ', $italic, 'italic' );
1035
1036 $worksheet->write_rich_string( 'A3',
1037 'This is ', $red, 'red', ' and this is ', $blue, 'blue' );
1038
1039 $worksheet->write_rich_string( 'A5',
1040 'Some ', $bold, 'bold text', ' centered', $center );
1041
1042 $worksheet->write_rich_string( 'A7',
1043 $italic, 'j = k', $super, '(n-1)', $center );
1044
1045 As with "write_sting()" the maximum string size is 32767 characters.
1046 See also the note about "Cell notation".
1047
1048 keep_leading_zeros()
1049 This method changes the default handling of integers with leading zeros
1050 when using the "write()" method.
1051
1052 The "write()" method uses regular expressions to determine what type of
1053 data to write to an Excel worksheet. If the data looks like a number it
1054 writes a number using "write_number()". One problem with this approach
1055 is that occasionally data looks like a number but you don't want it
1056 treated as a number.
1057
1058 Zip codes and ID numbers, for example, often start with a leading zero.
1059 If you write this data as a number then the leading zero(s) will be
1060 stripped. This is the also the default behaviour when you enter data
1061 manually in Excel.
1062
1063 To get around this you can use one of three options. Write a formatted
1064 number, write the number as a string or use the "keep_leading_zeros()"
1065 method to change the default behaviour of "write()":
1066
1067 # Implicitly write a number, the leading zero is removed: 1209
1068 $worksheet->write( 'A1', '01209' );
1069
1070 # Write a zero padded number using a format: 01209
1071 my $format1 = $workbook->add_format( num_format => '00000' );
1072 $worksheet->write( 'A2', '01209', $format1 );
1073
1074 # Write explicitly as a string: 01209
1075 $worksheet->write_string( 'A3', '01209' );
1076
1077 # Write implicitly as a string: 01209
1078 $worksheet->keep_leading_zeros();
1079 $worksheet->write( 'A4', '01209' );
1080
1081 The above code would generate a worksheet that looked like the
1082 following:
1083
1084 -----------------------------------------------------------
1085 | | A | B | C | D | ...
1086 -----------------------------------------------------------
1087 | 1 | 1209 | | | | ...
1088 | 2 | 01209 | | | | ...
1089 | 3 | 01209 | | | | ...
1090 | 4 | 01209 | | | | ...
1091
1092 The examples are on different sides of the cells due to the fact that
1093 Excel displays strings with a left justification and numbers with a
1094 right justification by default. You can change this by using a format
1095 to justify the data, see "CELL FORMATTING".
1096
1097 It should be noted that if the user edits the data in examples "A3" and
1098 "A4" the strings will revert back to numbers. Again this is Excel's
1099 default behaviour. To avoid this you can use the text format "@":
1100
1101 # Format as a string (01209)
1102 my $format2 = $workbook->add_format( num_format => '@' );
1103 $worksheet->write_string( 'A5', '01209', $format2 );
1104
1105 The "keep_leading_zeros()" property is off by default. The
1106 "keep_leading_zeros()" method takes 0 or 1 as an argument. It defaults
1107 to 1 if an argument isn't specified:
1108
1109 $worksheet->keep_leading_zeros(); # Set on
1110 $worksheet->keep_leading_zeros( 1 ); # Set on
1111 $worksheet->keep_leading_zeros( 0 ); # Set off
1112
1113 See also the "add_write_handler()" method.
1114
1115 write_blank( $row, $column, $format )
1116 Write a blank cell specified by $row and $column:
1117
1118 $worksheet->write_blank( 0, 0, $format );
1119
1120 This method is used to add formatting to a cell which doesn't contain a
1121 string or number value.
1122
1123 Excel differentiates between an "Empty" cell and a "Blank" cell. An
1124 "Empty" cell is a cell which doesn't contain data whilst a "Blank" cell
1125 is a cell which doesn't contain data but does contain formatting. Excel
1126 stores "Blank" cells but ignores "Empty" cells.
1127
1128 As such, if you write an empty cell without formatting it is ignored:
1129
1130 $worksheet->write( 'A1', undef, $format ); # write_blank()
1131 $worksheet->write( 'A2', undef ); # Ignored
1132
1133 This seemingly uninteresting fact means that you can write arrays of
1134 data without special treatment for "undef" or empty string values.
1135
1136 See the note about "Cell notation".
1137
1138 write_row( $row, $column, $array_ref, $format )
1139 The "write_row()" method can be used to write a 1D or 2D array of data
1140 in one go. This is useful for converting the results of a database
1141 query into an Excel worksheet. You must pass a reference to the array
1142 of data rather than the array itself. The "write()" method is then
1143 called for each element of the data. For example:
1144
1145 @array = ( 'awk', 'gawk', 'mawk' );
1146 $array_ref = \@array;
1147
1148 $worksheet->write_row( 0, 0, $array_ref );
1149
1150 # The above example is equivalent to:
1151 $worksheet->write( 0, 0, $array[0] );
1152 $worksheet->write( 0, 1, $array[1] );
1153 $worksheet->write( 0, 2, $array[2] );
1154
1155 Note: For convenience the "write()" method behaves in the same way as
1156 "write_row()" if it is passed an array reference. Therefore the
1157 following two method calls are equivalent:
1158
1159 $worksheet->write_row( 'A1', $array_ref ); # Write a row of data
1160 $worksheet->write( 'A1', $array_ref ); # Same thing
1161
1162 As with all of the write methods the $format parameter is optional. If
1163 a format is specified it is applied to all the elements of the data
1164 array.
1165
1166 Array references within the data will be treated as columns. This
1167 allows you to write 2D arrays of data in one go. For example:
1168
1169 @eec = (
1170 ['maggie', 'milly', 'molly', 'may' ],
1171 [13, 14, 15, 16 ],
1172 ['shell', 'star', 'crab', 'stone']
1173 );
1174
1175 $worksheet->write_row( 'A1', \@eec );
1176
1177 Would produce a worksheet as follows:
1178
1179 -----------------------------------------------------------
1180 | | A | B | C | D | E | ...
1181 -----------------------------------------------------------
1182 | 1 | maggie | 13 | shell | ... | ... | ...
1183 | 2 | milly | 14 | star | ... | ... | ...
1184 | 3 | molly | 15 | crab | ... | ... | ...
1185 | 4 | may | 16 | stone | ... | ... | ...
1186 | 5 | ... | ... | ... | ... | ... | ...
1187 | 6 | ... | ... | ... | ... | ... | ...
1188
1189 To write the data in a row-column order refer to the "write_col()"
1190 method below.
1191
1192 Any "undef" values in the data will be ignored unless a format is
1193 applied to the data, in which case a formatted blank cell will be
1194 written. In either case the appropriate row or column value will still
1195 be incremented.
1196
1197 To find out more about array references refer to "perlref" and
1198 "perlreftut" in the main Perl documentation. To find out more about 2D
1199 arrays or "lists of lists" refer to "perllol".
1200
1201 The "write_row()" method returns the first error encountered when
1202 writing the elements of the data or zero if no errors were encountered.
1203 See the return values described for the "write()" method above.
1204
1205 See also the "write_arrays.pl" program in the "examples" directory of
1206 the distro.
1207
1208 The "write_row()" method allows the following idiomatic conversion of a
1209 text file to an Excel file:
1210
1211 #!/usr/bin/perl -w
1212
1213 use strict;
1214 use Excel::Writer::XLSX;
1215
1216 my $workbook = Excel::Writer::XLSX->new( 'file.xlsx' );
1217 my $worksheet = $workbook->add_worksheet();
1218
1219 open INPUT, 'file.txt' or die "Couldn't open file: $!";
1220
1221 $worksheet->write( $. -1, 0, [split] ) while <INPUT>;
1222
1223 $workbook->close();
1224
1225 write_col( $row, $column, $array_ref, $format )
1226 The "write_col()" method can be used to write a 1D or 2D array of data
1227 in one go. This is useful for converting the results of a database
1228 query into an Excel worksheet. You must pass a reference to the array
1229 of data rather than the array itself. The "write()" method is then
1230 called for each element of the data. For example:
1231
1232 @array = ( 'awk', 'gawk', 'mawk' );
1233 $array_ref = \@array;
1234
1235 $worksheet->write_col( 0, 0, $array_ref );
1236
1237 # The above example is equivalent to:
1238 $worksheet->write( 0, 0, $array[0] );
1239 $worksheet->write( 1, 0, $array[1] );
1240 $worksheet->write( 2, 0, $array[2] );
1241
1242 As with all of the write methods the $format parameter is optional. If
1243 a format is specified it is applied to all the elements of the data
1244 array.
1245
1246 Array references within the data will be treated as rows. This allows
1247 you to write 2D arrays of data in one go. For example:
1248
1249 @eec = (
1250 ['maggie', 'milly', 'molly', 'may' ],
1251 [13, 14, 15, 16 ],
1252 ['shell', 'star', 'crab', 'stone']
1253 );
1254
1255 $worksheet->write_col( 'A1', \@eec );
1256
1257 Would produce a worksheet as follows:
1258
1259 -----------------------------------------------------------
1260 | | A | B | C | D | E | ...
1261 -----------------------------------------------------------
1262 | 1 | maggie | milly | molly | may | ... | ...
1263 | 2 | 13 | 14 | 15 | 16 | ... | ...
1264 | 3 | shell | star | crab | stone | ... | ...
1265 | 4 | ... | ... | ... | ... | ... | ...
1266 | 5 | ... | ... | ... | ... | ... | ...
1267 | 6 | ... | ... | ... | ... | ... | ...
1268
1269 To write the data in a column-row order refer to the "write_row()"
1270 method above.
1271
1272 Any "undef" values in the data will be ignored unless a format is
1273 applied to the data, in which case a formatted blank cell will be
1274 written. In either case the appropriate row or column value will still
1275 be incremented.
1276
1277 As noted above the "write()" method can be used as a synonym for
1278 "write_row()" and "write_row()" handles nested array refs as columns.
1279 Therefore, the following two method calls are equivalent although the
1280 more explicit call to "write_col()" would be preferable for
1281 maintainability:
1282
1283 $worksheet->write_col( 'A1', $array_ref ); # Write a column of data
1284 $worksheet->write( 'A1', [ $array_ref ] ); # Same thing
1285
1286 To find out more about array references refer to "perlref" and
1287 "perlreftut" in the main Perl documentation. To find out more about 2D
1288 arrays or "lists of lists" refer to "perllol".
1289
1290 The "write_col()" method returns the first error encountered when
1291 writing the elements of the data or zero if no errors were encountered.
1292 See the return values described for the "write()" method above.
1293
1294 See also the "write_arrays.pl" program in the "examples" directory of
1295 the distro.
1296
1297 write_date_time( $row, $col, $date_string, $format )
1298 The "write_date_time()" method can be used to write a date or time to
1299 the cell specified by $row and $column:
1300
1301 $worksheet->write_date_time( 'A1', '2004-05-13T23:20', $date_format );
1302
1303 The $date_string should be in the following format:
1304
1305 yyyy-mm-ddThh:mm:ss.sss
1306
1307 This conforms to an ISO8601 date but it should be noted that the full
1308 range of ISO8601 formats are not supported.
1309
1310 The following variations on the $date_string parameter are permitted:
1311
1312 yyyy-mm-ddThh:mm:ss.sss # Standard format
1313 yyyy-mm-ddT # No time
1314 Thh:mm:ss.sss # No date
1315 yyyy-mm-ddThh:mm:ss.sssZ # Additional Z (but not time zones)
1316 yyyy-mm-ddThh:mm:ss # No fractional seconds
1317 yyyy-mm-ddThh:mm # No seconds
1318
1319 Note that the "T" is required in all cases.
1320
1321 A date should always have a $format, otherwise it will appear as a
1322 number, see "DATES AND TIME IN EXCEL" and "CELL FORMATTING". Here is a
1323 typical example:
1324
1325 my $date_format = $workbook->add_format( num_format => 'mm/dd/yy' );
1326 $worksheet->write_date_time( 'A1', '2004-05-13T23:20', $date_format );
1327
1328 Valid dates should be in the range 1900-01-01 to 9999-12-31, for the
1329 1900 epoch and 1904-01-01 to 9999-12-31, for the 1904 epoch. As with
1330 Excel, dates outside these ranges will be written as a string.
1331
1332 See also the date_time.pl program in the "examples" directory of the
1333 distro.
1334
1335 write_url( $row, $col, $url, $format, $label )
1336 Write a hyperlink to a URL in the cell specified by $row and $column.
1337 The hyperlink is comprised of two elements: the visible label and the
1338 invisible link. The visible label is the same as the link unless an
1339 alternative label is specified. The $label parameter is optional. The
1340 label is written using the "write()" method. Therefore it is possible
1341 to write strings, numbers or formulas as labels.
1342
1343 The $format parameter is also optional and the default Excel hyperlink
1344 style will be used if it isn't specified. If required you can access
1345 the default url format using the Workbook "get_default_url_format"
1346 method:
1347
1348 my $url_format = $workbook->get_default_url_format();
1349
1350 There are four web style URI's supported: "http://", "https://",
1351 "ftp://" and "mailto:":
1352
1353 $worksheet->write_url( 0, 0, 'ftp://www.perl.org/' );
1354 $worksheet->write_url( 'A3', 'http://www.perl.com/' );
1355 $worksheet->write_url( 'A4', 'mailto:jmcnamara@cpan.org' );
1356
1357 You can display an alternative string using the $label parameter:
1358
1359 $worksheet->write_url( 1, 0, 'http://www.perl.com/', undef, 'Perl' );
1360
1361 If you wish to have some other cell data such as a number or a formula
1362 you can overwrite the cell using another call to "write_*()":
1363
1364 $worksheet->write_url( 'A1', 'http://www.perl.com/' );
1365
1366 # Overwrite the URL string with a formula. The cell is still a link.
1367 # Note the use of the default url format for consistency with other links.
1368 my $url_format = $workbook->get_default_url_format();
1369 $worksheet->write_formula( 'A1', '=1+1', $url_format );
1370
1371 There are two local URIs supported: "internal:" and "external:". These
1372 are used for hyperlinks to internal worksheet references or external
1373 workbook and worksheet references:
1374
1375 $worksheet->write_url( 'A6', 'internal:Sheet2!A1' );
1376 $worksheet->write_url( 'A7', 'internal:Sheet2!A1' );
1377 $worksheet->write_url( 'A8', 'internal:Sheet2!A1:B2' );
1378 $worksheet->write_url( 'A9', q{internal:'Sales Data'!A1} );
1379 $worksheet->write_url( 'A10', 'external:c:\temp\foo.xlsx' );
1380 $worksheet->write_url( 'A11', 'external:c:\foo.xlsx#Sheet2!A1' );
1381 $worksheet->write_url( 'A12', 'external:..\foo.xlsx' );
1382 $worksheet->write_url( 'A13', 'external:..\foo.xlsx#Sheet2!A1' );
1383 $worksheet->write_url( 'A13', 'external:\\\\NET\share\foo.xlsx' );
1384
1385 All of the these URI types are recognised by the "write()" method, see
1386 above.
1387
1388 Worksheet references are typically of the form "Sheet1!A1". You can
1389 also refer to a worksheet range using the standard Excel notation:
1390 "Sheet1!A1:B2".
1391
1392 In external links the workbook and worksheet name must be separated by
1393 the "#" character: "external:Workbook.xlsx#Sheet1!A1'".
1394
1395 You can also link to a named range in the target worksheet. For example
1396 say you have a named range called "my_name" in the workbook
1397 "c:\temp\foo.xlsx" you could link to it as follows:
1398
1399 $worksheet->write_url( 'A14', 'external:c:\temp\foo.xlsx#my_name' );
1400
1401 Excel requires that worksheet names containing spaces or non
1402 alphanumeric characters are single quoted as follows "'Sales Data'!A1".
1403 If you need to do this in a single quoted string then you can either
1404 escape the single quotes "\'" or use the quote operator "q{}" as
1405 described in "perlop" in the main Perl documentation.
1406
1407 Links to network files are also supported. MS/Novell Network files
1408 normally begin with two back slashes as follows "\\NETWORK\etc". In
1409 order to generate this in a single or double quoted string you will
1410 have to escape the backslashes, '\\\\NETWORK\etc'.
1411
1412 If you are using double quote strings then you should be careful to
1413 escape anything that looks like a metacharacter. For more information
1414 see "perlfaq5: Why can't I use "C:\temp\foo" in DOS paths?".
1415
1416 Finally, you can avoid most of these quoting problems by using forward
1417 slashes. These are translated internally to backslashes:
1418
1419 $worksheet->write_url( 'A14', "external:c:/temp/foo.xlsx" );
1420 $worksheet->write_url( 'A15', 'external://NETWORK/share/foo.xlsx' );
1421
1422 Note: Excel::Writer::XLSX will escape the following characters in URLs
1423 as required by Excel: "\s " < > \ [ ] ` ^ { }" unless the URL already
1424 contains %xx style escapes. In which case it is assumed that the URL
1425 was escaped correctly by the user and will by passed directly to Excel.
1426
1427 Versions of Excel prior to Excel 2015 limited hyperlink links and
1428 anchor/locations to 255 characters each. Versions after that support
1429 urls up to 2079 characters. Excel::Writer::XLSX versions >= 1.0.2
1430 support the new longer limit by default.
1431
1432 See also, the note about "Cell notation".
1433
1434 write_formula( $row, $column, $formula, $format, $value )
1435 Write a formula or function to the cell specified by $row and $column:
1436
1437 $worksheet->write_formula( 0, 0, '=$B$3 + B4' );
1438 $worksheet->write_formula( 1, 0, '=SIN(PI()/4)' );
1439 $worksheet->write_formula( 2, 0, '=SUM(B1:B5)' );
1440 $worksheet->write_formula( 'A4', '=IF(A3>1,"Yes", "No")' );
1441 $worksheet->write_formula( 'A5', '=AVERAGE(1, 2, 3, 4)' );
1442 $worksheet->write_formula( 'A6', '=DATEVALUE("1-Jan-2001")' );
1443
1444 Array formulas are also supported:
1445
1446 $worksheet->write_formula( 'A7', '{=SUM(A1:B1*A2:B2)}' );
1447
1448 See also the "write_array_formula()" method below.
1449
1450 See the note about "Cell notation". For more information about writing
1451 Excel formulas see "FORMULAS AND FUNCTIONS IN EXCEL"
1452
1453 If required, it is also possible to specify the calculated value of the
1454 formula. This is occasionally necessary when working with non-Excel
1455 applications that don't calculate the value of the formula. The
1456 calculated $value is added at the end of the argument list:
1457
1458 $worksheet->write( 'A1', '=2+2', $format, 4 );
1459
1460 However, this probably isn't something that you will ever need to do.
1461 If you do use this feature then do so with care.
1462
1463 write_array_formula($first_row, $first_col, $last_row, $last_col, $formula,
1464 $format, $value)
1465 Write an array formula to a cell range. In Excel an array formula is a
1466 formula that performs a calculation on a set of values. It can return a
1467 single value or a range of values.
1468
1469 An array formula is indicated by a pair of braces around the formula:
1470 "{=SUM(A1:B1*A2:B2)}". If the array formula returns a single value
1471 then the $first_ and $last_ parameters should be the same:
1472
1473 $worksheet->write_array_formula('A1:A1', '{=SUM(B1:C1*B2:C2)}');
1474
1475 It this case however it is easier to just use the "write_formula()" or
1476 "write()" methods:
1477
1478 # Same as above but more concise.
1479 $worksheet->write( 'A1', '{=SUM(B1:C1*B2:C2)}' );
1480 $worksheet->write_formula( 'A1', '{=SUM(B1:C1*B2:C2)}' );
1481
1482 For array formulas that return a range of values you must specify the
1483 range that the return values will be written to:
1484
1485 $worksheet->write_array_formula( 'A1:A3', '{=TREND(C1:C3,B1:B3)}' );
1486 $worksheet->write_array_formula( 0, 0, 2, 0, '{=TREND(C1:C3,B1:B3)}' );
1487
1488 If required, it is also possible to specify the calculated value of the
1489 formula. This is occasionally necessary when working with non-Excel
1490 applications that don't calculate the value of the formula. However,
1491 using this parameter only writes a single value to the upper left cell
1492 in the result array. For a multi-cell array formula where the results
1493 are required, the other result values can be specified by using
1494 "write_number()" to write to the appropriate cell:
1495
1496 # Specify the result for a single cell range.
1497 $worksheet->write_array_formula( 'A1:A3', '{=SUM(B1:C1*B2:C2)}, $format, 2005 );
1498
1499 # Specify the results for a multi cell range.
1500 $worksheet->write_array_formula( 'A1:A3', '{=TREND(C1:C3,B1:B3)}', $format, 105 );
1501 $worksheet->write_number( 'A2', 12, format );
1502 $worksheet->write_number( 'A3', 14, format );
1503
1504 In addition, some early versions of Excel 2007 don't calculate the
1505 values of array formulas when they aren't supplied. Installing the
1506 latest Office Service Pack should fix this issue.
1507
1508 See also the "array_formula.pl" program in the "examples" directory of
1509 the distro.
1510
1511 Note: Array formulas are not supported by Spreadsheet::WriteExcel.
1512
1513 write_boolean( $row, $column, $value, $format )
1514 Write an Excel boolean value to the cell specified by $row and $column:
1515
1516 $worksheet->write_boolean( 'A1', 1 ); # TRUE
1517 $worksheet->write_boolean( 'A2', 0 ); # FALSE
1518 $worksheet->write_boolean( 'A3', undef ); # FALSE
1519 $worksheet->write_boolean( 'A3', 0, $format ); # FALSE, with format.
1520
1521 A $value that is true or false using Perl's rules will be written as an
1522 Excel boolean "TRUE" or "FALSE" value.
1523
1524 See the note about "Cell notation".
1525
1526 store_formula( $formula )
1527 Deprecated. This is a Spreadsheet::WriteExcel method that is no longer
1528 required by Excel::Writer::XLSX. See below.
1529
1530 repeat_formula( $row, $col, $formula, $format )
1531 Deprecated. This is a Spreadsheet::WriteExcel method that is no longer
1532 required by Excel::Writer::XLSX.
1533
1534 In Spreadsheet::WriteExcel it was computationally expensive to write
1535 formulas since they were parsed by a recursive descent parser. The
1536 "store_formula()" and "repeat_formula()" methods were used as a way of
1537 avoiding the overhead of repeated formulas by reusing a pre-parsed
1538 formula.
1539
1540 In Excel::Writer::XLSX this is no longer necessary since it is just as
1541 quick to write a formula as it is to write a string or a number.
1542
1543 The methods remain for backward compatibility but new
1544 Excel::Writer::XLSX programs shouldn't use them.
1545
1546 write_comment( $row, $column, $string, ... )
1547 The "write_comment()" method is used to add a comment to a cell. A cell
1548 comment is indicated in Excel by a small red triangle in the upper
1549 right-hand corner of the cell. Moving the cursor over the red triangle
1550 will reveal the comment.
1551
1552 The following example shows how to add a comment to a cell:
1553
1554 $worksheet->write ( 2, 2, 'Hello' );
1555 $worksheet->write_comment( 2, 2, 'This is a comment.' );
1556
1557 As usual you can replace the $row and $column parameters with an "A1"
1558 cell reference. See the note about "Cell notation".
1559
1560 $worksheet->write ( 'C3', 'Hello');
1561 $worksheet->write_comment( 'C3', 'This is a comment.' );
1562
1563 The "write_comment()" method will also handle strings in "UTF-8"
1564 format.
1565
1566 $worksheet->write_comment( 'C3', "\x{263a}" ); # Smiley
1567 $worksheet->write_comment( 'C4', 'Comment ca va?' );
1568
1569 In addition to the basic 3 argument form of "write_comment()" you can
1570 pass in several optional key/value pairs to control the format of the
1571 comment. For example:
1572
1573 $worksheet->write_comment( 'C3', 'Hello', visible => 1, author => 'Perl' );
1574
1575 Most of these options are quite specific and in general the default
1576 comment behaves will be all that you need. However, should you need
1577 greater control over the format of the cell comment the following
1578 options are available:
1579
1580 author
1581 visible
1582 x_scale
1583 width
1584 y_scale
1585 height
1586 color
1587 start_cell
1588 start_row
1589 start_col
1590 x_offset
1591 y_offset
1592 font
1593 font_size
1594
1595 Option: author
1596 This option is used to indicate who is the author of the cell
1597 comment. Excel displays the author of the comment in the status bar
1598 at the bottom of the worksheet. This is usually of interest in
1599 corporate environments where several people might review and
1600 provide comments to a workbook.
1601
1602 $worksheet->write_comment( 'C3', 'Atonement', author => 'Ian McEwan' );
1603
1604 The default author for all cell comments can be set using the
1605 "set_comments_author()" method (see below).
1606
1607 $worksheet->set_comments_author( 'Perl' );
1608
1609 Option: visible
1610 This option is used to make a cell comment visible when the
1611 worksheet is opened. The default behaviour in Excel is that
1612 comments are initially hidden. However, it is also possible in
1613 Excel to make individual or all comments visible. In
1614 Excel::Writer::XLSX individual comments can be made visible as
1615 follows:
1616
1617 $worksheet->write_comment( 'C3', 'Hello', visible => 1 );
1618
1619 It is possible to make all comments in a worksheet visible using
1620 the "show_comments()" worksheet method (see below). Alternatively,
1621 if all of the cell comments have been made visible you can hide
1622 individual comments:
1623
1624 $worksheet->write_comment( 'C3', 'Hello', visible => 0 );
1625
1626 Option: x_scale
1627 This option is used to set the width of the cell comment box as a
1628 factor of the default width.
1629
1630 $worksheet->write_comment( 'C3', 'Hello', x_scale => 2 );
1631 $worksheet->write_comment( 'C4', 'Hello', x_scale => 4.2 );
1632
1633 Option: width
1634 This option is used to set the width of the cell comment box
1635 explicitly in pixels.
1636
1637 $worksheet->write_comment( 'C3', 'Hello', width => 200 );
1638
1639 Option: y_scale
1640 This option is used to set the height of the cell comment box as a
1641 factor of the default height.
1642
1643 $worksheet->write_comment( 'C3', 'Hello', y_scale => 2 );
1644 $worksheet->write_comment( 'C4', 'Hello', y_scale => 4.2 );
1645
1646 Option: height
1647 This option is used to set the height of the cell comment box
1648 explicitly in pixels.
1649
1650 $worksheet->write_comment( 'C3', 'Hello', height => 200 );
1651
1652 Option: color
1653 This option is used to set the background colour of cell comment
1654 box. You can use one of the named colours recognised by
1655 Excel::Writer::XLSX or a Html style "#RRGGBB" colour. See "WORKING
1656 WITH COLOURS".
1657
1658 $worksheet->write_comment( 'C3', 'Hello', color => 'green' );
1659 $worksheet->write_comment( 'C4', 'Hello', color => '#FF6600' ); # Orange
1660
1661 Option: start_cell
1662 This option is used to set the cell in which the comment will
1663 appear. By default Excel displays comments one cell to the right
1664 and one cell above the cell to which the comment relates. However,
1665 you can change this behaviour if you wish. In the following example
1666 the comment which would appear by default in cell "D2" is moved to
1667 "E2".
1668
1669 $worksheet->write_comment( 'C3', 'Hello', start_cell => 'E2' );
1670
1671 Option: start_row
1672 This option is used to set the row in which the comment will
1673 appear. See the "start_cell" option above. The row is zero indexed.
1674
1675 $worksheet->write_comment( 'C3', 'Hello', start_row => 0 );
1676
1677 Option: start_col
1678 This option is used to set the column in which the comment will
1679 appear. See the "start_cell" option above. The column is zero
1680 indexed.
1681
1682 $worksheet->write_comment( 'C3', 'Hello', start_col => 4 );
1683
1684 Option: x_offset
1685 This option is used to change the x offset, in pixels, of a comment
1686 within a cell:
1687
1688 $worksheet->write_comment( 'C3', $comment, x_offset => 30 );
1689
1690 Option: y_offset
1691 This option is used to change the y offset, in pixels, of a comment
1692 within a cell:
1693
1694 $worksheet->write_comment('C3', $comment, x_offset => 30);
1695
1696 Option: font
1697 This option is used to change the font used in the comment from
1698 'Tahoma' which is the default.
1699
1700 $worksheet->write_comment('C3', $comment, font => 'Calibri');
1701
1702 Option: font_size
1703 This option is used to change the font size used in the comment
1704 from 8 which is the default.
1705
1706 $worksheet->write_comment('C3', $comment, font_size => 20);
1707
1708 You can apply as many of these options as you require.
1709
1710 Note about using options that adjust the position of the cell comment
1711 such as start_cell, start_row, start_col, x_offset and y_offset: Excel
1712 only displays offset cell comments when they are displayed as
1713 "visible". Excel does not display hidden cells as moved when you mouse
1714 over them.
1715
1716 Note about row height and comments. If you specify the height of a row
1717 that contains a comment then Excel::Writer::XLSX will adjust the height
1718 of the comment to maintain the default or user specified dimensions.
1719 However, the height of a row can also be adjusted automatically by
1720 Excel if the text wrap property is set or large fonts are used in the
1721 cell. This means that the height of the row is unknown to the module at
1722 run time and thus the comment box is stretched with the row. Use the
1723 "set_row()" method to specify the row height explicitly and avoid this
1724 problem.
1725
1726 show_comments()
1727 This method is used to make all cell comments visible when a worksheet
1728 is opened.
1729
1730 $worksheet->show_comments();
1731
1732 Individual comments can be made visible using the "visible" parameter
1733 of the "write_comment" method (see above):
1734
1735 $worksheet->write_comment( 'C3', 'Hello', visible => 1 );
1736
1737 If all of the cell comments have been made visible you can hide
1738 individual comments as follows:
1739
1740 $worksheet->show_comments();
1741 $worksheet->write_comment( 'C3', 'Hello', visible => 0 );
1742
1743 set_comments_author()
1744 This method is used to set the default author of all cell comments.
1745
1746 $worksheet->set_comments_author( 'Perl' );
1747
1748 Individual comment authors can be set using the "author" parameter of
1749 the "write_comment" method (see above).
1750
1751 The default comment author is an empty string, '', if no author is
1752 specified.
1753
1754 add_write_handler( $re, $code_ref )
1755 This method is used to extend the Excel::Writer::XLSX write() method to
1756 handle user defined data.
1757
1758 If you refer to the section on "write()" above you will see that it
1759 acts as an alias for several more specific "write_*" methods. However,
1760 it doesn't always act in exactly the way that you would like it to.
1761
1762 One solution is to filter the input data yourself and call the
1763 appropriate "write_*" method. Another approach is to use the
1764 "add_write_handler()" method to add your own automated behaviour to
1765 "write()".
1766
1767 The "add_write_handler()" method take two arguments, $re, a regular
1768 expression to match incoming data and $code_ref a callback function to
1769 handle the matched data:
1770
1771 $worksheet->add_write_handler( qr/^\d\d\d\d$/, \&my_write );
1772
1773 (In the these examples the "qr" operator is used to quote the regular
1774 expression strings, see perlop for more details).
1775
1776 The method is used as follows. say you wished to write 7 digit ID
1777 numbers as a string so that any leading zeros were preserved*, you
1778 could do something like the following:
1779
1780 $worksheet->add_write_handler( qr/^\d{7}$/, \&write_my_id );
1781
1782
1783 sub write_my_id {
1784 my $worksheet = shift;
1785 return $worksheet->write_string( @_ );
1786 }
1787
1788 * You could also use the "keep_leading_zeros()" method for this.
1789
1790 Then if you call "write()" with an appropriate string it will be
1791 handled automatically:
1792
1793 # Writes 0000000. It would normally be written as a number; 0.
1794 $worksheet->write( 'A1', '0000000' );
1795
1796 The callback function will receive a reference to the calling worksheet
1797 and all of the other arguments that were passed to "write()". The
1798 callback will see an @_ argument list that looks like the following:
1799
1800 $_[0] A ref to the calling worksheet. *
1801 $_[1] Zero based row number.
1802 $_[2] Zero based column number.
1803 $_[3] A number or string or token.
1804 $_[4] A format ref if any.
1805 $_[5] Any other arguments.
1806 ...
1807
1808 * It is good style to shift this off the list so the @_ is the same
1809 as the argument list seen by write().
1810
1811 Your callback should "return()" the return value of the "write_*"
1812 method that was called or "undef" to indicate that you rejected the
1813 match and want "write()" to continue as normal.
1814
1815 So for example if you wished to apply the previous filter only to ID
1816 values that occur in the first column you could modify your callback
1817 function as follows:
1818
1819 sub write_my_id {
1820 my $worksheet = shift;
1821 my $col = $_[1];
1822
1823 if ( $col == 0 ) {
1824 return $worksheet->write_string( @_ );
1825 }
1826 else {
1827 # Reject the match and return control to write()
1828 return undef;
1829 }
1830 }
1831
1832 Now, you will get different behaviour for the first column and other
1833 columns:
1834
1835 $worksheet->write( 'A1', '0000000' ); # Writes 0000000
1836 $worksheet->write( 'B1', '0000000' ); # Writes 0
1837
1838 You may add more than one handler in which case they will be called in
1839 the order that they were added.
1840
1841 Note, the "add_write_handler()" method is particularly suited for
1842 handling dates.
1843
1844 See the "write_handler 1-4" programs in the "examples" directory for
1845 further examples.
1846
1847 insert_image( $row, $col, $filename, { %options } )
1848 This method can be used to insert a image into a worksheet. The image
1849 can be in PNG, JPEG, GIF or BMP format.
1850
1851 $worksheet1->insert_image( 'A1', 'perl.bmp' );
1852 $worksheet2->insert_image( 'A1', '../images/perl.bmp' );
1853 $worksheet3->insert_image( 'A1', '.c:\images\perl.bmp' );
1854
1855 The optional "options" hash/hashref parameter can be used to set
1856 various options for the image. The defaults are:
1857
1858 %options = (
1859 x_offset => 0,
1860 y_offset => 0,
1861 x_scale => 1,
1862 y_scale => 1,
1863 object_position => 2,
1864 url => undef,
1865 tip => undef,
1866 description => $filename,
1867 decorative => 0,
1868 );
1869
1870 The parameters "x_offset" and "y_offset" can be used to specify an
1871 offset from the top left hand corner of the cell specified by $row and
1872 $col. The offset values are in pixels.
1873
1874 $worksheet1->insert_image('A1', 'perl.bmp', { x_offset =>32, y_offset => 10 });
1875
1876 The offsets can be greater than the width or height of the underlying
1877 cell. This can be occasionally useful if you wish to align two or more
1878 images relative to the same cell.
1879
1880 The parameters "x_scale" and "y_scale" can be used to scale the
1881 inserted image horizontally and vertically:
1882
1883 # Scale the inserted image: width x 2.0, height x 0.8
1884 $worksheet->insert_image( 'A1', 'perl.bmp', { y_scale => 2, y_scale => 0.8 } );
1885
1886 The positioning of the image when cells are resized can be set with the
1887 "object_position" parameter:
1888
1889 $worksheet->insert_image( 'A1', 'perl.bmp', { object_position => 1 } );
1890
1891 The "object_position" parameter can have one of the following allowable
1892 values:
1893
1894 1. Move and size with cells.
1895 2. Move but don't size with cells.
1896 3. Don't move or size with cells.
1897 4. Same as Option 1, see below.
1898
1899 Option 4 appears in Excel as Option 1. However, the worksheet object is
1900 sized to take hidden rows or columns into account. This allows the user
1901 to hide an image in a cell, possibly as part of an autofilter.
1902
1903 The "url" option can be use to used to add a hyperlink to an image:
1904
1905 $worksheet->insert_image( 'A1', 'logo.png',
1906 { url => 'https://github.com/jmcnamara' } );
1907
1908 The supported url formats are the same as those supported by the
1909 "write_url()" method and the same rules/limits apply.
1910
1911 The "tip" option can be use to used to add a mouseover tip to the
1912 hyperlink:
1913
1914 $worksheet->insert_image( 'A1', 'logo.png',
1915 {
1916 url => 'https://github.com/jmcnamara',
1917 tip => 'GitHub'
1918 }
1919 );
1920
1921 The "description" parameter can be used to specify a description or
1922 "alt text" string for the image. In general this would be used to
1923 provide a text description of the image to help accessibility. It is an
1924 optional parameter and defaults to the filename of the image. It can be
1925 used as follows:
1926
1927 $worksheet->insert_image( 'E9', 'logo.png',
1928 {description => "This is some alternative text"} );
1929
1930 The optional "decorative" parameter is also used to help accessibility.
1931 It is used to mark the image as decorative, and thus uninformative, for
1932 automated screen readers. As in Excel, if this parameter is in use the
1933 "description" field isn't written. It is used as follows:
1934
1935 $worksheet->insert_image( 'E9', 'logo.png', {decorative => 1} );
1936
1937 Note: you must call "set_row()" or "set_column()" before
1938 "insert_image()" if you wish to change the default dimensions of any of
1939 the rows or columns that the image occupies. The height of a row can
1940 also change if you use a font that is larger than the default. This in
1941 turn will affect the scaling of your image. To avoid this you should
1942 explicitly set the height of the row using "set_row()" if it contains a
1943 font size that will change the row height.
1944
1945 BMP images must be 24 bit, true colour, bitmaps. In general it is best
1946 to avoid BMP images since they aren't compressed.
1947
1948 insert_chart( $row, $col, $chart, { %options } )
1949 This method can be used to insert a Chart object into a worksheet. The
1950 Chart must be created by the "add_chart()" Workbook method and it must
1951 have the "embedded" option set.
1952
1953 my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
1954
1955 # Configure the chart.
1956 ...
1957
1958 # Insert the chart into the a worksheet.
1959 $worksheet->insert_chart( 'E2', $chart );
1960
1961 See "add_chart()" for details on how to create the Chart object and
1962 Excel::Writer::XLSX::Chart for details on how to configure it. See also
1963 the "chart_*.pl" programs in the examples directory of the distro.
1964
1965 The optional "options" hash/hashref parameter can be used to set
1966 various options for the chart. The defaults are:
1967
1968 %options = (
1969 x_offset => 0,
1970 y_offset => 0,
1971 x_scale => 1,
1972 y_scale => 1,
1973 object_position => 1,
1974 );
1975
1976 The parameters "x_offset" and "y_offset" can be used to specify an
1977 offset from the top left hand corner of the cell specified by $row and
1978 $col. The offset values are in pixels.
1979
1980 $worksheet1->insert_chart( 'E2', $chart, { x_offset =>10, y_offset => 20 });
1981
1982 The parameters "x_scale" and "y_scale" can be used to scale the
1983 inserted chart horizontally and vertically:
1984
1985 # Scale the width by 120% and the height by 150%
1986 $worksheet->insert_chart( 'E2', $chart, { y_scale => 1.2, y_scale => 1.5 } );
1987
1988 The positioning of the chart when cells are resized can be set with the
1989 "object_position" parameter:
1990
1991 $worksheet->insert_chart( 'E2', $chart, { object_position => 2 } );
1992
1993 The "object_position" parameter can have one of the following allowable
1994 values:
1995
1996 1. Move and size with cells.
1997 2. Move but don't size with cells.
1998 3. Don't move or size with cells.
1999 4. Same as Option 1, see below.
2000
2001 Option 4 appears in Excel as Option 1. However, the worksheet object is
2002 sized to take hidden rows or columns into account. This is generally
2003 only useful for images and not for charts.
2004
2005 insert_shape( $row, $col, $shape, $x, $y, $x_scale, $y_scale )
2006 This method can be used to insert a Shape object into a worksheet. The
2007 Shape must be created by the "add_shape()" Workbook method.
2008
2009 my $shape = $workbook->add_shape( name => 'My Shape', type => 'plus' );
2010
2011 # Configure the shape.
2012 $shape->set_text('foo');
2013 ...
2014
2015 # Insert the shape into the a worksheet.
2016 $worksheet->insert_shape( 'E2', $shape );
2017
2018 See "add_shape()" for details on how to create the Shape object and
2019 Excel::Writer::XLSX::Shape for details on how to configure it.
2020
2021 The $x, $y, $x_scale and $y_scale parameters are optional.
2022
2023 The parameters $x and $y can be used to specify an offset from the top
2024 left hand corner of the cell specified by $row and $col. The offset
2025 values are in pixels.
2026
2027 $worksheet1->insert_shape( 'E2', $chart, 3, 3 );
2028
2029 The parameters $x_scale and $y_scale can be used to scale the inserted
2030 shape horizontally and vertically:
2031
2032 # Scale the width by 120% and the height by 150%
2033 $worksheet->insert_shape( 'E2', $shape, 0, 0, 1.2, 1.5 );
2034
2035 See also the "shape*.pl" programs in the examples directory of the
2036 distro.
2037
2038 insert_button( $row, $col, { %options })
2039 The "insert_button()" method can be used to insert an Excel form button
2040 into a worksheet.
2041
2042 This method is generally only useful when used in conjunction with the
2043 Workbook "add_vba_project()" method to tie the button to a macro from
2044 an embedded VBA project:
2045
2046 my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' );
2047 ...
2048 $workbook->add_vba_project( './vbaProject.bin' );
2049
2050 $worksheet->insert_button( 'C2', { macro => 'my_macro' } );
2051
2052 The properties of the button that can be set are:
2053
2054 macro
2055 caption
2056 width
2057 height
2058 x_scale
2059 y_scale
2060 x_offset
2061 y_offset
2062
2063 Option: macro
2064 This option is used to set the macro that the button will invoke
2065 when the user clicks on it. The macro should be included using the
2066 Workbook "add_vba_project()" method shown above.
2067
2068 $worksheet->insert_button( 'C2', { macro => 'my_macro' } );
2069
2070 The default macro is "ButtonX_Click" where X is the button number.
2071
2072 Option: caption
2073 This option is used to set the caption on the button. The default
2074 is "Button X" where X is the button number.
2075
2076 $worksheet->insert_button( 'C2', { macro => 'my_macro', caption => 'Hello' } );
2077
2078 Option: width
2079 This option is used to set the width of the button in pixels.
2080
2081 $worksheet->insert_button( 'C2', { macro => 'my_macro', width => 128 } );
2082
2083 The default button width is 64 pixels which is the width of a
2084 default cell.
2085
2086 Option: height
2087 This option is used to set the height of the button in pixels.
2088
2089 $worksheet->insert_button( 'C2', { macro => 'my_macro', height => 40 } );
2090
2091 The default button height is 20 pixels which is the height of a
2092 default cell.
2093
2094 Option: x_scale
2095 This option is used to set the width of the button as a factor of
2096 the default width.
2097
2098 $worksheet->insert_button( 'C2', { macro => 'my_macro', x_scale => 2.0 );
2099
2100 Option: y_scale
2101 This option is used to set the height of the button as a factor of
2102 the default height.
2103
2104 $worksheet->insert_button( 'C2', { macro => 'my_macro', y_scale => 2.0 );
2105
2106 Option: x_offset
2107 This option is used to change the x offset, in pixels, of a button
2108 within a cell:
2109
2110 $worksheet->insert_button( 'C2', { macro => 'my_macro', x_offset => 2 );
2111
2112 Option: y_offset
2113 This option is used to change the y offset, in pixels, of a comment
2114 within a cell.
2115
2116 Note: Button is the only Excel form element that is available in
2117 Excel::Writer::XLSX. Form elements represent a lot of work to implement
2118 and the underlying VML syntax isn't very much fun.
2119
2120 data_validation()
2121 The "data_validation()" method is used to construct an Excel data
2122 validation or to limit the user input to a dropdown list of values.
2123
2124 $worksheet->data_validation('B3',
2125 {
2126 validate => 'integer',
2127 criteria => '>',
2128 value => 100,
2129 });
2130
2131 $worksheet->data_validation('B5:B9',
2132 {
2133 validate => 'list',
2134 value => ['open', 'high', 'close'],
2135 });
2136
2137 This method contains a lot of parameters and is described in detail in
2138 a separate section "DATA VALIDATION IN EXCEL".
2139
2140 See also the "data_validate.pl" program in the examples directory of
2141 the distro
2142
2143 conditional_formatting()
2144 The "conditional_formatting()" method is used to add formatting to a
2145 cell or range of cells based on user defined criteria.
2146
2147 $worksheet->conditional_formatting( 'A1:J10',
2148 {
2149 type => 'cell',
2150 criteria => '>=',
2151 value => 50,
2152 format => $format1,
2153 }
2154 );
2155
2156 This method contains a lot of parameters and is described in detail in
2157 a separate section "CONDITIONAL FORMATTING IN EXCEL".
2158
2159 See also the "conditional_format.pl" program in the examples directory
2160 of the distro
2161
2162 add_sparkline()
2163 The "add_sparkline()" worksheet method is used to add sparklines to a
2164 cell or a range of cells.
2165
2166 $worksheet->add_sparkline(
2167 {
2168 location => 'F2',
2169 range => 'Sheet1!A2:E2',
2170 type => 'column',
2171 style => 12,
2172 }
2173 );
2174
2175 This method contains a lot of parameters and is described in detail in
2176 a separate section "SPARKLINES IN EXCEL".
2177
2178 See also the "sparklines1.pl" and "sparklines2.pl" example programs in
2179 the "examples" directory of the distro.
2180
2181 Note: Sparklines are a feature of Excel 2010+ only. You can write them
2182 to an XLSX file that can be read by Excel 2007 but they won't be
2183 displayed.
2184
2185 add_table()
2186 The "add_table()" method is used to group a range of cells into an
2187 Excel Table.
2188
2189 $worksheet->add_table( 'B3:F7', { ... } );
2190
2191 This method contains a lot of parameters and is described in detail in
2192 a separate section "TABLES IN EXCEL".
2193
2194 See also the "tables.pl" program in the examples directory of the
2195 distro
2196
2197 get_name()
2198 The "get_name()" method is used to retrieve the name of a worksheet.
2199 For example:
2200
2201 for my $sheet ( $workbook->sheets() ) {
2202 print $sheet->get_name();
2203 }
2204
2205 For reasons related to the design of Excel::Writer::XLSX and to the
2206 internals of Excel there is no "set_name()" method. The only way to set
2207 the worksheet name is via the "add_worksheet()" method.
2208
2209 activate()
2210 The "activate()" method is used to specify which worksheet is initially
2211 visible in a multi-sheet workbook:
2212
2213 $worksheet1 = $workbook->add_worksheet( 'To' );
2214 $worksheet2 = $workbook->add_worksheet( 'the' );
2215 $worksheet3 = $workbook->add_worksheet( 'wind' );
2216
2217 $worksheet3->activate();
2218
2219 This is similar to the Excel VBA activate method. More than one
2220 worksheet can be selected via the "select()" method, see below, however
2221 only one worksheet can be active.
2222
2223 The default active worksheet is the first worksheet.
2224
2225 select()
2226 The "select()" method is used to indicate that a worksheet is selected
2227 in a multi-sheet workbook:
2228
2229 $worksheet1->activate();
2230 $worksheet2->select();
2231 $worksheet3->select();
2232
2233 A selected worksheet has its tab highlighted. Selecting worksheets is a
2234 way of grouping them together so that, for example, several worksheets
2235 could be printed in one go. A worksheet that has been activated via the
2236 "activate()" method will also appear as selected.
2237
2238 hide()
2239 The "hide()" method is used to hide a worksheet:
2240
2241 $worksheet2->hide();
2242
2243 You may wish to hide a worksheet in order to avoid confusing a user
2244 with intermediate data or calculations.
2245
2246 A hidden worksheet can not be activated or selected so this method is
2247 mutually exclusive with the "activate()" and "select()" methods. In
2248 addition, since the first worksheet will default to being the active
2249 worksheet, you cannot hide the first worksheet without activating
2250 another sheet:
2251
2252 $worksheet2->activate();
2253 $worksheet1->hide();
2254
2255 set_first_sheet()
2256 The "activate()" method determines which worksheet is initially
2257 selected. However, if there are a large number of worksheets the
2258 selected worksheet may not appear on the screen. To avoid this you can
2259 select which is the leftmost visible worksheet using
2260 "set_first_sheet()":
2261
2262 for ( 1 .. 20 ) {
2263 $workbook->add_worksheet;
2264 }
2265
2266 $worksheet21 = $workbook->add_worksheet();
2267 $worksheet22 = $workbook->add_worksheet();
2268
2269 $worksheet21->set_first_sheet();
2270 $worksheet22->activate();
2271
2272 This method is not required very often. The default value is the first
2273 worksheet.
2274
2275 protect( $password, \%options )
2276 The "protect()" method is used to protect a worksheet from
2277 modification:
2278
2279 $worksheet->protect();
2280
2281 The "protect()" method also has the effect of enabling a cell's
2282 "locked" and "hidden" properties if they have been set. A locked cell
2283 cannot be edited and this property is on by default for all cells. A
2284 hidden cell will display the results of a formula but not the formula
2285 itself.
2286
2287 See the "protection.pl" program in the examples directory of the distro
2288 for an illustrative example and the "set_locked" and "set_hidden"
2289 format methods in "CELL FORMATTING".
2290
2291 You can optionally add a password to the worksheet protection:
2292
2293 $worksheet->protect( 'drowssap' );
2294
2295 Passing the empty string '' is the same as turning on protection
2296 without a password.
2297
2298 Note, the worksheet level password in Excel provides very weak
2299 protection. It does not encrypt your data and is very easy to
2300 deactivate. Full workbook encryption is not supported by
2301 "Excel::Writer::XLSX" since it requires a completely different file
2302 format and would take several man months to implement.
2303
2304 You can specify which worksheet elements you wish to protect by passing
2305 a hash_ref with any or all of the following keys:
2306
2307 # Default shown.
2308 %options = (
2309 objects => 0,
2310 scenarios => 0,
2311 format_cells => 0,
2312 format_columns => 0,
2313 format_rows => 0,
2314 insert_columns => 0,
2315 insert_rows => 0,
2316 insert_hyperlinks => 0,
2317 delete_columns => 0,
2318 delete_rows => 0,
2319 select_locked_cells => 1,
2320 sort => 0,
2321 autofilter => 0,
2322 pivot_tables => 0,
2323 select_unlocked_cells => 1,
2324 );
2325
2326 The default boolean values are shown above. Individual elements can be
2327 protected as follows:
2328
2329 $worksheet->protect( 'drowssap', { insert_rows => 1 } );
2330
2331 For chartsheets the allowable options and default values are:
2332
2333 %options = (
2334 objects => 1,
2335 content => 1,
2336 );
2337
2338 unprotect_range( $cell_range, $range_name )
2339 The "unprotect_range()" method is used to unprotect ranges in a
2340 protected worksheet. It can be used to set a single range or multiple
2341 ranges:
2342
2343 $worksheet->unprotect_range( 'A1' );
2344 $worksheet->unprotect_range( 'C1' );
2345 $worksheet->unprotect_range( 'E1:E3' );
2346 $worksheet->unprotect_range( 'G1:K100' );
2347
2348 As in Excel the ranges are given sequential names like "Range1" and
2349 "Range2" but a user defined name can also be specified:
2350
2351 $worksheet->unprotect_range( 'G4:I6', 'MyRange' );
2352
2353 set_selection( $first_row, $first_col, $last_row, $last_col )
2354 This method can be used to specify which cell or cells are selected in
2355 a worksheet. The most common requirement is to select a single cell, in
2356 which case $last_row and $last_col can be omitted. The active cell
2357 within a selected range is determined by the order in which $first and
2358 $last are specified. It is also possible to specify a cell or a range
2359 using A1 notation. See the note about "Cell notation".
2360
2361 Examples:
2362
2363 $worksheet1->set_selection( 3, 3 ); # 1. Cell D4.
2364 $worksheet2->set_selection( 3, 3, 6, 6 ); # 2. Cells D4 to G7.
2365 $worksheet3->set_selection( 6, 6, 3, 3 ); # 3. Cells G7 to D4.
2366 $worksheet4->set_selection( 'D4' ); # Same as 1.
2367 $worksheet5->set_selection( 'D4:G7' ); # Same as 2.
2368 $worksheet6->set_selection( 'G7:D4' ); # Same as 3.
2369
2370 The default cell selections is (0, 0), 'A1'.
2371
2372 set_row( $row, $height, $format, $hidden, $level, $collapsed )
2373 This method can be used to change the default properties of a row. All
2374 parameters apart from $row are optional.
2375
2376 The most common use for this method is to change the height of a row.
2377
2378 $worksheet->set_row( 0, 20 ); # Row 1 height set to 20
2379
2380 Note: the row height is in Excel character units. To set the height in
2381 pixels use the "set_row_pixels()" method, see below.
2382
2383 If you wish to set the format without changing the height you can pass
2384 "undef" as the height parameter:
2385
2386 $worksheet->set_row( 0, undef, $format );
2387
2388 The $format parameter will be applied to any cells in the row that
2389 don't have a format. For example
2390
2391 $worksheet->set_row( 0, undef, $format1 ); # Set the format for row 1
2392 $worksheet->write( 'A1', 'Hello' ); # Defaults to $format1
2393 $worksheet->write( 'B1', 'Hello', $format2 ); # Keeps $format2
2394
2395 If you wish to define a row format in this way you should call the
2396 method before any calls to "write()". Calling it afterwards will
2397 overwrite any format that was previously specified.
2398
2399 The $hidden parameter should be set to 1 if you wish to hide a row.
2400 This can be used, for example, to hide intermediary steps in a
2401 complicated calculation:
2402
2403 $worksheet->set_row( 0, 20, $format, 1 );
2404 $worksheet->set_row( 1, undef, undef, 1 );
2405
2406 The $level parameter is used to set the outline level of the row.
2407 Outlines are described in "OUTLINES AND GROUPING IN EXCEL". Adjacent
2408 rows with the same outline level are grouped together into a single
2409 outline.
2410
2411 The following example sets an outline level of 1 for rows 2 and 3
2412 (zero-indexed):
2413
2414 $worksheet->set_row( 1, undef, undef, 0, 1 );
2415 $worksheet->set_row( 2, undef, undef, 0, 1 );
2416
2417 The $hidden parameter can also be used to hide collapsed outlined rows
2418 when used in conjunction with the $level parameter.
2419
2420 $worksheet->set_row( 1, undef, undef, 1, 1 );
2421 $worksheet->set_row( 2, undef, undef, 1, 1 );
2422
2423 For collapsed outlines you should also indicate which row has the
2424 collapsed "+" symbol using the optional $collapsed parameter.
2425
2426 $worksheet->set_row( 3, undef, undef, 0, 0, 1 );
2427
2428 For a more complete example see the "outline.pl" and
2429 "outline_collapsed.pl" programs in the examples directory of the
2430 distro.
2431
2432 Excel allows up to 7 outline levels. Therefore the $level parameter
2433 should be in the range "0 <= $level <= 7".
2434
2435 set_row_pixels( $row, $height, $format, $hidden, $level, $collapsed )
2436 This method is the same as "set_row()" except that $height is in
2437 pixels.
2438
2439 $worksheet->set_row ( 0, 24 ); # Set row height in character units
2440 $worksheet->set_row_pixels( 1, 18 ); # Set row to same height in pixels
2441
2442 set_column( $first_col, $last_col, $width, $format, $hidden, $level,
2443 $collapsed )
2444 This method can be used to change the default properties of a single
2445 column or a range of columns. All parameters apart from $first_col and
2446 $last_col are optional.
2447
2448 If "set_column()" is applied to a single column the value of $first_col
2449 and $last_col should be the same. In the case where $last_col is zero
2450 it is set to the same value as $first_col.
2451
2452 It is also possible, and generally clearer, to specify a column range
2453 using the form of A1 notation used for columns. See the note about
2454 "Cell notation".
2455
2456 Examples:
2457
2458 $worksheet->set_column( 0, 0, 20 ); # Column A width set to 20
2459 $worksheet->set_column( 1, 3, 30 ); # Columns B-D width set to 30
2460 $worksheet->set_column( 'E:E', 20 ); # Column E width set to 20
2461 $worksheet->set_column( 'F:H', 30 ); # Columns F-H width set to 30
2462
2463 The width corresponds to the column width value that is specified in
2464 Excel. It is approximately equal to the length of a string in the
2465 default font of Calibri 11. To set the width in pixels use the
2466 "set_column_pixels()" method, see below.
2467
2468 Unfortunately, there is no way to specify "AutoFit" for a column in the
2469 Excel file format. This feature is only available at runtime from
2470 within Excel.
2471
2472 As usual the $format parameter is optional, for additional information,
2473 see "CELL FORMATTING". If you wish to set the format without changing
2474 the width you can pass "undef" as the width parameter:
2475
2476 $worksheet->set_column( 0, 0, undef, $format );
2477
2478 The $format parameter will be applied to any cells in the column that
2479 don't have a format. For example
2480
2481 $worksheet->set_column( 'A:A', undef, $format1 ); # Set format for col 1
2482 $worksheet->write( 'A1', 'Hello' ); # Defaults to $format1
2483 $worksheet->write( 'A2', 'Hello', $format2 ); # Keeps $format2
2484
2485 If you wish to define a column format in this way you should call the
2486 method before any calls to "write()". If you call it afterwards it
2487 won't have any effect.
2488
2489 A default row format takes precedence over a default column format
2490
2491 $worksheet->set_row( 0, undef, $format1 ); # Set format for row 1
2492 $worksheet->set_column( 'A:A', undef, $format2 ); # Set format for col 1
2493 $worksheet->write( 'A1', 'Hello' ); # Defaults to $format1
2494 $worksheet->write( 'A2', 'Hello' ); # Defaults to $format2
2495
2496 The $hidden parameter should be set to 1 if you wish to hide a column.
2497 This can be used, for example, to hide intermediary steps in a
2498 complicated calculation:
2499
2500 $worksheet->set_column( 'D:D', 20, $format, 1 );
2501 $worksheet->set_column( 'E:E', undef, undef, 1 );
2502
2503 The $level parameter is used to set the outline level of the column.
2504 Outlines are described in "OUTLINES AND GROUPING IN EXCEL". Adjacent
2505 columns with the same outline level are grouped together into a single
2506 outline.
2507
2508 The following example sets an outline level of 1 for columns B to G:
2509
2510 $worksheet->set_column( 'B:G', undef, undef, 0, 1 );
2511
2512 The $hidden parameter can also be used to hide collapsed outlined
2513 columns when used in conjunction with the $level parameter.
2514
2515 $worksheet->set_column( 'B:G', undef, undef, 1, 1 );
2516
2517 For collapsed outlines you should also indicate which row has the
2518 collapsed "+" symbol using the optional $collapsed parameter.
2519
2520 $worksheet->set_column( 'H:H', undef, undef, 0, 0, 1 );
2521
2522 For a more complete example see the "outline.pl" and
2523 "outline_collapsed.pl" programs in the examples directory of the
2524 distro.
2525
2526 Excel allows up to 7 outline levels. Therefore the $level parameter
2527 should be in the range "0 <= $level <= 7".
2528
2529 set_column_pixels( $first_col, $last_col, $width, $format, $hidden, $level,
2530 $collapsed )
2531 This method is the same as "set_column()" except that $width is in
2532 pixels.
2533
2534 $worksheet->set_column( 0, 0, 10 ); # Column A width set to 20 in character units
2535 $worksheet->set_column( 1, 1, 75 ); # Column B set to the same width in pixels
2536
2537 set_default_row( $height, $hide_unused_rows )
2538 The "set_default_row()" method is used to set the limited number of
2539 default row properties allowed by Excel. These are the default height
2540 and the option to hide unused rows.
2541
2542 $worksheet->set_default_row( 24 ); # Set the default row height to 24.
2543
2544 The option to hide unused rows is used by Excel as an optimisation so
2545 that the user can hide a large number of rows without generating a very
2546 large file with an entry for each hidden row.
2547
2548 $worksheet->set_default_row( undef, 1 );
2549
2550 See the "hide_row_col.pl" example program.
2551
2552 outline_settings( $visible, $symbols_below, $symbols_right, $auto_style )
2553 The "outline_settings()" method is used to control the appearance of
2554 outlines in Excel. Outlines are described in "OUTLINES AND GROUPING IN
2555 EXCEL".
2556
2557 The $visible parameter is used to control whether or not outlines are
2558 visible. Setting this parameter to 0 will cause all outlines on the
2559 worksheet to be hidden. They can be unhidden in Excel by means of the
2560 "Show Outline Symbols" command button. The default setting is 1 for
2561 visible outlines.
2562
2563 $worksheet->outline_settings( 0 );
2564
2565 The $symbols_below parameter is used to control whether the row outline
2566 symbol will appear above or below the outline level bar. The default
2567 setting is 1 for symbols to appear below the outline level bar.
2568
2569 The $symbols_right parameter is used to control whether the column
2570 outline symbol will appear to the left or the right of the outline
2571 level bar. The default setting is 1 for symbols to appear to the right
2572 of the outline level bar.
2573
2574 The $auto_style parameter is used to control whether the automatic
2575 outline generator in Excel uses automatic styles when creating an
2576 outline. This has no effect on a file generated by
2577 "Excel::Writer::XLSX" but it does have an effect on how the worksheet
2578 behaves after it is created. The default setting is 0 for "Automatic
2579 Styles" to be turned off.
2580
2581 The default settings for all of these parameters correspond to Excel's
2582 default parameters.
2583
2584 The worksheet parameters controlled by "outline_settings()" are rarely
2585 used.
2586
2587 freeze_panes( $row, $col, $top_row, $left_col )
2588 This method can be used to divide a worksheet into horizontal or
2589 vertical regions known as panes and to also "freeze" these panes so
2590 that the splitter bars are not visible. This is the same as the
2591 "Window->Freeze Panes" menu command in Excel
2592
2593 The parameters $row and $col are used to specify the location of the
2594 split. It should be noted that the split is specified at the top or
2595 left of a cell and that the method uses zero based indexing. Therefore
2596 to freeze the first row of a worksheet it is necessary to specify the
2597 split at row 2 (which is 1 as the zero-based index). This might lead
2598 you to think that you are using a 1 based index but this is not the
2599 case.
2600
2601 You can set one of the $row and $col parameters as zero if you do not
2602 want either a vertical or horizontal split.
2603
2604 Examples:
2605
2606 $worksheet->freeze_panes( 1, 0 ); # Freeze the first row
2607 $worksheet->freeze_panes( 'A2' ); # Same using A1 notation
2608 $worksheet->freeze_panes( 0, 1 ); # Freeze the first column
2609 $worksheet->freeze_panes( 'B1' ); # Same using A1 notation
2610 $worksheet->freeze_panes( 1, 2 ); # Freeze first row and first 2 columns
2611 $worksheet->freeze_panes( 'C2' ); # Same using A1 notation
2612
2613 The parameters $top_row and $left_col are optional. They are used to
2614 specify the top-most or left-most visible row or column in the
2615 scrolling region of the panes. For example to freeze the first row and
2616 to have the scrolling region begin at row twenty:
2617
2618 $worksheet->freeze_panes( 1, 0, 20, 0 );
2619
2620 You cannot use A1 notation for the $top_row and $left_col parameters.
2621
2622 See also the "panes.pl" program in the "examples" directory of the
2623 distribution.
2624
2625 split_panes( $y, $x, $top_row, $left_col )
2626 This method can be used to divide a worksheet into horizontal or
2627 vertical regions known as panes. This method is different from the
2628 "freeze_panes()" method in that the splits between the panes will be
2629 visible to the user and each pane will have its own scroll bars.
2630
2631 The parameters $y and $x are used to specify the vertical and
2632 horizontal position of the split. The units for $y and $x are the same
2633 as those used by Excel to specify row height and column width. However,
2634 the vertical and horizontal units are different from each other.
2635 Therefore you must specify the $y and $x parameters in terms of the row
2636 heights and column widths that you have set or the default values which
2637 are 15 for a row and 8.43 for a column.
2638
2639 You can set one of the $y and $x parameters as zero if you do not want
2640 either a vertical or horizontal split. The parameters $top_row and
2641 $left_col are optional. They are used to specify the top-most or left-
2642 most visible row or column in the bottom-right pane.
2643
2644 Example:
2645
2646 $worksheet->split_panes( 15, 0, ); # First row
2647 $worksheet->split_panes( 0, 8.43 ); # First column
2648 $worksheet->split_panes( 15, 8.43 ); # First row and column
2649
2650 You cannot use A1 notation with this method.
2651
2652 See also the "freeze_panes()" method and the "panes.pl" program in the
2653 "examples" directory of the distribution.
2654
2655 merge_range( $first_row, $first_col, $last_row, $last_col, $token, $format
2656 )
2657 The "merge_range()" method allows you to merge cells that contain other
2658 types of alignment in addition to the merging:
2659
2660 my $format = $workbook->add_format(
2661 border => 6,
2662 valign => 'vcenter',
2663 align => 'center',
2664 );
2665
2666 $worksheet->merge_range( 'B3:D4', 'Vertical and horizontal', $format );
2667
2668 "merge_range()" writes its $token argument using the worksheet
2669 "write()" method. Therefore it will handle numbers, strings, formulas
2670 or urls as required. If you need to specify the required "write_*()"
2671 method use the "merge_range_type()" method, see below.
2672
2673 The full possibilities of this method are shown in the "merge3.pl" to
2674 "merge6.pl" programs in the "examples" directory of the distribution.
2675
2676 merge_range_type( $type, $first_row, $first_col, $last_row, $last_col, ...
2677 )
2678 The "merge_range()" method, see above, uses "write()" to insert the
2679 required data into to a merged range. However, there may be times where
2680 this isn't what you require so as an alternative the "merge_range_type
2681 ()" method allows you to specify the type of data you wish to write.
2682 For example:
2683
2684 $worksheet->merge_range_type( 'number', 'B2:C2', 123, $format1 );
2685 $worksheet->merge_range_type( 'string', 'B4:C4', 'foo', $format2 );
2686 $worksheet->merge_range_type( 'formula', 'B6:C6', '=1+2', $format3 );
2687
2688 The $type must be one of the following, which corresponds to a
2689 "write_*()" method:
2690
2691 'number'
2692 'string'
2693 'formula'
2694 'array_formula'
2695 'blank'
2696 'rich_string'
2697 'date_time'
2698 'url'
2699
2700 Any arguments after the range should be whatever the appropriate method
2701 accepts:
2702
2703 $worksheet->merge_range_type( 'rich_string', 'B8:C8',
2704 'This is ', $bold, 'bold', $format4 );
2705
2706 Note, you must always pass a $format object as an argument, even if it
2707 is a default format.
2708
2709 set_zoom( $scale )
2710 Set the worksheet zoom factor in the range "10 <= $scale <= 400":
2711
2712 $worksheet1->set_zoom( 50 );
2713 $worksheet2->set_zoom( 75 );
2714 $worksheet3->set_zoom( 300 );
2715 $worksheet4->set_zoom( 400 );
2716
2717 The default zoom factor is 100. You cannot zoom to "Selection" because
2718 it is calculated by Excel at run-time.
2719
2720 Note, "set_zoom()" does not affect the scale of the printed page. For
2721 that you should use "set_print_scale()".
2722
2723 right_to_left()
2724 The "right_to_left()" method is used to change the default direction of
2725 the worksheet from left-to-right, with the A1 cell in the top left, to
2726 right-to-left, with the A1 cell in the top right.
2727
2728 $worksheet->right_to_left();
2729
2730 This is useful when creating Arabic, Hebrew or other near or far
2731 eastern worksheets that use right-to-left as the default direction.
2732
2733 hide_zero()
2734 The "hide_zero()" method is used to hide any zero values that appear in
2735 cells.
2736
2737 $worksheet->hide_zero();
2738
2739 In Excel this option is found under Tools->Options->View.
2740
2741 set_background( $filename )
2742 The "set_background()" method can be used to set the background image
2743 for the worksheet:
2744
2745 $worksheet->set_background( 'logo.png' )
2746
2747 The "set_background()" method supports all the image formats supported
2748 by "insert_image()".
2749
2750 Some people use this method to add a watermark background to their
2751 document. However, Microsoft recommends using a header image to set a
2752 watermark <https://support.microsoft.com/en-us/office/add-a-watermark-
2753 in-excel-a372182a-d733-484e-825c-18ddf3edf009>. The choice of method
2754 depends on whether you want the watermark to be visible in normal
2755 viewing mode or just when the file is printed. In Excel::Writer::XLSX
2756 you can get the header watermark effect using "set_header()":
2757
2758 $worksheet->set_header( '&C&G', undef, { image_center => 'watermark.png' } )
2759
2760 set_tab_color()
2761 The "set_tab_color()" method is used to change the colour of the
2762 worksheet tab. You can use one of the standard colour names provided by
2763 the Format object or a Html style "#RRGGBB" colour. See "WORKING WITH
2764 COLOURS".
2765
2766 $worksheet1->set_tab_color( 'red' );
2767 $worksheet2->set_tab_color( '#FF6600' );
2768
2769 See the "tab_colors.pl" program in the examples directory of the
2770 distro.
2771
2772 autofilter( $first_row, $first_col, $last_row, $last_col )
2773 This method allows an autofilter to be added to a worksheet. An
2774 autofilter is a way of adding drop down lists to the headers of a 2D
2775 range of worksheet data. This allows users to filter the data based on
2776 simple criteria so that some data is shown and some is hidden.
2777
2778 To add an autofilter to a worksheet:
2779
2780 $worksheet->autofilter( 0, 0, 10, 3 );
2781 $worksheet->autofilter( 'A1:D11' ); # Same as above in A1 notation.
2782
2783 Filter conditions can be applied using the "filter_column()" or
2784 "filter_column_list()" method.
2785
2786 See the "autofilter.pl" program in the examples directory of the distro
2787 for a more detailed example.
2788
2789 filter_column( $column, $expression )
2790 The "filter_column" method can be used to filter columns in a
2791 autofilter range based on simple conditions.
2792
2793 NOTE: It isn't sufficient to just specify the filter condition. You
2794 must also hide any rows that don't match the filter condition. Rows are
2795 hidden using the "set_row()" "visible" parameter. "Excel::Writer::XLSX"
2796 cannot do this automatically since it isn't part of the file format.
2797 See the "autofilter.pl" program in the examples directory of the distro
2798 for an example.
2799
2800 The conditions for the filter are specified using simple expressions:
2801
2802 $worksheet->filter_column( 'A', 'x > 2000' );
2803 $worksheet->filter_column( 'B', 'x > 2000 and x < 5000' );
2804
2805 The $column parameter can either be a zero indexed column number or a
2806 string column name.
2807
2808 The following operators are available:
2809
2810 Operator Synonyms
2811 == = eq =~
2812 != <> ne !=
2813 >
2814 <
2815 >=
2816 <=
2817
2818 and &&
2819 or ||
2820
2821 The operator synonyms are just syntactic sugar to make you more
2822 comfortable using the expressions. It is important to remember that the
2823 expressions will be interpreted by Excel and not by perl.
2824
2825 An expression can comprise a single statement or two statements
2826 separated by the "and" and "or" operators. For example:
2827
2828 'x < 2000'
2829 'x > 2000'
2830 'x == 2000'
2831 'x > 2000 and x < 5000'
2832 'x == 2000 or x == 5000'
2833
2834 Filtering of blank or non-blank data can be achieved by using a value
2835 of "Blanks" or "NonBlanks" in the expression:
2836
2837 'x == Blanks'
2838 'x == NonBlanks'
2839
2840 Excel also allows some simple string matching operations:
2841
2842 'x =~ b*' # begins with b
2843 'x !~ b*' # doesn't begin with b
2844 'x =~ *b' # ends with b
2845 'x !~ *b' # doesn't end with b
2846 'x =~ *b*' # contains b
2847 'x !~ *b*' # doesn't contains b
2848
2849 You can also use "*" to match any character or number and "?" to match
2850 any single character or number. No other regular expression quantifier
2851 is supported by Excel's filters. Excel's regular expression characters
2852 can be escaped using "~".
2853
2854 The placeholder variable "x" in the above examples can be replaced by
2855 any simple string. The actual placeholder name is ignored internally so
2856 the following are all equivalent:
2857
2858 'x < 2000'
2859 'col < 2000'
2860 'Price < 2000'
2861
2862 Also, note that a filter condition can only be applied to a column in a
2863 range specified by the "autofilter()" Worksheet method.
2864
2865 See the "autofilter.pl" program in the examples directory of the distro
2866 for a more detailed example.
2867
2868 Note Spreadsheet::WriteExcel supports Top 10 style filters. These
2869 aren't currently supported by Excel::Writer::XLSX but may be added
2870 later.
2871
2872 filter_column_list( $column, @matches )
2873 Prior to Excel 2007 it was only possible to have either 1 or 2 filter
2874 conditions such as the ones shown above in the "filter_column" method.
2875
2876 Excel 2007 introduced a new list style filter where it is possible to
2877 specify 1 or more 'or' style criteria. For example if your column
2878 contained data for the first six months the initial data would be
2879 displayed as all selected as shown on the left. Then if you selected
2880 'March', 'April' and 'May' they would be displayed as shown on the
2881 right.
2882
2883 No criteria selected Some criteria selected.
2884
2885 [/] (Select all) [X] (Select all)
2886 [/] January [ ] January
2887 [/] February [ ] February
2888 [/] March [/] March
2889 [/] April [/] April
2890 [/] May [/] May
2891 [/] June [ ] June
2892
2893 The "filter_column_list()" method can be used to represent these types
2894 of filters:
2895
2896 $worksheet->filter_column_list( 'A', 'March', 'April', 'May' );
2897
2898 The $column parameter can either be a zero indexed column number or a
2899 string column name.
2900
2901 One or more criteria can be selected:
2902
2903 $worksheet->filter_column_list( 0, 'March' );
2904 $worksheet->filter_column_list( 1, 100, 110, 120, 130 );
2905
2906 NOTE: It isn't sufficient to just specify the filter condition. You
2907 must also hide any rows that don't match the filter condition. Rows are
2908 hidden using the "set_row()" "visible" parameter. "Excel::Writer::XLSX"
2909 cannot do this automatically since it isn't part of the file format.
2910 See the "autofilter.pl" program in the examples directory of the distro
2911 for an example.
2912
2913 convert_date_time( $date_string )
2914 The "convert_date_time()" method is used internally by the
2915 "write_date_time()" method to convert date strings to a number that
2916 represents an Excel date and time.
2917
2918 It is exposed as a public method for utility purposes.
2919
2920 The $date_string format is detailed in the "write_date_time()" method.
2921
2922 set_vba_name()
2923 The Worksheet "set_vba_name()" method can be used to set the VBA
2924 codename for the worksheet (there is a similar method for the workbook
2925 VBA name). This is sometimes required when a "vbaProject" macro
2926 included via "add_vba_project()" refers to the worksheet. The default
2927 Excel VBA name of "Sheet1", etc., is used if a user defined name isn't
2928 specified.
2929
2930 See also "WORKING WITH VBA MACROS".
2931
2932 ignore_errors()
2933 The "ignore_errors()" method can be used to ignore various worksheet
2934 cell errors/warnings. For example the following code writes a string
2935 that looks like a number:
2936
2937 $worksheet->write_string('D2', '123');
2938
2939 This causes Excel to display a small green triangle in the top left
2940 hand corner of the cell to indicate an error/warning.
2941
2942 Sometimes these warnings are useful indicators that there is an issue
2943 in the spreadsheet but sometimes it is preferable to turn them off.
2944 Warnings can be turned off at the Excel level for all workbooks and
2945 worksheets by using the using "Excel options -> Formulas -> Error
2946 checking rules". Alternatively you can turn them off for individual
2947 cells in a worksheet, or ranges of cells, using the "ignore_errors()"
2948 method with a hashref of options and ranges like this:
2949
2950 $worksheet->ignore_errors({number_stored_as_text => 'A1:H50'});
2951
2952 # Or for more than one option:
2953 $worksheet->ignore_errors({number_stored_as_text => 'A1:H50',
2954 eval_error => 'A1:H50'});
2955
2956 The range can be a single cell, a range of cells, or multiple cells and
2957 ranges separated by spaces:
2958
2959 # Single cell.
2960 $worksheet->ignore_errors({eval_error => 'C6'});
2961
2962 # Or a single range:
2963 $worksheet->ignore_errors({eval_error => 'C6:G8'});
2964
2965 # Or multiple cells and ranges:
2966 $worksheet->ignore_errors({eval_error => 'C6 E6 G1:G20 J2:J6'});
2967
2968 Note: calling "ignore_errors" multiple times will overwrite the
2969 previous settings.
2970
2971 You can turn off warnings for an entire column by specifying the range
2972 from the first cell in the column to the last cell in the column:
2973
2974 $worksheet->ignore_errors({number_stored_as_text => 'A1:A1048576'});
2975
2976 Or for the entire worksheet by specifying the range from the first cell
2977 in the worksheet to the last cell in the worksheet:
2978
2979 $worksheet->ignore_errors({number_stored_as_text => 'A1:XFD1048576'});
2980
2981 The worksheet errors/warnings that can be ignored are:
2982
2983 • "number_stored_as_text": Turn off errors/warnings for numbers
2984 stores as text.
2985
2986 • "eval_error": Turn off errors/warnings for formula errors (such as
2987 divide by zero).
2988
2989 • "formula_differs": Turn off errors/warnings for formulas that
2990 differ from surrounding formulas.
2991
2992 • "formula_range": Turn off errors/warnings for formulas that omit
2993 cells in a range.
2994
2995 • "formula_unlocked": Turn off errors/warnings for unlocked cells
2996 that contain formulas.
2997
2998 • "empty_cell_reference": Turn off errors/warnings for formulas that
2999 refer to empty cells.
3000
3001 • "list_data_validation": Turn off errors/warnings for cells in a
3002 table that do not comply with applicable data validation rules.
3003
3004 • "calculated_column": Turn off errors/warnings for cell formulas
3005 that differ from the column formula.
3006
3007 • "two_digit_text_year": Turn off errors/warnings for formulas that
3008 contain a two digit text representation of a year.
3009
3011 Page set-up methods affect the way that a worksheet looks when it is
3012 printed. They control features such as page headers and footers and
3013 margins. These methods are really just standard worksheet methods. They
3014 are documented here in a separate section for the sake of clarity.
3015
3016 The following methods are available for page set-up:
3017
3018 set_landscape()
3019 set_portrait()
3020 set_page_view()
3021 set_paper()
3022 center_horizontally()
3023 center_vertically()
3024 set_margins()
3025 set_header()
3026 set_footer()
3027 repeat_rows()
3028 repeat_columns()
3029 hide_gridlines()
3030 print_row_col_headers()
3031 print_area()
3032 print_across()
3033 fit_to_pages()
3034 set_start_page()
3035 set_print_scale()
3036 print_black_and_white()
3037 set_h_pagebreaks()
3038 set_v_pagebreaks()
3039
3040 A common requirement when working with Excel::Writer::XLSX is to apply
3041 the same page set-up features to all of the worksheets in a workbook.
3042 To do this you can use the "sheets()" method of the "workbook" class to
3043 access the array of worksheets in a workbook:
3044
3045 for $worksheet ( $workbook->sheets() ) {
3046 $worksheet->set_landscape();
3047 }
3048
3049 set_landscape()
3050 This method is used to set the orientation of a worksheet's printed
3051 page to landscape:
3052
3053 $worksheet->set_landscape(); # Landscape mode
3054
3055 set_portrait()
3056 This method is used to set the orientation of a worksheet's printed
3057 page to portrait. The default worksheet orientation is portrait, so you
3058 won't generally need to call this method.
3059
3060 $worksheet->set_portrait(); # Portrait mode
3061
3062 set_page_view()
3063 This method is used to display the worksheet in "Page View/Layout"
3064 mode.
3065
3066 $worksheet->set_page_view();
3067
3068 set_paper( $index )
3069 This method is used to set the paper format for the printed output of a
3070 worksheet. The following paper styles are available:
3071
3072 Index Paper format Paper size
3073 ===== ============ ==========
3074 0 Printer default -
3075 1 Letter 8 1/2 x 11 in
3076 2 Letter Small 8 1/2 x 11 in
3077 3 Tabloid 11 x 17 in
3078 4 Ledger 17 x 11 in
3079 5 Legal 8 1/2 x 14 in
3080 6 Statement 5 1/2 x 8 1/2 in
3081 7 Executive 7 1/4 x 10 1/2 in
3082 8 A3 297 x 420 mm
3083 9 A4 210 x 297 mm
3084 10 A4 Small 210 x 297 mm
3085 11 A5 148 x 210 mm
3086 12 B4 250 x 354 mm
3087 13 B5 182 x 257 mm
3088 14 Folio 8 1/2 x 13 in
3089 15 Quarto 215 x 275 mm
3090 16 - 10x14 in
3091 17 - 11x17 in
3092 18 Note 8 1/2 x 11 in
3093 19 Envelope 9 3 7/8 x 8 7/8
3094 20 Envelope 10 4 1/8 x 9 1/2
3095 21 Envelope 11 4 1/2 x 10 3/8
3096 22 Envelope 12 4 3/4 x 11
3097 23 Envelope 14 5 x 11 1/2
3098 24 C size sheet -
3099 25 D size sheet -
3100 26 E size sheet -
3101 27 Envelope DL 110 x 220 mm
3102 28 Envelope C3 324 x 458 mm
3103 29 Envelope C4 229 x 324 mm
3104 30 Envelope C5 162 x 229 mm
3105 31 Envelope C6 114 x 162 mm
3106 32 Envelope C65 114 x 229 mm
3107 33 Envelope B4 250 x 353 mm
3108 34 Envelope B5 176 x 250 mm
3109 35 Envelope B6 176 x 125 mm
3110 36 Envelope 110 x 230 mm
3111 37 Monarch 3.875 x 7.5 in
3112 38 Envelope 3 5/8 x 6 1/2 in
3113 39 Fanfold 14 7/8 x 11 in
3114 40 German Std Fanfold 8 1/2 x 12 in
3115 41 German Legal Fanfold 8 1/2 x 13 in
3116
3117 Note, it is likely that not all of these paper types will be available
3118 to the end user since it will depend on the paper formats that the
3119 user's printer supports. Therefore, it is best to stick to standard
3120 paper types.
3121
3122 $worksheet->set_paper( 1 ); # US Letter
3123 $worksheet->set_paper( 9 ); # A4
3124
3125 If you do not specify a paper type the worksheet will print using the
3126 printer's default paper.
3127
3128 center_horizontally()
3129 Center the worksheet data horizontally between the margins on the
3130 printed page:
3131
3132 $worksheet->center_horizontally();
3133
3134 center_vertically()
3135 Center the worksheet data vertically between the margins on the printed
3136 page:
3137
3138 $worksheet->center_vertically();
3139
3140 set_margins( $inches )
3141 There are several methods available for setting the worksheet margins
3142 on the printed page:
3143
3144 set_margins() # Set all margins to the same value
3145 set_margins_LR() # Set left and right margins to the same value
3146 set_margins_TB() # Set top and bottom margins to the same value
3147 set_margin_left(); # Set left margin
3148 set_margin_right(); # Set right margin
3149 set_margin_top(); # Set top margin
3150 set_margin_bottom(); # Set bottom margin
3151
3152 All of these methods take a distance in inches as a parameter. Note: 1
3153 inch = 25.4mm. ";-)" The default left and right margin is 0.7 inch. The
3154 default top and bottom margin is 0.75 inch. Note, these defaults are
3155 different from the defaults used in the binary file format by
3156 Spreadsheet::WriteExcel.
3157
3158 set_header( $string, $margin )
3159 Headers and footers are generated using a $string which is a
3160 combination of plain text and control characters. The $margin parameter
3161 is optional.
3162
3163 The available control character are:
3164
3165 Control Category Description
3166 ======= ======== ===========
3167 &L Justification Left
3168 &C Center
3169 &R Right
3170
3171 &P Information Page number
3172 &N Total number of pages
3173 &D Date
3174 &T Time
3175 &F File name
3176 &A Worksheet name
3177 &Z Workbook path
3178
3179 &fontsize Font Font size
3180 &"font,style" Font name and style
3181 &U Single underline
3182 &E Double underline
3183 &S Strikethrough
3184 &X Superscript
3185 &Y Subscript
3186
3187 &[Picture] Images Image placeholder
3188 &G Same as &[Picture]
3189
3190 && Miscellaneous Literal ampersand &
3191
3192 Text in headers and footers can be justified (aligned) to the left,
3193 center and right by prefixing the text with the control characters &L,
3194 &C and &R.
3195
3196 For example (with ASCII art representation of the results):
3197
3198 $worksheet->set_header('&LHello');
3199
3200 ---------------------------------------------------------------
3201 | |
3202 | Hello |
3203 | |
3204
3205
3206 $worksheet->set_header('&CHello');
3207
3208 ---------------------------------------------------------------
3209 | |
3210 | Hello |
3211 | |
3212
3213
3214 $worksheet->set_header('&RHello');
3215
3216 ---------------------------------------------------------------
3217 | |
3218 | Hello |
3219 | |
3220
3221 For simple text, if you do not specify any justification the text will
3222 be centred. However, you must prefix the text with &C if you specify a
3223 font name or any other formatting:
3224
3225 $worksheet->set_header('Hello');
3226
3227 ---------------------------------------------------------------
3228 | |
3229 | Hello |
3230 | |
3231
3232 You can have text in each of the justification regions:
3233
3234 $worksheet->set_header('&LCiao&CBello&RCielo');
3235
3236 ---------------------------------------------------------------
3237 | |
3238 | Ciao Bello Cielo |
3239 | |
3240
3241 The information control characters act as variables that Excel will
3242 update as the workbook or worksheet changes. Times and dates are in the
3243 users default format:
3244
3245 $worksheet->set_header('&CPage &P of &N');
3246
3247 ---------------------------------------------------------------
3248 | |
3249 | Page 1 of 6 |
3250 | |
3251
3252
3253 $worksheet->set_header('&CUpdated at &T');
3254
3255 ---------------------------------------------------------------
3256 | |
3257 | Updated at 12:30 PM |
3258 | |
3259
3260 Images can be inserted using the options shown below. Each image must
3261 have a placeholder in header string using the "&[Picture]" or &G
3262 control characters:
3263
3264 $worksheet->set_header( '&L&G', 0.3, { image_left => 'logo.jpg' });
3265
3266 You can specify the font size of a section of the text by prefixing it
3267 with the control character &n where "n" is the font size:
3268
3269 $worksheet1->set_header( '&C&30Hello Big' );
3270 $worksheet2->set_header( '&C&10Hello Small' );
3271
3272 You can specify the font of a section of the text by prefixing it with
3273 the control sequence "&"font,style"" where "fontname" is a font name
3274 such as "Courier New" or "Times New Roman" and "style" is one of the
3275 standard Windows font descriptions: "Regular", "Italic", "Bold" or
3276 "Bold Italic":
3277
3278 $worksheet1->set_header( '&C&"Courier New,Italic"Hello' );
3279 $worksheet2->set_header( '&C&"Courier New,Bold Italic"Hello' );
3280 $worksheet3->set_header( '&C&"Times New Roman,Regular"Hello' );
3281
3282 It is possible to combine all of these features together to create
3283 sophisticated headers and footers. As an aid to setting up complicated
3284 headers and footers you can record a page set-up as a macro in Excel
3285 and look at the format strings that VBA produces. Remember however that
3286 VBA uses two double quotes "" to indicate a single double quote. For
3287 the last example above the equivalent VBA code looks like this:
3288
3289 .LeftHeader = ""
3290 .CenterHeader = "&""Times New Roman,Regular""Hello"
3291 .RightHeader = ""
3292
3293 To include a single literal ampersand "&" in a header or footer you
3294 should use a double ampersand "&&":
3295
3296 $worksheet1->set_header('&CCuriouser && Curiouser - Attorneys at Law');
3297
3298 As stated above the margin parameter is optional. As with the other
3299 margins the value should be in inches. The default header and footer
3300 margin is 0.3 inch. Note, the default margin is different from the
3301 default used in the binary file format by Spreadsheet::WriteExcel. The
3302 header and footer margin size can be set as follows:
3303
3304 $worksheet->set_header( '&CHello', 0.75 );
3305
3306 The header and footer margins are independent of the top and bottom
3307 margins.
3308
3309 The available options are:
3310
3311 • "image_left" The path to the image. Requires a &G or "&[Picture]"
3312 placeholder.
3313
3314 • "image_center" Same as above.
3315
3316 • "image_right" Same as above.
3317
3318 • "scale_with_doc" Scale header with document. Defaults to true.
3319
3320 • "align_with_margins" Align header to margins. Defaults to true.
3321
3322 The image options must have an accompanying "&[Picture]" or &G control
3323 character in the header string:
3324
3325 $worksheet->set_header(
3326 '&L&[Picture]&C&[Picture]&R&[Picture]',
3327 undef, # If you don't want to change the margin.
3328 {
3329 image_left => 'red.jpg',
3330 image_center => 'blue.jpg',
3331 image_right => 'yellow.jpg'
3332 }
3333 );
3334
3335 Note, the header or footer string must be less than 255 characters.
3336 Strings longer than this will not be written and a warning will be
3337 generated.
3338
3339 The "set_header()" method can also handle Unicode strings in "UTF-8"
3340 format.
3341
3342 $worksheet->set_header( "&C\x{263a}" )
3343
3344 See, also the "headers.pl" program in the "examples" directory of the
3345 distribution.
3346
3347 set_footer( $string, $margin )
3348 The syntax of the "set_footer()" method is the same as "set_header()",
3349 see above.
3350
3351 repeat_rows( $first_row, $last_row )
3352 Set the number of rows to repeat at the top of each printed page.
3353
3354 For large Excel documents it is often desirable to have the first row
3355 or rows of the worksheet print out at the top of each page. This can be
3356 achieved by using the "repeat_rows()" method. The parameters $first_row
3357 and $last_row are zero based. The $last_row parameter is optional if
3358 you only wish to specify one row:
3359
3360 $worksheet1->repeat_rows( 0 ); # Repeat the first row
3361 $worksheet2->repeat_rows( 0, 1 ); # Repeat the first two rows
3362
3363 repeat_columns( $first_col, $last_col )
3364 Set the columns to repeat at the left hand side of each printed page.
3365
3366 For large Excel documents it is often desirable to have the first
3367 column or columns of the worksheet print out at the left hand side of
3368 each page. This can be achieved by using the "repeat_columns()" method.
3369 The parameters $first_column and $last_column are zero based. The
3370 $last_column parameter is optional if you only wish to specify one
3371 column. You can also specify the columns using A1 column notation, see
3372 the note about "Cell notation".
3373
3374 $worksheet1->repeat_columns( 0 ); # Repeat the first column
3375 $worksheet2->repeat_columns( 0, 1 ); # Repeat the first two columns
3376 $worksheet3->repeat_columns( 'A:A' ); # Repeat the first column
3377 $worksheet4->repeat_columns( 'A:B' ); # Repeat the first two columns
3378
3379 hide_gridlines( $option )
3380 This method is used to hide the gridlines on the screen and printed
3381 page. Gridlines are the lines that divide the cells on a worksheet.
3382 Screen and printed gridlines are turned on by default in an Excel
3383 worksheet. If you have defined your own cell borders you may wish to
3384 hide the default gridlines.
3385
3386 $worksheet->hide_gridlines();
3387
3388 The following values of $option are valid:
3389
3390 0 : Don't hide gridlines
3391 1 : Hide printed gridlines only
3392 2 : Hide screen and printed gridlines
3393
3394 If you don't supply an argument or use "undef" the default option is 1,
3395 i.e. only the printed gridlines are hidden.
3396
3397 print_row_col_headers()
3398 Set the option to print the row and column headers on the printed page.
3399
3400 An Excel worksheet looks something like the following;
3401
3402 ------------------------------------------
3403 | | A | B | C | D | ...
3404 ------------------------------------------
3405 | 1 | | | | | ...
3406 | 2 | | | | | ...
3407 | 3 | | | | | ...
3408 | 4 | | | | | ...
3409 |...| ... | ... | ... | ... | ...
3410
3411 The headers are the letters and numbers at the top and the left of the
3412 worksheet. Since these headers serve mainly as a indication of position
3413 on the worksheet they generally do not appear on the printed page. If
3414 you wish to have them printed you can use the "print_row_col_headers()"
3415 method:
3416
3417 $worksheet->print_row_col_headers();
3418
3419 Do not confuse these headers with page headers as described in the
3420 "set_header()" section above.
3421
3422 hide_row_col_headers()
3423 Similar to "print_row_col_headers()" above but set the option to hide
3424 the row and column headers within Excel so that they aren't visible to
3425 the user:
3426
3427 $worksheet->hide_row_col_headers();
3428
3429 print_area( $first_row, $first_col, $last_row, $last_col )
3430 This method is used to specify the area of the worksheet that will be
3431 printed. All four parameters must be specified. You can also use A1
3432 notation, see the note about "Cell notation".
3433
3434 $worksheet1->print_area( 'A1:H20' ); # Cells A1 to H20
3435 $worksheet2->print_area( 0, 0, 19, 7 ); # The same
3436 $worksheet2->print_area( 'A:H' ); # Columns A to H if rows have data
3437
3438 print_across()
3439 The "print_across" method is used to change the default print
3440 direction. This is referred to by Excel as the sheet "page order".
3441
3442 $worksheet->print_across();
3443
3444 The default page order is shown below for a worksheet that extends over
3445 4 pages. The order is called "down then across":
3446
3447 [1] [3]
3448 [2] [4]
3449
3450 However, by using the "print_across" method the print order will be
3451 changed to "across then down":
3452
3453 [1] [2]
3454 [3] [4]
3455
3456 fit_to_pages( $width, $height )
3457 The "fit_to_pages()" method is used to fit the printed area to a
3458 specific number of pages both vertically and horizontally. If the
3459 printed area exceeds the specified number of pages it will be scaled
3460 down to fit. This guarantees that the printed area will always appear
3461 on the specified number of pages even if the page size or margins
3462 change.
3463
3464 $worksheet1->fit_to_pages( 1, 1 ); # Fit to 1x1 pages
3465 $worksheet2->fit_to_pages( 2, 1 ); # Fit to 2x1 pages
3466 $worksheet3->fit_to_pages( 1, 2 ); # Fit to 1x2 pages
3467
3468 The print area can be defined using the "print_area()" method as
3469 described above.
3470
3471 A common requirement is to fit the printed output to n pages wide but
3472 have the height be as long as necessary. To achieve this set the
3473 $height to zero:
3474
3475 $worksheet1->fit_to_pages( 1, 0 ); # 1 page wide and as long as necessary
3476
3477 Note that although it is valid to use both "fit_to_pages()" and
3478 "set_print_scale()" on the same worksheet only one of these options can
3479 be active at a time. The last method call made will set the active
3480 option.
3481
3482 Note that "fit_to_pages()" will override any manual page breaks that
3483 are defined in the worksheet.
3484
3485 Note: When using "fit_to_pages()" it may also be required to set the
3486 printer paper size using "set_paper()" or else Excel will default to
3487 "US Letter".
3488
3489 set_start_page( $start_page )
3490 The "set_start_page()" method is used to set the number of the starting
3491 page when the worksheet is printed out. The default value is 1.
3492
3493 $worksheet->set_start_page( 2 );
3494
3495 set_print_scale( $scale )
3496 Set the scale factor of the printed page. Scale factors in the range
3497 "10 <= $scale <= 400" are valid:
3498
3499 $worksheet1->set_print_scale( 50 );
3500 $worksheet2->set_print_scale( 75 );
3501 $worksheet3->set_print_scale( 300 );
3502 $worksheet4->set_print_scale( 400 );
3503
3504 The default scale factor is 100. Note, "set_print_scale()" does not
3505 affect the scale of the visible page in Excel. For that you should use
3506 "set_zoom()".
3507
3508 Note also that although it is valid to use both "fit_to_pages()" and
3509 "set_print_scale()" on the same worksheet only one of these options can
3510 be active at a time. The last method call made will set the active
3511 option.
3512
3513 print_black_and_white()
3514 Set the option to print the worksheet in black and white:
3515
3516 $worksheet->print_black_and_white();
3517
3518 set_h_pagebreaks( @breaks )
3519 Add horizontal page breaks to a worksheet. A page break causes all the
3520 data that follows it to be printed on the next page. Horizontal page
3521 breaks act between rows. To create a page break between rows 20 and 21
3522 you must specify the break at row 21. However in zero index notation
3523 this is actually row 20. So you can pretend for a small while that you
3524 are using 1 index notation:
3525
3526 $worksheet1->set_h_pagebreaks( 20 ); # Break between row 20 and 21
3527
3528 The "set_h_pagebreaks()" method will accept a list of page breaks and
3529 you can call it more than once:
3530
3531 $worksheet2->set_h_pagebreaks( 20, 40, 60, 80, 100 ); # Add breaks
3532 $worksheet2->set_h_pagebreaks( 120, 140, 160, 180, 200 ); # Add some more
3533
3534 Note: If you specify the "fit to page" option via the "fit_to_pages()"
3535 method it will override all manual page breaks.
3536
3537 There is a silent limitation of about 1000 horizontal page breaks per
3538 worksheet in line with an Excel internal limitation.
3539
3540 set_v_pagebreaks( @breaks )
3541 Add vertical page breaks to a worksheet. A page break causes all the
3542 data that follows it to be printed on the next page. Vertical page
3543 breaks act between columns. To create a page break between columns 20
3544 and 21 you must specify the break at column 21. However in zero index
3545 notation this is actually column 20. So you can pretend for a small
3546 while that you are using 1 index notation:
3547
3548 $worksheet1->set_v_pagebreaks(20); # Break between column 20 and 21
3549
3550 The "set_v_pagebreaks()" method will accept a list of page breaks and
3551 you can call it more than once:
3552
3553 $worksheet2->set_v_pagebreaks( 20, 40, 60, 80, 100 ); # Add breaks
3554 $worksheet2->set_v_pagebreaks( 120, 140, 160, 180, 200 ); # Add some more
3555
3556 Note: If you specify the "fit to page" option via the "fit_to_pages()"
3557 method it will override all manual page breaks.
3558
3560 This section describes the methods and properties that are available
3561 for formatting cells in Excel. The properties of a cell that can be
3562 formatted include: fonts, colours, patterns, borders, alignment and
3563 number formatting.
3564
3565 Creating and using a Format object
3566 Cell formatting is defined through a Format object. Format objects are
3567 created by calling the workbook "add_format()" method as follows:
3568
3569 my $format1 = $workbook->add_format(); # Set properties later
3570 my $format2 = $workbook->add_format( %props ); # Set at creation
3571
3572 The format object holds all the formatting properties that can be
3573 applied to a cell, a row or a column. The process of setting these
3574 properties is discussed in the next section.
3575
3576 Once a Format object has been constructed and its properties have been
3577 set it can be passed as an argument to the worksheet "write" methods as
3578 follows:
3579
3580 $worksheet->write( 0, 0, 'One', $format );
3581 $worksheet->write_string( 1, 0, 'Two', $format );
3582 $worksheet->write_number( 2, 0, 3, $format );
3583 $worksheet->write_blank( 3, 0, $format );
3584
3585 Formats can also be passed to the worksheet "set_row()" and
3586 "set_column()" methods to define the default property for a row or
3587 column.
3588
3589 $worksheet->set_row( 0, 15, $format );
3590 $worksheet->set_column( 0, 0, 15, $format );
3591
3592 Format methods and Format properties
3593 The following table shows the Excel format categories, the formatting
3594 properties that can be applied and the equivalent object method:
3595
3596 Category Description Property Method Name
3597 -------- ----------- -------- -----------
3598 Font Font type font set_font()
3599 Font size size set_size()
3600 Font color color set_color()
3601 Bold bold set_bold()
3602 Italic italic set_italic()
3603 Underline underline set_underline()
3604 Strikeout font_strikeout set_font_strikeout()
3605 Super/Subscript font_script set_font_script()
3606 Outline font_outline set_font_outline()
3607 Shadow font_shadow set_font_shadow()
3608
3609 Number Numeric format num_format set_num_format()
3610
3611 Protection Lock cells locked set_locked()
3612 Hide formulas hidden set_hidden()
3613
3614 Alignment Horizontal align align set_align()
3615 Vertical align valign set_align()
3616 Rotation rotation set_rotation()
3617 Text wrap text_wrap set_text_wrap()
3618 Justify last text_justlast set_text_justlast()
3619 Center across center_across set_center_across()
3620 Indentation indent set_indent()
3621 Shrink to fit shrink set_shrink()
3622
3623 Pattern Cell pattern pattern set_pattern()
3624 Background color bg_color set_bg_color()
3625 Foreground color fg_color set_fg_color()
3626
3627 Border Cell border border set_border()
3628 Bottom border bottom set_bottom()
3629 Top border top set_top()
3630 Left border left set_left()
3631 Right border right set_right()
3632 Border color border_color set_border_color()
3633 Bottom color bottom_color set_bottom_color()
3634 Top color top_color set_top_color()
3635 Left color left_color set_left_color()
3636 Right color right_color set_right_color()
3637 Diagonal type diag_type set_diag_type()
3638 Diagonal border diag_border set_diag_border()
3639 Diagonal color diag_color set_diag_color()
3640
3641 There are two ways of setting Format properties: by using the object
3642 method interface or by setting the property directly. For example, a
3643 typical use of the method interface would be as follows:
3644
3645 my $format = $workbook->add_format();
3646 $format->set_bold();
3647 $format->set_color( 'red' );
3648
3649 By comparison the properties can be set directly by passing a hash of
3650 properties to the Format constructor:
3651
3652 my $format = $workbook->add_format( bold => 1, color => 'red' );
3653
3654 or after the Format has been constructed by means of the
3655 "set_format_properties()" method as follows:
3656
3657 my $format = $workbook->add_format();
3658 $format->set_format_properties( bold => 1, color => 'red' );
3659
3660 You can also store the properties in one or more named hashes and pass
3661 them to the required method:
3662
3663 my %font = (
3664 font => 'Calibri',
3665 size => 12,
3666 color => 'blue',
3667 bold => 1,
3668 );
3669
3670 my %shading = (
3671 bg_color => 'green',
3672 pattern => 1,
3673 );
3674
3675
3676 my $format1 = $workbook->add_format( %font ); # Font only
3677 my $format2 = $workbook->add_format( %font, %shading ); # Font and shading
3678
3679 The provision of two ways of setting properties might lead you to
3680 wonder which is the best way. The method mechanism may be better if you
3681 prefer setting properties via method calls (which the author did when
3682 the code was first written) otherwise passing properties to the
3683 constructor has proved to be a little more flexible and self
3684 documenting in practice. An additional advantage of working with
3685 property hashes is that it allows you to share formatting between
3686 workbook objects as shown in the example above.
3687
3688 The Perl/Tk style of adding properties is also supported:
3689
3690 my %font = (
3691 -font => 'Calibri',
3692 -size => 12,
3693 -color => 'blue',
3694 -bold => 1,
3695 );
3696
3697 Working with formats
3698 The default format is Calibri 11 with all other properties off.
3699
3700 Each unique format in Excel::Writer::XLSX must have a corresponding
3701 Format object. It isn't possible to use a Format with a write() method
3702 and then redefine the Format for use at a later stage. This is because
3703 a Format is applied to a cell not in its current state but in its final
3704 state. Consider the following example:
3705
3706 my $format = $workbook->add_format();
3707 $format->set_bold();
3708 $format->set_color( 'red' );
3709 $worksheet->write( 'A1', 'Cell A1', $format );
3710 $format->set_color( 'green' );
3711 $worksheet->write( 'B1', 'Cell B1', $format );
3712
3713 Cell A1 is assigned the Format $format which is initially set to the
3714 colour red. However, the colour is subsequently set to green. When
3715 Excel displays Cell A1 it will display the final state of the Format
3716 which in this case will be the colour green.
3717
3718 In general a method call without an argument will turn a property on,
3719 for example:
3720
3721 my $format1 = $workbook->add_format();
3722 $format1->set_bold(); # Turns bold on
3723 $format1->set_bold( 1 ); # Also turns bold on
3724 $format1->set_bold( 0 ); # Turns bold off
3725
3727 The Format object methods are described in more detail in the following
3728 sections. In addition, there is a Perl program called "formats.pl" in
3729 the "examples" directory of the WriteExcel distribution. This program
3730 creates an Excel workbook called "formats.xlsx" which contains examples
3731 of almost all the format types.
3732
3733 The following Format methods are available:
3734
3735 set_font()
3736 set_size()
3737 set_color()
3738 set_bold()
3739 set_italic()
3740 set_underline()
3741 set_font_strikeout()
3742 set_font_script()
3743 set_font_outline()
3744 set_font_shadow()
3745 set_num_format()
3746 set_locked()
3747 set_hidden()
3748 set_align()
3749 set_rotation()
3750 set_text_wrap()
3751 set_text_justlast()
3752 set_center_across()
3753 set_indent()
3754 set_shrink()
3755 set_pattern()
3756 set_bg_color()
3757 set_fg_color()
3758 set_border()
3759 set_bottom()
3760 set_top()
3761 set_left()
3762 set_right()
3763 set_border_color()
3764 set_bottom_color()
3765 set_top_color()
3766 set_left_color()
3767 set_right_color()
3768 set_diag_type()
3769 set_diag_border()
3770 set_diag_color()
3771
3772 The above methods can also be applied directly as properties. For
3773 example "$format->set_bold()" is equivalent to
3774 "$workbook->add_format(bold => 1)".
3775
3776 set_format_properties( %properties )
3777 The properties of an existing Format object can be also be set by means
3778 of "set_format_properties()":
3779
3780 my $format = $workbook->add_format();
3781 $format->set_format_properties( bold => 1, color => 'red' );
3782
3783 However, this method is here mainly for legacy reasons. It is
3784 preferable to set the properties in the format constructor:
3785
3786 my $format = $workbook->add_format( bold => 1, color => 'red' );
3787
3788 set_font( $fontname )
3789 Default state: Font is Calibri
3790 Default action: None
3791 Valid args: Any valid font name
3792
3793 Specify the font used:
3794
3795 $format->set_font('Times New Roman');
3796
3797 Excel can only display fonts that are installed on the system that it
3798 is running on. Therefore it is best to use the fonts that come as
3799 standard such as 'Calibri', 'Times New Roman' and 'Courier New'. See
3800 also the Fonts worksheet created by formats.pl
3801
3802 set_size()
3803 Default state: Font size is 10
3804 Default action: Set font size to 1
3805 Valid args: Integer values from 1 to as big as your screen.
3806
3807 Set the font size. Excel adjusts the height of a row to accommodate the
3808 largest font size in the row. You can also explicitly specify the
3809 height of a row using the set_row() worksheet method.
3810
3811 my $format = $workbook->add_format();
3812 $format->set_size( 30 );
3813
3814 set_color()
3815 Default state: Excels default color, usually black
3816 Default action: Set the default color
3817 Valid args: Integers from 8..63 or the following strings:
3818 'black'
3819 'blue'
3820 'brown'
3821 'cyan'
3822 'gray'
3823 'green'
3824 'lime'
3825 'magenta'
3826 'navy'
3827 'orange'
3828 'pink'
3829 'purple'
3830 'red'
3831 'silver'
3832 'white'
3833 'yellow'
3834
3835 Set the font colour. The "set_color()" method is used as follows:
3836
3837 my $format = $workbook->add_format();
3838 $format->set_color( 'red' );
3839 $worksheet->write( 0, 0, 'wheelbarrow', $format );
3840
3841 Note: The "set_color()" method is used to set the colour of the font in
3842 a cell. To set the colour of a cell use the "set_bg_color()" and
3843 "set_pattern()" methods.
3844
3845 For additional examples see the 'Named colors' and 'Standard colors'
3846 worksheets created by formats.pl in the examples directory.
3847
3848 See also "WORKING WITH COLOURS".
3849
3850 set_bold()
3851 Default state: bold is off
3852 Default action: Turn bold on
3853 Valid args: 0, 1
3854
3855 Set the bold property of the font:
3856
3857 $format->set_bold(); # Turn bold on
3858
3859 set_italic()
3860 Default state: Italic is off
3861 Default action: Turn italic on
3862 Valid args: 0, 1
3863
3864 Set the italic property of the font:
3865
3866 $format->set_italic(); # Turn italic on
3867
3868 set_underline()
3869 Default state: Underline is off
3870 Default action: Turn on single underline
3871 Valid args: 0 = No underline
3872 1 = Single underline
3873 2 = Double underline
3874 33 = Single accounting underline
3875 34 = Double accounting underline
3876
3877 Set the underline property of the font.
3878
3879 $format->set_underline(); # Single underline
3880
3881 set_font_strikeout()
3882 Default state: Strikeout is off
3883 Default action: Turn strikeout on
3884 Valid args: 0, 1
3885
3886 Set the strikeout property of the font.
3887
3888 set_font_script()
3889 Default state: Super/Subscript is off
3890 Default action: Turn Superscript on
3891 Valid args: 0 = Normal
3892 1 = Superscript
3893 2 = Subscript
3894
3895 Set the superscript/subscript property of the font.
3896
3897 set_font_outline()
3898 Default state: Outline is off
3899 Default action: Turn outline on
3900 Valid args: 0, 1
3901
3902 Macintosh only.
3903
3904 set_font_shadow()
3905 Default state: Shadow is off
3906 Default action: Turn shadow on
3907 Valid args: 0, 1
3908
3909 Macintosh only.
3910
3911 set_num_format()
3912 Default state: General format
3913 Default action: Format index 1
3914 Valid args: See the following table
3915
3916 This method is used to define the numerical format of a number in
3917 Excel. It controls whether a number is displayed as an integer, a
3918 floating point number, a date, a currency value or some other user
3919 defined format.
3920
3921 The numerical format of a cell can be specified by using a format
3922 string or an index to one of Excel's built-in formats:
3923
3924 my $format1 = $workbook->add_format();
3925 my $format2 = $workbook->add_format();
3926 $format1->set_num_format( 'd mmm yyyy' ); # Format string
3927 $format2->set_num_format( 0x0f ); # Format index
3928
3929 $worksheet->write( 0, 0, 36892.521, $format1 ); # 1 Jan 2001
3930 $worksheet->write( 0, 0, 36892.521, $format2 ); # 1-Jan-01
3931
3932 Using format strings you can define very sophisticated formatting of
3933 numbers.
3934
3935 $format01->set_num_format( '0.000' );
3936 $worksheet->write( 0, 0, 3.1415926, $format01 ); # 3.142
3937
3938 $format02->set_num_format( '#,##0' );
3939 $worksheet->write( 1, 0, 1234.56, $format02 ); # 1,235
3940
3941 $format03->set_num_format( '#,##0.00' );
3942 $worksheet->write( 2, 0, 1234.56, $format03 ); # 1,234.56
3943
3944 $format04->set_num_format( '$0.00' );
3945 $worksheet->write( 3, 0, 49.99, $format04 ); # $49.99
3946
3947 # Note you can use other currency symbols such as the pound or yen as well.
3948 # Other currencies may require the use of Unicode.
3949
3950 $format07->set_num_format( 'mm/dd/yy' );
3951 $worksheet->write( 6, 0, 36892.521, $format07 ); # 01/01/01
3952
3953 $format08->set_num_format( 'mmm d yyyy' );
3954 $worksheet->write( 7, 0, 36892.521, $format08 ); # Jan 1 2001
3955
3956 $format09->set_num_format( 'd mmmm yyyy' );
3957 $worksheet->write( 8, 0, 36892.521, $format09 ); # 1 January 2001
3958
3959 $format10->set_num_format( 'dd/mm/yyyy hh:mm AM/PM' );
3960 $worksheet->write( 9, 0, 36892.521, $format10 ); # 01/01/2001 12:30 AM
3961
3962 $format11->set_num_format( '0 "dollar and" .00 "cents"' );
3963 $worksheet->write( 10, 0, 1.87, $format11 ); # 1 dollar and .87 cents
3964
3965 # Conditional numerical formatting.
3966 $format12->set_num_format( '[Green]General;[Red]-General;General' );
3967 $worksheet->write( 11, 0, 123, $format12 ); # > 0 Green
3968 $worksheet->write( 12, 0, -45, $format12 ); # < 0 Red
3969 $worksheet->write( 13, 0, 0, $format12 ); # = 0 Default colour
3970
3971 # Zip code
3972 $format13->set_num_format( '00000' );
3973 $worksheet->write( 14, 0, '01209', $format13 );
3974
3975 The number system used for dates is described in "DATES AND TIME IN
3976 EXCEL".
3977
3978 The colour format should have one of the following values:
3979
3980 [Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow]
3981
3982 Alternatively you can specify the colour based on a colour index as
3983 follows: "[Color n]", where n is a standard Excel colour index - 7. See
3984 the 'Standard colors' worksheet created by formats.pl.
3985
3986 For more information refer to the documentation on formatting in the
3987 "docs" directory of the Excel::Writer::XLSX distro, the Excel on-line
3988 help or
3989 <http://office.microsoft.com/en-gb/assistance/HP051995001033.aspx>.
3990
3991 You should ensure that the format string is valid in Excel prior to
3992 using it in WriteExcel.
3993
3994 Excel's built-in formats are shown in the following table:
3995
3996 Index Index Format String
3997 0 0x00 General
3998 1 0x01 0
3999 2 0x02 0.00
4000 3 0x03 #,##0
4001 4 0x04 #,##0.00
4002 5 0x05 ($#,##0_);($#,##0)
4003 6 0x06 ($#,##0_);[Red]($#,##0)
4004 7 0x07 ($#,##0.00_);($#,##0.00)
4005 8 0x08 ($#,##0.00_);[Red]($#,##0.00)
4006 9 0x09 0%
4007 10 0x0a 0.00%
4008 11 0x0b 0.00E+00
4009 12 0x0c # ?/?
4010 13 0x0d # ??/??
4011 14 0x0e m/d/yy
4012 15 0x0f d-mmm-yy
4013 16 0x10 d-mmm
4014 17 0x11 mmm-yy
4015 18 0x12 h:mm AM/PM
4016 19 0x13 h:mm:ss AM/PM
4017 20 0x14 h:mm
4018 21 0x15 h:mm:ss
4019 22 0x16 m/d/yy h:mm
4020 .. .... ...........
4021 37 0x25 (#,##0_);(#,##0)
4022 38 0x26 (#,##0_);[Red](#,##0)
4023 39 0x27 (#,##0.00_);(#,##0.00)
4024 40 0x28 (#,##0.00_);[Red](#,##0.00)
4025 41 0x29 _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
4026 42 0x2a _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)
4027 43 0x2b _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)
4028 44 0x2c _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)
4029 45 0x2d mm:ss
4030 46 0x2e [h]:mm:ss
4031 47 0x2f mm:ss.0
4032 48 0x30 ##0.0E+0
4033 49 0x31 @
4034
4035 For examples of these formatting codes see the 'Numerical formats'
4036 worksheet created by formats.pl. See also the number_formats1.html and
4037 the number_formats2.html documents in the "docs" directory of the
4038 distro.
4039
4040 Note 1. Numeric formats 23 to 36 are not documented by Microsoft and
4041 may differ in international versions.
4042
4043 Note 2. The built-in formats are localised according to the locale
4044 settings (regional settings on Windows) of the user when opening the
4045 file in Excel:
4046
4047 • The dot appears as the defined local decimal separator.
4048
4049 • The comma appears as the defined local digit groups separator.
4050
4051 • The dollar sign appears as the defined local currency symbol.
4052
4053 • The date, time and duration formats appear as the local equivalent
4054 date or time format.
4055
4056 set_locked()
4057 Default state: Cell locking is on
4058 Default action: Turn locking on
4059 Valid args: 0, 1
4060
4061 This property can be used to prevent modification of a cells contents.
4062 Following Excel's convention, cell locking is turned on by default.
4063 However, it only has an effect if the worksheet has been protected, see
4064 the worksheet "protect()" method.
4065
4066 my $locked = $workbook->add_format();
4067 $locked->set_locked( 1 ); # A non-op
4068
4069 my $unlocked = $workbook->add_format();
4070 $locked->set_locked( 0 );
4071
4072 # Enable worksheet protection
4073 $worksheet->protect();
4074
4075 # This cell cannot be edited.
4076 $worksheet->write( 'A1', '=1+2', $locked );
4077
4078 # This cell can be edited.
4079 $worksheet->write( 'A2', '=1+2', $unlocked );
4080
4081 Note: This offers weak protection even with a password, see the note in
4082 relation to the "protect()" method.
4083
4084 set_hidden()
4085 Default state: Formula hiding is off
4086 Default action: Turn hiding on
4087 Valid args: 0, 1
4088
4089 This property is used to hide a formula while still displaying its
4090 result. This is generally used to hide complex calculations from end
4091 users who are only interested in the result. It only has an effect if
4092 the worksheet has been protected, see the worksheet "protect()" method.
4093
4094 my $hidden = $workbook->add_format();
4095 $hidden->set_hidden();
4096
4097 # Enable worksheet protection
4098 $worksheet->protect();
4099
4100 # The formula in this cell isn't visible
4101 $worksheet->write( 'A1', '=1+2', $hidden );
4102
4103 Note: This offers weak protection even with a password, see the note in
4104 relation to the "protect()" method.
4105
4106 set_align()
4107 Default state: Alignment is off
4108 Default action: Left alignment
4109 Valid args: 'left' Horizontal
4110 'center'
4111 'right'
4112 'fill'
4113 'justify'
4114 'center_across'
4115
4116 'top' Vertical
4117 'vcenter'
4118 'bottom'
4119 'vjustify'
4120
4121 This method is used to set the horizontal and vertical text alignment
4122 within a cell. Vertical and horizontal alignments can be combined. The
4123 method is used as follows:
4124
4125 my $format = $workbook->add_format();
4126 $format->set_align( 'center' );
4127 $format->set_align( 'vcenter' );
4128 $worksheet->set_row( 0, 30 );
4129 $worksheet->write( 0, 0, 'X', $format );
4130
4131 Text can be aligned across two or more adjacent cells using the
4132 "center_across" property. However, for genuine merged cells it is
4133 better to use the "merge_range()" worksheet method.
4134
4135 The "vjustify" (vertical justify) option can be used to provide
4136 automatic text wrapping in a cell. The height of the cell will be
4137 adjusted to accommodate the wrapped text. To specify where the text
4138 wraps use the "set_text_wrap()" method.
4139
4140 For further examples see the 'Alignment' worksheet created by
4141 formats.pl.
4142
4143 set_center_across()
4144 Default state: Center across selection is off
4145 Default action: Turn center across on
4146 Valid args: 1
4147
4148 Text can be aligned across two or more adjacent cells using the
4149 "set_center_across()" method. This is an alias for the
4150 "set_align('center_across')" method call.
4151
4152 Only one cell should contain the text, the other cells should be blank:
4153
4154 my $format = $workbook->add_format();
4155 $format->set_center_across();
4156
4157 $worksheet->write( 1, 1, 'Center across selection', $format );
4158 $worksheet->write_blank( 1, 2, $format );
4159
4160 See also the "merge1.pl" to "merge6.pl" programs in the "examples"
4161 directory and the "merge_range()" method.
4162
4163 set_text_wrap()
4164 Default state: Text wrap is off
4165 Default action: Turn text wrap on
4166 Valid args: 0, 1
4167
4168 Here is an example using the text wrap property, the escape character
4169 "\n" is used to indicate the end of line:
4170
4171 my $format = $workbook->add_format();
4172 $format->set_text_wrap();
4173 $worksheet->write( 0, 0, "It's\na bum\nwrap", $format );
4174
4175 Excel will adjust the height of the row to accommodate the wrapped
4176 text. A similar effect can be obtained without newlines using the
4177 "set_align('vjustify')" method. See the "textwrap.pl" program in the
4178 "examples" directory.
4179
4180 set_rotation()
4181 Default state: Text rotation is off
4182 Default action: None
4183 Valid args: Integers in the range -90 to 90 and 270
4184
4185 Set the rotation of the text in a cell. The rotation can be any angle
4186 in the range -90 to 90 degrees.
4187
4188 my $format = $workbook->add_format();
4189 $format->set_rotation( 30 );
4190 $worksheet->write( 0, 0, 'This text is rotated', $format );
4191
4192 The angle 270 is also supported. This indicates text where the letters
4193 run from top to bottom.
4194
4195 set_indent()
4196 Default state: Text indentation is off
4197 Default action: Indent text 1 level
4198 Valid args: Positive integers
4199
4200 This method can be used to indent text. The argument, which should be
4201 an integer, is taken as the level of indentation:
4202
4203 my $format = $workbook->add_format();
4204 $format->set_indent( 2 );
4205 $worksheet->write( 0, 0, 'This text is indented', $format );
4206
4207 Indentation is a horizontal alignment property. It will override any
4208 other horizontal properties but it can be used in conjunction with
4209 vertical properties.
4210
4211 set_shrink()
4212 Default state: Text shrinking is off
4213 Default action: Turn "shrink to fit" on
4214 Valid args: 1
4215
4216 This method can be used to shrink text so that it fits in a cell.
4217
4218 my $format = $workbook->add_format();
4219 $format->set_shrink();
4220 $worksheet->write( 0, 0, 'Honey, I shrunk the text!', $format );
4221
4222 set_text_justlast()
4223 Default state: Justify last is off
4224 Default action: Turn justify last on
4225 Valid args: 0, 1
4226
4227 Only applies to Far Eastern versions of Excel.
4228
4229 set_pattern()
4230 Default state: Pattern is off
4231 Default action: Solid fill is on
4232 Valid args: 0 .. 18
4233
4234 Set the background pattern of a cell.
4235
4236 Examples of the available patterns are shown in the 'Patterns'
4237 worksheet created by formats.pl. However, it is unlikely that you will
4238 ever need anything other than Pattern 1 which is a solid fill of the
4239 background color.
4240
4241 set_bg_color()
4242 Default state: Color is off
4243 Default action: Solid fill.
4244 Valid args: See set_color()
4245
4246 The "set_bg_color()" method can be used to set the background colour of
4247 a pattern. Patterns are defined via the "set_pattern()" method. If a
4248 pattern hasn't been defined then a solid fill pattern is used as the
4249 default.
4250
4251 Here is an example of how to set up a solid fill in a cell:
4252
4253 my $format = $workbook->add_format();
4254
4255 $format->set_pattern(); # This is optional when using a solid fill
4256
4257 $format->set_bg_color( 'green' );
4258 $worksheet->write( 'A1', 'Ray', $format );
4259
4260 For further examples see the 'Patterns' worksheet created by
4261 formats.pl.
4262
4263 set_fg_color()
4264 Default state: Color is off
4265 Default action: Solid fill.
4266 Valid args: See set_color()
4267
4268 The "set_fg_color()" method can be used to set the foreground colour of
4269 a pattern.
4270
4271 For further examples see the 'Patterns' worksheet created by
4272 formats.pl.
4273
4274 set_border()
4275 Also applies to: set_bottom()
4276 set_top()
4277 set_left()
4278 set_right()
4279
4280 Default state: Border is off
4281 Default action: Set border type 1
4282 Valid args: 0-13, See below.
4283
4284 A cell border is comprised of a border on the bottom, top, left and
4285 right. These can be set to the same value using "set_border()" or
4286 individually using the relevant method calls shown above.
4287
4288 The following shows the border styles sorted by Excel::Writer::XLSX
4289 index number:
4290
4291 Index Name Weight Style
4292 ===== ============= ====== ===========
4293 0 None 0
4294 1 Continuous 1 -----------
4295 2 Continuous 2 -----------
4296 3 Dash 1 - - - - - -
4297 4 Dot 1 . . . . . .
4298 5 Continuous 3 -----------
4299 6 Double 3 ===========
4300 7 Continuous 0 -----------
4301 8 Dash 2 - - - - - -
4302 9 Dash Dot 1 - . - . - .
4303 10 Dash Dot 2 - . - . - .
4304 11 Dash Dot Dot 1 - . . - . .
4305 12 Dash Dot Dot 2 - . . - . .
4306 13 SlantDash Dot 2 / - . / - .
4307
4308 The following shows the borders sorted by style:
4309
4310 Name Weight Style Index
4311 ============= ====== =========== =====
4312 Continuous 0 ----------- 7
4313 Continuous 1 ----------- 1
4314 Continuous 2 ----------- 2
4315 Continuous 3 ----------- 5
4316 Dash 1 - - - - - - 3
4317 Dash 2 - - - - - - 8
4318 Dash Dot 1 - . - . - . 9
4319 Dash Dot 2 - . - . - . 10
4320 Dash Dot Dot 1 - . . - . . 11
4321 Dash Dot Dot 2 - . . - . . 12
4322 Dot 1 . . . . . . 4
4323 Double 3 =========== 6
4324 None 0 0
4325 SlantDash Dot 2 / - . / - . 13
4326
4327 The following shows the borders in the order shown in the Excel Dialog.
4328
4329 Index Style Index Style
4330 ===== ===== ===== =====
4331 0 None 12 - . . - . .
4332 7 ----------- 13 / - . / - .
4333 4 . . . . . . 10 - . - . - .
4334 11 - . . - . . 8 - - - - - -
4335 9 - . - . - . 2 -----------
4336 3 - - - - - - 5 -----------
4337 1 ----------- 6 ===========
4338
4339 Examples of the available border styles are shown in the 'Borders'
4340 worksheet created by formats.pl.
4341
4342 set_border_color()
4343 Also applies to: set_bottom_color()
4344 set_top_color()
4345 set_left_color()
4346 set_right_color()
4347
4348 Default state: Color is off
4349 Default action: Undefined
4350 Valid args: See set_color()
4351
4352 Set the colour of the cell borders. A cell border is comprised of a
4353 border on the bottom, top, left and right. These can be set to the same
4354 colour using "set_border_color()" or individually using the relevant
4355 method calls shown above. Examples of the border styles and colours are
4356 shown in the 'Borders' worksheet created by formats.pl.
4357
4358 set_diag_type()
4359 Default state: Diagonal border is off.
4360 Default action: None.
4361 Valid args: 1-3, See below.
4362
4363 Set the diagonal border type for the cell. Three types of diagonal
4364 borders are available in Excel:
4365
4366 1: From bottom left to top right.
4367 2: From top left to bottom right.
4368 3: Same as 1 and 2 combined.
4369
4370 For example:
4371
4372 $format->set_diag_type( 3 );
4373
4374 set_diag_border()
4375 Default state: Border is off
4376 Default action: Set border type 1
4377 Valid args: 0-13, See below.
4378
4379 Set the diagonal border style. Same as the parameter to "set_border()"
4380 above.
4381
4382 set_diag_color()
4383 Default state: Color is off
4384 Default action: Undefined
4385 Valid args: See set_color()
4386
4387 Set the colour of the diagonal cell border:
4388
4389 $format->set_diag_type( 3 );
4390 $format->set_diag_border( 7 );
4391 $format->set_diag_color( 'red' );
4392
4393 copy( $format )
4394 This method is used to copy all of the properties from one Format
4395 object to another:
4396
4397 my $lorry1 = $workbook->add_format();
4398 $lorry1->set_bold();
4399 $lorry1->set_italic();
4400 $lorry1->set_color( 'red' ); # lorry1 is bold, italic and red
4401
4402 my $lorry2 = $workbook->add_format();
4403 $lorry2->copy( $lorry1 );
4404 $lorry2->set_color( 'yellow' ); # lorry2 is bold, italic and yellow
4405
4406 The "copy()" method is only useful if you are using the method
4407 interface to Format properties. It generally isn't required if you are
4408 setting Format properties directly using hashes.
4409
4410 Note: this is not a copy constructor, both objects must exist prior to
4411 copying.
4412
4414 The following is a brief introduction to handling Unicode in
4415 "Excel::Writer::XLSX".
4416
4417 For a more general introduction to Unicode handling in Perl see
4418 perlunitut and perluniintro.
4419
4420 Excel::Writer::XLSX writer differs from Spreadsheet::WriteExcel in that
4421 it only handles Unicode data in "UTF-8" format and doesn't try to
4422 handle legacy UTF-16 Excel formats.
4423
4424 If the data is in "UTF-8" format then Excel::Writer::XLSX will handle
4425 it automatically.
4426
4427 If you are dealing with non-ASCII characters that aren't in "UTF-8"
4428 then perl provides useful tools in the guise of the "Encode" module to
4429 help you to convert to the required format. For example:
4430
4431 use Encode 'decode';
4432
4433 my $string = 'some string with koi8-r characters';
4434 $string = decode('koi8-r', $string); # koi8-r to utf8
4435
4436 Alternatively you can read data from an encoded file and convert it to
4437 "UTF-8" as you read it in:
4438
4439 my $file = 'unicode_koi8r.txt';
4440 open FH, '<:encoding(koi8-r)', $file or die "Couldn't open $file: $!\n";
4441
4442 my $row = 0;
4443 while ( <FH> ) {
4444 # Data read in is now in utf8 format.
4445 chomp;
4446 $worksheet->write( $row++, 0, $_ );
4447 }
4448
4449 These methodologies are explained in more detail in perlunitut,
4450 perluniintro and perlunicode.
4451
4452 If the program contains UTF-8 text then you will also need to add "use
4453 utf8" to the includes:
4454
4455 use utf8;
4456
4457 ...
4458
4459 $worksheet->write( 'A1', 'Some UTF-8 string' );
4460
4461 See also the "unicode_*.pl" programs in the examples directory of the
4462 distro.
4463
4465 Throughout Excel::Writer::XLSX colours can be specified using a Html
4466 style "#RRGGBB" value. For example with a Format object:
4467
4468 $format->set_color( '#FF0000' );
4469
4470 For backward compatibility a limited number of color names are
4471 supported:
4472
4473 $format->set_color( 'red' );
4474
4475 The color names supported are:
4476
4477 black
4478 blue
4479 brown
4480 cyan
4481 gray
4482 green
4483 lime
4484 magenta
4485 navy
4486 orange
4487 pink
4488 purple
4489 red
4490 silver
4491 white
4492 yellow
4493
4494 See also "colors.pl" in the "examples" directory.
4495
4497 There are two important things to understand about dates and times in
4498 Excel:
4499
4500 1 A date/time in Excel is a real number plus an Excel number format.
4501 2 Excel::Writer::XLSX doesn't automatically convert date/time strings
4502 in "write()" to an Excel date/time.
4503
4504 These two points are explained in more detail below along with some
4505 suggestions on how to convert times and dates to the required format.
4506
4507 An Excel date/time is a number plus a format
4508 If you write a date string with "write()" then all you will get is a
4509 string:
4510
4511 $worksheet->write( 'A1', '02/03/04' ); # !! Writes a string not a date. !!
4512
4513 Dates and times in Excel are represented by real numbers, for example
4514 "Jan 1 2001 12:30 AM" is represented by the number 36892.521.
4515
4516 The integer part of the number stores the number of days since the
4517 epoch and the fractional part stores the percentage of the day.
4518
4519 A date or time in Excel is just like any other number. To have the
4520 number display as a date you must apply an Excel number format to it.
4521 Here are some examples.
4522
4523 #!/usr/bin/perl -w
4524
4525 use strict;
4526 use Excel::Writer::XLSX;
4527
4528 my $workbook = Excel::Writer::XLSX->new( 'date_examples.xlsx' );
4529 my $worksheet = $workbook->add_worksheet();
4530
4531 $worksheet->set_column( 'A:A', 30 ); # For extra visibility.
4532
4533 my $number = 39506.5;
4534
4535 $worksheet->write( 'A1', $number ); # 39506.5
4536
4537 my $format2 = $workbook->add_format( num_format => 'dd/mm/yy' );
4538 $worksheet->write( 'A2', $number, $format2 ); # 28/02/08
4539
4540 my $format3 = $workbook->add_format( num_format => 'mm/dd/yy' );
4541 $worksheet->write( 'A3', $number, $format3 ); # 02/28/08
4542
4543 my $format4 = $workbook->add_format( num_format => 'd-m-yyyy' );
4544 $worksheet->write( 'A4', $number, $format4 ); # 28-2-2008
4545
4546 my $format5 = $workbook->add_format( num_format => 'dd/mm/yy hh:mm' );
4547 $worksheet->write( 'A5', $number, $format5 ); # 28/02/08 12:00
4548
4549 my $format6 = $workbook->add_format( num_format => 'd mmm yyyy' );
4550 $worksheet->write( 'A6', $number, $format6 ); # 28 Feb 2008
4551
4552 my $format7 = $workbook->add_format( num_format => 'mmm d yyyy hh:mm AM/PM' );
4553 $worksheet->write('A7', $number , $format7); # Feb 28 2008 12:00 PM
4554
4555 $workbook->close();
4556
4557 Excel::Writer::XLSX doesn't automatically convert date/time strings
4558 Excel::Writer::XLSX doesn't automatically convert input date strings
4559 into Excel's formatted date numbers due to the large number of possible
4560 date formats and also due to the possibility of misinterpretation.
4561
4562 For example, does "02/03/04" mean March 2 2004, February 3 2004 or even
4563 March 4 2002.
4564
4565 Therefore, in order to handle dates you will have to convert them to
4566 numbers and apply an Excel format. Some methods for converting dates
4567 are listed in the next section.
4568
4569 The most direct way is to convert your dates to the ISO8601
4570 "yyyy-mm-ddThh:mm:ss.sss" date format and use the "write_date_time()"
4571 worksheet method:
4572
4573 $worksheet->write_date_time( 'A2', '2001-01-01T12:20', $format );
4574
4575 See the "write_date_time()" section of the documentation for more
4576 details.
4577
4578 A general methodology for handling date strings with
4579 "write_date_time()" is:
4580
4581 1. Identify incoming date/time strings with a regex.
4582 2. Extract the component parts of the date/time using the same regex.
4583 3. Convert the date/time to the ISO8601 format.
4584 4. Write the date/time using write_date_time() and a number format.
4585
4586 Here is an example:
4587
4588 #!/usr/bin/perl -w
4589
4590 use strict;
4591 use Excel::Writer::XLSX;
4592
4593 my $workbook = Excel::Writer::XLSX->new( 'example.xlsx' );
4594 my $worksheet = $workbook->add_worksheet();
4595
4596 # Set the default format for dates.
4597 my $date_format = $workbook->add_format( num_format => 'mmm d yyyy' );
4598
4599 # Increase column width to improve visibility of data.
4600 $worksheet->set_column( 'A:C', 20 );
4601
4602 # Simulate reading from a data source.
4603 my $row = 0;
4604
4605 while ( <DATA> ) {
4606 chomp;
4607
4608 my $col = 0;
4609 my @data = split ' ';
4610
4611 for my $item ( @data ) {
4612
4613 # Match dates in the following formats: d/m/yy, d/m/yyyy
4614 if ( $item =~ qr[^(\d{1,2})/(\d{1,2})/(\d{4})$] ) {
4615
4616 # Change to the date format required by write_date_time().
4617 my $date = sprintf "%4d-%02d-%02dT", $3, $2, $1;
4618
4619 $worksheet->write_date_time( $row, $col++, $date,
4620 $date_format );
4621 }
4622 else {
4623
4624 # Just plain data
4625 $worksheet->write( $row, $col++, $item );
4626 }
4627 }
4628 $row++;
4629 }
4630
4631 $workbook->close();
4632
4633 __DATA__
4634 Item Cost Date
4635 Book 10 1/9/2007
4636 Beer 4 12/9/2007
4637 Bed 500 5/10/2007
4638
4639 For a slightly more advanced solution you can modify the "write()"
4640 method to handle date formats of your choice via the
4641 "add_write_handler()" method. See the "add_write_handler()" section of
4642 the docs and the write_handler3.pl and write_handler4.pl programs in
4643 the examples directory of the distro.
4644
4645 Converting dates and times to an Excel date or time
4646 The "write_date_time()" method above is just one way of handling dates
4647 and times.
4648
4649 You can also use the "convert_date_time()" worksheet method to convert
4650 from an ISO8601 style date string to an Excel date and time number.
4651
4652 The Excel::Writer::XLSX::Utility module which is included in the distro
4653 has date/time handling functions:
4654
4655 use Excel::Writer::XLSX::Utility;
4656
4657 $date = xl_date_list(2002, 1, 1); # 37257
4658 $date = xl_parse_date("11 July 1997"); # 35622
4659 $time = xl_parse_time('3:21:36 PM'); # 0.64
4660 $date = xl_decode_date_EU("13 May 2002"); # 37389
4661
4662 Note: some of these functions require additional CPAN modules.
4663
4664 For date conversions using the CPAN "DateTime" framework see
4665 DateTime::Format::Excel
4666 <http://search.cpan.org/search?dist=DateTime-Format-Excel>.
4667
4669 Excel allows you to group rows or columns so that they can be hidden or
4670 displayed with a single mouse click. This feature is referred to as
4671 outlines.
4672
4673 Outlines can reduce complex data down to a few salient sub-totals or
4674 summaries.
4675
4676 This feature is best viewed in Excel but the following is an ASCII
4677 representation of what a worksheet with three outlines might look like.
4678 Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
4679 level 1. The lines at the left hand side are called outline level bars.
4680
4681 ------------------------------------------
4682 1 2 3 | | A | B | C | D | ...
4683 ------------------------------------------
4684 _ | 1 | A | | | | ...
4685 | _ | 2 | B | | | | ...
4686 | | | 3 | (C) | | | | ...
4687 | | | 4 | (D) | | | | ...
4688 | - | 5 | E | | | | ...
4689 | _ | 6 | F | | | | ...
4690 | | | 7 | (G) | | | | ...
4691 | | | 8 | (H) | | | | ...
4692 | - | 9 | I | | | | ...
4693 - | . | ... | ... | ... | ... | ...
4694
4695 Clicking the minus sign on each of the level 2 outlines will collapse
4696 and hide the data as shown in the next figure. The minus sign changes
4697 to a plus sign to indicate that the data in the outline is hidden.
4698
4699 ------------------------------------------
4700 1 2 3 | | A | B | C | D | ...
4701 ------------------------------------------
4702 _ | 1 | A | | | | ...
4703 | | 2 | B | | | | ...
4704 | + | 5 | E | | | | ...
4705 | | 6 | F | | | | ...
4706 | + | 9 | I | | | | ...
4707 - | . | ... | ... | ... | ... | ...
4708
4709 Clicking on the minus sign on the level 1 outline will collapse the
4710 remaining rows as follows:
4711
4712 ------------------------------------------
4713 1 2 3 | | A | B | C | D | ...
4714 ------------------------------------------
4715 | 1 | A | | | | ...
4716 + | . | ... | ... | ... | ... | ...
4717
4718 Grouping in "Excel::Writer::XLSX" is achieved by setting the outline
4719 level via the "set_row()" and "set_column()" worksheet methods:
4720
4721 set_row( $row, $height, $format, $hidden, $level, $collapsed )
4722 set_column( $first_col, $last_col, $width, $format, $hidden, $level, $collapsed )
4723
4724 The following example sets an outline level of 1 for rows 2 and 3
4725 (zero-indexed) and columns B to G. The parameters $height and $XF are
4726 assigned default values since they are undefined:
4727
4728 $worksheet->set_row( 1, undef, undef, 0, 1 );
4729 $worksheet->set_row( 2, undef, undef, 0, 1 );
4730 $worksheet->set_column( 'B:G', undef, undef, 0, 1 );
4731
4732 Excel allows up to 7 outline levels. Therefore the $level parameter
4733 should be in the range "0 <= $level <= 7".
4734
4735 Rows and columns can be collapsed by setting the $hidden flag for the
4736 hidden rows/columns and setting the $collapsed flag for the row/column
4737 that has the collapsed "+" symbol:
4738
4739 $worksheet->set_row( 1, undef, undef, 1, 1 );
4740 $worksheet->set_row( 2, undef, undef, 1, 1 );
4741 $worksheet->set_row( 3, undef, undef, 0, 0, 1 ); # Collapsed flag.
4742
4743 $worksheet->set_column( 'B:G', undef, undef, 1, 1 );
4744 $worksheet->set_column( 'H:H', undef, undef, 0, 0, 1 ); # Collapsed flag.
4745
4746 Note: Setting the $collapsed flag is particularly important for
4747 compatibility with OpenOffice.org and Gnumeric.
4748
4749 For a more complete example see the "outline.pl" and
4750 "outline_collapsed.pl" programs in the examples directory of the
4751 distro.
4752
4753 Some additional outline properties can be set via the
4754 "outline_settings()" worksheet method, see above.
4755
4757 Data validation is a feature of Excel which allows you to restrict the
4758 data that a users enters in a cell and to display help and warning
4759 messages. It also allows you to restrict input to values in a drop down
4760 list.
4761
4762 A typical use case might be to restrict data in a cell to integer
4763 values in a certain range, to provide a help message to indicate the
4764 required value and to issue a warning if the input data doesn't meet
4765 the stated criteria. In Excel::Writer::XLSX we could do that as
4766 follows:
4767
4768 $worksheet->data_validation('B3',
4769 {
4770 validate => 'integer',
4771 criteria => 'between',
4772 minimum => 1,
4773 maximum => 100,
4774 input_title => 'Input an integer:',
4775 input_message => 'Between 1 and 100',
4776 error_message => 'Sorry, try again.',
4777 });
4778
4779 For more information on data validation see the following Microsoft
4780 support article "Description and examples of data validation in Excel":
4781 <http://support.microsoft.com/kb/211485>.
4782
4783 The following sections describe how to use the "data_validation()"
4784 method and its various options.
4785
4786 data_validation( $row, $col, { parameter => 'value', ... } )
4787 The "data_validation()" method is used to construct an Excel data
4788 validation.
4789
4790 It can be applied to a single cell or a range of cells. You can pass 3
4791 parameters such as "($row, $col, {...})" or 5 parameters such as
4792 "($first_row, $first_col, $last_row, $last_col, {...})". You can also
4793 use "A1" style notation. For example:
4794
4795 $worksheet->data_validation( 0, 0, {...} );
4796 $worksheet->data_validation( 0, 0, 4, 1, {...} );
4797
4798 # Which are the same as:
4799
4800 $worksheet->data_validation( 'A1', {...} );
4801 $worksheet->data_validation( 'A1:B5', {...} );
4802
4803 See also the note about "Cell notation" for more information.
4804
4805 The last parameter in "data_validation()" must be a hash ref containing
4806 the parameters that describe the type and style of the data validation.
4807 The allowable parameters are:
4808
4809 validate
4810 criteria
4811 value | minimum | source
4812 maximum
4813 ignore_blank
4814 dropdown
4815
4816 input_title
4817 input_message
4818 show_input
4819
4820 error_title
4821 error_message
4822 error_type
4823 show_error
4824
4825 These parameters are explained in the following sections. Most of the
4826 parameters are optional, however, you will generally require the three
4827 main options "validate", "criteria" and "value".
4828
4829 $worksheet->data_validation('B3',
4830 {
4831 validate => 'integer',
4832 criteria => '>',
4833 value => 100,
4834 });
4835
4836 The "data_validation" method returns:
4837
4838 0 for success.
4839 -1 for insufficient number of arguments.
4840 -2 for row or column out of bounds.
4841 -3 for incorrect parameter or value.
4842
4843 validate
4844 This parameter is passed in a hash ref to "data_validation()".
4845
4846 The "validate" parameter is used to set the type of data that you wish
4847 to validate. It is always required and it has no default value.
4848 Allowable values are:
4849
4850 any
4851 integer
4852 decimal
4853 list
4854 date
4855 time
4856 length
4857 custom
4858
4859 • any is used to specify that the type of data is unrestricted. This
4860 is useful to display an input message without restricting the data
4861 that can be entered.
4862
4863 • integer restricts the cell to integer values. Excel refers to this
4864 as 'whole number'.
4865
4866 validate => 'integer',
4867 criteria => '>',
4868 value => 100,
4869
4870 • decimal restricts the cell to decimal values.
4871
4872 validate => 'decimal',
4873 criteria => '>',
4874 value => 38.6,
4875
4876 • list restricts the cell to a set of user specified values. These
4877 can be passed in an array ref or as a cell range (named ranges
4878 aren't currently supported):
4879
4880 validate => 'list',
4881 value => ['open', 'high', 'close'],
4882 # Or like this:
4883 value => 'B1:B3',
4884
4885 Excel requires that range references are only to cells on the same
4886 worksheet.
4887
4888 • date restricts the cell to date values. Dates in Excel are
4889 expressed as integer values but you can also pass an ISO8601 style
4890 string as used in "write_date_time()". See also "DATES AND TIME IN
4891 EXCEL" for more information about working with Excel's dates.
4892
4893 validate => 'date',
4894 criteria => '>',
4895 value => 39653, # 24 July 2008
4896 # Or like this:
4897 value => '2008-07-24T',
4898
4899 • time restricts the cell to time values. Times in Excel are
4900 expressed as decimal values but you can also pass an ISO8601 style
4901 string as used in "write_date_time()". See also "DATES AND TIME IN
4902 EXCEL" for more information about working with Excel's times.
4903
4904 validate => 'time',
4905 criteria => '>',
4906 value => 0.5, # Noon
4907 # Or like this:
4908 value => 'T12:00:00',
4909
4910 • length restricts the cell data based on an integer string length.
4911 Excel refers to this as 'Text length'.
4912
4913 validate => 'length',
4914 criteria => '>',
4915 value => 10,
4916
4917 • custom restricts the cell based on an external Excel formula that
4918 returns a "TRUE/FALSE" value.
4919
4920 validate => 'custom',
4921 value => '=IF(A10>B10,TRUE,FALSE)',
4922
4923 criteria
4924 This parameter is passed in a hash ref to "data_validation()".
4925
4926 The "criteria" parameter is used to set the criteria by which the data
4927 in the cell is validated. It is almost always required except for the
4928 "list" and "custom" validate options. It has no default value.
4929 Allowable values are:
4930
4931 'between'
4932 'not between'
4933 'equal to' | '==' | '='
4934 'not equal to' | '!=' | '<>'
4935 'greater than' | '>'
4936 'less than' | '<'
4937 'greater than or equal to' | '>='
4938 'less than or equal to' | '<='
4939
4940 You can either use Excel's textual description strings, in the first
4941 column above, or the more common symbolic alternatives. The following
4942 are equivalent:
4943
4944 validate => 'integer',
4945 criteria => 'greater than',
4946 value => 100,
4947
4948 validate => 'integer',
4949 criteria => '>',
4950 value => 100,
4951
4952 The "list" and "custom" validate options don't require a "criteria". If
4953 you specify one it will be ignored.
4954
4955 validate => 'list',
4956 value => ['open', 'high', 'close'],
4957
4958 validate => 'custom',
4959 value => '=IF(A10>B10,TRUE,FALSE)',
4960
4961 value | minimum | source
4962 This parameter is passed in a hash ref to "data_validation()".
4963
4964 The "value" parameter is used to set the limiting value to which the
4965 "criteria" is applied. It is always required and it has no default
4966 value. You can also use the synonyms "minimum" or "source" to make the
4967 validation a little clearer and closer to Excel's description of the
4968 parameter:
4969
4970 # Use 'value'
4971 validate => 'integer',
4972 criteria => '>',
4973 value => 100,
4974
4975 # Use 'minimum'
4976 validate => 'integer',
4977 criteria => 'between',
4978 minimum => 1,
4979 maximum => 100,
4980
4981 # Use 'source'
4982 validate => 'list',
4983 source => '$B$1:$B$3',
4984
4985 maximum
4986 This parameter is passed in a hash ref to "data_validation()".
4987
4988 The "maximum" parameter is used to set the upper limiting value when
4989 the "criteria" is either 'between' or 'not between':
4990
4991 validate => 'integer',
4992 criteria => 'between',
4993 minimum => 1,
4994 maximum => 100,
4995
4996 ignore_blank
4997 This parameter is passed in a hash ref to "data_validation()".
4998
4999 The "ignore_blank" parameter is used to toggle on and off the 'Ignore
5000 blank' option in the Excel data validation dialog. When the option is
5001 on the data validation is not applied to blank data in the cell. It is
5002 on by default.
5003
5004 ignore_blank => 0, # Turn the option off
5005
5006 dropdown
5007 This parameter is passed in a hash ref to "data_validation()".
5008
5009 The "dropdown" parameter is used to toggle on and off the 'In-cell
5010 dropdown' option in the Excel data validation dialog. When the option
5011 is on a dropdown list will be shown for "list" validations. It is on by
5012 default.
5013
5014 dropdown => 0, # Turn the option off
5015
5016 input_title
5017 This parameter is passed in a hash ref to "data_validation()".
5018
5019 The "input_title" parameter is used to set the title of the input
5020 message that is displayed when a cell is entered. It has no default
5021 value and is only displayed if the input message is displayed. See the
5022 "input_message" parameter below.
5023
5024 input_title => 'This is the input title',
5025
5026 The maximum title length is 32 characters.
5027
5028 input_message
5029 This parameter is passed in a hash ref to "data_validation()".
5030
5031 The "input_message" parameter is used to set the input message that is
5032 displayed when a cell is entered. It has no default value.
5033
5034 validate => 'integer',
5035 criteria => 'between',
5036 minimum => 1,
5037 maximum => 100,
5038 input_title => 'Enter the applied discount:',
5039 input_message => 'between 1 and 100',
5040
5041 The message can be split over several lines using newlines, "\n" in
5042 double quoted strings.
5043
5044 input_message => "This is\na test.",
5045
5046 The maximum message length is 255 characters.
5047
5048 show_input
5049 This parameter is passed in a hash ref to "data_validation()".
5050
5051 The "show_input" parameter is used to toggle on and off the 'Show input
5052 message when cell is selected' option in the Excel data validation
5053 dialog. When the option is off an input message is not displayed even
5054 if it has been set using "input_message". It is on by default.
5055
5056 show_input => 0, # Turn the option off
5057
5058 error_title
5059 This parameter is passed in a hash ref to "data_validation()".
5060
5061 The "error_title" parameter is used to set the title of the error
5062 message that is displayed when the data validation criteria is not met.
5063 The default error title is 'Microsoft Excel'.
5064
5065 error_title => 'Input value is not valid',
5066
5067 The maximum title length is 32 characters.
5068
5069 error_message
5070 This parameter is passed in a hash ref to "data_validation()".
5071
5072 The "error_message" parameter is used to set the error message that is
5073 displayed when a cell is entered. The default error message is "The
5074 value you entered is not valid.\nA user has restricted values that can
5075 be entered into the cell.".
5076
5077 validate => 'integer',
5078 criteria => 'between',
5079 minimum => 1,
5080 maximum => 100,
5081 error_title => 'Input value is not valid',
5082 error_message => 'It should be an integer between 1 and 100',
5083
5084 The message can be split over several lines using newlines, "\n" in
5085 double quoted strings.
5086
5087 input_message => "This is\na test.",
5088
5089 The maximum message length is 255 characters.
5090
5091 error_type
5092 This parameter is passed in a hash ref to "data_validation()".
5093
5094 The "error_type" parameter is used to specify the type of error dialog
5095 that is displayed. There are 3 options:
5096
5097 'stop'
5098 'warning'
5099 'information'
5100
5101 The default is 'stop'.
5102
5103 show_error
5104 This parameter is passed in a hash ref to "data_validation()".
5105
5106 The "show_error" parameter is used to toggle on and off the 'Show error
5107 alert after invalid data is entered' option in the Excel data
5108 validation dialog. When the option is off an error message is not
5109 displayed even if it has been set using "error_message". It is on by
5110 default.
5111
5112 show_error => 0, # Turn the option off
5113
5114 Data Validation Examples
5115 Example 1. Limiting input to an integer greater than a fixed value.
5116
5117 $worksheet->data_validation('A1',
5118 {
5119 validate => 'integer',
5120 criteria => '>',
5121 value => 0,
5122 });
5123
5124 Example 2. Limiting input to an integer greater than a fixed value
5125 where the value is referenced from a cell.
5126
5127 $worksheet->data_validation('A2',
5128 {
5129 validate => 'integer',
5130 criteria => '>',
5131 value => '=E3',
5132 });
5133
5134 Example 3. Limiting input to a decimal in a fixed range.
5135
5136 $worksheet->data_validation('A3',
5137 {
5138 validate => 'decimal',
5139 criteria => 'between',
5140 minimum => 0.1,
5141 maximum => 0.5,
5142 });
5143
5144 Example 4. Limiting input to a value in a dropdown list.
5145
5146 $worksheet->data_validation('A4',
5147 {
5148 validate => 'list',
5149 source => ['open', 'high', 'close'],
5150 });
5151
5152 Example 5. Limiting input to a value in a dropdown list where the list
5153 is specified as a cell range.
5154
5155 $worksheet->data_validation('A5',
5156 {
5157 validate => 'list',
5158 source => '=$E$4:$G$4',
5159 });
5160
5161 Example 6. Limiting input to a date in a fixed range.
5162
5163 $worksheet->data_validation('A6',
5164 {
5165 validate => 'date',
5166 criteria => 'between',
5167 minimum => '2008-01-01T',
5168 maximum => '2008-12-12T',
5169 });
5170
5171 Example 7. Displaying a message when the cell is selected.
5172
5173 $worksheet->data_validation('A7',
5174 {
5175 validate => 'integer',
5176 criteria => 'between',
5177 minimum => 1,
5178 maximum => 100,
5179 input_title => 'Enter an integer:',
5180 input_message => 'between 1 and 100',
5181 });
5182
5183 See also the "data_validate.pl" program in the examples directory of
5184 the distro.
5185
5187 Conditional formatting is a feature of Excel which allows you to apply
5188 a format to a cell or a range of cells based on a certain criteria.
5189
5190 For example the following criteria is used to highlight cells >= 50 in
5191 red in the "conditional_format.pl" example from the distro:
5192
5193 # Write a conditional format over a range.
5194 $worksheet->conditional_formatting( 'B3:K12',
5195 {
5196 type => 'cell',
5197 criteria => '>=',
5198 value => 50,
5199 format => $format1,
5200 }
5201 );
5202
5203 conditional_formatting( $row, $col, { parameter => 'value', ... } )
5204 The "conditional_formatting()" method is used to apply formatting
5205 based on user defined criteria to an Excel::Writer::XLSX file.
5206
5207 It can be applied to a single cell or a range of cells. You can pass 3
5208 parameters such as "($row, $col, {...})" or 5 parameters such as
5209 "($first_row, $first_col, $last_row, $last_col, {...})". You can also
5210 use "A1" style notation. For example:
5211
5212 $worksheet->conditional_formatting( 0, 0, {...} );
5213 $worksheet->conditional_formatting( 0, 0, 4, 1, {...} );
5214
5215 # Which are the same as:
5216
5217 $worksheet->conditional_formatting( 'A1', {...} );
5218 $worksheet->conditional_formatting( 'A1:B5', {...} );
5219
5220 See also the note about "Cell notation" for more information.
5221
5222 Using "A1" style notation is also possible to specify non-contiguous
5223 ranges, separated by a comma. For example:
5224
5225 $worksheet->conditional_formatting( 'A1:D5,A8:D12', {...} );
5226
5227 The last parameter in "conditional_formatting()" must be a hash ref
5228 containing the parameters that describe the type and style of the data
5229 validation. The main parameters are:
5230
5231 type
5232 format
5233 criteria
5234 value
5235 minimum
5236 maximum
5237
5238 Other, less commonly used parameters are:
5239
5240 min_type
5241 mid_type
5242 max_type
5243 min_value
5244 mid_value
5245 max_value
5246 min_color
5247 mid_color
5248 max_color
5249 bar_color
5250 bar_only
5251 bar_solid
5252 bar_negative_color
5253 bar_border_color
5254 bar_negative_border_color
5255 bar_negative_color_same
5256 bar_negative_border_color_same
5257 bar_no_border
5258 bar_direction
5259 bar_axis_position
5260 bar_axis_color
5261 data_bar_2010
5262 icon_style
5263 icons
5264 reverse_icons
5265 icons_only
5266 stop_if_true
5267 multi_range
5268
5269 Additional parameters which are used for specific conditional format
5270 types are shown in the relevant sections below.
5271
5272 type
5273 This parameter is passed in a hash ref to "conditional_formatting()".
5274
5275 The "type" parameter is used to set the type of conditional formatting
5276 that you wish to apply. It is always required and it has no default
5277 value. Allowable "type" values and their associated parameters are:
5278
5279 Type Parameters
5280 ==== ==========
5281 cell criteria
5282 value
5283 minimum
5284 maximum
5285 format
5286
5287 date criteria
5288 value
5289 minimum
5290 maximum
5291 format
5292
5293 time_period criteria
5294 format
5295
5296 text criteria
5297 value
5298 format
5299
5300 average criteria
5301 format
5302
5303 duplicate format
5304
5305 unique format
5306
5307 top criteria
5308 value
5309 format
5310
5311 bottom criteria
5312 value
5313 format
5314
5315 blanks format
5316
5317 no_blanks format
5318
5319 errors format
5320
5321 no_errors format
5322
5323 formula criteria
5324 format
5325
5326 2_color_scale min_type
5327 max_type
5328 min_value
5329 max_value
5330 min_color
5331 max_color
5332
5333 3_color_scale min_type
5334 mid_type
5335 max_type
5336 min_value
5337 mid_value
5338 max_value
5339 min_color
5340 mid_color
5341 max_color
5342
5343 data_bar min_type
5344 max_type
5345 min_value
5346 max_value
5347 bar_only
5348 bar_color
5349 bar_solid*
5350 bar_negative_color*
5351 bar_border_color*
5352 bar_negative_border_color*
5353 bar_negative_color_same*
5354 bar_negative_border_color_same*
5355 bar_no_border*
5356 bar_direction*
5357 bar_axis_position*
5358 bar_axis_color*
5359 data_bar_2010*
5360
5361 icon_set icon_style
5362 reverse_icons
5363 icons
5364 icons_only
5365
5366 Data bar parameters marked with (*) are only available in Excel 2010
5367 and later. Files that use these properties can still be opened in Excel
5368 2007 but the data bars will be displayed without them.
5369
5370 type => 'cell'
5371 This is the most common conditional formatting type. It is used when a
5372 format is applied to a cell based on a simple criterion. For example:
5373
5374 $worksheet->conditional_formatting( 'A1',
5375 {
5376 type => 'cell',
5377 criteria => 'greater than',
5378 value => 5,
5379 format => $red_format,
5380 }
5381 );
5382
5383 Or, using the "between" criteria:
5384
5385 $worksheet->conditional_formatting( 'C1:C4',
5386 {
5387 type => 'cell',
5388 criteria => 'between',
5389 minimum => 20,
5390 maximum => 30,
5391 format => $green_format,
5392 }
5393 );
5394
5395 criteria
5396 The "criteria" parameter is used to set the criteria by which the cell
5397 data will be evaluated. It has no default value. The most common
5398 criteria as applied to "{ type => 'cell' }" are:
5399
5400 'between'
5401 'not between'
5402 'equal to' | '==' | '='
5403 'not equal to' | '!=' | '<>'
5404 'greater than' | '>'
5405 'less than' | '<'
5406 'greater than or equal to' | '>='
5407 'less than or equal to' | '<='
5408
5409 You can either use Excel's textual description strings, in the first
5410 column above, or the more common symbolic alternatives.
5411
5412 Additional criteria which are specific to other conditional format
5413 types are shown in the relevant sections below.
5414
5415 value
5416 The "value" is generally used along with the "criteria" parameter to
5417 set the rule by which the cell data will be evaluated.
5418
5419 type => 'cell',
5420 criteria => '>',
5421 value => 5
5422 format => $format,
5423
5424 The "value" property can also be an cell reference.
5425
5426 type => 'cell',
5427 criteria => '>',
5428 value => '$C$1',
5429 format => $format,
5430
5431 format
5432 The "format" parameter is used to specify the format that will be
5433 applied to the cell when the conditional formatting criterion is met.
5434 The format is created using the "add_format()" method in the same way
5435 as cell formats:
5436
5437 $format = $workbook->add_format( bold => 1, italic => 1 );
5438
5439 $worksheet->conditional_formatting( 'A1',
5440 {
5441 type => 'cell',
5442 criteria => '>',
5443 value => 5
5444 format => $format,
5445 }
5446 );
5447
5448 The conditional format follows the same rules as in Excel: it is
5449 superimposed over the existing cell format and not all font and border
5450 properties can be modified. Font properties that can't be modified are
5451 font name, font size, superscript and subscript. The border property
5452 that cannot be modified is diagonal borders.
5453
5454 Excel specifies some default formats to be used with conditional
5455 formatting. You can replicate them using the following
5456 Excel::Writer::XLSX formats:
5457
5458 # Light red fill with dark red text.
5459
5460 my $format1 = $workbook->add_format(
5461 bg_color => '#FFC7CE',
5462 color => '#9C0006',
5463 );
5464
5465 # Light yellow fill with dark yellow text.
5466
5467 my $format2 = $workbook->add_format(
5468 bg_color => '#FFEB9C',
5469 color => '#9C6500',
5470 );
5471
5472 # Green fill with dark green text.
5473
5474 my $format3 = $workbook->add_format(
5475 bg_color => '#C6EFCE',
5476 color => '#006100',
5477 );
5478
5479 minimum
5480 The "minimum" parameter is used to set the lower limiting value when
5481 the "criteria" is either 'between' or 'not between':
5482
5483 validate => 'integer',
5484 criteria => 'between',
5485 minimum => 1,
5486 maximum => 100,
5487
5488 maximum
5489 The "maximum" parameter is used to set the upper limiting value when
5490 the "criteria" is either 'between' or 'not between'. See the previous
5491 example.
5492
5493 type => 'date'
5494 The "date" type is the same as the "cell" type and uses the same
5495 criteria and values. However it allows the "value", "minimum" and
5496 "maximum" properties to be specified in the ISO8601
5497 "yyyy-mm-ddThh:mm:ss.sss" date format which is detailed in the
5498 "write_date_time()" method.
5499
5500 $worksheet->conditional_formatting( 'A1:A4',
5501 {
5502 type => 'date',
5503 criteria => 'greater than',
5504 value => '2011-01-01T',
5505 format => $format,
5506 }
5507 );
5508
5509 type => 'time_period'
5510 The "time_period" type is used to specify Excel's "Dates Occurring"
5511 style conditional format.
5512
5513 $worksheet->conditional_formatting( 'A1:A4',
5514 {
5515 type => 'time_period',
5516 criteria => 'yesterday',
5517 format => $format,
5518 }
5519 );
5520
5521 The period is set in the "criteria" and can have one of the following
5522 values:
5523
5524 criteria => 'yesterday',
5525 criteria => 'today',
5526 criteria => 'last 7 days',
5527 criteria => 'last week',
5528 criteria => 'this week',
5529 criteria => 'next week',
5530 criteria => 'last month',
5531 criteria => 'this month',
5532 criteria => 'next month'
5533
5534 type => 'text'
5535 The "text" type is used to specify Excel's "Specific Text" style
5536 conditional format. It is used to do simple string matching using the
5537 "criteria" and "value" parameters:
5538
5539 $worksheet->conditional_formatting( 'A1:A4',
5540 {
5541 type => 'text',
5542 criteria => 'containing',
5543 value => 'foo',
5544 format => $format,
5545 }
5546 );
5547
5548 The "criteria" can have one of the following values:
5549
5550 criteria => 'containing',
5551 criteria => 'not containing',
5552 criteria => 'begins with',
5553 criteria => 'ends with',
5554
5555 The "value" parameter should be a string or single character.
5556
5557 type => 'average'
5558 The "average" type is used to specify Excel's "Average" style
5559 conditional format.
5560
5561 $worksheet->conditional_formatting( 'A1:A4',
5562 {
5563 type => 'average',
5564 criteria => 'above',
5565 format => $format,
5566 }
5567 );
5568
5569 The type of average for the conditional format range is specified by
5570 the "criteria":
5571
5572 criteria => 'above',
5573 criteria => 'below',
5574 criteria => 'equal or above',
5575 criteria => 'equal or below',
5576 criteria => '1 std dev above',
5577 criteria => '1 std dev below',
5578 criteria => '2 std dev above',
5579 criteria => '2 std dev below',
5580 criteria => '3 std dev above',
5581 criteria => '3 std dev below',
5582
5583 type => 'duplicate'
5584 The "duplicate" type is used to highlight duplicate cells in a range:
5585
5586 $worksheet->conditional_formatting( 'A1:A4',
5587 {
5588 type => 'duplicate',
5589 format => $format,
5590 }
5591 );
5592
5593 type => 'unique'
5594 The "unique" type is used to highlight unique cells in a range:
5595
5596 $worksheet->conditional_formatting( 'A1:A4',
5597 {
5598 type => 'unique',
5599 format => $format,
5600 }
5601 );
5602
5603 type => 'top'
5604 The "top" type is used to specify the top "n" values by number or
5605 percentage in a range:
5606
5607 $worksheet->conditional_formatting( 'A1:A4',
5608 {
5609 type => 'top',
5610 value => 10,
5611 format => $format,
5612 }
5613 );
5614
5615 The "criteria" can be used to indicate that a percentage condition is
5616 required:
5617
5618 $worksheet->conditional_formatting( 'A1:A4',
5619 {
5620 type => 'top',
5621 value => 10,
5622 criteria => '%',
5623 format => $format,
5624 }
5625 );
5626
5627 type => 'bottom'
5628 The "bottom" type is used to specify the bottom "n" values by number or
5629 percentage in a range.
5630
5631 It takes the same parameters as "top", see above.
5632
5633 type => 'blanks'
5634 The "blanks" type is used to highlight blank cells in a range:
5635
5636 $worksheet->conditional_formatting( 'A1:A4',
5637 {
5638 type => 'blanks',
5639 format => $format,
5640 }
5641 );
5642
5643 type => 'no_blanks'
5644 The "no_blanks" type is used to highlight non blank cells in a range:
5645
5646 $worksheet->conditional_formatting( 'A1:A4',
5647 {
5648 type => 'no_blanks',
5649 format => $format,
5650 }
5651 );
5652
5653 type => 'errors'
5654 The "errors" type is used to highlight error cells in a range:
5655
5656 $worksheet->conditional_formatting( 'A1:A4',
5657 {
5658 type => 'errors',
5659 format => $format,
5660 }
5661 );
5662
5663 type => 'no_errors'
5664 The "no_errors" type is used to highlight non error cells in a range:
5665
5666 $worksheet->conditional_formatting( 'A1:A4',
5667 {
5668 type => 'no_errors',
5669 format => $format,
5670 }
5671 );
5672
5673 type => 'formula'
5674 The "formula" type is used to specify a conditional format based on a
5675 user defined formula:
5676
5677 $worksheet->conditional_formatting( 'A1:A4',
5678 {
5679 type => 'formula',
5680 criteria => '=$A$1 > 5',
5681 format => $format,
5682 }
5683 );
5684
5685 The formula is specified in the "criteria".
5686
5687 type => '2_color_scale'
5688 The "2_color_scale" type is used to specify Excel's "2 Color Scale"
5689 style conditional format.
5690
5691 $worksheet->conditional_formatting( 'A1:A12',
5692 {
5693 type => '2_color_scale',
5694 }
5695 );
5696
5697 This conditional type can be modified with "min_type", "max_type",
5698 "min_value", "max_value", "min_color" and "max_color", see below.
5699
5700 type => '3_color_scale'
5701 The "3_color_scale" type is used to specify Excel's "3 Color Scale"
5702 style conditional format.
5703
5704 $worksheet->conditional_formatting( 'A1:A12',
5705 {
5706 type => '3_color_scale',
5707 }
5708 );
5709
5710 This conditional type can be modified with "min_type", "mid_type",
5711 "max_type", "min_value", "mid_value", "max_value", "min_color",
5712 "mid_color" and "max_color", see below.
5713
5714 type => 'data_bar'
5715 The "data_bar" type is used to specify Excel's "Data Bar" style
5716 conditional format.
5717
5718 $worksheet->conditional_formatting( 'A1:A12',
5719 {
5720 type => 'data_bar',
5721 }
5722 );
5723
5724 This data bar conditional type can be modified with the following
5725 parameters, which are explained in the sections below. These properties
5726 were available in the original xlsx file specification used in Excel
5727 2007::
5728
5729 min_type
5730 max_type
5731 min_value
5732 max_value
5733 bar_color
5734 bar_only
5735
5736 In Excel 2010 additional data bar properties were added such as solid
5737 (non-gradient) bars and control over how negative values are displayed.
5738 These properties can be set using the following parameters:
5739
5740 bar_solid
5741 bar_negative_color
5742 bar_border_color
5743 bar_negative_border_color
5744 bar_negative_color_same
5745 bar_negative_border_color_same
5746 bar_no_border
5747 bar_direction
5748 bar_axis_position
5749 bar_axis_color
5750 data_bar_2010
5751
5752 Files that use these Excel 2010 properties can still be opened in Excel
5753 2007 but the data bars will be displayed without them.
5754
5755 type => 'icon_set'
5756 The "icon_set" type is used to specify a conditional format with a set
5757 of icons such as traffic lights or arrows:
5758
5759 $worksheet->conditional_formatting( 'A1:C1',
5760 {
5761 type => 'icon_set',
5762 icon_style => '3_traffic_lights',
5763 }
5764 );
5765
5766 The icon set style is specified by the "icon_style" parameter. Valid
5767 options are:
5768
5769 3_arrows
5770 3_arrows_gray
5771 3_flags
5772 3_signs
5773 3_symbols
5774 3_symbols_circled
5775 3_traffic_lights
5776 3_traffic_lights_rimmed
5777
5778 4_arrows
5779 4_arrows_gray
5780 4_ratings
5781 4_red_to_black
5782 4_traffic_lights
5783
5784 5_arrows
5785 5_arrows_gray
5786 5_quarters
5787 5_ratings
5788
5789 The criteria, type and value of each icon can be specified using the
5790 "icon" array of hash refs with optional "criteria", "type" and "value"
5791 parameters:
5792
5793 $worksheet->conditional_formatting( 'A1:D1',
5794 {
5795 type => 'icon_set',
5796 icon_style => '4_red_to_black',
5797 icons => [ {criteria => '>', type => 'number', value => 90},
5798 {criteria => '>=', type => 'percentile', value => 50},
5799 {criteria => '>', type => 'percent', value => 25},
5800 ],
5801 }
5802 );
5803
5804 The "icons criteria" parameter should be either ">=" or ">". The
5805 default "criteria" is ">=".
5806
5807 The "icons type" parameter should be one of the following values:
5808
5809 number
5810 percentile
5811 percent
5812 formula
5813
5814 The default "type" is "percent".
5815
5816 The "icons value" parameter can be a value or formula:
5817
5818 $worksheet->conditional_formatting( 'A1:D1',
5819 {
5820 type => 'icon_set',
5821 icon_style => '4_red_to_black',
5822 icons => [ {value => 90},
5823 {value => 50},
5824 {value => 25},
5825 ],
5826 }
5827 );
5828
5829 Note: The "icons" parameters should start with the highest value and
5830 with each subsequent one being lower. The default "value" is "(n * 100)
5831 / number_of_icons". The lowest number icon in an icon set has
5832 properties defined by Excel. Therefore in a "n" icon set, there is no
5833 "n-1" hash of parameters.
5834
5835 The order of the icons can be reversed using the "reverse_icons"
5836 parameter:
5837
5838 $worksheet->conditional_formatting( 'A1:C1',
5839 {
5840 type => 'icon_set',
5841 icon_style => '3_arrows',
5842 reverse_icons => 1,
5843 }
5844 );
5845
5846 The icons can be displayed without the cell value using the
5847 "icons_only" parameter:
5848
5849 $worksheet->conditional_formatting( 'A1:C1',
5850 {
5851 type => 'icon_set',
5852 icon_style => '3_flags',
5853 icons_only => 1,
5854 }
5855 );
5856
5857 min_type, mid_type, max_type
5858 The "min_type" and "max_type" properties are available when the
5859 conditional formatting type is "2_color_scale", "3_color_scale" or
5860 "data_bar". The "mid_type" is available for "3_color_scale". The
5861 properties are used as follows:
5862
5863 $worksheet->conditional_formatting( 'A1:A12',
5864 {
5865 type => '2_color_scale',
5866 min_type => 'percent',
5867 max_type => 'percent',
5868 }
5869 );
5870
5871 The available min/mid/max types are:
5872
5873 min (for min_type only)
5874 num
5875 percent
5876 percentile
5877 formula
5878 max (for max_type only)
5879
5880 min_value, mid_value, max_value
5881 The "min_value" and "max_value" properties are available when the
5882 conditional formatting type is "2_color_scale", "3_color_scale" or
5883 "data_bar". The "mid_value" is available for "3_color_scale". The
5884 properties are used as follows:
5885
5886 $worksheet->conditional_formatting( 'A1:A12',
5887 {
5888 type => '2_color_scale',
5889 min_value => 10,
5890 max_value => 90,
5891 }
5892 );
5893
5894 min_color, mid_color, max_color, bar_color
5895 The "min_color" and "max_color" properties are available when the
5896 conditional formatting type is "2_color_scale", "3_color_scale" or
5897 "data_bar". The "mid_color" is available for "3_color_scale". The
5898 properties are used as follows:
5899
5900 $worksheet->conditional_formatting( 'A1:A12',
5901 {
5902 type => '2_color_scale',
5903 min_color => "#C5D9F1",
5904 max_color => "#538ED5",
5905 }
5906 );
5907
5908 The color can be specified as an Excel::Writer::XLSX color index or,
5909 more usefully, as a HTML style RGB hex number, as shown above.
5910
5911 bar_only
5912 The "bar_only" parameter property displays a bar data but not the data
5913 in the cells:
5914
5915 $worksheet->conditional_formatting( 'D3:D14',
5916 {
5917 type => 'data_bar',
5918 bar_only => 1
5919 }
5920 );
5921
5922 bar_solid
5923 The "bar_solid" parameter turns on a solid (non-gradient) fill for data
5924 bars:
5925
5926 $worksheet->conditional_formatting( 'H3:H14',
5927 {
5928 type => 'data_bar',
5929 bar_solid => 1
5930 }
5931 );
5932
5933 Note, this property is only visible in Excel 2010 and later.
5934
5935 bar_negative_color
5936 The "bar_negative_color" parameter is used to set the color fill for
5937 the negative portion of a data bar.
5938
5939 The color can be specified as an Excel::Writer::XLSX color index or as
5940 a HTML style RGB hex number, as shown in the other examples.
5941
5942 Note, this property is only visible in Excel 2010 and later.
5943
5944 bar_border_color
5945 The "bar_border_color" parameter is used to set the border color of a
5946 data bar.
5947
5948 The color can be specified as an Excel::Writer::XLSX color index or as
5949 a HTML style RGB hex number, as shown in the other examples.
5950
5951 Note, this property is only visible in Excel 2010 and later.
5952
5953 bar_negative_border_color
5954 The "bar_negative_border_color" parameter is used to set the border
5955 color of the negative portion of a data bar.
5956
5957 The color can be specified as an Excel::Writer::XLSX color index or as
5958 a HTML style RGB hex number, as shown in the other examples.
5959
5960 Note, this property is only visible in Excel 2010 and later.
5961
5962 bar_negative_color_same
5963 The "bar_negative_color_same" parameter sets the fill color for the
5964 negative portion of a data bar to be the same as the fill color for the
5965 positive portion of the data bar:
5966
5967 $worksheet->conditional_formatting( 'N3:N14',
5968 {
5969 type => 'data_bar',
5970 bar_negative_color_same => 1,
5971 bar_negative_border_color_same => 1
5972 }
5973 );
5974
5975 Note, this property is only visible in Excel 2010 and later.
5976
5977 bar_negative_border_color_same
5978 The "bar_negative_border_color_same" parameter sets the border color
5979 for the negative portion of a data bar to be the same as the border
5980 color for the positive portion of the data bar.
5981
5982 Note, this property is only visible in Excel 2010 and later.
5983
5984 bar_no_border
5985 The "bar_no_border" parameter turns off the border of a data bar.
5986
5987 Note, this property is only visible in Excel 2010 and later, however
5988 the default in Excel 2007 is not to have a border.
5989
5990 bar_direction
5991 The "bar_direction" parameter sets the direction for data bars. This
5992 property can be either "left" for left-to-right or "right" for right-
5993 to-left. If the property isn't set then Excel will adjust the position
5994 automatically based on the context:
5995
5996 $worksheet->conditional_formatting( 'J3:J14',
5997 {
5998 type => 'data_bar',
5999 bar_direction => 'right'
6000 }
6001 );
6002
6003 Note, this property is only visible in Excel 2010 and later.
6004
6005 bar_axis_position
6006 The "bar_axis_position" parameter sets the position within the cells
6007 for the axis that is shown in data bars when there are negative values
6008 to display. The property can be either "middle" or "none". If the
6009 property isn't set then Excel will position the axis based on the range
6010 of positive and negative values.
6011
6012 Note, this property is only visible in Excel 2010 and later.
6013
6014 bar_axis_color
6015 The "bar_axis_color" parameter sets the color for the axis that is
6016 shown in data bars when there are negative values to display.
6017
6018 The color can be specified as an Excel::Writer::XLSX color index or as
6019 a HTML style RGB hex number, as shown in the other examples.
6020
6021 Note, this property is only visible in Excel 2010 and later.
6022
6023 data_bar_2010
6024 The "data_bar_2010" parameter sets Excel 2010 style data bars even when
6025 Excel 2010 specific properties aren't used. This can be used to create
6026 consistency across all the data bar formatting in a worksheet:
6027
6028 $worksheet->conditional_formatting( 'L3:L14',
6029 {
6030 type => 'data_bar',
6031 data_bar_2010 => 1
6032 }
6033 );
6034
6035 Note, this property is only visible in Excel 2010 and later.
6036
6037 stop_if_true
6038 The "stop_if_true" parameter, if set to a true value, will enable the
6039 "stop if true" feature on the conditional formatting rule, so that
6040 subsequent rules are not examined for any cell on which the conditions
6041 for this rule are met.
6042
6043 Conditional Formatting Examples
6044 Example 1. Highlight cells greater than an integer value.
6045
6046 $worksheet->conditional_formatting( 'A1:F10',
6047 {
6048 type => 'cell',
6049 criteria => 'greater than',
6050 value => 5,
6051 format => $format,
6052 }
6053 );
6054
6055 Example 2. Highlight cells greater than a value in a reference cell.
6056
6057 $worksheet->conditional_formatting( 'A1:F10',
6058 {
6059 type => 'cell',
6060 criteria => 'greater than',
6061 value => '$H$1',
6062 format => $format,
6063 }
6064 );
6065
6066 Example 3. Highlight cells greater than a certain date:
6067
6068 $worksheet->conditional_formatting( 'A1:F10',
6069 {
6070 type => 'date',
6071 criteria => 'greater than',
6072 value => '2011-01-01T',
6073 format => $format,
6074 }
6075 );
6076
6077 Example 4. Highlight cells with a date in the last seven days:
6078
6079 $worksheet->conditional_formatting( 'A1:F10',
6080 {
6081 type => 'time_period',
6082 criteria => 'last 7 days',
6083 format => $format,
6084 }
6085 );
6086
6087 Example 5. Highlight cells with strings starting with the letter "b":
6088
6089 $worksheet->conditional_formatting( 'A1:F10',
6090 {
6091 type => 'text',
6092 criteria => 'begins with',
6093 value => 'b',
6094 format => $format,
6095 }
6096 );
6097
6098 Example 6. Highlight cells that are 1 std deviation above the average
6099 for the range:
6100
6101 $worksheet->conditional_formatting( 'A1:F10',
6102 {
6103 type => 'average',
6104 format => $format,
6105 }
6106 );
6107
6108 Example 7. Highlight duplicate cells in a range:
6109
6110 $worksheet->conditional_formatting( 'A1:F10',
6111 {
6112 type => 'duplicate',
6113 format => $format,
6114 }
6115 );
6116
6117 Example 8. Highlight unique cells in a range.
6118
6119 $worksheet->conditional_formatting( 'A1:F10',
6120 {
6121 type => 'unique',
6122 format => $format,
6123 }
6124 );
6125
6126 Example 9. Highlight the top 10 cells.
6127
6128 $worksheet->conditional_formatting( 'A1:F10',
6129 {
6130 type => 'top',
6131 value => 10,
6132 format => $format,
6133 }
6134 );
6135
6136 Example 10. Highlight blank cells.
6137
6138 $worksheet->conditional_formatting( 'A1:F10',
6139 {
6140 type => 'blanks',
6141 format => $format,
6142 }
6143 );
6144
6145 Example 11. Set traffic light icons in 3 cells:
6146
6147 $worksheet->conditional_formatting( 'A1:C1',
6148 {
6149 type => 'icon_set',
6150 icon_style => '3_traffic_lights',
6151 }
6152 );
6153
6154 See also the "conditional_format.pl" example program in "EXAMPLES".
6155
6157 Sparklines are a feature of Excel 2010+ which allows you to add small
6158 charts to worksheet cells. These are useful for showing visual trends
6159 in data in a compact format.
6160
6161 In Excel::Writer::XLSX Sparklines can be added to cells using the
6162 "add_sparkline()" worksheet method:
6163
6164 $worksheet->add_sparkline(
6165 {
6166 location => 'F2',
6167 range => 'Sheet1!A2:E2',
6168 type => 'column',
6169 style => 12,
6170 }
6171 );
6172
6173 Note: Sparklines are a feature of Excel 2010+ only. You can write them
6174 to an XLSX file that can be read by Excel 2007 but they won't be
6175 displayed.
6176
6177 add_sparkline( { parameter => 'value', ... } )
6178 The "add_sparkline()" worksheet method is used to add sparklines to a
6179 cell or a range of cells.
6180
6181 The parameters to "add_sparkline()" must be passed in a hash ref. The
6182 main sparkline parameters are:
6183
6184 location (required)
6185 range (required)
6186 type
6187 style
6188
6189 markers
6190 negative_points
6191 axis
6192 reverse
6193
6194 Other, less commonly used parameters are:
6195
6196 high_point
6197 low_point
6198 first_point
6199 last_point
6200 max
6201 min
6202 empty_cells
6203 show_hidden
6204 date_axis
6205 weight
6206
6207 series_color
6208 negative_color
6209 markers_color
6210 first_color
6211 last_color
6212 high_color
6213 low_color
6214
6215 These parameters are explained in the sections below:
6216
6217 location
6218 This is the cell where the sparkline will be displayed:
6219
6220 location => 'F1'
6221
6222 The "location" should be a single cell. (For multiple cells see
6223 "Grouped Sparklines" below).
6224
6225 To specify the location in row-column notation use the
6226 "xl_rowcol_to_cell()" function from the Excel::Writer::XLSX::Utility
6227 module.
6228
6229 use Excel::Writer::XLSX::Utility ':rowcol';
6230 ...
6231 location => xl_rowcol_to_cell( 0, 5 ), # F1
6232
6233 range
6234 This specifies the cell data range that the sparkline will plot:
6235
6236 $worksheet->add_sparkline(
6237 {
6238 location => 'F1',
6239 range => 'A1:E1',
6240 }
6241 );
6242
6243 The "range" should be a 2D array. (For 3D arrays of cells see "Grouped
6244 Sparklines" below).
6245
6246 If "range" is not on the same worksheet you can specify its location
6247 using the usual Excel notation:
6248
6249 range => 'Sheet1!A1:E1',
6250
6251 If the worksheet contains spaces or special characters you should quote
6252 the worksheet name in the same way that Excel does:
6253
6254 range => q('Monthly Data'!A1:E1),
6255
6256 To specify the location in row-column notation use the "xl_range()" or
6257 "xl_range_formula()" functions from the Excel::Writer::XLSX::Utility
6258 module.
6259
6260 use Excel::Writer::XLSX::Utility ':rowcol';
6261 ...
6262 range => xl_range( 1, 1, 0, 4 ), # 'A1:E1'
6263 range => xl_range_formula( 'Sheet1', 0, 0, 0, 4 ), # 'Sheet1!A2:E2'
6264
6265 type
6266 Specifies the type of sparkline. There are 3 available sparkline types:
6267
6268 line (default)
6269 column
6270 win_loss
6271
6272 For example:
6273
6274 {
6275 location => 'F1',
6276 range => 'A1:E1',
6277 type => 'column',
6278 }
6279
6280 style
6281 Excel provides 36 built-in Sparkline styles in 6 groups of 6. The
6282 "style" parameter can be used to replicate these and should be a
6283 corresponding number from 1 .. 36.
6284
6285 {
6286 location => 'A14',
6287 range => 'Sheet2!A2:J2',
6288 style => 3,
6289 }
6290
6291 The style number starts in the top left of the style grid and runs left
6292 to right. The default style is 1. It is possible to override colour
6293 elements of the sparklines using the *_color parameters below.
6294
6295 markers
6296 Turn on the markers for "line" style sparklines.
6297
6298 {
6299 location => 'A6',
6300 range => 'Sheet2!A1:J1',
6301 markers => 1,
6302 }
6303
6304 Markers aren't shown in Excel for "column" and "win_loss" sparklines.
6305
6306 negative_points
6307 Highlight negative values in a sparkline range. This is usually
6308 required with "win_loss" sparklines.
6309
6310 {
6311 location => 'A21',
6312 range => 'Sheet2!A3:J3',
6313 type => 'win_loss',
6314 negative_points => 1,
6315 }
6316
6317 axis
6318 Display a horizontal axis in the sparkline:
6319
6320 {
6321 location => 'A10',
6322 range => 'Sheet2!A1:J1',
6323 axis => 1,
6324 }
6325
6326 reverse
6327 Plot the data from right-to-left instead of the default left-to-right:
6328
6329 {
6330 location => 'A24',
6331 range => 'Sheet2!A4:J4',
6332 type => 'column',
6333 reverse => 1,
6334 }
6335
6336 weight
6337 Adjust the default line weight (thickness) for "line" style sparklines.
6338
6339 weight => 0.25,
6340
6341 The weight value should be one of the following values allowed by
6342 Excel:
6343
6344 0.25 0.5 0.75
6345 1 1.25
6346 2.25
6347 3
6348 4.25
6349 6
6350
6351 high_point, low_point, first_point, last_point
6352 Highlight points in a sparkline range.
6353
6354 high_point => 1,
6355 low_point => 1,
6356 first_point => 1,
6357 last_point => 1,
6358
6359 max, min
6360 Specify the maximum and minimum vertical axis values:
6361
6362 max => 0.5,
6363 min => -0.5,
6364
6365 As a special case you can set the maximum and minimum to be for a group
6366 of sparklines rather than one:
6367
6368 max => 'group',
6369
6370 See "Grouped Sparklines" below.
6371
6372 empty_cells
6373 Define how empty cells are handled in a sparkline.
6374
6375 empty_cells => 'zero',
6376
6377 The available options are:
6378
6379 gaps : show empty cells as gaps (the default).
6380 zero : plot empty cells as 0.
6381 connect: Connect points with a line ("line" type sparklines only).
6382
6383 show_hidden
6384 Plot data in hidden rows and columns:
6385
6386 show_hidden => 1,
6387
6388 Note, this option is off by default.
6389
6390 date_axis
6391 Specify an alternative date axis for the sparkline. This is useful if
6392 the data being plotted isn't at fixed width intervals:
6393
6394 {
6395 location => 'F3',
6396 range => 'A3:E3',
6397 date_axis => 'A4:E4',
6398 }
6399
6400 The number of cells in the date range should correspond to the number
6401 of cells in the data range.
6402
6403 series_color
6404 It is possible to override the colour of a sparkline style using the
6405 following parameters:
6406
6407 series_color
6408 negative_color
6409 markers_color
6410 first_color
6411 last_color
6412 high_color
6413 low_color
6414
6415 The color should be specified as a HTML style "#rrggbb" hex value:
6416
6417 {
6418 location => 'A18',
6419 range => 'Sheet2!A2:J2',
6420 type => 'column',
6421 series_color => '#E965E0',
6422 }
6423
6424 Grouped Sparklines
6425 The "add_sparkline()" worksheet method can be used multiple times to
6426 write as many sparklines as are required in a worksheet.
6427
6428 However, it is sometimes necessary to group contiguous sparklines so
6429 that changes that are applied to one are applied to all. In Excel this
6430 is achieved by selecting a 3D range of cells for the data "range" and a
6431 2D range of cells for the "location".
6432
6433 In Excel::Writer::XLSX, you can simulate this by passing an array refs
6434 of values to "location" and "range":
6435
6436 {
6437 location => [ 'A27', 'A28', 'A29' ],
6438 range => [ 'Sheet2!A5:J5', 'Sheet2!A6:J6', 'Sheet2!A7:J7' ],
6439 markers => 1,
6440 }
6441
6442 Sparkline examples
6443 See the "sparklines1.pl" and "sparklines2.pl" example programs in the
6444 "examples" directory of the distro.
6445
6447 Tables in Excel are a way of grouping a range of cells into a single
6448 entity that has common formatting or that can be referenced from
6449 formulas. Tables can have column headers, autofilters, total rows,
6450 column formulas and default formatting.
6451
6452 For more information see "An Overview of Excel Tables"
6453 <http://office.microsoft.com/en-us/excel-help/overview-of-excel-tables-HA010048546.aspx>.
6454
6455 Note, tables don't work in Excel::Writer::XLSX when
6456 "set_optimization()" mode in on.
6457
6458 add_table( $row1, $col1, $row2, $col2, { parameter => 'value', ... })
6459 Tables are added to a worksheet using the "add_table()" method:
6460
6461 $worksheet->add_table( 'B3:F7', { %parameters } );
6462
6463 The data range can be specified in 'A1' or 'row/col' notation (see also
6464 the note about "Cell notation" for more information):
6465
6466 $worksheet->add_table( 'B3:F7' );
6467 # Same as:
6468 $worksheet->add_table( 2, 1, 6, 5 );
6469
6470 The last parameter in "add_table()" should be a hash ref containing the
6471 parameters that describe the table options and data. The available
6472 parameters are:
6473
6474 data
6475 autofilter
6476 header_row
6477 banded_columns
6478 banded_rows
6479 first_column
6480 last_column
6481 style
6482 total_row
6483 columns
6484 name
6485
6486 The table parameters are detailed below. There are no required
6487 parameters and the hash ref isn't required if no options are specified.
6488
6489 data
6490 The "data" parameter can be used to specify the data in the cells of
6491 the table.
6492
6493 my $data = [
6494 [ 'Apples', 10000, 5000, 8000, 6000 ],
6495 [ 'Pears', 2000, 3000, 4000, 5000 ],
6496 [ 'Bananas', 6000, 6000, 6500, 6000 ],
6497 [ 'Oranges', 500, 300, 200, 700 ],
6498
6499 ];
6500
6501 $worksheet->add_table( 'B3:F7', { data => $data } );
6502
6503 Table data can also be written separately, as an array or individual
6504 cells.
6505
6506 # These two statements are the same as the single statement above.
6507 $worksheet->add_table( 'B3:F7' );
6508 $worksheet->write_col( 'B4', $data );
6509
6510 Writing the cell data separately is occasionally required when you need
6511 to control the "write_*()" method used to populate the cells or if you
6512 wish to tweak the cell formatting.
6513
6514 The "data" structure should be an array ref of array refs holding row
6515 data as shown above.
6516
6517 header_row
6518 The "header_row" parameter can be used to turn on or off the header row
6519 in the table. It is on by default.
6520
6521 $worksheet->add_table( 'B4:F7', { header_row => 0 } ); # Turn header off.
6522
6523 The header row will contain default captions such as "Column 1",
6524 "Column 2", etc. These captions can be overridden using the "columns"
6525 parameter below.
6526
6527 autofilter
6528 The "autofilter" parameter can be used to turn on or off the autofilter
6529 in the header row. It is on by default.
6530
6531 $worksheet->add_table( 'B3:F7', { autofilter => 0 } ); # Turn autofilter off.
6532
6533 The "autofilter" is only shown if the "header_row" is on. Filters
6534 within the table are not supported.
6535
6536 banded_rows
6537 The "banded_rows" parameter can be used to used to create rows of
6538 alternating colour in the table. It is on by default.
6539
6540 $worksheet->add_table( 'B3:F7', { banded_rows => 0 } );
6541
6542 banded_columns
6543 The "banded_columns" parameter can be used to used to create columns of
6544 alternating colour in the table. It is off by default.
6545
6546 $worksheet->add_table( 'B3:F7', { banded_columns => 1 } );
6547
6548 first_column
6549 The "first_column" parameter can be used to highlight the first column
6550 of the table. The type of highlighting will depend on the "style" of
6551 the table. It may be bold text or a different colour. It is off by
6552 default.
6553
6554 $worksheet->add_table( 'B3:F7', { first_column => 1 } );
6555
6556 last_column
6557 The "last_column" parameter can be used to highlight the last column of
6558 the table. The type of highlighting will depend on the "style" of the
6559 table. It may be bold text or a different colour. It is off by default.
6560
6561 $worksheet->add_table( 'B3:F7', { last_column => 1 } );
6562
6563 style
6564 The "style" parameter can be used to set the style of the table.
6565 Standard Excel table format names should be used (with matching
6566 capitalisation):
6567
6568 $worksheet11->add_table(
6569 'B3:F7',
6570 {
6571 data => $data,
6572 style => 'Table Style Light 11',
6573 }
6574 );
6575
6576 The default table style is 'Table Style Medium 9'.
6577
6578 You can also turn the table style off by setting it to 'None':
6579
6580 $worksheet11->add_table( 'B3:F7', { style => 'None' } );
6581
6582 name
6583 By default tables are named "Table1", "Table2", etc. The "name"
6584 parameter can be used to set the name of the table:
6585
6586 $worksheet->add_table( 'B3:F7', { name => 'SalesData' } );
6587
6588 If you override the table name you must ensure that it doesn't clash
6589 with an existing table name and that it follows Excel's requirements
6590 for table names
6591 <http://office.microsoft.com/en-001/excel-help/define-and-use-names-in-formulas-HA010147120.aspx#BMsyntax_rules_for_names>.
6592
6593 If you need to know the name of the table, for example to use it in a
6594 formula, you can get it as follows:
6595
6596 my $table = $worksheet2->add_table( 'B3:F7' );
6597 my $table_name = $table->{_name};
6598
6599 total_row
6600 The "total_row" parameter can be used to turn on the total row in the
6601 last row of a table. It is distinguished from the other rows by a
6602 different formatting and also with dropdown "SUBTOTAL" functions.
6603
6604 $worksheet->add_table( 'B3:F7', { total_row => 1 } );
6605
6606 The default total row doesn't have any captions or functions. These
6607 must by specified via the "columns" parameter below.
6608
6609 columns
6610 The "columns" parameter can be used to set properties for columns
6611 within the table.
6612
6613 The sub-properties that can be set are:
6614
6615 header
6616 formula
6617 total_string
6618 total_function
6619 total_value
6620 format
6621 header_format
6622
6623 The column data must be specified as an array ref of hash refs. For
6624 example to override the default 'Column n' style table headers:
6625
6626 $worksheet->add_table(
6627 'B3:F7',
6628 {
6629 data => $data,
6630 columns => [
6631 { header => 'Product' },
6632 { header => 'Quarter 1' },
6633 { header => 'Quarter 2' },
6634 { header => 'Quarter 3' },
6635 { header => 'Quarter 4' },
6636 ]
6637 }
6638 );
6639
6640 If you don't wish to specify properties for a specific column you pass
6641 an empty hash ref and the defaults will be applied:
6642
6643 ...
6644 columns => [
6645 { header => 'Product' },
6646 { header => 'Quarter 1' },
6647 { }, # Defaults to 'Column 3'.
6648 { header => 'Quarter 3' },
6649 { header => 'Quarter 4' },
6650 ]
6651 ...
6652
6653 Column formulas can by applied using the "formula" column property:
6654
6655 $worksheet8->add_table(
6656 'B3:G7',
6657 {
6658 data => $data,
6659 columns => [
6660 { header => 'Product' },
6661 { header => 'Quarter 1' },
6662 { header => 'Quarter 2' },
6663 { header => 'Quarter 3' },
6664 { header => 'Quarter 4' },
6665 {
6666 header => 'Year',
6667 formula => '=SUM(Table8[@[Quarter 1]:[Quarter 4]])'
6668 },
6669 ]
6670 }
6671 );
6672
6673 The Excel 2007 "[#This Row]" and Excel 2010 "@" structural references
6674 are supported within the formula.
6675
6676 As stated above the "total_row" table parameter turns on the "Total"
6677 row in the table but it doesn't populate it with any defaults. Total
6678 captions and functions must be specified via the "columns" property and
6679 the "total_string", "total_function" and "total_value" sub properties:
6680
6681 $worksheet10->add_table(
6682 'B3:F8',
6683 {
6684 data => $data,
6685 total_row => 1,
6686 columns => [
6687 { header => 'Product', total_string => 'Totals' },
6688 { header => 'Quarter 1', total_function => 'sum' },
6689 { header => 'Quarter 2', total_function => 'sum' },
6690 { header => 'Quarter 3', total_function => 'sum' },
6691 { header => 'Quarter 4', total_function => 'sum' },
6692 ]
6693 }
6694 );
6695
6696 The supported totals row "SUBTOTAL" functions are:
6697
6698 average
6699 count_nums
6700 count
6701 max
6702 min
6703 std_dev
6704 sum
6705 var
6706
6707 User defined functions or formulas aren't supported.
6708
6709 It is also possible to set a calculated value for the "total_function"
6710 using the "total_value" sub property. This is only necessary when
6711 creating workbooks for applications that cannot calculate the value of
6712 formulas automatically. This is similar to setting the "value" optional
6713 property in "write_formula()":
6714
6715 $worksheet10->add_table(
6716 'B3:F8',
6717 {
6718 data => $data,
6719 total_row => 1,
6720 columns => [
6721 { total_string => 'Totals' },
6722 { total_function => 'sum', total_value => 100 },
6723 { total_function => 'sum', total_value => 200 },
6724 { total_function => 'sum', total_value => 100 },
6725 { total_function => 'sum', total_value => 400 },
6726 ]
6727 }
6728 );
6729
6730 Formatting can also be applied to columns, to the column data using
6731 "format" and to the header using "header_format":
6732
6733 my $currency_format = $workbook->add_format( num_format => '$#,##0' );
6734
6735 $worksheet->add_table(
6736 'B3:D8',
6737 {
6738 data => $data,
6739 total_row => 1,
6740 columns => [
6741 { header => 'Product', total_string => 'Totals' },
6742 {
6743 header => 'Quarter 1',
6744 total_function => 'sum',
6745 format => $currency_format,
6746 },
6747 {
6748 header => 'Quarter 2',
6749 header_format => $bold,
6750 total_function => 'sum',
6751 format => $currency_format,
6752 },
6753 ]
6754 }
6755 );
6756
6757 Standard Excel::Writer::XLSX format objects can be used. However, they
6758 should be limited to numerical formats for the columns and simple
6759 formatting like text wrap for the headers. Overriding other table
6760 formatting may produce inconsistent results.
6761
6763 Introduction
6764 The following is a brief introduction to formulas and functions in
6765 Excel and Excel::Writer::XLSX.
6766
6767 A formula is a string that begins with an equals sign:
6768
6769 '=A1+B1'
6770 '=AVERAGE(1, 2, 3)'
6771
6772 The formula can contain numbers, strings, boolean values, cell
6773 references, cell ranges and functions. Named ranges are not supported.
6774 Formulas should be written as they appear in Excel, that is cells and
6775 functions must be in uppercase.
6776
6777 Cells in Excel are referenced using the A1 notation system where the
6778 column is designated by a letter and the row by a number. Columns range
6779 from A to XFD i.e. 0 to 16384, rows range from 1 to 1048576. The
6780 "Excel::Writer::XLSX::Utility" module that is included in the distro
6781 contains helper functions for dealing with A1 notation, for example:
6782
6783 use Excel::Writer::XLSX::Utility;
6784
6785 ( $row, $col ) = xl_cell_to_rowcol( 'C2' ); # (1, 2)
6786 $str = xl_rowcol_to_cell( 1, 2 ); # C2
6787
6788 The Excel "$" notation in cell references is also supported. This
6789 allows you to specify whether a row or column is relative or absolute.
6790 This only has an effect if the cell is copied. The following examples
6791 show relative and absolute values.
6792
6793 '=A1' # Column and row are relative
6794 '=$A1' # Column is absolute and row is relative
6795 '=A$1' # Column is relative and row is absolute
6796 '=$A$1' # Column and row are absolute
6797
6798 Formulas can also refer to cells in other worksheets of the current
6799 workbook. For example:
6800
6801 '=Sheet2!A1'
6802 '=Sheet2!A1:A5'
6803 '=Sheet2:Sheet3!A1'
6804 '=Sheet2:Sheet3!A1:A5'
6805 q{='Test Data'!A1}
6806 q{='Test Data1:Test Data2'!A1}
6807
6808 The sheet reference and the cell reference are separated by "!" the
6809 exclamation mark symbol. If worksheet names contain spaces, commas or
6810 parentheses then Excel requires that the name is enclosed in single
6811 quotes as shown in the last two examples above. In order to avoid using
6812 a lot of escape characters you can use the quote operator "q{}" to
6813 protect the quotes. See "perlop" in the main Perl documentation. Only
6814 valid sheet names that have been added using the "add_worksheet()"
6815 method can be used in formulas. You cannot reference external
6816 workbooks.
6817
6818 The following table lists the operators that are available in Excel's
6819 formulas. The majority of the operators are the same as Perl's,
6820 differences are indicated:
6821
6822 Arithmetic operators:
6823 =====================
6824 Operator Meaning Example
6825 + Addition 1+2
6826 - Subtraction 2-1
6827 * Multiplication 2*3
6828 / Division 1/4
6829 ^ Exponentiation 2^3 # Equivalent to **
6830 - Unary minus -(1+2)
6831 % Percent (Not modulus) 13%
6832
6833
6834 Comparison operators:
6835 =====================
6836 Operator Meaning Example
6837 = Equal to A1 = B1 # Equivalent to ==
6838 <> Not equal to A1 <> B1 # Equivalent to !=
6839 > Greater than A1 > B1
6840 < Less than A1 < B1
6841 >= Greater than or equal to A1 >= B1
6842 <= Less than or equal to A1 <= B1
6843
6844
6845 String operator:
6846 ================
6847 Operator Meaning Example
6848 & Concatenation "Hello " & "World!" # [1]
6849
6850
6851 Reference operators:
6852 ====================
6853 Operator Meaning Example
6854 : Range operator A1:A4 # [2]
6855 , Union operator SUM(1, 2+2, B3) # [3]
6856
6857
6858 Notes:
6859 [1]: Equivalent to "Hello " . "World!" in Perl.
6860 [2]: This range is equivalent to cells A1, A2, A3 and A4.
6861 [3]: The comma behaves like the list separator in Perl.
6862
6863 The range and comma operators can have different symbols in non-English
6864 versions of Excel, see below.
6865
6866 For a general introduction to Excel's formulas and an explanation of
6867 the syntax of the function refer to the Excel help files or the
6868 following:
6869 <http://office.microsoft.com/en-us/assistance/CH062528031033.aspx>.
6870
6871 In most cases a formula in Excel can be used directly in the
6872 "write_formula" method. However, there are a few potential issues and
6873 differences that the user should be aware of. These are explained in
6874 the following sections.
6875
6876 Non US Excel functions and syntax
6877 Excel stores formulas in the format of the US English version,
6878 regardless of the language or locale of the end-user's version of
6879 Excel. Therefore all formula function names written using
6880 Excel::Writer::XLSX must be in English:
6881
6882 worksheet->write_formula('A1', '=SUM(1, 2, 3)'); # OK
6883 worksheet->write_formula('A2', '=SOMME(1, 2, 3)'); # French. Error on load.
6884
6885 Also, formulas must be written with the US style separator/range
6886 operator which is a comma (not semi-colon). Therefore a formula with
6887 multiple values should be written as follows:
6888
6889 worksheet->write_formula('A1', '=SUM(1, 2, 3)'); # OK
6890 worksheet->write_formula('A2', '=SUM(1; 2; 3)'); # Semi-colon. Error on load.
6891
6892 If you have a non-English version of Excel you can use the following
6893 multi-lingual Formula Translator
6894 (<http://en.excel-translator.de/language/>) to help you convert the
6895 formula. It can also replace semi-colons with commas.
6896
6897 Formulas added in Excel 2010 and later
6898 Excel 2010 and later added functions which weren't defined in the
6899 original file specification. These functions are referred to by
6900 Microsoft as future functions. Examples of these functions are "ACOT",
6901 "CHISQ.DIST.RT" , "CONFIDENCE.NORM", "STDEV.P", "STDEV.S" and
6902 "WORKDAY.INTL".
6903
6904 When written using "write_formula()" these functions need to be fully
6905 qualified with a "_xlfn." (or other) prefix as they are shown the list
6906 below. For example:
6907
6908 worksheet->write_formula('A1', '=_xlfn.STDEV.S(B1:B10)')
6909
6910 They will appear without the prefix in Excel.
6911
6912 The following list is taken from the MS XLSX extensions documentation
6913 on future functions:
6914 <http://msdn.microsoft.com/en-us/library/dd907480%28v=office.12%29.aspx>:
6915
6916 _xlfn.ACOT
6917 _xlfn.ACOTH
6918 _xlfn.AGGREGATE
6919 _xlfn.ARABIC
6920 _xlfn.BASE
6921 _xlfn.BETA.DIST
6922 _xlfn.BETA.INV
6923 _xlfn.BINOM.DIST
6924 _xlfn.BINOM.DIST.RANGE
6925 _xlfn.BINOM.INV
6926 _xlfn.BITAND
6927 _xlfn.BITLSHIFT
6928 _xlfn.BITOR
6929 _xlfn.BITRSHIFT
6930 _xlfn.BITXOR
6931 _xlfn.CEILING.MATH
6932 _xlfn.CEILING.PRECISE
6933 _xlfn.CHISQ.DIST
6934 _xlfn.CHISQ.DIST.RT
6935 _xlfn.CHISQ.INV
6936 _xlfn.CHISQ.INV.RT
6937 _xlfn.CHISQ.TEST
6938 _xlfn.COMBINA
6939 _xlfn.CONFIDENCE.NORM
6940 _xlfn.CONFIDENCE.T
6941 _xlfn.COT
6942 _xlfn.COTH
6943 _xlfn.COVARIANCE.P
6944 _xlfn.COVARIANCE.S
6945 _xlfn.CSC
6946 _xlfn.CSCH
6947 _xlfn.DAYS
6948 _xlfn.DECIMAL
6949 ECMA.CEILING
6950 _xlfn.ERF.PRECISE
6951 _xlfn.ERFC.PRECISE
6952 _xlfn.EXPON.DIST
6953 _xlfn.F.DIST
6954 _xlfn.F.DIST.RT
6955 _xlfn.F.INV
6956 _xlfn.F.INV.RT
6957 _xlfn.F.TEST
6958 _xlfn.FILTERXML
6959 _xlfn.FLOOR.MATH
6960 _xlfn.FLOOR.PRECISE
6961 _xlfn.FORECAST.ETS
6962 _xlfn.FORECAST.ETS.CONFINT
6963 _xlfn.FORECAST.ETS.SEASONALITY
6964 _xlfn.FORECAST.ETS.STAT
6965 _xlfn.FORECAST.LINEAR
6966 _xlfn.FORMULATEXT
6967 _xlfn.GAMMA
6968 _xlfn.GAMMA.DIST
6969 _xlfn.GAMMA.INV
6970 _xlfn.GAMMALN.PRECISE
6971 _xlfn.GAUSS
6972 _xlfn.HYPGEOM.DIST
6973 _xlfn.IFNA
6974 _xlfn.IMCOSH
6975 _xlfn.IMCOT
6976 _xlfn.IMCSC
6977 _xlfn.IMCSCH
6978 _xlfn.IMSEC
6979 _xlfn.IMSECH
6980 _xlfn.IMSINH
6981 _xlfn.IMTAN
6982 _xlfn.ISFORMULA
6983 ISO.CEILING
6984 _xlfn.ISOWEEKNUM
6985 _xlfn.LOGNORM.DIST
6986 _xlfn.LOGNORM.INV
6987 _xlfn.MODE.MULT
6988 _xlfn.MODE.SNGL
6989 _xlfn.MUNIT
6990 _xlfn.NEGBINOM.DIST
6991 NETWORKDAYS.INTL
6992 _xlfn.NORM.DIST
6993 _xlfn.NORM.INV
6994 _xlfn.NORM.S.DIST
6995 _xlfn.NORM.S.INV
6996 _xlfn.NUMBERVALUE
6997 _xlfn.PDURATION
6998 _xlfn.PERCENTILE.EXC
6999 _xlfn.PERCENTILE.INC
7000 _xlfn.PERCENTRANK.EXC
7001 _xlfn.PERCENTRANK.INC
7002 _xlfn.PERMUTATIONA
7003 _xlfn.PHI
7004 _xlfn.POISSON.DIST
7005 _xlfn.QUARTILE.EXC
7006 _xlfn.QUARTILE.INC
7007 _xlfn.QUERYSTRING
7008 _xlfn.RANK.AVG
7009 _xlfn.RANK.EQ
7010 _xlfn.RRI
7011 _xlfn.SEC
7012 _xlfn.SECH
7013 _xlfn.SHEET
7014 _xlfn.SHEETS
7015 _xlfn.SKEW.P
7016 _xlfn.STDEV.P
7017 _xlfn.STDEV.S
7018 _xlfn.T.DIST
7019 _xlfn.T.DIST.2T
7020 _xlfn.T.DIST.RT
7021 _xlfn.T.INV
7022 _xlfn.T.INV.2T
7023 _xlfn.T.TEST
7024 _xlfn.UNICHAR
7025 _xlfn.UNICODE
7026 _xlfn.VAR.P
7027 _xlfn.VAR.S
7028 _xlfn.WEBSERVICE
7029 _xlfn.WEIBULL.DIST
7030 WORKDAY.INTL
7031 _xlfn.XOR
7032 _xlfn.Z.TEST
7033
7034 Using Tables in Formulas
7035 Worksheet tables can be added with Excel::Writer::XLSX using the
7036 "add_table()" method:
7037
7038 worksheet->add_table('B3:F7', {options});
7039
7040 By default tables are named "Table1", "Table2", etc., in the order that
7041 they are added. However it can also be set by the user using the "name"
7042 parameter:
7043
7044 worksheet->add_table('B3:F7', {'name': 'SalesData'});
7045
7046 If you need to know the name of the table, for example to use it in a
7047 formula, you can get it as follows:
7048
7049 table = worksheet->add_table('B3:F7');
7050 table_name = table->{_name};
7051
7052 When used in a formula a table name such as "TableX" should be referred
7053 to as "TableX[]" (like a Perl array):
7054
7055 worksheet->write_formula('A5', '=VLOOKUP("Sales", Table1[], 2, FALSE');
7056
7057 Dealing with #NAME? errors
7058 If there is an error in the syntax of a formula it is usually displayed
7059 in Excel as "#NAME?". If you encounter an error like this you can debug
7060 it as follows:
7061
7062 1. Ensure the formula is valid in Excel by copying and pasting it into
7063 a cell. Note, this should be done in Excel and not other applications
7064 such as OpenOffice or LibreOffice since they may have slightly
7065 different syntax.
7066 2. Ensure the formula is using comma separators instead of semi-colons,
7067 see "Non US Excel functions and syntax" above.
7068 3. Ensure the formula is in English, see "Non US Excel functions and
7069 syntax" above.
7070 4. Ensure that the formula doesn't contain an Excel 2010+ future
7071 function as listed in "Formulas added in Excel 2010 and later" above.
7072 If it does then ensure that the correct prefix is used.
7073
7074 Finally if you have completed all the previous steps and still get a
7075 "#NAME?" error you can examine a valid Excel file to see what the
7076 correct syntax should be. To do this you should create a valid formula
7077 in Excel and save the file. You can then examine the XML in the
7078 unzipped file.
7079
7080 The following shows how to do that using Linux "unzip" and libxml's
7081 xmllint <http://xmlsoft.org/xmllint.html> to format the XML for
7082 clarity:
7083
7084 $ unzip myfile.xlsx -d myfile
7085 $ xmllint --format myfile/xl/worksheets/sheet1.xml | grep '<f>'
7086
7087 <f>SUM(1, 2, 3)</f>
7088
7089 Formula Results
7090 Excel::Writer::XLSX doesn't calculate the result of a formula and
7091 instead stores the value 0 as the formula result. It then sets a global
7092 flag in the XLSX file to say that all formulas and functions should be
7093 recalculated when the file is opened.
7094
7095 This is the method recommended in the Excel documentation and in
7096 general it works fine with spreadsheet applications. However,
7097 applications that don't have a facility to calculate formulas will only
7098 display the 0 results. Examples of such applications are Excel Viewer,
7099 PDF Converters, and some mobile device applications.
7100
7101 If required, it is also possible to specify the calculated result of
7102 the formula using the optional last "value" parameter in
7103 "write_formula":
7104
7105 worksheet->write_formula('A1', '=2+2', num_format, 4);
7106
7107 The "value" parameter can be a number, a string, a boolean sting
7108 ('TRUE' or 'FALSE') or one of the following Excel error codes:
7109
7110 #DIV/0!
7111 #N/A
7112 #NAME?
7113 #NULL!
7114 #NUM!
7115 #REF!
7116 #VALUE!
7117
7118 It is also possible to specify the calculated result of an array
7119 formula created with "write_array_formula":
7120
7121 # Specify the result for a single cell range.
7122 worksheet->write_array_formula('A1:A1', '{=SUM(B1:C1*B2:C2)}', format, 2005);
7123
7124 However, using this parameter only writes a single value to the upper
7125 left cell in the result array. For a multi-cell array formula where the
7126 results are required, the other result values can be specified by using
7127 "write_number()" to write to the appropriate cell:
7128
7129 # Specify the results for a multi cell range.
7130 worksheet->write_array_formula('A1:A3', '{=TREND(C1:C3,B1:B3)}', format, 15);
7131 worksheet->write_number('A2', 12, format);
7132 worksheet->write_number('A3', 14, format);
7133
7135 An Excel "xlsm" file is exactly the same as a "xlsx" file except that
7136 is includes an additional "vbaProject.bin" file which contains
7137 functions and/or macros. Excel uses a different extension to
7138 differentiate between the two file formats since files containing
7139 macros are usually subject to additional security checks.
7140
7141 The "vbaProject.bin" file is a binary OLE COM container. This was the
7142 format used in older "xls" versions of Excel prior to Excel 2007.
7143 Unlike all of the other components of an xlsx/xlsm file the data isn't
7144 stored in XML format. Instead the functions and macros as stored as
7145 pre-parsed binary format. As such it wouldn't be feasible to define
7146 macros and create a "vbaProject.bin" file from scratch (at least not in
7147 the remaining lifespan and interest levels of the author).
7148
7149 Instead a workaround is used to extract "vbaProject.bin" files from
7150 existing xlsm files and then add these to Excel::Writer::XLSX files.
7151
7152 The extract_vba utility
7153 The "extract_vba" utility is used to extract the "vbaProject.bin"
7154 binary from an Excel 2007+ xlsm file. The utility is included in the
7155 Excel::Writer::XLSX bin directory and is also installed as a standalone
7156 executable file:
7157
7158 $ extract_vba macro_file.xlsm
7159 Extracted: vbaProject.bin
7160
7161 Adding the VBA macros to a Excel::Writer::XLSX file
7162 Once the "vbaProject.bin" file has been extracted it can be added to
7163 the Excel::Writer::XLSX workbook using the "add_vba_project()" method:
7164
7165 $workbook->add_vba_project( './vbaProject.bin' );
7166
7167 If the VBA file contains functions you can then refer to them in
7168 calculations using "write_formula":
7169
7170 $worksheet->write_formula( 'A1', '=MyMortgageCalc(200000, 25)' );
7171
7172 Excel files that contain functions and macros should use an "xlsm"
7173 extension or else Excel will complain and possibly not open the file:
7174
7175 my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' );
7176
7177 It is also possible to assign a macro to a button that is inserted into
7178 a worksheet using the "insert_button()" method:
7179
7180 my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' );
7181 ...
7182 $workbook->add_vba_project( './vbaProject.bin' );
7183
7184 $worksheet->insert_button( 'C2', { macro => 'my_macro' } );
7185
7186 It may be necessary to specify a more explicit macro name prefixed by
7187 the workbook VBA name as follows:
7188
7189 $worksheet->insert_button( 'C2', { macro => 'ThisWorkbook.my_macro' } );
7190
7191 See the "macros.pl" from the examples directory for a working example.
7192
7193 Note: Button is the only VBA Control supported by Excel::Writer::XLSX.
7194 Due to the large effort in implementation (1+ man months) it is
7195 unlikely that any other form elements will be added in the future.
7196
7197 Setting the VBA codenames
7198 VBA macros generally refer to workbook and worksheet objects. If the
7199 VBA codenames aren't specified then Excel::Writer::XLSX will use the
7200 Excel defaults of "ThisWorkbook" and "Sheet1", "Sheet2" etc.
7201
7202 If the macro uses other codenames you can set them using the workbook
7203 and worksheet "set_vba_name()" methods as follows:
7204
7205 $workbook->set_vba_name( 'MyWorkbook' );
7206 $worksheet->set_vba_name( 'MySheet' );
7207
7208 You can find the names that are used in the VBA editor or by unzipping
7209 the "xlsm" file and grepping the files. The following shows how to do
7210 that using libxml's xmllint <http://xmlsoft.org/xmllint.html> to format
7211 the XML for clarity:
7212
7213 $ unzip myfile.xlsm -d myfile
7214 $ xmllint --format `find myfile -name "*.xml" | xargs` | grep "Pr.*codeName"
7215
7216 <workbookPr codeName="MyWorkbook" defaultThemeVersion="124226"/>
7217 <sheetPr codeName="MySheet"/>
7218
7219 Note: This step is particularly important for macros created with non-
7220 English versions of Excel.
7221
7222 What to do if it doesn't work
7223 This feature should be considered experimental and there is no
7224 guarantee that it will work in all cases. Some effort may be required
7225 and some knowledge of VBA will certainly help. If things don't work out
7226 here are some things to try:
7227
7228 • Start with a simple macro file, ensure that it works and then add
7229 complexity.
7230
7231 • Try to extract the macros from an Excel 2007 file. The method
7232 should work with macros from later versions (it was also tested
7233 with Excel 2010 macros). However there may be features in the macro
7234 files of more recent version of Excel that aren't backward
7235 compatible.
7236
7237 • Check the code names that macros use to refer to the workbook and
7238 worksheets (see the previous section above). In general VBA uses a
7239 code name of "ThisWorkbook" to refer to the current workbook and
7240 the sheet name (such as "Sheet1") to refer to the worksheets. These
7241 are the defaults used by Excel::Writer::XLSX. If the macro uses
7242 other names then you can specify these using the workbook and
7243 worksheet "set_vba_name()" methods:
7244
7245 $workbook>set_vba_name( 'MyWorkbook' );
7246 $worksheet->set_vba_name( 'MySheet' );
7247
7249 See Excel::Writer::XLSX::Examples for a full list of examples.
7250
7251 Example 1
7252 The following example shows some of the basic features of
7253 Excel::Writer::XLSX.
7254
7255 #!/usr/bin/perl -w
7256
7257 use strict;
7258 use Excel::Writer::XLSX;
7259
7260 # Create a new workbook called simple.xlsx and add a worksheet
7261 my $workbook = Excel::Writer::XLSX->new( 'simple.xlsx' );
7262 my $worksheet = $workbook->add_worksheet();
7263
7264 # The general syntax is write($row, $column, $token). Note that row and
7265 # column are zero indexed
7266
7267 # Write some text
7268 $worksheet->write( 0, 0, 'Hi Excel!' );
7269
7270
7271 # Write some numbers
7272 $worksheet->write( 2, 0, 3 );
7273 $worksheet->write( 3, 0, 3.00000 );
7274 $worksheet->write( 4, 0, 3.00001 );
7275 $worksheet->write( 5, 0, 3.14159 );
7276
7277
7278 # Write some formulas
7279 $worksheet->write( 7, 0, '=A3 + A6' );
7280 $worksheet->write( 8, 0, '=IF(A5>3,"Yes", "No")' );
7281
7282
7283 # Write a hyperlink
7284 my $hyperlink_format = $workbook->add_format(
7285 color => 'blue',
7286 underline => 1,
7287 );
7288
7289 $worksheet->write( 10, 0, 'http://www.perl.com/', $hyperlink_format );
7290
7291 $workbook->close();
7292
7293 Example 2
7294 The following is a general example which demonstrates some features of
7295 working with multiple worksheets.
7296
7297 #!/usr/bin/perl -w
7298
7299 use strict;
7300 use Excel::Writer::XLSX;
7301
7302 # Create a new Excel workbook
7303 my $workbook = Excel::Writer::XLSX->new( 'regions.xlsx' );
7304
7305 # Add some worksheets
7306 my $north = $workbook->add_worksheet( 'North' );
7307 my $south = $workbook->add_worksheet( 'South' );
7308 my $east = $workbook->add_worksheet( 'East' );
7309 my $west = $workbook->add_worksheet( 'West' );
7310
7311 # Add a Format
7312 my $format = $workbook->add_format();
7313 $format->set_bold();
7314 $format->set_color( 'blue' );
7315
7316 # Add a caption to each worksheet
7317 for my $worksheet ( $workbook->sheets() ) {
7318 $worksheet->write( 0, 0, 'Sales', $format );
7319 }
7320
7321 # Write some data
7322 $north->write( 0, 1, 200000 );
7323 $south->write( 0, 1, 100000 );
7324 $east->write( 0, 1, 150000 );
7325 $west->write( 0, 1, 100000 );
7326
7327 # Set the active worksheet
7328 $south->activate();
7329
7330 # Set the width of the first column
7331 $south->set_column( 0, 0, 20 );
7332
7333 # Set the active cell
7334 $south->set_selection( 0, 1 );
7335
7336 $workbook->close();
7337
7338 Example 3
7339 Example of how to add conditional formatting to an Excel::Writer::XLSX
7340 file. The example below highlights cells that have a value greater than
7341 or equal to 50 in red and cells below that value in green.
7342
7343 #!/usr/bin/perl
7344
7345 use strict;
7346 use warnings;
7347 use Excel::Writer::XLSX;
7348
7349 my $workbook = Excel::Writer::XLSX->new( 'conditional_format.xlsx' );
7350 my $worksheet = $workbook->add_worksheet();
7351
7352
7353 # This example below highlights cells that have a value greater than or
7354 # equal to 50 in red and cells below that value in green.
7355
7356 # Light red fill with dark red text.
7357 my $format1 = $workbook->add_format(
7358 bg_color => '#FFC7CE',
7359 color => '#9C0006',
7360
7361 );
7362
7363 # Green fill with dark green text.
7364 my $format2 = $workbook->add_format(
7365 bg_color => '#C6EFCE',
7366 color => '#006100',
7367
7368 );
7369
7370 # Some sample data to run the conditional formatting against.
7371 my $data = [
7372 [ 34, 72, 38, 30, 75, 48, 75, 66, 84, 86 ],
7373 [ 6, 24, 1, 84, 54, 62, 60, 3, 26, 59 ],
7374 [ 28, 79, 97, 13, 85, 93, 93, 22, 5, 14 ],
7375 [ 27, 71, 40, 17, 18, 79, 90, 93, 29, 47 ],
7376 [ 88, 25, 33, 23, 67, 1, 59, 79, 47, 36 ],
7377 [ 24, 100, 20, 88, 29, 33, 38, 54, 54, 88 ],
7378 [ 6, 57, 88, 28, 10, 26, 37, 7, 41, 48 ],
7379 [ 52, 78, 1, 96, 26, 45, 47, 33, 96, 36 ],
7380 [ 60, 54, 81, 66, 81, 90, 80, 93, 12, 55 ],
7381 [ 70, 5, 46, 14, 71, 19, 66, 36, 41, 21 ],
7382 ];
7383
7384 my $caption = 'Cells with values >= 50 are in light red. '
7385 . 'Values < 50 are in light green';
7386
7387 # Write the data.
7388 $worksheet->write( 'A1', $caption );
7389 $worksheet->write_col( 'B3', $data );
7390
7391 # Write a conditional format over a range.
7392 $worksheet->conditional_formatting( 'B3:K12',
7393 {
7394 type => 'cell',
7395 criteria => '>=',
7396 value => 50,
7397 format => $format1,
7398 }
7399 );
7400
7401 # Write another conditional format over the same range.
7402 $worksheet->conditional_formatting( 'B3:K12',
7403 {
7404 type => 'cell',
7405 criteria => '<',
7406 value => 50,
7407 format => $format2,
7408 }
7409 );
7410
7411 $workbook->close();
7412
7413 Example 4
7414 The following is a simple example of using functions.
7415
7416 #!/usr/bin/perl -w
7417
7418 use strict;
7419 use Excel::Writer::XLSX;
7420
7421 # Create a new workbook and add a worksheet
7422 my $workbook = Excel::Writer::XLSX->new( 'stats.xlsx' );
7423 my $worksheet = $workbook->add_worksheet( 'Test data' );
7424
7425 # Set the column width for columns 1
7426 $worksheet->set_column( 0, 0, 20 );
7427
7428
7429 # Create a format for the headings
7430 my $format = $workbook->add_format();
7431 $format->set_bold();
7432
7433
7434 # Write the sample data
7435 $worksheet->write( 0, 0, 'Sample', $format );
7436 $worksheet->write( 0, 1, 1 );
7437 $worksheet->write( 0, 2, 2 );
7438 $worksheet->write( 0, 3, 3 );
7439 $worksheet->write( 0, 4, 4 );
7440 $worksheet->write( 0, 5, 5 );
7441 $worksheet->write( 0, 6, 6 );
7442 $worksheet->write( 0, 7, 7 );
7443 $worksheet->write( 0, 8, 8 );
7444
7445 $worksheet->write( 1, 0, 'Length', $format );
7446 $worksheet->write( 1, 1, 25.4 );
7447 $worksheet->write( 1, 2, 25.4 );
7448 $worksheet->write( 1, 3, 24.8 );
7449 $worksheet->write( 1, 4, 25.0 );
7450 $worksheet->write( 1, 5, 25.3 );
7451 $worksheet->write( 1, 6, 24.9 );
7452 $worksheet->write( 1, 7, 25.2 );
7453 $worksheet->write( 1, 8, 24.8 );
7454
7455 # Write some statistical functions
7456 $worksheet->write( 4, 0, 'Count', $format );
7457 $worksheet->write( 4, 1, '=COUNT(B1:I1)' );
7458
7459 $worksheet->write( 5, 0, 'Sum', $format );
7460 $worksheet->write( 5, 1, '=SUM(B2:I2)' );
7461
7462 $worksheet->write( 6, 0, 'Average', $format );
7463 $worksheet->write( 6, 1, '=AVERAGE(B2:I2)' );
7464
7465 $worksheet->write( 7, 0, 'Min', $format );
7466 $worksheet->write( 7, 1, '=MIN(B2:I2)' );
7467
7468 $worksheet->write( 8, 0, 'Max', $format );
7469 $worksheet->write( 8, 1, '=MAX(B2:I2)' );
7470
7471 $worksheet->write( 9, 0, 'Standard Deviation', $format );
7472 $worksheet->write( 9, 1, '=STDEV(B2:I2)' );
7473
7474 $worksheet->write( 10, 0, 'Kurtosis', $format );
7475 $worksheet->write( 10, 1, '=KURT(B2:I2)' );
7476
7477 $workbook->close();
7478
7479 Example 5
7480 The following example converts a tab separated file called "tab.txt"
7481 into an Excel file called "tab.xlsx".
7482
7483 #!/usr/bin/perl -w
7484
7485 use strict;
7486 use Excel::Writer::XLSX;
7487
7488 open( TABFILE, 'tab.txt' ) or die "tab.txt: $!";
7489
7490 my $workbook = Excel::Writer::XLSX->new( 'tab.xlsx' );
7491 my $worksheet = $workbook->add_worksheet();
7492
7493 # Row and column are zero indexed
7494 my $row = 0;
7495
7496 while ( <TABFILE> ) {
7497 chomp;
7498
7499 # Split on single tab
7500 my @fields = split( '\t', $_ );
7501
7502 my $col = 0;
7503 for my $token ( @fields ) {
7504 $worksheet->write( $row, $col, $token );
7505 $col++;
7506 }
7507 $row++;
7508 }
7509
7510 $workbook->close();
7511
7512 NOTE: This is a simple conversion program for illustrative purposes
7513 only. For converting a CSV or Tab separated or any other type of
7514 delimited text file to Excel I recommend the more rigorous csv2xls
7515 program that is part of H.Merijn Brand's Text::CSV_XS module distro.
7516
7517 See the examples/csv2xls link here:
7518 <http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>.
7519
7520 Additional Examples
7521 The following is a description of the example files that are provided
7522 in the standard Excel::Writer::XLSX distribution. They demonstrate the
7523 different features and options of the module. See
7524 Excel::Writer::XLSX::Examples for more details.
7525
7526 Getting started
7527 ===============
7528 a_simple.pl A simple demo of some of the features.
7529 bug_report.pl A template for submitting bug reports.
7530 demo.pl A demo of some of the available features.
7531 formats.pl All the available formatting on several worksheets.
7532 regions.pl A simple example of multiple worksheets.
7533 stats.pl Basic formulas and functions.
7534
7535
7536 Intermediate
7537 ============
7538 autofilter.pl Examples of worksheet autofilters.
7539 array_formula.pl Examples of how to write array formulas.
7540 cgi.pl A simple CGI program.
7541 chart_area.pl A demo of area style charts.
7542 chart_bar.pl A demo of bar (vertical histogram) style charts.
7543 chart_column.pl A demo of column (histogram) style charts.
7544 chart_line.pl A demo of line style charts.
7545 chart_pie.pl A demo of pie style charts.
7546 chart_doughnut.pl A demo of doughnut style charts.
7547 chart_radar.pl A demo of radar style charts.
7548 chart_scatter.pl A demo of scatter style charts.
7549 chart_secondary_axis.pl A demo of a line chart with a secondary axis.
7550 chart_combined.pl A demo of a combined column and line chart.
7551 chart_pareto.pl A demo of a combined Pareto chart.
7552 chart_stock.pl A demo of stock style charts.
7553 chart_data_table.pl A demo of a chart with a data table on the axis.
7554 chart_data_tools.pl A demo of charts with data highlighting options.
7555 chart_data_labels.pl A demo of standard and custom chart data labels.
7556 chart_clustered.pl A demo of a chart with a clustered axis.
7557 chart_styles.pl A demo of the available chart styles.
7558 chart_gauge.pl A demo of a gauge style chart.
7559 colors.pl A demo of the colour palette and named colours.
7560 comments1.pl Add comments to worksheet cells.
7561 comments2.pl Add comments with advanced options.
7562 conditional_format.pl Add conditional formats to a range of cells.
7563 data_validate.pl An example of data validation and dropdown lists.
7564 date_time.pl Write dates and times with write_date_time().
7565 defined_name.pl Example of how to create defined names.
7566 diag_border.pl A simple example of diagonal cell borders.
7567 filehandle.pl Examples of working with filehandles.
7568 headers.pl Examples of worksheet headers and footers.
7569 hide_row_col.pl Example of hiding rows and columns.
7570 hide_sheet.pl Simple example of hiding a worksheet.
7571 hyperlink1.pl Shows how to create web hyperlinks.
7572 hyperlink2.pl Examples of internal and external hyperlinks.
7573 indent.pl An example of cell indentation.
7574 ignore_errors.pl An example of turning off worksheet cells errors/warnings.
7575 macros.pl An example of adding macros from an existing file.
7576 merge1.pl A simple example of cell merging.
7577 merge2.pl A simple example of cell merging with formatting.
7578 merge3.pl Add hyperlinks to merged cells.
7579 merge4.pl An advanced example of merging with formatting.
7580 merge5.pl An advanced example of merging with formatting.
7581 merge6.pl An example of merging with Unicode strings.
7582 mod_perl1.pl A simple mod_perl 1 program.
7583 mod_perl2.pl A simple mod_perl 2 program.
7584 outline.pl An example of outlines and grouping.
7585 outline_collapsed.pl An example of collapsed outlines.
7586 panes.pl An example of how to create panes.
7587 properties.pl Add document properties to a workbook.
7588 protection.pl Example of cell locking and formula hiding.
7589 rich_strings.pl Example of strings with multiple formats.
7590 right_to_left.pl Change default sheet direction to right to left.
7591 sales.pl An example of a simple sales spreadsheet.
7592 shape1.pl Insert shapes in worksheet.
7593 shape2.pl Insert shapes in worksheet. With properties.
7594 shape3.pl Insert shapes in worksheet. Scaled.
7595 shape4.pl Insert shapes in worksheet. With modification.
7596 shape5.pl Insert shapes in worksheet. With connections.
7597 shape6.pl Insert shapes in worksheet. With connections.
7598 shape7.pl Insert shapes in worksheet. One to many connections.
7599 shape8.pl Insert shapes in worksheet. One to many connections.
7600 shape_all.pl Demo of all the available shape and connector types.
7601 sparklines1.pl Simple sparklines demo.
7602 sparklines2.pl Sparklines demo showing formatting options.
7603 stats_ext.pl Same as stats.pl with external references.
7604 stocks.pl Demonstrates conditional formatting.
7605 background.pl Example of how to set the background image for a worksheet.
7606 tab_colors.pl Example of how to set worksheet tab colours.
7607 tables.pl Add Excel tables to a worksheet.
7608 write_handler1.pl Example of extending the write() method. Step 1.
7609 write_handler2.pl Example of extending the write() method. Step 2.
7610 write_handler3.pl Example of extending the write() method. Step 3.
7611 write_handler4.pl Example of extending the write() method. Step 4.
7612 write_to_scalar.pl Example of writing an Excel file to a Perl scalar.
7613
7614
7615 Unicode
7616 =======
7617 unicode_2022_jp.pl Japanese: ISO-2022-JP.
7618 unicode_8859_11.pl Thai: ISO-8859_11.
7619 unicode_8859_7.pl Greek: ISO-8859_7.
7620 unicode_big5.pl Chinese: BIG5.
7621 unicode_cp1251.pl Russian: CP1251.
7622 unicode_cp1256.pl Arabic: CP1256.
7623 unicode_cyrillic.pl Russian: Cyrillic.
7624 unicode_koi8r.pl Russian: KOI8-R.
7625 unicode_polish_utf8.pl Polish : UTF8.
7626 unicode_shift_jis.pl Japanese: Shift JIS.
7627
7629 The following limits are imposed by Excel 2007+:
7630
7631 Description Limit
7632 -------------------------------------- ------
7633 Maximum number of chars in a string 32,767
7634 Maximum number of columns 16,384
7635 Maximum number of rows 1,048,576
7636 Maximum chars in a sheet name 31
7637 Maximum chars in a header/footer 254
7638
7639 Maximum characters in hyperlink url (1) 2079
7640 Maximum number of unique hyperlinks (2) 65,530
7641
7642 (1) Versions of Excel prior to Excel 2015 limited hyperlink links and
7643 anchor/locations to 255 characters each. Versions after that support
7644 urls up to 2079 characters. Excel::Writer::XLSX versions >= 1.0.2
7645 support the new longer limit by default.
7646
7647 (2) Per worksheet. Excel allows a greater number of non-unique
7648 hyperlinks if they are contiguous and can be grouped into a single
7649 range. This isn't supported by Excel::Writer::XLSX.
7650
7652 <http://search.cpan.org/search?dist=Archive-Zip/>.
7653
7654 Perl 5.8.2.
7655
7657 "Spreadsheet::WriteExcel" was written to optimise speed and reduce
7658 memory usage. However, these design goals meant that it wasn't easy to
7659 implement features that many users requested such as writing formatting
7660 and data separately.
7661
7662 As a result "Excel::Writer::XLSX" takes a different design approach and
7663 holds a lot more data in memory so that it is functionally more
7664 flexible.
7665
7666 The effect of this is that Excel::Writer::XLSX is about 30% slower than
7667 Spreadsheet::WriteExcel and uses 5 times more memory.
7668
7669 In addition the extended row and column ranges in Excel 2007+ mean that
7670 it is possible to run out of memory creating large files. This was
7671 almost never an issue with Spreadsheet::WriteExcel.
7672
7673 This memory usage can be reduced almost completely by using the
7674 Workbook "set_optimization()" method:
7675
7676 $workbook->set_optimization();
7677
7678 The trade-off is that you won't be able to take advantage of features
7679 that manipulate cell data after it is written. One such feature is
7680 Tables.
7681
7683 The latest version of this module is always available at:
7684 <http://search.cpan.org/search?dist=Excel-Writer-XLSX/>.
7685
7687 The module can be installed using the standard Perl procedure:
7688
7689 perl Makefile.PL
7690 make
7691 make test
7692 make install # You may need to be sudo/root
7693
7695 Filename required by Excel::Writer::XLSX->new()
7696 A filename must be given in the constructor.
7697
7698 Can't open filename. It may be in use or protected.
7699 The file cannot be opened for writing. The directory that you are
7700 writing to may be protected or the file may be in use by another
7701 program.
7702
7703 Can't call method "XXX" on an undefined value at someprogram.pl.
7704 On Windows this is usually caused by the file that you are trying
7705 to create clashing with a version that is already open and locked
7706 by Excel.
7707
7708 The file you are trying to open 'file.xls' is in a different format
7709 than specified by the file extension.
7710 This warning occurs when you create an XLSX file but give it an xls
7711 extension.
7712
7714 Depending on your requirements, background and general sensibilities
7715 you may prefer one of the following methods of getting data into Excel:
7716
7717 • Spreadsheet::WriteExcel
7718
7719 This module is the precursor to Excel::Writer::XLSX and uses the
7720 same interface. It produces files in the Excel Biff xls format that
7721 was used in Excel versions 97-2003. These files can still be read
7722 by Excel 2007 but have some limitations in relation to the number
7723 of rows and columns that the format supports.
7724
7725 Spreadsheet::WriteExcel.
7726
7727 • Win32::OLE module and office automation
7728
7729 This requires a Windows platform and an installed copy of Excel.
7730 This is the most powerful and complete method for interfacing with
7731 Excel.
7732
7733 Win32::OLE
7734
7735 • CSV, comma separated variables or text
7736
7737 Excel will open and automatically convert files with a "csv"
7738 extension.
7739
7740 To create CSV files refer to the Text::CSV_XS module.
7741
7742 • DBI with DBD::ADO or DBD::ODBC
7743
7744 Excel files contain an internal index table that allows them to act
7745 like a database file. Using one of the standard Perl database
7746 modules you can connect to an Excel file as a database.
7747
7748 For other Perl-Excel modules try the following search:
7749 <http://search.cpan.org/search?mode=module&query=excel>.
7750
7752 To read data from Excel files try:
7753
7754 • Spreadsheet::ParseXLSX
7755
7756 A module for reading data from XLSX files. It also imports most, if
7757 not all, of the metadata to be found in Excel XLSX files. As its
7758 author describes it: "This module is an adaptor for
7759 Spreadsheet::ParseExcel that reads XLSX files. For documentation
7760 about the various data that you can retrieve from these classes,
7761 please see Spreadsheet::ParseExcel,
7762 Spreadsheet::ParseExcel::Workbook,
7763 Spreadsheet::ParseExcel::Worksheet, and
7764 Spreadsheet::ParseExcel::Cell."
7765
7766 • Spreadsheet::XLSX
7767
7768 A module for reading formatted or unformatted data from XLSX files.
7769
7770 Spreadsheet::XLSX
7771
7772 • SimpleXlsx
7773
7774 A lightweight module for reading data from XLSX files.
7775
7776 SimpleXlsx
7777
7778 • Spreadsheet::ParseExcel
7779
7780 This module can read data from an Excel XLS file but it doesn't
7781 support the XLSX format.
7782
7783 Spreadsheet::ParseExcel
7784
7785 • Win32::OLE module and office automation (reading)
7786
7787 See above.
7788
7789 • DBI with DBD::ADO or DBD::ODBC.
7790
7791 See above.
7792
7793 For other Perl-Excel modules try the following search:
7794 <http://search.cpan.org/search?mode=module&query=excel>.
7795
7797 • Memory usage is very high for large worksheets.
7798
7799 If you run out of memory creating large worksheets use the
7800 "set_optimization()" method. See "SPEED AND MEMORY USAGE" for more
7801 information.
7802
7803 • Perl packaging programs can't find chart modules.
7804
7805 When using Excel::Writer::XLSX charts with Perl packagers such as
7806 PAR or Cava you should explicitly include the chart that you are
7807 trying to create in your "use" statements. This isn't a bug as such
7808 but it might help someone from banging their head off a wall:
7809
7810 ...
7811 use Excel::Writer::XLSX;
7812 use Excel::Writer::XLSX::Chart::Column;
7813 ...
7814
7815 If you wish to submit a bug report run the "bug_report.pl" program in
7816 the "examples" directory of the distro.
7817
7818 The bug tracker is on Github:
7819 <https://github.com/jmcnamara/excel-writer-xlsx/issues>.
7820
7822 The Excel::Writer::XLSX source code in host on github:
7823 <http://github.com/jmcnamara/excel-writer-xlsx>.
7824
7826 If you'd care to donate to the Excel::Writer::XLSX project or sponsor a
7827 new feature, you can do so via PayPal: <http://tinyurl.com/7ayes>.
7828
7830 Spreadsheet::WriteExcel:
7831 <http://search.cpan.org/dist/Spreadsheet-WriteExcel>.
7832
7833 Spreadsheet::ParseExcel:
7834 <http://search.cpan.org/dist/Spreadsheet-ParseExcel>.
7835
7836 Spreadsheet::XLSX: <http://search.cpan.org/dist/Spreadsheet-XLSX>.
7837
7839 The following people contributed to the debugging, testing or
7840 enhancement of Excel::Writer::XLSX:
7841
7842 Rob Messer of IntelliSurvey gave me the initial prompt to port
7843 Spreadsheet::WriteExcel to the XLSX format. IntelliSurvey
7844 (<http://www.intellisurvey.com>) also sponsored large files
7845 optimisations and the charting feature.
7846
7847 Bariatric Advantage (<http://www.bariatricadvantage.com>) sponsored
7848 work on chart formatting.
7849
7850 Eric Johnson provided the ability to use secondary axes with charts.
7851 Thanks to Foxtons (<http://foxtons.co.uk>) for sponsoring this work.
7852
7853 BuildFax (<http://www.buildfax.com>) sponsored the Tables feature and
7854 the Chart point formatting feature.
7855
7857 Because this software is licensed free of charge, there is no warranty
7858 for the software, to the extent permitted by applicable law. Except
7859 when otherwise stated in writing the copyright holders and/or other
7860 parties provide the software "as is" without warranty of any kind,
7861 either expressed or implied, including, but not limited to, the implied
7862 warranties of merchantability and fitness for a particular purpose. The
7863 entire risk as to the quality and performance of the software is with
7864 you. Should the software prove defective, you assume the cost of all
7865 necessary servicing, repair, or correction.
7866
7867 In no event unless required by applicable law or agreed to in writing
7868 will any copyright holder, or any other party who may modify and/or
7869 redistribute the software as permitted by the above licence, be liable
7870 to you for damages, including any general, special, incidental, or
7871 consequential damages arising out of the use or inability to use the
7872 software (including but not limited to loss of data or data being
7873 rendered inaccurate or losses sustained by you or third parties or a
7874 failure of the software to operate with any other software), even if
7875 such holder or other party has been advised of the possibility of such
7876 damages.
7877
7879 The Perl Artistic Licence <http://dev.perl.org/licenses/artistic.html>.
7880
7882 John McNamara jmcnamara@cpan.org
7883
7885 Copyright MM-MMXXI, John McNamara.
7886
7887 All Rights Reserved. This module is free software. It may be used,
7888 redistributed and/or modified under the same terms as Perl itself.
7889
7890
7891
7892perl v5.36.0 2022-07-22 Excel::Writer::XLSX(3)