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