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