1Spreadsheet::WriteExcelU(s3e)r Contributed Perl DocumentaStpiroenadsheet::WriteExcel(3)
2
3
4
6 Spreadsheet::WriteExcel - Write to a cross-platform Excel binary file.
7
9 This document refers to version 2.17 of Spreadsheet::WriteExcel,
10 released May 21, 2006.
11
13 To write a string, a formatted string, a number and a formula to the
14 first worksheet in an Excel workbook called perl.xls:
15
16 use Spreadsheet::WriteExcel;
17
18 # Create a new Excel workbook
19 my $workbook = Spreadsheet::WriteExcel->new("perl.xls");
20
21 # Add a worksheet
22 $worksheet = $workbook->add_worksheet();
23
24 # Add and define a format
25 $format = $workbook->add_format(); # Add a format
26 $format->set_bold();
27 $format->set_color('red');
28 $format->set_align('center');
29
30 # Write a formatted and unformatted string, row and column notation.
31 $col = $row = 0;
32 $worksheet->write($row, $col, "Hi Excel!", $format);
33 $worksheet->write(1, $col, "Hi Excel!");
34
35 # Write a number and a formula using A1 notation
36 $worksheet->write('A3', 1.2345);
37 $worksheet->write('A4', '=SIN(PI()/4)');
38
40 The Spreadsheet::WriteExcel module can be used to create a cross-plat‐
41 form Excel binary file. Multiple worksheets can be added to a workbook
42 and formatting can be applied to cells. Text, numbers, formulas, hyper‐
43 links and images can be written to the cells.
44
45 The Excel file produced by this module is compatible with 97, 2000,
46 2002 and 2003.
47
48 The module will work on the majority of Windows, UNIX and Macintosh
49 platforms. Generated files are also compatible with the Linux/UNIX
50 spreadsheet applications Gnumeric and OpenOffice.org.
51
52 This module cannot be used to write to an existing Excel file.
53
55 Spreadsheet::WriteExcel tries to provide an interface to as many of
56 Excel's features as possible. As a result there is a lot of documenta‐
57 tion to accompany the interface and it can be difficult at first glance
58 to see what it important and what is not. So for those of you who pre‐
59 fer to assemble Ikea furniture first and then read the instructions,
60 here are three easy steps:
61
62 1. Create a new Excel workbook (i.e. file) using "new()".
63
64 2. Add a worksheet to the new workbook using "add_worksheet()".
65
66 3. Write to the worksheet using "write()".
67
68 Like this:
69
70 use Spreadsheet::WriteExcel; # Step 0
71
72 my $workbook = Spreadsheet::WriteExcel->new("perl.xls"); # Step 1
73 $worksheet = $workbook->add_worksheet(); # Step 2
74 $worksheet->write('A1', "Hi Excel!"); # Step 3
75
76 This will create an Excel file called "perl.xls" with a single work‐
77 sheet and the text "Hi Excel!" in the relevant cell. And that's it.
78 Okay, so there is actually a zeroth step as well, but "use module" goes
79 without saying. There are also more than 70 examples that come with the
80 distribution and which you can use to get you started. See EXAMPLES.
81
82 Those of you who read the instructions first and assemble the furniture
83 afterwards will know how to proceed. ;-)
84
86 The Spreadsheet::WriteExcel module provides an object oriented inter‐
87 face to a new Excel workbook. The following methods are available
88 through a new workbook.
89
90 new()
91 close()
92 set_tempdir()
93 add_worksheet()
94 add_chart_ext()
95 add_format()
96 set_custom_color()
97 sheets()
98 set_1904()
99 set_codepage()
100
101 If you are unfamiliar with object oriented interfaces or the way that
102 they are implemented in Perl have a look at "perlobj" and "perltoot" in
103 the main Perl documentation.
104
105 new()
106
107 A new Excel workbook is created using the "new()" constructor which
108 accepts either a filename or a filehandle as a parameter. The following
109 example creates a new Excel file based on a filename:
110
111 my $workbook = Spreadsheet::WriteExcel->new('filename.xls');
112 my $worksheet = $workbook->add_worksheet();
113 $worksheet->write(0, 0, "Hi Excel!");
114
115 Here are some other examples of using "new()" with filenames:
116
117 my $workbook1 = Spreadsheet::WriteExcel->new($filename);
118 my $workbook2 = Spreadsheet::WriteExcel->new("/tmp/filename.xls");
119 my $workbook3 = Spreadsheet::WriteExcel->new("c:\\tmp\\filename.xls");
120 my $workbook4 = Spreadsheet::WriteExcel->new('c:\tmp\filename.xls');
121
122 The last two examples demonstrates how to create a file on DOS or Win‐
123 dows where it is necessary to either escape the directory separator "\"
124 or to use single quotes to ensure that it isn't interpolated. For more
125 information see "perlfaq5: Why can't I use "C:\temp\foo" in DOS
126 paths?".
127
128 The "new()" constructor returns a Spreadsheet::WriteExcel object that
129 you can use to add worksheets and store data. It should be noted that
130 although "my" is not specifically required it defines the scope of the
131 new workbook variable and, in the majority of cases, ensures that the
132 workbook is closed properly without explicitly calling the "close()"
133 method.
134
135 If the file cannot be created, due to file permissions or some other
136 reason, "new" will return "undef". Therefore, it is good practice to
137 check the return value of "new" before proceeding. As usual the Perl
138 variable $! will be set if there is a file creation error. You will
139 also see one of the warning messages detailed in DIAGNOSTICS:
140
141 my $workbook = Spreadsheet::WriteExcel->new('protected.xls');
142 die "Problems creating new Excel file: $!" unless defined $workbook;
143
144 You can also pass a valid filehandle to the "new()" constructor. For
145 example in a CGI program you could do something like this:
146
147 binmode(STDOUT);
148 my $workbook = Spreadsheet::WriteExcel->new(\*STDOUT);
149
150 The requirement for "binmode()" is explained below.
151
152 For CGI programs you can also use the special Perl filename '-' which
153 will redirect the output to STDOUT:
154
155 my $workbook = Spreadsheet::WriteExcel->new('-');
156
157 See also, the "cgi.pl" program in the "examples" directory of the dis‐
158 tro.
159
160 However, this special case will not work in "mod_perl" programs where
161 you will have to do something like the following:
162
163 # mod_perl 1
164 ...
165 tie *XLS, 'Apache';
166 binmode(XLS);
167 my $workbook = Spreadsheet::WriteExcel->new(\*XLS);
168 ...
169
170 # mod_perl 2
171 ...
172 tie *XLS => $r; # Tie to the Apache::RequestRec object
173 binmode(*XLS);
174 my $workbook = Spreadsheet::WriteExcel->new(\*XLS);
175 ...
176
177 See also, the "mod_perl1.pl" and "mod_perl2.pl" programs in the "exam‐
178 ples" directory of the distro.
179
180 Filehandles can also be useful if you want to stream an Excel file over
181 a socket or if you want to store an Excel file in a scalar.
182
183 For example here is a way to write an Excel file to a scalar with "perl
184 5.8":
185
186 #!/usr/bin/perl -w
187
188 use strict;
189 use Spreadsheet::WriteExcel;
190
191 # Requires perl 5.8 or later
192 open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
193
194 my $workbook = Spreadsheet::WriteExcel->new($fh);
195 my $worksheet = $workbook->add_worksheet();
196
197 $worksheet->write(0, 0, "Hi Excel!");
198
199 $workbook->close();
200
201 # The Excel file in now in $str. Remember to binmode() the output
202 # filehandle before printing it.
203 binmode STDOUT;
204 print $str;
205
206 See also the "write_to_scalar.pl" and "filehandle.pl" programs in the
207 "examples" directory of the distro.
208
209 Note about the requirement for "binmode()": An Excel file is comprised
210 of binary data. Therefore, if you are using a filehandle you should
211 ensure that you "binmode()" it prior to passing it to "new()".You
212 should do this regardless of whether you are on a Windows platform or
213 not. This applies especially to users of perl 5.8 on systems where utf8
214 is likely to be in operation such as RedHat Linux 9. If your program,
215 either intentionally or not, writes UTF8 data to a filehandle that is
216 passed to "new()" it will corrupt the Excel file that is created.
217
218 You don't have to worry about "binmode()" if you are using filenames
219 instead of filehandles. Spreadsheet::WriteExcel performs the "bin‐
220 mode()" internally when it converts the filename to a filehandle. For
221 more information about "binmode()" see "perlfunc" and "perlopentut" in
222 the main Perl documentation.
223
224 close()
225
226 In general your Excel file will be closed automatically when your pro‐
227 gram ends or when the Workbook object goes out of scope, however the
228 "close()" method can be used to explicitly close an Excel file.
229
230 $workbook->close();
231
232 An explicit "close()" is required if the file must be closed prior to
233 performing some external action on it such as copying it, reading its
234 size or attaching it to an email.
235
236 In addition, "close()" may be required to prevent perl's garbage col‐
237 lector from disposing of the Workbook, Worksheet and Format objects in
238 the wrong order. Situations where this can occur are:
239
240 · If "my()" was not used to declare the scope of a workbook variable
241 created using "new()".
242
243 · If the "new()", "add_worksheet()" or "add_format()" methods are
244 called in subroutines.
245
246 The reason for this is that Spreadsheet::WriteExcel relies on Perl's
247 "DESTROY" mechanism to trigger destructor methods in a specific
248 sequence. This may not happen in cases where the Workbook, Worksheet
249 and Format variables are not lexically scoped or where they have dif‐
250 ferent lexical scopes.
251
252 In general, if you create a file with a size of 0 bytes or you fail to
253 create a file you need to call "close()".
254
255 The return value of "close()" is the same as that returned by perl when
256 it closes the file created by "new()". This allows you to handle error
257 conditions in the usual way:
258
259 $workbook->close() or die "Error closing file: $!";
260
261 set_tempdir()
262
263 For speed and efficiency "Spreadsheet::WriteExcel" stores worksheet
264 data in temporary files prior to assembling the final workbook.
265
266 If Spreadsheet::WriteExcel is unable to create these temporary files it
267 will store the required data in memory. This can be slow for large
268 files.
269
270 The problem occurs mainly with IIS on Windows although it could feasi‐
271 bly occur on Unix systems as well. The problem generally occurs because
272 the default temp file directory is defined as "C:/" or some other
273 directory that IIS doesn't provide write access to.
274
275 To check if this might be a problem on a particular system you can run
276 a simple test program with "-w" or "use warnings". This will generate a
277 warning if the module cannot create the required temporary files:
278
279 #!/usr/bin/perl -w
280
281 use Spreadsheet::WriteExcel;
282
283 my $workbook = Spreadsheet::WriteExcel->new("test.xls");
284 my $worksheet = $workbook->add_worksheet();
285
286 To avoid this problem the "set_tempdir()" method can be used to specify
287 a directory that is accessible for the creation of temporary files.
288
289 The "File::Temp" module is used to create the temporary files.
290 File::Temp uses "File::Spec" to determine an appropriate location for
291 these files such as "/tmp" or "c:\windows\temp". You can find out which
292 directory is used on your system as follows:
293
294 perl -MFile::Spec -le "print File::Spec->tmpdir"
295
296 Even if the default temporary file directory is accessible you may wish
297 to specify an alternative location for security or maintenance reasons:
298
299 $workbook->set_tempdir('/tmp/writeexcel');
300 $workbook->set_tempdir('c:\windows\temp\writeexcel');
301
302 The directory for the temporary file must exist, "set_tempdir()" will
303 not create a new directory.
304
305 One disadvantage of using the "set_tempdir()" method is that on some
306 Windows systems it will limit you to approximately 800 concurrent temp‐
307 files. This means that a single program running on one of these systems
308 will be limited to creating a total of 800 workbook and worksheet
309 objects. You can run multiple, non-concurrent programs to work around
310 this if necessary.
311
312 add_worksheet($sheetname, $encoding)
313
314 At least one worksheet should be added to a new workbook. A worksheet
315 is used to write data into cells:
316
317 $worksheet1 = $workbook->add_worksheet(); # Sheet1
318 $worksheet2 = $workbook->add_worksheet('Foglio2'); # Foglio2
319 $worksheet3 = $workbook->add_worksheet('Data'); # Data
320 $worksheet4 = $workbook->add_worksheet(); # Sheet4
321
322 If $sheetname is not specified the default Excel convention will be
323 followed, i.e. Sheet1, Sheet2, etc. The $encoding parameter is
324 optional, see below.
325
326 The worksheet name must be a valid Excel worksheet name, i.e. it cannot
327 contain any of the following characters, "[ ] : * ? / \" and it must be
328 less than 32 characters. In addition, you cannot use the same, case
329 insensitive, $sheetname for more than one worksheet.
330
331 On systems with "perl 5.8" and later the "add_worksheet()" method will
332 also handle strings in Perl's "utf8" format.
333
334 $worksheet = $workbook->add_worksheet("\x{263a}"); # Smiley
335
336 On earlier Perl systems your can specify UTF-16BE worksheet names using
337 an additional encoding parameter:
338
339 my $name = pack "n", 0x263a;
340 $worksheet = $workbook->add_worksheet($name, 1); # Smiley
341
342 add_chart_ext($chart_data, $chartname)
343
344 This method is use to include externally generated charts in a Spread‐
345 sheet::WriteExcel file.
346
347 my $chart = $workbook->add_chart_ext('chart01.bin', 'Chart1');
348
349 This feature is new and would be best described as experimental. Read
350 "charts.txt" in the charts directory of the distro for a full explana‐
351 tion.
352
353 add_format(%properties)
354
355 The "add_format()" method can be used to create new Format objects
356 which are used to apply formatting to a cell. You can either define the
357 properties at creation time via a hash of property values or later via
358 method calls.
359
360 $format1 = $workbook->add_format(%props); # Set properties at creation
361 $format2 = $workbook->add_format(); # Set properties later
362
363 See the "CELL FORMATTING" section for more details about Format proper‐
364 ties and how to set them.
365
366 set_custom_color($index, $red, $green, $blue)
367
368 The "set_custom_color()" method can be used to override one of the
369 built-in palette values with a more suitable colour.
370
371 The value for $index should be in the range 8..63, see "COLOURS IN
372 EXCEL".
373
374 The default named colours use the following indices:
375
376 8 => black
377 9 => white
378 10 => red
379 11 => lime
380 12 => blue
381 13 => yellow
382 14 => magenta
383 15 => cyan
384 16 => brown
385 17 => green
386 18 => navy
387 20 => purple
388 22 => silver
389 23 => gray
390 33 => pink
391 53 => orange
392
393 A new colour is set using its RGB (red green blue) components. The
394 $red, $green and $blue values must be in the range 0..255. You can
395 determine the required values in Excel using the "Tools->Options->Col‐
396 ors->Modify" dialog.
397
398 The "set_custom_color()" workbook method can also be used with a HTML
399 style "#rrggbb" hex value:
400
401 $workbook->set_custom_color(40, 255, 102, 0 ); # Orange
402 $workbook->set_custom_color(40, 0xFF, 0x66, 0x00); # Same thing
403 $workbook->set_custom_color(40, '#FF6600' ); # Same thing
404
405 my $font = $workbook->add_format(color => 40); # Use the modified colour
406
407 The return value from "set_custom_color()" is the index of the colour
408 that was changed:
409
410 my $ferrari = $workbook->set_custom_color(40, 216, 12, 12);
411
412 my $format = $workbook->add_format(
413 bg_color => $ferrari,
414 pattern => 1,
415 border => 1
416 );
417
418 sheets(0, 1, ...)
419
420 The "sheets()" method returns a list, or a sliced list, of the work‐
421 sheets in a workbook.
422
423 If no arguments are passed the method returns a list of all the work‐
424 sheets in the workbook. This is useful if you want to repeat an opera‐
425 tion on each worksheet:
426
427 foreach $worksheet ($workbook->sheets()) {
428 print $worksheet->get_name();
429 }
430
431 You can also specify a slice list to return one or more worksheet
432 objects:
433
434 $worksheet = $workbook->sheets(0);
435 $worksheet->write('A1', "Hello");
436
437 Or since return value from "sheets()" is a reference to a worksheet
438 object you can write the above example as:
439
440 $workbook->sheets(0)->write('A1', "Hello");
441
442 The following example returns the first and last worksheet in a work‐
443 book:
444
445 foreach $worksheet ($workbook->sheets(0, -1)) {
446 # Do something
447 }
448
449 Array slices are explained in the perldata manpage.
450
451 set_1904()
452
453 Excel stores dates as real numbers where the integer part stores the
454 number of days since the epoch and the fractional part stores the per‐
455 centage of the day. The epoch can be either 1900 or 1904. Excel for
456 Windows uses 1900 and Excel for Macintosh uses 1904. However, Excel on
457 either platform will convert automatically between one system and the
458 other.
459
460 Spreadsheet::WriteExcel stores dates in the 1900 format by default. If
461 you wish to change this you can call the "set_1904()" workbook method.
462 You can query the current value by calling the "get_1904()" workbook
463 method. This returns 0 for 1900 and 1 for 1904.
464
465 See also "DATES IN EXCEL" for more information about working with
466 Excel's date system.
467
468 In general you probably won't need to use "set_1904()".
469
470 set_codepage($codepage)
471
472 The default code page or character set used by Spreadsheet::WriteExcel
473 is ANSI. This is also the default used by Excel for Windows. Occasion‐
474 ally however it may be necessary to change the code page via the
475 "set_codepage()" method.
476
477 Changing the code page may be required if your are using Spread‐
478 sheet::WriteExcel on the Macintosh and you are using characters outside
479 the ASCII 128 character set:
480
481 $workbook->set_codepage(1); # ANSI, MS Windows
482 $workbook->set_codepage(2); # Apple Macintosh
483
484 The "set_codepage()" method is rarely required.
485
487 A new worksheet is created by calling the "add_worksheet()" method from
488 a workbook object:
489
490 $worksheet1 = $workbook->add_worksheet();
491 $worksheet2 = $workbook->add_worksheet();
492
493 The following methods are available through a new worksheet:
494
495 write()
496 write_number()
497 write_string()
498 write_unicode()
499 write_unicode_le()
500 keep_leading_zeros()
501 write_blank()
502 write_row()
503 write_col()
504 write_date_time()
505 write_url()
506 write_url_range()
507 write_formula()
508 store_formula()
509 repeat_formula()
510 write_comment()
511 show_comments()
512 add_write_handler()
513 insert_bitmap()
514 get_name()
515 activate()
516 select()
517 hide()
518 set_first_sheet()
519 protect()
520 set_selection()
521 set_row()
522 set_column()
523 outline_settings()
524 freeze_panes()
525 thaw_panes()
526 merge_range()
527 set_zoom()
528 right_to_left()
529 hide_zero()
530 set_tab_color()
531
532 Cell notation
533
534 Spreadsheet::WriteExcel supports two forms of notation to designate the
535 position of cells: Row-column notation and A1 notation.
536
537 Row-column notation uses a zero based index for both row and column
538 while A1 notation uses the standard Excel alphanumeric sequence of col‐
539 umn letter and 1-based row. For example:
540
541 (0, 0) # The top left cell in row-column notation.
542 ('A1') # The top left cell in A1 notation.
543
544 (1999, 29) # Row-column notation.
545 ('AD2000') # The same cell in A1 notation.
546
547 Row-column notation is useful if you are referring to cells programmat‐
548 ically:
549
550 for my $i (0 .. 9) {
551 $worksheet->write($i, 0, 'Hello'); # Cells A1 to A10
552 }
553
554 A1 notation is useful for setting up a worksheet manually and for work‐
555 ing with formulas:
556
557 $worksheet->write('H1', 200);
558 $worksheet->write('H2', '=H1+1');
559
560 In formulas and applicable methods you can also use the "A:A" column
561 notation:
562
563 $worksheet->write('A1', '=SUM(B:B)');
564
565 The "Spreadsheet::WriteExcel::Utility" module that is included in the
566 distro contains helper functions for dealing with A1 notation, for
567 example:
568
569 use Spreadsheet::WriteExcel::Utility;
570
571 ($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
572 $str = xl_rowcol_to_cell(1, 2); # C2
573
574 For simplicity, the parameter lists for the worksheet method calls in
575 the following sections are given in terms of row-column notation. In
576 all cases it is also possible to use A1 notation.
577
578 Note: in Excel it is also possible to use a R1C1 notation. This is not
579 supported by Spreadsheet::WriteExcel.
580
581 write($row, $column, $token, $format)
582
583 Excel makes a distinction between data types such as strings, numbers,
584 blanks, formulas and hyperlinks. To simplify the process of writing
585 data the "write()" method acts as a general alias for several more spe‐
586 cific methods:
587
588 write_string()
589 write_number()
590 write_blank()
591 write_formula()
592 write_url()
593 write_row()
594 write_col()
595
596 The general rule is that if the data looks like a something then a
597 something is written. Here are some examples in both row-column and A1
598 notation:
599
600 # Same as:
601 $worksheet->write(0, 0, "Hello" ); # write_string()
602 $worksheet->write(1, 0, 'One' ); # write_string()
603 $worksheet->write(2, 0, 2 ); # write_number()
604 $worksheet->write(3, 0, 3.00001 ); # write_number()
605 $worksheet->write(4, 0, "" ); # write_blank()
606 $worksheet->write(5, 0, '' ); # write_blank()
607 $worksheet->write(6, 0, undef ); # write_blank()
608 $worksheet->write(7, 0 ); # write_blank()
609 $worksheet->write(8, 0, 'http://www.perl.com/'); # write_url()
610 $worksheet->write('A9', 'ftp://ftp.cpan.org/' ); # write_url()
611 $worksheet->write('A10', 'internal:Sheet1!A1' ); # write_url()
612 $worksheet->write('A11', 'external:c:\foo.xls' ); # write_url()
613 $worksheet->write('A12', '=A3 + 3*A4' ); # write_formula()
614 $worksheet->write('A13', '=SIN(PI()/4)' ); # write_formula()
615 $worksheet->write('A14', \@array ); # write_row()
616 $worksheet->write('A15', [\@array] ); # write_col()
617
618 # And if the keep_leading_zeros property is set:
619 $worksheet->write('A16, 2 ); # write_number()
620 $worksheet->write('A17, 02 ); # write_string()
621 $worksheet->write('A18, 00002 ); # write_string()
622
623 The "looks like" rule is defined by regular expressions:
624
625 "write_number()" if $token is a number based on the following regex:
626 "$token =~ /^([+-]?)(?=\d⎪\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/".
627
628 "write_string()" if "keep_leading_zeros()" is set and $token is an
629 integer with leading zeros based on the following regex: "$token =~
630 /^0\d+$/".
631
632 "write_blank()" if $token is undef or a blank string: "undef", "" or
633 ''.
634
635 "write_url()" if $token is a http, https, ftp or mailto URL based on
636 the following regexes: "$token =~ m⎪^[fh]tt?ps?://⎪" or "$token =~
637 m⎪^mailto:⎪".
638
639 "write_url()" if $token is an internal or external sheet reference
640 based on the following regex: "$token =~ m[^(in⎪ex)ternal:]".
641
642 "write_formula()" if the first character of $token is "=".
643
644 "write_row()" if $token is an array ref.
645
646 "write_col()" if $token is an array ref of array refs.
647
648 "write_string()" if none of the previous conditions apply.
649
650 The $format parameter is optional. It should be a valid Format object,
651 see "CELL FORMATTING":
652
653 my $format = $workbook->add_format();
654 $format->set_bold();
655 $format->set_color('red');
656 $format->set_align('center');
657
658 $worksheet->write(4, 0, "Hello", $format ); # Formatted string
659
660 The write() method will ignore empty strings or "undef" tokens unless a
661 format is also supplied. As such you needn't worry about special han‐
662 dling for empty or "undef" values in your data. See also the
663 "write_blank()" method.
664
665 One problem with the "write()" method is that occasionally data looks
666 like a number but you don't want it treated as a number. For example,
667 zip codes or ID numbers often start with a leading zero. If you write
668 this data as a number then the leading zero(s) will be stripped. You
669 can change this default behaviour by using the "keep_leading_zeros()"
670 method. While this property is in place any integers with leading zeros
671 will be treated as strings and the zeros will be preserved. See the
672 "keep_leading_zeros()" section for a full discussion of this issue.
673
674 You can also add your own data handlers to the "write()" method using
675 "add_write_handler()".
676
677 On systems with "perl 5.8" and later the "write()" method will also
678 handle Unicode strings in Perl's "utf8" format.
679
680 The "write" methods return:
681
682 0 for success.
683 -1 for insufficient number of arguments.
684 -2 for row or column out of bounds.
685 -3 for string too long.
686
687 write_number($row, $column, $number, $format)
688
689 Write an integer or a float to the cell specified by $row and $column:
690
691 $worksheet->write_number(0, 0, 123456);
692 $worksheet->write_number('A2', 2.3451);
693
694 See the note about "Cell notation". The $format parameter is optional.
695
696 In general it is sufficient to use the "write()" method.
697
698 write_string($row, $column, $string, $format)
699
700 Write a string to the cell specified by $row and $column:
701
702 $worksheet->write_string(0, 0, "Your text here" );
703 $worksheet->write_string('A2', "or here" );
704
705 The maximum string size is 32767 characters. However the maximum string
706 segment that Excel can display in a cell is 1000. All 32767 characters
707 can be displayed in the formula bar.
708
709 The $format parameter is optional.
710
711 On systems with "perl 5.8" and later the "write()" method will also
712 handle strings in Perl's "utf8" format. With older perls you can also
713 write Unicode in "UTF16" format via the "write_unicode()" method. See
714 also the "unicode_*.pl" programs in the examples directory of the dis‐
715 tro.
716
717 In general it is sufficient to use the "write()" method. However, you
718 may sometimes wish to use the "write_string()" method to write data
719 that looks like a number but that you don't want treated as a number.
720 For example, zip codes or phone numbers:
721
722 # Write as a plain string
723 $worksheet->write_string('A1', '01209');
724
725 However, if the user edits this string Excel may convert it back to a
726 number. To get around this you can use the Excel text format "@":
727
728 # Format as a string. Doesn't change to a number when edited
729 my $format1 = $workbook->add_format(num_format => '@');
730 $worksheet->write_string('A2', '01209', $format1);
731
732 See also the note about "Cell notation".
733
734 write_unicode($row, $column, $string, $format)
735
736 This method is used to write Unicode strings to a cell in Excel. It is
737 functionally the same as the "write_string()" method except that the
738 string should be in UTF-16 Unicode format.
739
740 Note: on systems with "perl 5.8" and later the "write()" and
741 "write_string()"methods will also handle strings in Perl's "utf8" for‐
742 mat. With older perls you must use the "write_unicode()" method.
743
744 The Unicode format required by Excel is UTF-16. Additionally "Spread‐
745 sheet::WriteExcel" requires that the 16-bit characters are in big-
746 endian order. This is generally referred to as UTF-16BE. To write
747 UTF-16 strings in little-endian format use the "write_unicode_le()"
748 method.
749
750 The following is a simple example showing how to write some Unicode
751 strings:
752
753 #!/usr/bin/perl -w
754
755 use strict;
756 use Spreadsheet::WriteExcel;
757 use Unicode::Map();
758
759 my $workbook = Spreadsheet::WriteExcel->new('unicode.xls');
760 my $worksheet = $workbook->add_worksheet();
761
762 # Increase the column width for clarity
763 $worksheet->set_column('A:A', 25);
764
765 # Write a Unicode character
766 #
767 my $smiley = pack "n", 0x263a;
768
769 # Increase the font size for legibility.
770 my $big_font = $workbook->add_format(size => 72);
771
772 $worksheet->write_unicode('A3', $smiley, $big_font);
773
774 # Write a phrase in Cyrillic using a hex-encoded string
775 #
776 my $uni_str = pack "H*", "042d0442043e0020044404400430043704300020043d" .
777 "043000200440044304410441043a043e043c0021";
778
779 $worksheet->write_unicode('A5', $uni_str);
780
781 # Map a string to UTF-16BE using an external module.
782 #
783 my $map = Unicode::Map->new("ISO-8859-1");
784 my $utf16 = $map->to_unicode("Hello world!");
785
786 $worksheet->write_unicode('A7', $utf16);
787
788 The following is an example of creating an Excel file with some Japa‐
789 nese text. You will need to have a Unicode font installed, such as
790 "Arial Unicode MS", to view the results:
791
792 #!/usr/bin/perl -w
793
794 use strict;
795 use Spreadsheet::WriteExcel;
796
797 my $workbook = Spreadsheet::WriteExcel->new('unicode.xls');
798 my $worksheet = $workbook->add_worksheet();
799
800 # It is only required to specify a Unicode font via add_format() if
801 # you are using Excel 97. For Excel 2000+ the text will display
802 # with the default font (if you have Unicode fonts installed).
803 #
804 my $uni_font = $workbook->add_format(font => 'Arial Unicode MS');
805
806 my $kanji = pack 'n*', 0x65e5, 0x672c;
807 my $katakana = pack 'n*', 0xff86, 0xff8e, 0xff9d;
808 my $hiragana = pack 'n*', 0x306b, 0x307b, 0x3093;
809
810 $worksheet->write_unicode('A1', $kanji, $uni_font);
811 $worksheet->write_unicode('A2', $katakana, $uni_font);
812 $worksheet->write_unicode('A3', $hiragana, $uni_font);
813
814 $worksheet->write('B1', 'Kanji');
815 $worksheet->write('B2', 'Katakana');
816 $worksheet->write('B3', 'Hiragana');
817
818 Note: You can convert ascii encodings to the required UTF-16BE format
819 using one of the many Unicode modules on CPAN. For example "Uni‐
820 code::Map" and "Unicode::String":
821 http://search.cpan.org/author/MSCHWARTZ/Unicode-Map/Map.pm and
822 http://search.cpan.org/author/GAAS/Unicode-String/String.pm
823
824 For a full list of the Perl Unicode modules see:
825 http://search.cpan.org/search?query=unicode&mode=all
826
827 See also the "unicode_*.pl" programs in the examples directory of the
828 distro.
829
830 write_unicode_le($row, $column, $string, $format)
831
832 This method is the same as "write_unicode()" except that the string
833 should be 16-bit characters in little-endian format. This is generally
834 referred to as UTF-16LE.
835
836 UTF-16 data can be changed from little-endian to big-endian format (and
837 vice-versa) as follows:
838
839 $utf16 = pack "n*", unpack "v*", $utf16;
840
841 Note, it is slightly faster to write little-endian data via write_uni‐
842 code_le() than it is to write big-endian data via write_unicode().
843
844 keep_leading_zeros()
845
846 This method changes the default handling of integers with leading zeros
847 when using the "write()" method.
848
849 The "write()" method uses regular expressions to determine what type of
850 data to write to an Excel worksheet. If the data looks like a number it
851 writes a number using "write_number()". One problem with this approach
852 is that occasionally data looks like a number but you don't want it
853 treated as a number.
854
855 Zip codes and ID numbers, for example, often start with a leading zero.
856 If you write this data as a number then the leading zero(s) will be
857 stripped. This is the also the default behaviour when you enter data
858 manually in Excel.
859
860 To get around this you can use one of three options. Write a formatted
861 number, write the number as a string or use the "keep_leading_zeros()"
862 method to change the default behaviour of "write()":
863
864 # Implicitly write a number, the leading zero is removed: 1209
865 $worksheet->write('A1', '01209');
866
867 # Write a zero padded number using a format: 01209
868 my $format1 = $workbook->add_format(num_format => '00000');
869 $worksheet->write('A2', '01209', $format1);
870
871 # Write explicitly as a string: 01209
872 $worksheet->write_string('A3', '01209');
873
874 # Write implicitly as a string: 01209
875 $worksheet->keep_leading_zeros();
876 $worksheet->write('A4', '01209');
877
878 The above code would generate a worksheet that looked like the follow‐
879 ing:
880
881 -----------------------------------------------------------
882 ⎪ ⎪ A ⎪ B ⎪ C ⎪ D ⎪ ...
883 -----------------------------------------------------------
884 ⎪ 1 ⎪ 1209 ⎪ ⎪ ⎪ ⎪ ...
885 ⎪ 2 ⎪ 01209 ⎪ ⎪ ⎪ ⎪ ...
886 ⎪ 3 ⎪ 01209 ⎪ ⎪ ⎪ ⎪ ...
887 ⎪ 4 ⎪ 01209 ⎪ ⎪ ⎪ ⎪ ...
888
889 The examples are on different sides of the cells due to the fact that
890 Excel displays strings with a left justification and numbers with a
891 right justification by default. You can change this by using a format
892 to justify the data, see "CELL FORMATTING".
893
894 It should be noted that if the user edits the data in examples "A3" and
895 "A4" the strings will revert back to numbers. Again this is Excel's
896 default behaviour. To avoid this you can use the text format "@":
897
898 # Format as a string (01209)
899 my $format2 = $workbook->add_format(num_format => '@');
900 $worksheet->write_string('A5', '01209', $format2);
901
902 The "keep_leading_zeros()" property is off by default. The "keep_lead‐
903 ing_zeros()" method takes 0 or 1 as an argument. It defaults to 1 if an
904 argument isn't specified:
905
906 $worksheet->keep_leading_zeros(); # Set on
907 $worksheet->keep_leading_zeros(1); # Set on
908 $worksheet->keep_leading_zeros(0); # Set off
909
910 See also the "add_write_handler()" method.
911
912 write_blank($row, $column, $format)
913
914 Write a blank cell specified by $row and $column:
915
916 $worksheet->write_blank(0, 0, $format);
917
918 This method is used to add formatting to a cell which doesn't contain a
919 string or number value.
920
921 Excel differentiates between an "Empty" cell and a "Blank" cell. An
922 "Empty" cell is a cell which doesn't contain data whilst a "Blank" cell
923 is a cell which doesn't contain data but does contain formatting. Excel
924 stores "Blank" cells but ignores "Empty" cells.
925
926 As such, if you write an empty cell without formatting it is ignored:
927
928 $worksheet->write('A1', undef, $format); # write_blank()
929 $worksheet->write('A2', undef ); # Ignored
930
931 This seemingly uninteresting fact means that you can write arrays of
932 data without special treatment for undef or empty string values.
933
934 See the note about "Cell notation".
935
936 write_row($row, $column, $array_ref, $format)
937
938 The "write_row()" method can be used to write a 1D or 2D array of data
939 in one go. This is useful for converting the results of a database
940 query into an Excel worksheet. You must pass a reference to the array
941 of data rather than the array itself. The "write()" method is then
942 called for each element of the data. For example:
943
944 @array = ('awk', 'gawk', 'mawk');
945 $array_ref = \@array;
946
947 $worksheet->write_row(0, 0, $array_ref);
948
949 # The above example is equivalent to:
950 $worksheet->write(0, 0, $array[0]);
951 $worksheet->write(0, 1, $array[1]);
952 $worksheet->write(0, 2, $array[2]);
953
954 Note: For convenience the "write()" method behaves in the same way as
955 "write_row()" if it is passed an array reference. Therefore the follow‐
956 ing two method calls are equivalent:
957
958 $worksheet->write_row('A1', $array_ref); # Write a row of data
959 $worksheet->write( 'A1', $array_ref); # Same thing
960
961 As with all of the write methods the $format parameter is optional. If
962 a format is specified it is applied to all the elements of the data
963 array.
964
965 Array references within the data will be treated as columns. This
966 allows you to write 2D arrays of data in one go. For example:
967
968 @eec = (
969 ['maggie', 'milly', 'molly', 'may' ],
970 [13, 14, 15, 16 ],
971 ['shell', 'star', 'crab', 'stone']
972 );
973
974 $worksheet->write_row('A1', \@eec);
975
976 Would produce a worksheet as follows:
977
978 -----------------------------------------------------------
979 ⎪ ⎪ A ⎪ B ⎪ C ⎪ D ⎪ E ⎪ ...
980 -----------------------------------------------------------
981 ⎪ 1 ⎪ maggie ⎪ 13 ⎪ shell ⎪ ... ⎪ ... ⎪ ...
982 ⎪ 2 ⎪ milly ⎪ 14 ⎪ star ⎪ ... ⎪ ... ⎪ ...
983 ⎪ 3 ⎪ molly ⎪ 15 ⎪ crab ⎪ ... ⎪ ... ⎪ ...
984 ⎪ 4 ⎪ may ⎪ 16 ⎪ stone ⎪ ... ⎪ ... ⎪ ...
985 ⎪ 5 ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
986 ⎪ 6 ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
987
988 To write the data in a row-column order refer to the "write_col()"
989 method below.
990
991 Any "undef" values in the data will be ignored unless a format is
992 applied to the data, in which case a formatted blank cell will be writ‐
993 ten. In either case the appropriate row or column value will still be
994 incremented.
995
996 To find out more about array references refer to "perlref" and "perl‐
997 reftut" in the main Perl documentation. To find out more about 2D
998 arrays or "lists of lists" refer to "perllol".
999
1000 The "write_row()" method returns the first error encountered when writ‐
1001 ing the elements of the data or zero if no errors were encountered. See
1002 the return values described for the "write()" method above.
1003
1004 See also the "write_arrays.pl" program in the "examples" directory of
1005 the distro.
1006
1007 The "write_row()" method allows the following idiomatic conversion of a
1008 text file to an Excel file:
1009
1010 #!/usr/bin/perl -w
1011
1012 use strict;
1013 use Spreadsheet::WriteExcel;
1014
1015 my $workbook = Spreadsheet::WriteExcel->new('file.xls');
1016 my $worksheet = $workbook->add_worksheet();
1017
1018 open INPUT, "file.txt" or die "Couldn't open file: $!";
1019
1020 $worksheet->write($.-1, 0, [split]) while <INPUT>;
1021
1022 write_col($row, $column, $array_ref, $format)
1023
1024 The "write_col()" method can be used to write a 1D or 2D array of data
1025 in one go. This is useful for converting the results of a database
1026 query into an Excel worksheet. You must pass a reference to the array
1027 of data rather than the array itself. The "write()" method is then
1028 called for each element of the data. For example:
1029
1030 @array = ('awk', 'gawk', 'mawk');
1031 $array_ref = \@array;
1032
1033 $worksheet->write_col(0, 0, $array_ref);
1034
1035 # The above example is equivalent to:
1036 $worksheet->write(0, 0, $array[0]);
1037 $worksheet->write(1, 0, $array[1]);
1038 $worksheet->write(2, 0, $array[2]);
1039
1040 As with all of the write methods the $format parameter is optional. If
1041 a format is specified it is applied to all the elements of the data
1042 array.
1043
1044 Array references within the data will be treated as rows. This allows
1045 you to write 2D arrays of data in one go. For example:
1046
1047 @eec = (
1048 ['maggie', 'milly', 'molly', 'may' ],
1049 [13, 14, 15, 16 ],
1050 ['shell', 'star', 'crab', 'stone']
1051 );
1052
1053 $worksheet->write_col('A1', \@eec);
1054
1055 Would produce a worksheet as follows:
1056
1057 -----------------------------------------------------------
1058 ⎪ ⎪ A ⎪ B ⎪ C ⎪ D ⎪ E ⎪ ...
1059 -----------------------------------------------------------
1060 ⎪ 1 ⎪ maggie ⎪ milly ⎪ molly ⎪ may ⎪ ... ⎪ ...
1061 ⎪ 2 ⎪ 13 ⎪ 14 ⎪ 15 ⎪ 16 ⎪ ... ⎪ ...
1062 ⎪ 3 ⎪ shell ⎪ star ⎪ crab ⎪ stone ⎪ ... ⎪ ...
1063 ⎪ 4 ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
1064 ⎪ 5 ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
1065 ⎪ 6 ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
1066
1067 To write the data in a column-row order refer to the "write_row()"
1068 method above.
1069
1070 Any "undef" values in the data will be ignored unless a format is
1071 applied to the data, in which case a formatted blank cell will be writ‐
1072 ten. In either case the appropriate row or column value will still be
1073 incremented.
1074
1075 As noted above the "write()" method can be used as a synonym for
1076 "write_row()" and "write_row()" handles nested array refs as columns.
1077 Therefore, the following two method calls are equivalent although the
1078 more explicit call to "write_col()" would be preferable for maintain‐
1079 ability:
1080
1081 $worksheet->write_col('A1', $array_ref ); # Write a column of data
1082 $worksheet->write( 'A1', [ $array_ref ]); # Same thing
1083
1084 To find out more about array references refer to "perlref" and "perl‐
1085 reftut" in the main Perl documentation. To find out more about 2D
1086 arrays or "lists of lists" refer to "perllol".
1087
1088 The "write_col()" method returns the first error encountered when writ‐
1089 ing the elements of the data or zero if no errors were encountered. See
1090 the return values described for the "write()" method above.
1091
1092 See also the "write_arrays.pl" program in the "examples" directory of
1093 the distro.
1094
1095 write_date_time($row, $col, $date_string, $format)
1096
1097 The "write_date_time()" method can be used to write a date or time to
1098 the cell specified by $row and $column:
1099
1100 $worksheet->write_date_time('A1', '2004-05-13T23:20', $date_format);
1101
1102 The $date_string should be in the following format:
1103
1104 yyyy-mm-ddThh:mm:ss.sss
1105
1106 This conforms to am ISO8601 date but it should be noted that the full
1107 range of ISO8601 formats are not supported.
1108
1109 The following variations on the $date_string parameter are permitted:
1110
1111 yyyy-mm-ddThh:mm:ss.sss # Standard format
1112 yyyy-mm-ddT # No time
1113 Thh:mm:ss.sss # No date
1114 yyyy-mm-ddThh:mm:ss.sssZ # Additional Z (but not time zones)
1115 yyyy-mm-ddThh:mm:ss # No fractional seconds
1116 yyyy-mm-ddThh:mm # No seconds
1117
1118 Note that the "T" is required in all cases.
1119
1120 A date should always have a $format, otherwise it will appear as a num‐
1121 ber, see "DATES IN EXCEL" and "CELL FORMATTING". Here is a typical
1122 example:
1123
1124 my $date_format = $workbook->add_format(num_format => 'mm/dd/yy');
1125 $worksheet->write_date_time('A1', '2004-05-13T23:20', $date_format);
1126
1127 Valid dates should be in the range 1900-01-01 to 9999-12-31, for the
1128 1900 epoch and 1904-01-01 to 9999-12-31, for the 1904 epoch. As with
1129 Excel, dates outside these ranges will be written as a string.
1130
1131 See also the date_time.pl program in the "examples" directory of the
1132 distro.
1133
1134 write_url($row, $col, $url, $label, $format)
1135
1136 Write a hyperlink to a URL in the cell specified by $row and $column.
1137 The hyperlink is comprised of two elements: the visible label and the
1138 invisible link. The visible label is the same as the link unless an
1139 alternative label is specified. The parameters $label and the $format
1140 are optional and their position is interchangeable.
1141
1142 The label is written using the "write()" method. Therefore it is possi‐
1143 ble to write strings, numbers or formulas as labels.
1144
1145 There are four web style URI's supported: "http://", "https://",
1146 "ftp://" and "mailto:":
1147
1148 $worksheet->write_url(0, 0, 'ftp://www.perl.org/' );
1149 $worksheet->write_url(1, 0, 'http://www.perl.com/', 'Perl home' );
1150 $worksheet->write_url('A3', 'http://www.perl.com/', $format );
1151 $worksheet->write_url('A4', 'http://www.perl.com/', 'Perl', $format);
1152 $worksheet->write_url('A5', 'mailto:jmcnamara@cpan.org' );
1153
1154 There are two local URIs supported: "internal:" and "external:". These
1155 are used for hyperlinks to internal worksheet references or external
1156 workbook and worksheet references:
1157
1158 $worksheet->write_url('A6', 'internal:Sheet2!A1' );
1159 $worksheet->write_url('A7', 'internal:Sheet2!A1', $format );
1160 $worksheet->write_url('A8', 'internal:Sheet2!A1:B2' );
1161 $worksheet->write_url('A9', q{internal:'Sales Data'!A1} );
1162 $worksheet->write_url('A10', 'external:c:\temp\foo.xls' );
1163 $worksheet->write_url('A11', 'external:c:\temp\foo.xls#Sheet2!A1' );
1164 $worksheet->write_url('A12', 'external:..\..\..\foo.xls' );
1165 $worksheet->write_url('A13', 'external:..\..\..\foo.xls#Sheet2!A1' );
1166 $worksheet->write_url('A13', 'external:\\\\NETWORK\share\foo.xls' );
1167
1168 All of the these URI types are recognised by the "write()" method, see
1169 above.
1170
1171 Worksheet references are typically of the form "Sheet1!A1". You can
1172 also refer to a worksheet range using the standard Excel notation:
1173 "Sheet1!A1:B2".
1174
1175 In external links the workbook and worksheet name must be separated by
1176 the "#" character: "external:Workbook.xls#Sheet1!A1'".
1177
1178 You can also link to a named range in the target worksheet. For example
1179 say you have a named range called "my_name" in the workbook
1180 "c:\temp\foo.xls" you could link to it as follows:
1181
1182 $worksheet->write_url('A14', 'external:c:\temp\foo.xls#my_name');
1183
1184 Note, you cannot currently create named ranges with "Spread‐
1185 sheet::WriteExcel".
1186
1187 Excel requires that worksheet names containing spaces or non alphanu‐
1188 meric characters are single quoted as follows "'Sales Data'!A1". If you
1189 need to do this in a single quoted string then you can either escape
1190 the single quotes "\'" or use the quote operator "q{}" as described in
1191 "perlop" in the main Perl documentation.
1192
1193 Links to network files are also supported. MS/Novell Network files nor‐
1194 mally begin with two back slashes as follows "\\NETWORK\etc". In order
1195 to generate this in a single or double quoted string you will have to
1196 escape the backslashes, '\\\\NETWORK\etc'.
1197
1198 If you are using double quote strings then you should be careful to
1199 escape anything that looks like a metacharacter. For more information
1200 see "perlfaq5: Why can't I use "C:\temp\foo" in DOS paths?".
1201
1202 Finally, you can avoid most of these quoting problems by using forward
1203 slashes. These are translated internally to backslashes:
1204
1205 $worksheet->write_url('A14', "external:c:/temp/foo.xls" );
1206 $worksheet->write_url('A15', 'external://NETWORK/share/foo.xls' );
1207
1208 See also, the note about "Cell notation".
1209
1210 write_url_range($row1, $col1, $row2, $col2, $url, $string, $format)
1211
1212 This method is essentially the same as the "write_url()" method
1213 described above. The main difference is that you can specify a link for
1214 a range of cells:
1215
1216 $worksheet->write_url(0, 0, 0, 3, 'ftp://www.perl.org/' );
1217 $worksheet->write_url(1, 0, 0, 3, 'http://www.perl.com/', 'Perl home');
1218 $worksheet->write_url('A3:D3', 'internal:Sheet2!A1' );
1219 $worksheet->write_url('A4:D4', 'external:c:\temp\foo.xls' );
1220
1221 This method is generally only required when used in conjunction with
1222 merged cells. See the "merge_range()" method and the "merge" property
1223 of a Format object, "CELL FORMATTING".
1224
1225 There is no way to force this behaviour through the "write()" method.
1226
1227 The parameters $string and the $format are optional and their position
1228 is interchangeable. However, they are applied only to the first cell in
1229 the range.
1230
1231 See also, the note about "Cell notation".
1232
1233 write_formula($row, $column, $formula, $format, $value)
1234
1235 Write a formula or function to the cell specified by $row and $column:
1236
1237 $worksheet->write_formula(0, 0, '=$B$3 + B4' );
1238 $worksheet->write_formula(1, 0, '=SIN(PI()/4)');
1239 $worksheet->write_formula(2, 0, '=SUM(B1:B5)' );
1240 $worksheet->write_formula('A4', '=IF(A3>1,"Yes", "No")' );
1241 $worksheet->write_formula('A5', '=AVERAGE(1, 2, 3, 4)' );
1242 $worksheet->write_formula('A6', '=DATEVALUE("1-Jan-2001")');
1243
1244 See the note about "Cell notation". For more information about writing
1245 Excel formulas see "FORMULAS AND FUNCTIONS IN EXCEL"
1246
1247 See also the section "Improving performance when working with formulas"
1248 and the "store_formula()" and "repeat_formula()" methods.
1249
1250 If required, it is also possible to specify the calculated value of the
1251 formula. This is occasionally necessary when working with non-Excel
1252 applications that don't calculated the value of the formula. The calcu‐
1253 lated $value is added at the end of the argument list:
1254
1255 $worksheet->write('A1', '=2+2', $format, 4);
1256
1257 However, this probably isn't something that will ever need to do. If
1258 you do use this feature then do so with care.
1259
1260 store_formula($formula)
1261
1262 The "store_formula()" method is used in conjunction with "repeat_for‐
1263 mula()" to speed up the generation of repeated formulas. See "Improving
1264 performance when working with formulas" in "FORMULAS AND FUNCTIONS IN
1265 EXCEL".
1266
1267 The "store_formula()" method pre-parses a textual representation of a
1268 formula and stores it for use at a later stage by the "repeat_for‐
1269 mula()" method.
1270
1271 "store_formula()" carries the same speed penalty as "write_formula()".
1272 However, in practice it will be used less frequently.
1273
1274 The return value of this method is a scalar that can be thought of as a
1275 reference to a formula.
1276
1277 my $sin = $worksheet->store_formula('=SIN(A1)');
1278 my $cos = $worksheet->store_formula('=COS(A1)');
1279
1280 $worksheet->repeat_formula('B1', $sin, $format, 'A1', 'A2');
1281 $worksheet->repeat_formula('C1', $cos, $format, 'A1', 'A2');
1282
1283 Although "store_formula()" is a worksheet method the return value can
1284 be used in any worksheet:
1285
1286 my $now = $worksheet->store_formula('=NOW()');
1287
1288 $worksheet1->repeat_formula('B1', $now);
1289 $worksheet2->repeat_formula('B1', $now);
1290 $worksheet3->repeat_formula('B1', $now);
1291
1292 repeat_formula($row, $col, $formula, $format, ($pattern => $replace,
1293 ...))
1294
1295 The "repeat_formula()" method is used in conjunction with "store_for‐
1296 mula()" to speed up the generation of repeated formulas. See "Improv‐
1297 ing performance when working with formulas" in "FORMULAS AND FUNCTIONS
1298 IN EXCEL".
1299
1300 In many respects "repeat_formula()" behaves like "write_formula()"
1301 except that it is significantly faster.
1302
1303 The "repeat_formula()" method creates a new formula based on the pre-
1304 parsed tokens returned by "store_formula()". The new formula is gener‐
1305 ated by substituting $pattern, $replace pairs in the stored formula:
1306
1307 my $formula = $worksheet->store_formula('=A1 * 3 + 50');
1308
1309 for my $row (0..99) {
1310 $worksheet->repeat_formula($row, 1, $formula, $format, 'A1', 'A'.($row +1));
1311 }
1312
1313 It should be noted that "repeat_formula()" doesn't modify the tokens.
1314 In the above example the substitution is always made against the origi‐
1315 nal token, "A1", which doesn't change.
1316
1317 As usual, you can use "undef" if you don't wish to specify a $format:
1318
1319 $worksheet->repeat_formula('B2', $formula, $format, 'A1', 'A2');
1320 $worksheet->repeat_formula('B3', $formula, undef, 'A1', 'A3');
1321
1322 The substitutions are made from left to right and you can use as many
1323 $pattern, $replace pairs as you need. However, each substitution is
1324 made only once:
1325
1326 my $formula = $worksheet->store_formula('=A1 + A1');
1327
1328 # Gives '=B1 + A1'
1329 $worksheet->repeat_formula('B1', $formula, undef, 'A1', 'B1');
1330
1331 # Gives '=B1 + B1'
1332 $worksheet->repeat_formula('B2', $formula, undef, ('A1', 'B1') x 2);
1333
1334 Since the $pattern is interpolated each time that it is used it is
1335 worth using the "qr" operator to quote the pattern. The "qr" operator
1336 is explained in the "perlop" man page.
1337
1338 $worksheet->repeat_formula('B1', $formula, $format, qr/A1/, 'A2');
1339
1340 Care should be taken with the values that are substituted. The formula
1341 returned by "repeat_formula()" contains several other tokens in addi‐
1342 tion to those in the formula and these might also match the pattern
1343 that you are trying to replace. In particular you should avoid substi‐
1344 tuting a single 0, 1, 2 or 3.
1345
1346 You should also be careful to avoid false matches. For example the fol‐
1347 lowing snippet is meant to change the stored formula in steps from "=A1
1348 + SIN(A1)" to "=A10 + SIN(A10)".
1349
1350 my $formula = $worksheet->store_formula('=A1 + SIN(A1)');
1351
1352 for my $row (1 .. 10) {
1353 $worksheet->repeat_formula($row -1, 1, $formula, undef,
1354 qw/A1/, 'A' . $row, #! Bad.
1355 qw/A1/, 'A' . $row #! Bad.
1356 );
1357 }
1358
1359 However it contains a bug. In the last iteration of the loop when $row
1360 is 10 the following substitutions will occur:
1361
1362 s/A1/A10/; changes =A1 + SIN(A1) to =A10 + SIN(A1)
1363 s/A1/A10/; changes =A10 + SIN(A1) to =A100 + SIN(A1) # !!
1364
1365 The solution in this case is to use a more explicit match such as
1366 "qw/^A1$/":
1367
1368 $worksheet->repeat_formula($row -1, 1, $formula, undef,
1369 qw/^A1$/, 'A' . $row,
1370 qw/^A1$/, 'A' . $row
1371 );
1372
1373 Another similar problem occurs due to the fact that substitutions are
1374 made in order. For example the following snippet is meant to change the
1375 stored formula from "=A10 + A11" to "=A11 + A12":
1376
1377 my $formula = $worksheet->store_formula('=A10 + A11');
1378
1379 $worksheet->repeat_formula('A1', $formula, undef,
1380 qw/A10/, 'A11', #! Bad.
1381 qw/A11/, 'A12' #! Bad.
1382 );
1383
1384 However, the actual substitution yields "=A12 + A11":
1385
1386 s/A10/A11/; changes =A10 + A11 to =A11 + A11
1387 s/A11/A12/; changes =A11 + A11 to =A12 + A11 # !!
1388
1389 The solution here would be to reverse the order of the substitutions or
1390 to start with a stored formula that won't yield a false match such as
1391 "=X10 + Y11":
1392
1393 my $formula = $worksheet->store_formula('=X10 + Y11');
1394
1395 $worksheet->repeat_formula('A1', $formula, undef,
1396 qw/X10/, 'A11',
1397 qw/Y11/, 'A12'
1398 );
1399
1400 If you think that you have a problem related to a false match you can
1401 check the tokens that you are substituting against as follows.
1402
1403 my $formula = $worksheet->store_formula('=A1*5+4');
1404 print "@$formula\n";
1405
1406 See also the "repeat.pl" program in the "examples" directory of the
1407 distro.
1408
1409 write_comment($row, $column, $string, ...)
1410
1411 NOTE: This method is currently incompatible with "insert_bitmap()". You
1412 can use either method but not both in the same workbook. This will be
1413 fixed soon.
1414
1415 The "write_comment()" method is used to add a comment to a cell. A cell
1416 comment is indicated in Excel by a small red triangle in the upper
1417 right-hand corner of the cell. Moving the cursor over the red triangle
1418 will reveal the comment.
1419
1420 The following example shows how to add a comment to a cell:
1421
1422 $worksheet->write (2, 2, 'Hello');
1423 $worksheet->write_comment(2, 2, 'This is a comment.');
1424
1425 As usual you can replace the $row and $column parameters with an "A1"
1426 cell reference. See the note about "Cell notation".
1427
1428 $worksheet->write ('C3', 'Hello');
1429 $worksheet->write_comment('C3', 'This is a comment.');
1430
1431 On systems with "perl 5.8" and later the "write_comment()" method will
1432 also handle strings in Perl's "utf8" format.
1433
1434 $worksheet->write_comment('C3', "\x{263a}"); # Smiley
1435 $worksheet->write_comment('C4', 'Comment ça va?');
1436
1437 In addition to the basic 3 argument form of "write_comment()" you can
1438 pass in several optional key/value pairs to control the format of the
1439 comment. For example:
1440
1441 $worksheet->write_comment('C3', 'Hello', visible => 1, author => 'Perl');
1442
1443 Most of these options are quite specific and in general the default
1444 comment behaviour will be all that you need. However, should you need
1445 greater control over the format of the cell comment the following
1446 options are available:
1447
1448 encoding
1449 author
1450 author_encoding
1451 visible
1452 x_scale
1453 width
1454 y_scale
1455 height
1456 color
1457 start_cell
1458 start_row
1459 start_col
1460 x_offset
1461 y_offset
1462
1463 Option: encoding
1464 This option is used to indicate that the comment string is encoded
1465 as UTF-16BE.
1466
1467 my $comment = pack "n", 0x263a; # UTF-16BE Smiley symbol
1468
1469 $worksheet->write_comment('C3', $comment, encoding => 1);
1470
1471 If you wish to use Unicode characters in the comment string then
1472 the preferred method is to use perl 5.8 and UTF-8 strings.
1473
1474 Option: author
1475 This option is used to indicate who the author of the comment is.
1476 Excel displays the author of the comment in the status bar at the
1477 bottom of the worksheet. This is usually of interest in corporate
1478 environments where several people might review and provide comments
1479 to a workbook.
1480
1481 $worksheet->write_comment('C3', 'Atonement', author => 'Ian McEwan');
1482
1483 Option: author_encoding
1484 This option is used to indicate that the author string is encoded
1485 as UTF-16BE.
1486
1487 Option: visible
1488 This option is used to make a cell comment visible when the work‐
1489 sheet is opened. The default behaviour in Excel is that comments
1490 are initially hidden. However, it is also possible in Excel to make
1491 individual or all comments visible. In Spreadsheet::WriteExcel
1492 individual comments can be made visible as follows:
1493
1494 $worksheet->write_comment('C3', 'Hello', visible => 1);
1495
1496 It is possible to make all comments in a worksheet visible using
1497 the "show_comments()" worksheet method (see below). Alternatively,
1498 if all of the cell comments have been made visible you can hide
1499 individual comments:
1500
1501 $worksheet->write_comment('C3', 'Hello', visible => 0);
1502
1503 Option: x_scale
1504 This option is used to set the width of the cell comment box as a
1505 factor of the default width.
1506
1507 $worksheet->write_comment('C3', 'Hello', x_scale => 2);
1508 $worksheet->write_comment('C4', 'Hello', x_scale => 4.2);
1509
1510 Option: width
1511 This option is used to set the width of the cell comment box
1512 explicitly in pixels.
1513
1514 $worksheet->write_comment('C3', 'Hello', width => 200);
1515
1516 Option: y_scale
1517 This option is used to set the height of the cell comment box as a
1518 factor of the default height.
1519
1520 $worksheet->write_comment('C3', 'Hello', y_scale => 2);
1521 $worksheet->write_comment('C4', 'Hello', y_scale => 4.2);
1522
1523 Option: height
1524 This option is used to set the height of the cell comment box
1525 explicitly in pixels.
1526
1527 $worksheet->write_comment('C3', 'Hello', height => 200);
1528
1529 Option: color
1530 This option is used to set the background colour of cell comment
1531 box. You can use one of the named colours recognised by Spread‐
1532 sheet::WriteExcel or a colour index. See "COLOURS IN EXCEL".
1533
1534 $worksheet->write_comment('C3', 'Hello', color => 'green');
1535 $worksheet->write_comment('C4', 'Hello', color => 0x35); # Orange
1536
1537 Option: start_cell
1538 This option is used to set the cell in which the comment will
1539 appear. By default Excel displays comments one cell to the right
1540 and one cell above the cell to which the comment relates. However,
1541 you can change this behaviour if you wish. In the following example
1542 the comment which would appear by default in cell "D2" is moved to
1543 "E2".
1544
1545 $worksheet->write_comment('C3', 'Hello', start_cell => 'E2');
1546
1547 Option: start_row
1548 This option is used to set the row in which the comment will
1549 appear. See the "start_cell" option above. The row is zero indexed.
1550
1551 $worksheet->write_comment('C3', 'Hello', start_row => 0);
1552
1553 Option: start_col
1554 This option is used to set the column in which the comment will
1555 appear. See the "start_cell" option above. The column is zero
1556 indexed.
1557
1558 $worksheet->write_comment('C3', 'Hello', start_col => 4);
1559
1560 Option: x_offset
1561 This option is used to change the x offset, in pixels, of a comment
1562 within a cell:
1563
1564 $worksheet->write_comment('C3', $comment, x_offset => 30);
1565
1566 Option: y_offset
1567 This option is used to change the y offset, in pixels, of a comment
1568 within a cell:
1569
1570 $worksheet->write_comment('C3', $comment, x_offset => 30);
1571
1572 You can apply as many of these options as you require.
1573
1574 Note about row height and comments. If you specify the height of a row
1575 that contains a comment then Spreadsheet::WriteExcel will adjust the
1576 height of the comment to maintain the default or user specified dimen‐
1577 sions. However, the height of a row can also be adjusted automatically
1578 by Excel if the text wrap property is set or large fonts are used in
1579 the cell. This means that the height of the row is unknown to WriteEx‐
1580 cel at run time and thus the comment box is stretched with the row. Use
1581 the "set_row()" method to specify the row height explicitly and avoid
1582 this problem.
1583
1584 show_comments()
1585
1586 This method is used to make all cell comments visible when a worksheet
1587 is opened.
1588
1589 Individual comments can be made visible using the "visible" parameter
1590 of the "write_comment" method (see above):
1591
1592 $worksheet->write_comment('C3', 'Hello', visible => 1);
1593
1594 If all of the cell comments have been made visible you can hide indi‐
1595 vidual comments as follows:
1596
1597 $worksheet->write_comment('C3', 'Hello', visible => 0);
1598
1599 add_write_handler($re, $code_ref)
1600
1601 This method is used to extend the Spreadsheet::WriteExcel write()
1602 method to handle user defined data.
1603
1604 If you refer to the section on "write()" above you will see that it
1605 acts as an alias for several more specific "write_*" methods. However,
1606 it doesn't always act in exactly the way that you would like it to.
1607
1608 One solution is to filter the input data yourself and call the appro‐
1609 priate "write_*" method. Another approach is to use the "add_write_han‐
1610 dler()" method to add your own automated behaviour to "write()".
1611
1612 The "add_write_handler()" method take two arguments, $re, a regular
1613 expression to match incoming data and $code_ref a callback function to
1614 handle the matched data:
1615
1616 $worksheet->add_write_handler(qr/^\d\d\d\d$/, \&my_write);
1617
1618 (In the these examples the "qr" operator is used to quote the regular
1619 expression strings, see perlop for more details).
1620
1621 The method is use as follows. say you wished to write 7 digit ID num‐
1622 bers as a string so that any leading zeros were preserved*, you could
1623 do something like the following:
1624
1625 $worksheet->add_write_handler(qr/^\d{7}$/, \&write_my_id);
1626
1627 sub write_my_id {
1628 my $worksheet = shift;
1629 return $worksheet->write_string(@_);
1630 }
1631
1632 * You could also use the "keep_leading_zeros()" method for this.
1633
1634 Then if you call "write()" with an appropriate string it will be han‐
1635 dled automatically:
1636
1637 # Writes 0000000. It would normally be written as a number; 0.
1638 $worksheet->write('A1', '0000000');
1639
1640 The callback function will receive a reference to the calling worksheet
1641 and all of the other arguments that were passed to "write()". The call‐
1642 back will see an @_ argument list that looks like the following:
1643
1644 $_[0] A ref to the calling worksheet. *
1645 $_[1] Zero based row number.
1646 $_[2] Zero based column number.
1647 $_[3] A number or string or token.
1648 $_[4] A format ref if any.
1649 $_[5] Any other arguments.
1650 ...
1651
1652 * It is good style to shift this off the list so the @_ is the same
1653 as the argument list seen by write().
1654
1655 Your callback should "return()" the return value of the "write_*"
1656 method that was called or "undef" to indicate that you rejected the
1657 match and want "write()" to continue as normal.
1658
1659 So for example if you wished to apply the previous filter only to ID
1660 values that occur in the first column you could modify your callback
1661 function as follows:
1662
1663 sub write_my_id {
1664 my $worksheet = shift;
1665 my $col = $_[1];
1666
1667 if ($col == 0) {
1668 return $worksheet->write_string(@_);
1669 }
1670 else {
1671 # Reject the match and return control to write()
1672 return undef;
1673 }
1674 }
1675
1676 Now, you will get different behaviour for the first column and other
1677 columns:
1678
1679 $worksheet->write('A1', '0000000'); # Writes 0000000
1680 $worksheet->write('B1', '0000000'); # Writes 0
1681
1682 You may add more than one handler in which case they will be called in
1683 the order that they were added.
1684
1685 Note, the "add_write_handler()" method is particularly suited for han‐
1686 dling dates.
1687
1688 See the "write_handler 1-4" programs in the "examples" directory for
1689 further examples.
1690
1691 insert_bitmap($row, $col, $filename, $x, $y, $scale_x, $scale_y)
1692
1693 NOTE: This method is currently incompatible with "write_comment()". You
1694 can use either method but not both in the same workbook. This will be
1695 fixed soon.
1696
1697 NOTE: The images inserted using this method do not display in Open‐
1698 Office.org or Gnumeric. This is related to the previous note and will
1699 also be fixed soon.
1700
1701 This method can be used to insert a bitmap into a worksheet. The bitmap
1702 must be a 24 bit, true colour, bitmap. No other format is supported.
1703 The $x, $y, $scale_x and $scale_y parameters are optional.
1704
1705 $worksheet1->insert_bitmap('A1', 'perl.bmp');
1706 $worksheet2->insert_bitmap('A1', '../images/perl.bmp');
1707 $worksheet3->insert_bitmap('A1', '.c:\images\perl.bmp');
1708
1709 Note: you must call "set_row()" or "set_column()" before "insert_bit‐
1710 map()" if you wish to change the default dimensions of any of the rows
1711 or columns that the images occupies. The height of a row can also
1712 change if you use a font that is larger than the default. This in turn
1713 will affect the scaling of your image. To avoid this you should explic‐
1714 itly set the height of the row using "set_row()" if it contains a font
1715 size that will change the row height.
1716
1717 The parameters $x and $y can be used to specify an offset from the top
1718 left hand corner of the cell specified by $row and $col. The offset
1719 values are in pixels.
1720
1721 $worksheet1->insert_bitmap('A1', 'perl.bmp', 32, 10);
1722
1723 The default width of a cell is 63 pixels. The default height of a cell
1724 is 17 pixels. The pixels offsets can be calculated using the following
1725 relationships:
1726
1727 Wp = int(12We) if We < 1
1728 Wp = int(7We +5) if We >= 1
1729 Hp = int(4/3He)
1730
1731 where:
1732 We is the cell width in Excels units
1733 Wp is width in pixels
1734 He is the cell height in Excels units
1735 Hp is height in pixels
1736
1737 The offsets can be greater than the width or height of the underlying
1738 cell. This can be occasionally useful if you wish to align two or more
1739 images relative to the same cell.
1740
1741 The parameters $scale_x and $scale_y can be used to scale the inserted
1742 image horizontally and vertically:
1743
1744 # Scale the inserted image: width x 2.0, height x 0.8
1745 $worksheet->insert_bitmap('A1', 'perl.bmp', 0, 0, 2, 0.8);
1746
1747 Note: although Excel allows you to import several graphics formats such
1748 as gif, jpeg, png and eps these are converted internally into a propri‐
1749 etary format. One of the few non-proprietary formats that Excel sup‐
1750 ports is 24 bit, true colour, bitmaps. Therefore if you wish to use
1751 images in any other format you must first use an external application
1752 such as the ImageMagick convert utility to convert them to 24 bit bit‐
1753 maps.
1754
1755 convert test.png test.bmp
1756
1757 A later release will support the use of file handles and pre-encoded
1758 bitmap strings.
1759
1760 See also the "images.pl" program in the "examples" directory of the
1761 distro.
1762
1763 get_name()
1764
1765 The "get_name()" method is used to retrieve the name of a worksheet.
1766 For example:
1767
1768 foreach my $sheet ($workbook->sheets()) {
1769 print $sheet->get_name();
1770 }
1771
1772 activate()
1773
1774 The "activate()" method is used to specify which worksheet is initially
1775 visible in a multi-sheet workbook:
1776
1777 $worksheet1 = $workbook->add_worksheet('To');
1778 $worksheet2 = $workbook->add_worksheet('the');
1779 $worksheet3 = $workbook->add_worksheet('wind');
1780
1781 $worksheet3->activate();
1782
1783 This is similar to the Excel VBA activate method. More than one work‐
1784 sheet can be selected via the "select()" method, however only one work‐
1785 sheet can be active. The default value is the first worksheet.
1786
1787 select()
1788
1789 The "select()" method is used to indicate that a worksheet is selected
1790 in a multi-sheet workbook:
1791
1792 $worksheet1->activate();
1793 $worksheet2->select();
1794 $worksheet3->select();
1795
1796 A selected worksheet has its tab highlighted. Selecting worksheets is a
1797 way of grouping them together so that, for example, several worksheets
1798 could be printed in one go. A worksheet that has been activated via the
1799 "activate()" method will also appear as selected. You probably won't
1800 need to use the "select()" method very often.
1801
1802 hide()
1803
1804 The "hide()" method is used to hide a worksheet:
1805
1806 $worksheet->hide();
1807
1808 You may wish to hide a worksheet in order to avoid confusing a user
1809 with intermediate data or calculations.
1810
1811 A hidden worksheet can not be activated or selected so this method is
1812 mutually exclusive with the "activate()" and "select()" methods.
1813
1814 set_first_sheet()
1815
1816 The "activate()" method determines which worksheet is initially
1817 selected. However, if there are a large number of worksheets the
1818 selected worksheet may not appear on the screen. To avoid this you can
1819 select which is the leftmost visible worksheet using
1820 "set_first_sheet()":
1821
1822 for (1..20) {
1823 $workbook->add_worksheet;
1824 }
1825
1826 $worksheet21 = $workbook->add_worksheet();
1827 $worksheet22 = $workbook->add_worksheet();
1828
1829 $worksheet21->set_first_sheet();
1830 $worksheet22->activate();
1831
1832 This method is not required very often. The default value is the first
1833 worksheet.
1834
1835 protect($password)
1836
1837 The "protect()" method is used to protect a worksheet from modifica‐
1838 tion:
1839
1840 $worksheet->protect();
1841
1842 It can be turned off in Excel via the "Tools->Protection->Unprotect
1843 Sheet" menu command.
1844
1845 The "protect()" method also has the effect of enabling a cell's
1846 "locked" and "hidden" properties if they have been set. A "locked" cell
1847 cannot be edited. A "hidden" cell will display the results of a formula
1848 but not the formula itself. In Excel a cell's locked property is on by
1849 default.
1850
1851 # Set some format properties
1852 my $unlocked = $workbook->add_format(locked => 0);
1853 my $hidden = $workbook->add_format(hidden => 1);
1854
1855 # Enable worksheet protection
1856 $worksheet->protect();
1857
1858 # This cell cannot be edited, it is locked by default
1859 $worksheet->write('A1', '=1+2');
1860
1861 # This cell can be edited
1862 $worksheet->write('A2', '=1+2', $unlocked);
1863
1864 # The formula in this cell isn't visible
1865 $worksheet->write('A3', '=1+2', $hidden);
1866
1867 See also the "set_locked" and "set_hidden" format methods in "CELL FOR‐
1868 MATTING".
1869
1870 You can optionally add a password to the worksheet protection:
1871
1872 $worksheet->protect('drowssap');
1873
1874 Note, the worksheet level password in Excel provides very weak protec‐
1875 tion. It does not encrypt your data in any way and it is very easy to
1876 deactivate. Therefore, do not use the above method if you wish to pro‐
1877 tect sensitive data or calculations. However, before you get worried,
1878 Excel's own workbook level password protection does provide strong
1879 encryption in Excel 97+. For technical reasons this will never be sup‐
1880 ported by "Spreadsheet::WriteExcel".
1881
1882 set_selection($first_row, $first_col, $last_row, $last_col)
1883
1884 This method can be used to specify which cell or cells are selected in
1885 a worksheet. The most common requirement is to select a single cell, in
1886 which case $last_row and $last_col can be omitted. The active cell
1887 within a selected range is determined by the order in which $first and
1888 $last are specified. It is also possible to specify a cell or a range
1889 using A1 notation. See the note about "Cell notation".
1890
1891 Examples:
1892
1893 $worksheet1->set_selection(3, 3); # 1. Cell D4.
1894 $worksheet2->set_selection(3, 3, 6, 6); # 2. Cells D4 to G7.
1895 $worksheet3->set_selection(6, 6, 3, 3); # 3. Cells G7 to D4.
1896 $worksheet4->set_selection('D4'); # Same as 1.
1897 $worksheet5->set_selection('D4:G7'); # Same as 2.
1898 $worksheet6->set_selection('G7:D4'); # Same as 3.
1899
1900 The default cell selections is (0, 0), 'A1'.
1901
1902 set_row($row, $height, $format, $hidden, $level)
1903
1904 This method can be used to change the default properties of a row. All
1905 parameters apart from $row are optional.
1906
1907 The most common use for this method is to change the height of a row:
1908
1909 $worksheet->set_row(0, 20); # Row 1 height set to 20
1910
1911 If you wish to set the format without changing the height you can pass
1912 "undef" as the height parameter:
1913
1914 $worksheet->set_row(0, undef, $format);
1915
1916 The $format parameter will be applied to any cells in the row that
1917 don't have a format. For example
1918
1919 $worksheet->set_row(0, undef, $format1); # Set the format for row 1
1920 $worksheet->write('A1', "Hello"); # Defaults to $format1
1921 $worksheet->write('B1', "Hello", $format2); # Keeps $format2
1922
1923 If you wish to define a row format in this way you should call the
1924 method before any calls to "write()". Calling it afterwards will over‐
1925 write any format that was previously specified.
1926
1927 The $hidden parameter should be set to 1 if you wish to hide a row.
1928 This can be used, for example, to hide intermediary steps in a compli‐
1929 cated calculation:
1930
1931 $worksheet->set_row(0, 20, $format, 1);
1932 $worksheet->set_row(1, undef, undef, 1);
1933
1934 The $level parameter is used to set the outline level of the row. Out‐
1935 lines are described in "OUTLINES AND GROUPING IN EXCEL". Adjacent rows
1936 with the same outline level are grouped together into a single outline.
1937
1938 The following example sets an outline level of 1 for rows 1 and 2
1939 (zero-indexed):
1940
1941 $worksheet->set_row(1, undef, undef, 0, 1);
1942 $worksheet->set_row(2, undef, undef, 0, 1);
1943
1944 The $hidden parameter can also be used to collapse outlined rows when
1945 used in conjunction with the $level parameter.
1946
1947 $worksheet->set_row(1, undef, undef, 1, 1);
1948 $worksheet->set_row(2, undef, undef, 1, 1);
1949
1950 Excel allows up to 7 outline levels. Therefore the $level parameter
1951 should be in the range "0 <= $level <= 7".
1952
1953 set_column($first_col, $last_col, $width, $format, $hidden, $level)
1954
1955 This method can be used to change the default properties of a single
1956 column or a range of columns. All parameters apart from $first_col and
1957 $last_col are optional.
1958
1959 If "set_column()" is applied to a single column the value of $first_col
1960 and $last_col should be the same. It is also possible to specify a col‐
1961 umn range using the form of A1 notation used for columns. See the note
1962 about "Cell notation".
1963
1964 Examples:
1965
1966 $worksheet->set_column(0, 0, 20); # Column A width set to 20
1967 $worksheet->set_column(1, 3, 30); # Columns B-D width set to 30
1968 $worksheet->set_column('E:E', 20); # Column E width set to 20
1969 $worksheet->set_column('F:H', 30); # Columns F-H width set to 30
1970
1971 The width corresponds to the column width value that is specified in
1972 Excel. It is approximately equal to the length of a string in the
1973 default font of Arial 10. Unfortunately, there is no way to specify
1974 "AutoFit" for a column in the Excel file format. This feature is only
1975 available at runtime from within Excel.
1976
1977 As usual the $format parameter is optional, for additional information,
1978 see "CELL FORMATTING". If you wish to set the format without changing
1979 the width you can pass "undef" as the width parameter:
1980
1981 $worksheet->set_column(0, 0, undef, $format);
1982
1983 The $format parameter will be applied to any cells in the column that
1984 don't have a format. For example
1985
1986 $worksheet->set_column('A:A', undef, $format1); # Set format for col 1
1987 $worksheet->write('A1', "Hello"); # Defaults to $format1
1988 $worksheet->write('A2', "Hello", $format2); # Keeps $format2
1989
1990 If you wish to define a column format in this way you should call the
1991 method before any calls to "write()". If you call it afterwards it
1992 won't have any effect.
1993
1994 A default row format takes precedence over a default column format
1995
1996 $worksheet->set_row(0, undef, $format1); # Set format for row 1
1997 $worksheet->set_column('A:A', undef, $format2); # Set format for col 1
1998 $worksheet->write('A1', "Hello"); # Defaults to $format1
1999 $worksheet->write('A2', "Hello"); # Defaults to $format2
2000
2001 The $hidden parameter should be set to 1 if you wish to hide a column.
2002 This can be used, for example, to hide intermediary steps in a compli‐
2003 cated calculation:
2004
2005 $worksheet->set_column('D:D', 20, $format, 1);
2006 $worksheet->set_column('E:E', undef, undef, 1);
2007
2008 The $level parameter is used to set the outline level of the column.
2009 Outlines are described in "OUTLINES AND GROUPING IN EXCEL". Adjacent
2010 columns with the same outline level are grouped together into a single
2011 outline.
2012
2013 The following example sets an outline level of 1 for columns B to G:
2014
2015 $worksheet->set_column('B:G', undef, undef, 0, 1);
2016
2017 The $hidden parameter can also be used to collapse outlined columns
2018 when used in conjunction with the $level parameter.
2019
2020 $worksheet->set_column('B:G', undef, undef, 1, 1);
2021
2022 Excel allows up to 7 outline levels. Therefore the $level parameter
2023 should be in the range "0 <= $level <= 7".
2024
2025 outline_settings($visible, $symbols_below, $symbols_right, $auto_style)
2026
2027 The "outline_settings()" method is used to control the appearance of
2028 outlines in Excel. Outlines are described in "OUTLINES AND GROUPING IN
2029 EXCEL".
2030
2031 The $visible parameter is used to control whether or not outlines are
2032 visible. Setting this parameter to 0 will cause all outlines on the
2033 worksheet to be hidden. They can be unhidden in Excel by means of the
2034 "Show Outline Symbols" command button. The default setting is 1 for
2035 visible outlines.
2036
2037 $worksheet->outline_settings(0);
2038
2039 The $symbols_below parameter is used to control whether the row outline
2040 symbol will appear above or below the outline level bar. The default
2041 setting is 1 for symbols to appear below the outline level bar.
2042
2043 The "symbols_right" parameter is used to control whether the column
2044 outline symbol will appear to the left or the right of the outline
2045 level bar. The default setting is 1 for symbols to appear to the right
2046 of the outline level bar.
2047
2048 The $auto_style parameter is used to control whether the automatic out‐
2049 line generator in Excel uses automatic styles when creating an outline.
2050 This has no effect on a file generated by "Spreadsheet::WriteExcel" but
2051 it does have an effect on how the worksheet behaves after it is cre‐
2052 ated. The default setting is 0 for "Automatic Styles" to be turned off.
2053
2054 The default settings for all of these parameters correspond to Excel's
2055 default parameters.
2056
2057 The worksheet parameters controlled by "outline_settings()" are rarely
2058 used.
2059
2060 freeze_panes($row, $col, $top_row, $left_col)
2061
2062 This method can be used to divide a worksheet into horizontal or verti‐
2063 cal regions known as panes and to also "freeze" these panes so that the
2064 splitter bars are not visible. This is the same as the "Window->Freeze
2065 Panes" menu command in Excel
2066
2067 The parameters $row and $col are used to specify the location of the
2068 split. It should be noted that the split is specified at the top or
2069 left of a cell and that the method uses zero based indexing. Therefore
2070 to freeze the first row of a worksheet it is necessary to specify the
2071 split at row 2 (which is 1 as the zero-based index). This might lead
2072 you to think that you are using a 1 based index but this is not the
2073 case.
2074
2075 You can set one of the $row and $col parameters as zero if you do not
2076 want either a vertical or horizontal split.
2077
2078 Examples:
2079
2080 $worksheet->freeze_panes(1, 0); # Freeze the first row
2081 $worksheet->freeze_panes('A2'); # Same using A1 notation
2082 $worksheet->freeze_panes(0, 1); # Freeze the first column
2083 $worksheet->freeze_panes('B1'); # Same using A1 notation
2084 $worksheet->freeze_panes(1, 2); # Freeze first row and first 2 columns
2085 $worksheet->freeze_panes('C2'); # Same using A1 notation
2086
2087 The parameters $top_row and $left_col are optional. They are used to
2088 specify the top-most or left-most visible row or column in the
2089 scrolling region of the panes. For example to freeze the first row and
2090 to have the scrolling region begin at row twenty:
2091
2092 $worksheet->freeze_panes(1, 0, 20, 0);
2093
2094 You cannot use A1 notation for the $top_row and $left_col parameters.
2095
2096 See also the "panes.pl" program in the "examples" directory of the dis‐
2097 tribution.
2098
2099 thaw_panes($y, $x, $top_row, $left_col)
2100
2101 This method can be used to divide a worksheet into horizontal or verti‐
2102 cal regions known as panes. This method is different from the
2103 "freeze_panes()" method in that the splits between the panes will be
2104 visible to the user and each pane will have its own scroll bars.
2105
2106 The parameters $y and $x are used to specify the vertical and horizon‐
2107 tal position of the split. The units for $y and $x are the same as
2108 those used by Excel to specify row height and column width. However,
2109 the vertical and horizontal units are different from each other. There‐
2110 fore you must specify the $y and $x parameters in terms of the row
2111 heights and column widths that you have set or the default values which
2112 are 12.75 for a row and 8.43 for a column.
2113
2114 You can set one of the $y and $x parameters as zero if you do not want
2115 either a vertical or horizontal split. The parameters $top_row and
2116 $left_col are optional. They are used to specify the top-most or left-
2117 most visible row or column in the bottom-right pane.
2118
2119 Example:
2120
2121 $worksheet->thaw_panes(12.75, 0, 1, 0); # First row
2122 $worksheet->thaw_panes(0, 8.43, 0, 1); # First column
2123 $worksheet->thaw_panes(12.75, 8.43, 1, 1); # First row and column
2124
2125 You cannot use A1 notation with this method.
2126
2127 See also the "freeze_panes()" method and the "panes.pl" program in the
2128 "examples" directory of the distribution.
2129
2130 merge_range($first_row, $first_col, $last_row, $last_col, $token, $for‐
2131 mat, $encoding)
2132
2133 Merging cells can be achieved by setting the "merge" property of a For‐
2134 mat object, see "CELL FORMATTING". However, this only allows simple
2135 Excel5 style horizontal merging which Excel refers to as "center across
2136 selection".
2137
2138 The "merge_range()" method allows you to do Excel97+ style formatting
2139 where the cells can contain other types of alignment in addition to the
2140 merging:
2141
2142 my $format = $workbook->add_format(
2143 border => 6,
2144 valign => 'vcenter',
2145 align => 'center',
2146 );
2147
2148 $worksheet->merge_range('B3:D4', 'Vertical and horizontal', $format);
2149
2150 WARNING. The format object that is used with a "merge_range()" method
2151 call is marked internally as being associated with a merged range. It
2152 is a fatal error to use a merged format in a non-merged cell. Instead
2153 you should use separate formats for merged and non-merged cells. This
2154 restriction will be removed in a future release.
2155
2156 The $encoding parameter is optional, see below.
2157
2158 "merge_range()" writes its $token argument using the worksheet
2159 "write()" method. Therefore it will handle numbers, strings, formulas
2160 or urls as required.
2161
2162 Setting the "merge" property of the format isn't required when you are
2163 using "merge_range()". In fact using it will exclude the use of any
2164 other horizontal alignment option.
2165
2166 On systems with "perl 5.8" and later the "merge_range()" method will
2167 also handle strings in Perl's "utf8" format.
2168
2169 $worksheet->merge_range('B3:D4', "\x{263a}", $format); # Smiley
2170
2171 On earlier Perl systems your can specify UTF-16BE worksheet names using
2172 an additional encoding parameter:
2173
2174 my $str = pack "n", 0x263a;
2175 $worksheet->merge_range('B3:D4', $str, $format, 1); # Smiley
2176
2177 The full possibilities of this method are shown in the "merge3.pl" to
2178 "merge6.pl" programs in the "examples" directory of the distribution.
2179
2180 set_zoom($scale)
2181
2182 Set the worksheet zoom factor in the range "10 <= $scale <= 400":
2183
2184 $worksheet1->set_zoom(50);
2185 $worksheet2->set_zoom(75);
2186 $worksheet3->set_zoom(300);
2187 $worksheet4->set_zoom(400);
2188
2189 The default zoom factor is 100. You cannot zoom to "Selection" because
2190 it is calculated by Excel at run-time.
2191
2192 Note, "set_zoom()" does not affect the scale of the printed page. For
2193 that you should use "set_print_scale()".
2194
2195 right_to_left()
2196
2197 The "right_to_left()" method is used to change the default direction of
2198 the worksheet from left-to-right, with the A1 cell in the top left, to
2199 right-to-left, with the he A1 cell in the top right.
2200
2201 $worksheet->right_to_left();
2202
2203 This is useful when creating Arabic, Hebrew or other near or far east‐
2204 ern worksheets that use right-to-left as the default direction.
2205
2206 hide_zero()
2207
2208 The "hide_zero()" method is used to hide any zero values that appear in
2209 cells.
2210
2211 $worksheet->right_to_left();
2212
2213 In Excel this option is found under Tools->Options->View.
2214
2215 set_tab_color()
2216
2217 The "set_tab_color()" method is used to change the colour of the work‐
2218 sheet tab. This feature is only available in Excel 2002 and later. You
2219 can use one of the standard colour names provided by the Format object
2220 or a colour index. See "COLOURS IN EXCEL" and the "set_custom_color()"
2221 method.
2222
2223 $worksheet1->set_tab_color('red');
2224 $worksheet2->set_tab_color(0x0C);
2225
2226 See the "tab_colors.pl" program in the examples directory of the dis‐
2227 tro.
2228
2230 Page set-up methods affect the way that a worksheet looks when it is
2231 printed. They control features such as page headers and footers and
2232 margins. These methods are really just standard worksheet methods. They
2233 are documented here in a separate section for the sake of clarity.
2234
2235 The following methods are available for page set-up:
2236
2237 set_landscape()
2238 set_portrait()
2239 set_page_view()
2240 set_paper()
2241 center_horizontally()
2242 center_vertically()
2243 set_margins()
2244 set_header()
2245 set_footer()
2246 repeat_rows()
2247 repeat_columns()
2248 hide_gridlines()
2249 print_row_col_headers()
2250 print_area()
2251 print_across()
2252 fit_to_pages()
2253 set_print_scale()
2254 set_h_pagebreaks()
2255 set_v_pagebreaks()
2256
2257 A common requirement when working with Spreadsheet::WriteExcel is to
2258 apply the same page set-up features to all of the worksheets in a work‐
2259 book. To do this you can use the "sheets()" method of the "workbook"
2260 class to access the array of worksheets in a workbook:
2261
2262 foreach $worksheet ($workbook->sheets()) {
2263 $worksheet->set_landscape();
2264 }
2265
2266 set_landscape()
2267
2268 This method is used to set the orientation of a worksheet's printed
2269 page to landscape:
2270
2271 $worksheet->set_landscape(); # Landscape mode
2272
2273 set_portrait()
2274
2275 This method is used to set the orientation of a worksheet's printed
2276 page to portrait. The default worksheet orientation is portrait, so you
2277 won't generally need to call this method.
2278
2279 $worksheet->set_portrait(); # Portrait mode
2280
2281 set_page_view()
2282
2283 This method is used to display the worksheet in "Page View" mode. This
2284 is currently only supported by Mac Excel, where it is the default.
2285
2286 $worksheet->set_page_view();
2287
2288 set_paper($index)
2289
2290 This method is used to set the paper format for the printed output of a
2291 worksheet. The following paper styles are available:
2292
2293 Index Paper format Paper size
2294 ===== ============ ==========
2295 0 Printer default -
2296 1 Letter 8 1/2 x 11 in
2297 2 Letter Small 8 1/2 x 11 in
2298 3 Tabloid 11 x 17 in
2299 4 Ledger 17 x 11 in
2300 5 Legal 8 1/2 x 14 in
2301 6 Statement 5 1/2 x 8 1/2 in
2302 7 Executive 7 1/4 x 10 1/2 in
2303 8 A3 297 x 420 mm
2304 9 A4 210 x 297 mm
2305 10 A4 Small 210 x 297 mm
2306 11 A5 148 x 210 mm
2307 12 B4 250 x 354 mm
2308 13 B5 182 x 257 mm
2309 14 Folio 8 1/2 x 13 in
2310 15 Quarto 215 x 275 mm
2311 16 - 10x14 in
2312 17 - 11x17 in
2313 18 Note 8 1/2 x 11 in
2314 19 Envelope 9 3 7/8 x 8 7/8
2315 20 Envelope 10 4 1/8 x 9 1/2
2316 21 Envelope 11 4 1/2 x 10 3/8
2317 22 Envelope 12 4 3/4 x 11
2318 23 Envelope 14 5 x 11 1/2
2319 24 C size sheet -
2320 25 D size sheet -
2321 26 E size sheet -
2322 27 Envelope DL 110 x 220 mm
2323 28 Envelope C3 324 x 458 mm
2324 29 Envelope C4 229 x 324 mm
2325 30 Envelope C5 162 x 229 mm
2326 31 Envelope C6 114 x 162 mm
2327 32 Envelope C65 114 x 229 mm
2328 33 Envelope B4 250 x 353 mm
2329 34 Envelope B5 176 x 250 mm
2330 35 Envelope B6 176 x 125 mm
2331 36 Envelope 110 x 230 mm
2332 37 Monarch 3.875 x 7.5 in
2333 38 Envelope 3 5/8 x 6 1/2 in
2334 39 Fanfold 14 7/8 x 11 in
2335 40 German Std Fanfold 8 1/2 x 12 in
2336 41 German Legal Fanfold 8 1/2 x 13 in
2337
2338 Note, it is likely that not all of these paper types will be available
2339 to the end user since it will depend on the paper formats that the
2340 user's printer supports. Therefore, it is best to stick to standard
2341 paper types.
2342
2343 $worksheet->set_paper(1); # US Letter
2344 $worksheet->set_paper(9); # A4
2345
2346 If you do not specify a paper type the worksheet will print using the
2347 printer's default paper.
2348
2349 center_horizontally()
2350
2351 Center the worksheet data horizontally between the margins on the
2352 printed page:
2353
2354 $worksheet->center_horizontally();
2355
2356 center_vertically()
2357
2358 Center the worksheet data vertically between the margins on the printed
2359 page:
2360
2361 $worksheet->center_vertically();
2362
2363 set_margins($inches)
2364
2365 There are several methods available for setting the worksheet margins
2366 on the printed page:
2367
2368 set_margins() # Set all margins to the same value
2369 set_margins_LR() # Set left and right margins to the same value
2370 set_margins_TB() # Set top and bottom margins to the same value
2371 set_margin_left(); # Set left margin
2372 set_margin_right(); # Set right margin
2373 set_margin_top(); # Set top margin
2374 set_margin_bottom(); # Set bottom margin
2375
2376 All of these methods take a distance in inches as a parameter. Note: 1
2377 inch = 25.4mm. ;-) The default left and right margin is 0.75 inch. The
2378 default top and bottom margin is 1.00 inch.
2379
2380 set_header($string, $margin)
2381
2382 Headers and footers are generated using a $string which is a combina‐
2383 tion of plain text and control characters. The $margin parameter is
2384 optional.
2385
2386 The available control character are:
2387
2388 Control Category Description
2389 ======= ======== ===========
2390 &L Justification Left
2391 &C Center
2392 &R Right
2393
2394 &P Information Page number
2395 &N Total number of pages
2396 &D Date
2397 &T Time
2398 &F File name
2399 &A Worksheet name
2400 &Z Workbook path
2401
2402 &fontsize Font Font size
2403 &"font,style" Font name and style
2404 &U Single underline
2405 &E Double underline
2406 &S Strikethrough
2407 &X Superscript
2408 &Y Subscript
2409
2410 && Miscellaneous Literal ampersand &
2411
2412 Text in headers and footers can be justified (aligned) to the left,
2413 center and right by prefixing the text with the control characters &L,
2414 &C and &R.
2415
2416 For example (with ASCII art representation of the results):
2417
2418 $worksheet->set_header('&LHello');
2419
2420 ---------------------------------------------------------------
2421 ⎪ ⎪
2422 ⎪ Hello ⎪
2423 ⎪ ⎪
2424
2425 $worksheet->set_header('&CHello');
2426
2427 ---------------------------------------------------------------
2428 ⎪ ⎪
2429 ⎪ Hello ⎪
2430 ⎪ ⎪
2431
2432 $worksheet->set_header('&RHello');
2433
2434 ---------------------------------------------------------------
2435 ⎪ ⎪
2436 ⎪ Hello ⎪
2437 ⎪ ⎪
2438
2439 For simple text, if you do not specify any justification the text will
2440 be centred. However, you must prefix the text with &C if you specify a
2441 font name or any other formatting:
2442
2443 $worksheet->set_header('Hello');
2444
2445 ---------------------------------------------------------------
2446 ⎪ ⎪
2447 ⎪ Hello ⎪
2448 ⎪ ⎪
2449
2450 You can have text in each of the justification regions:
2451
2452 $worksheet->set_header('&LCiao&CBello&RCielo');
2453
2454 ---------------------------------------------------------------
2455 ⎪ ⎪
2456 ⎪ Ciao Bello Cielo ⎪
2457 ⎪ ⎪
2458
2459 The information control characters act as variables that Excel will
2460 update as the workbook or worksheet changes. Times and dates are in the
2461 users default format:
2462
2463 $worksheet->set_header('&CPage &P of &N');
2464
2465 ---------------------------------------------------------------
2466 ⎪ ⎪
2467 ⎪ Page 1 of 6 ⎪
2468 ⎪ ⎪
2469
2470 $worksheet->set_header('&CUpdated at &T');
2471
2472 ---------------------------------------------------------------
2473 ⎪ ⎪
2474 ⎪ Updated at 12:30 PM ⎪
2475 ⎪ ⎪
2476
2477 You can specify the font size of a section of the text by prefixing it
2478 with the control character &n where "n" is the font size:
2479
2480 $worksheet1->set_header('&C&30Hello Big' );
2481 $worksheet2->set_header('&C&10Hello Small');
2482
2483 You can specify the font of a section of the text by prefixing it with
2484 the control sequence "&"font,style"" where "fontname" is a font name
2485 such as "Courier New" or "Times New Roman" and "style" is one of the
2486 standard Windows font descriptions: "Regular", "Italic", "Bold" or
2487 "Bold Italic":
2488
2489 $worksheet1->set_header('&C&"Courier New,Italic"Hello');
2490 $worksheet2->set_header('&C&"Courier New,Bold Italic"Hello');
2491 $worksheet3->set_header('&C&"Times New Roman,Regular"Hello');
2492
2493 It is possible to combine all of these features together to create
2494 sophisticated headers and footers. As an aid to setting up complicated
2495 headers and footers you can record a page set-up as a macro in Excel
2496 and look at the format strings that VBA produces. Remember however that
2497 VBA uses two double quotes "" to indicate a single double quote. For
2498 the last example above the equivalent VBA code looks like this:
2499
2500 .LeftHeader = ""
2501 .CenterHeader = "&""Times New Roman,Regular""Hello"
2502 .RightHeader = ""
2503
2504 To include a single literal ampersand "&" in a header or footer you
2505 should use a double ampersand "&&":
2506
2507 $worksheet1->set_header('&CCuriouser && Curiouser - Attorneys at Law');
2508
2509 As stated above the margin parameter is optional. As with the other
2510 margins the value should be in inches. The default header and footer
2511 margin is 0.50 inch. The header and footer margin size can be set as
2512 follows:
2513
2514 $worksheet->set_header('&CHello', 0.75);
2515
2516 The header and footer margins are independent of the top and bottom
2517 margins.
2518
2519 Note, the header or footer string must be less than 255 characters.
2520 Strings longer than this will not be written and a warning will be gen‐
2521 erated.
2522
2523 On systems with "perl 5.8" and later the "set_header()" method can also
2524 handle Unicode strings in Perl's "utf8" format.
2525
2526 $worksheet->set_header("&C\x{263a}")
2527
2528 See, also the "headers.pl" program in the "examples" directory of the
2529 distribution.
2530
2531 set_footer()
2532
2533 The syntax of the "set_footer()" method is the same as "set_header()",
2534 see above.
2535
2536 repeat_rows($first_row, $last_row)
2537
2538 Set the number of rows to repeat at the top of each printed page.
2539
2540 For large Excel documents it is often desirable to have the first row
2541 or rows of the worksheet print out at the top of each page. This can be
2542 achieved by using the "repeat_rows()" method. The parameters $first_row
2543 and $last_row are zero based. The $last_row parameter is optional if
2544 you only wish to specify one row:
2545
2546 $worksheet1->repeat_rows(0); # Repeat the first row
2547 $worksheet2->repeat_rows(0, 1); # Repeat the first two rows
2548
2549 repeat_columns($first_col, $last_col)
2550
2551 Set the columns to repeat at the left hand side of each printed page.
2552
2553 For large Excel documents it is often desirable to have the first col‐
2554 umn or columns of the worksheet print out at the left hand side of each
2555 page. This can be achieved by using the "repeat_columns()" method. The
2556 parameters $first_column and $last_column are zero based. The
2557 $last_column parameter is optional if you only wish to specify one col‐
2558 umn. You can also specify the columns using A1 column notation, see the
2559 note about "Cell notation".
2560
2561 $worksheet1->repeat_columns(0); # Repeat the first column
2562 $worksheet2->repeat_columns(0, 1); # Repeat the first two columns
2563 $worksheet3->repeat_columns('A:A'); # Repeat the first column
2564 $worksheet4->repeat_columns('A:B'); # Repeat the first two columns
2565
2566 hide_gridlines($option)
2567
2568 This method is used to hide the gridlines on the screen and printed
2569 page. Gridlines are the lines that divide the cells on a worksheet.
2570 Screen and printed gridlines are turned on by default in an Excel work‐
2571 sheet. If you have defined your own cell borders you may wish to hide
2572 the default gridlines.
2573
2574 $worksheet->hide_gridlines();
2575
2576 The following values of $option are valid:
2577
2578 0 : Don't hide gridlines
2579 1 : Hide printed gridlines only
2580 2 : Hide screen and printed gridlines
2581
2582 If you don't supply an argument or use "undef" the default option is 1,
2583 i.e. only the printed gridlines are hidden.
2584
2585 print_row_col_headers()
2586
2587 Set the option to print the row and column headers on the printed page.
2588
2589 An Excel worksheet looks something like the following;
2590
2591 ------------------------------------------
2592 ⎪ ⎪ A ⎪ B ⎪ C ⎪ D ⎪ ...
2593 ------------------------------------------
2594 ⎪ 1 ⎪ ⎪ ⎪ ⎪ ⎪ ...
2595 ⎪ 2 ⎪ ⎪ ⎪ ⎪ ⎪ ...
2596 ⎪ 3 ⎪ ⎪ ⎪ ⎪ ⎪ ...
2597 ⎪ 4 ⎪ ⎪ ⎪ ⎪ ⎪ ...
2598 ⎪...⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
2599
2600 The headers are the letters and numbers at the top and the left of the
2601 worksheet. Since these headers serve mainly as a indication of position
2602 on the worksheet they generally do not appear on the printed page. If
2603 you wish to have them printed you can use the "print_row_col_headers()"
2604 method :
2605
2606 $worksheet->print_row_col_headers();
2607
2608 Do not confuse these headers with page headers as described in the
2609 "set_header()" section above.
2610
2611 print_area($first_row, $first_col, $last_row, $last_col)
2612
2613 This method is used to specify the area of the worksheet that will be
2614 printed. All four parameters must be specified. You can also use A1
2615 notation, see the note about "Cell notation".
2616
2617 $worksheet1->print_area("A1:H20"); # Cells A1 to H20
2618 $worksheet2->print_area(0, 0, 19, 7); # The same
2619 $worksheet2->print_area('A:H'); # Columns A to H if rows have data
2620
2621 print_across()
2622
2623 The "print_across" method is used to change the default print direc‐
2624 tion. This is referred to by Excel as the sheet "page order".
2625
2626 $worksheet->print_across();
2627
2628 The default page order is shown below for a worksheet that extends over
2629 4 pages. The order is called "down then across":
2630
2631 [1] [3]
2632 [2] [4]
2633
2634 However, by using the "print_across" method the print order will be
2635 changed to "across then down":
2636
2637 [1] [2]
2638 [3] [4]
2639
2640 fit_to_pages($width, $height)
2641
2642 The "fit_to_pages()" method is used to fit the printed area to a spe‐
2643 cific number of pages both vertically and horizontally. If the printed
2644 area exceeds the specified number of pages it will be scaled down to
2645 fit. This guarantees that the printed area will always appear on the
2646 specified number of pages even if the page size or margins change.
2647
2648 $worksheet1->fit_to_pages(1, 1); # Fit to 1x1 pages
2649 $worksheet2->fit_to_pages(2, 1); # Fit to 2x1 pages
2650 $worksheet3->fit_to_pages(1, 2); # Fit to 1x2 pages
2651
2652 The print area can be defined using the "print_area()" method as
2653 described above.
2654
2655 A common requirement is to fit the printed output to n pages wide but
2656 have the height be as long as necessary. To achieve this set the
2657 $height to zero or leave it blank:
2658
2659 $worksheet1->fit_to_pages(1, 0); # 1 page wide and as long as necessary
2660 $worksheet2->fit_to_pages(1); # The same
2661
2662 Note that although it is valid to use both "fit_to_pages()" and
2663 "set_print_scale()" on the same worksheet only one of these options can
2664 be active at a time. The last method call made will set the active
2665 option.
2666
2667 Note that "fit_to_pages()" will override any manual page breaks that
2668 are defined in the worksheet.
2669
2670 set_print_scale($scale)
2671
2672 Set the scale factor of the printed page. Scale factors in the range
2673 "10 <= $scale <= 400" are valid:
2674
2675 $worksheet1->set_print_scale(50);
2676 $worksheet2->set_print_scale(75);
2677 $worksheet3->set_print_scale(300);
2678 $worksheet4->set_print_scale(400);
2679
2680 The default scale factor is 100. Note, "set_print_scale()" does not
2681 affect the scale of the visible page in Excel. For that you should use
2682 "set_zoom()".
2683
2684 Note also that although it is valid to use both "fit_to_pages()" and
2685 "set_print_scale()" on the same worksheet only one of these options can
2686 be active at a time. The last method call made will set the active
2687 option.
2688
2689 set_h_pagebreaks(@breaks)
2690
2691 Add horizontal page breaks to a worksheet. A page break causes all the
2692 data that follows it to be printed on the next page. Horizontal page
2693 breaks act between rows. To create a page break between rows 20 and 21
2694 you must specify the break at row 21. However in zero index notation
2695 this is actually row 20. So you can pretend for a small while that you
2696 are using 1 index notation:
2697
2698 $worksheet1->set_h_pagebreaks(20); # Break between row 20 and 21
2699
2700 The "set_h_pagebreaks()" method will accept a list of page breaks and
2701 you can call it more than once:
2702
2703 $worksheet2->set_h_pagebreaks( 20, 40, 60, 80, 100); # Add breaks
2704 $worksheet2->set_h_pagebreaks(120, 140, 160, 180, 200); # Add some more
2705
2706 Note: If you specify the "fit to page" option via the "fit_to_pages()"
2707 method it will override all manual page breaks.
2708
2709 There is a silent limitation of about 1000 horizontal page breaks per
2710 worksheet in line with an Excel internal limitation.
2711
2712 set_v_pagebreaks(@breaks)
2713
2714 Add vertical page breaks to a worksheet. A page break causes all the
2715 data that follows it to be printed on the next page. Vertical page
2716 breaks act between columns. To create a page break between columns 20
2717 and 21 you must specify the break at column 21. However in zero index
2718 notation this is actually column 20. So you can pretend for a small
2719 while that you are using 1 index notation:
2720
2721 $worksheet1->set_v_pagebreaks(20); # Break between column 20 and 21
2722
2723 The "set_v_pagebreaks()" method will accept a list of page breaks and
2724 you can call it more than once:
2725
2726 $worksheet2->set_v_pagebreaks( 20, 40, 60, 80, 100); # Add breaks
2727 $worksheet2->set_v_pagebreaks(120, 140, 160, 180, 200); # Add some more
2728
2729 Note: If you specify the "fit to page" option via the "fit_to_pages()"
2730 method it will override all manual page breaks.
2731
2733 This section describes the methods and properties that are available
2734 for formatting cells in Excel. The properties of a cell that can be
2735 formatted include: fonts, colours, patterns, borders, alignment and
2736 number formatting.
2737
2738 Creating and using a Format object
2739
2740 Cell formatting is defined through a Format object. Format objects are
2741 created by calling the workbook "add_format()" method as follows:
2742
2743 my $format1 = $workbook->add_format(); # Set properties later
2744 my $format2 = $workbook->add_format(%props); # Set at creation
2745
2746 The format object holds all the formatting properties that can be
2747 applied to a cell, a row or a column. The process of setting these
2748 properties is discussed in the next section.
2749
2750 Once a Format object has been constructed and it properties have been
2751 set it can be passed as an argument to the worksheet "write" methods as
2752 follows:
2753
2754 $worksheet->write(0, 0, "One", $format);
2755 $worksheet->write_string(1, 0, "Two", $format);
2756 $worksheet->write_number(2, 0, 3, $format);
2757 $worksheet->write_blank(3, 0, $format);
2758
2759 Formats can also be passed to the worksheet "set_row()" and "set_col‐
2760 umn()" methods to define the default property for a row or column.
2761
2762 $worksheet->set_row(0, 15, $format);
2763 $worksheet->set_column(0, 0, 15, $format);
2764
2765 Format methods and Format properties
2766
2767 The following table shows the Excel format categories, the formatting
2768 properties that can be applied and the equivalent object method:
2769
2770 Category Description Property Method Name
2771 -------- ----------- -------- -----------
2772 Font Font type font set_font()
2773 Font size size set_size()
2774 Font color color set_color()
2775 Bold bold set_bold()
2776 Italic italic set_italic()
2777 Underline underline set_underline()
2778 Strikeout font_strikeout set_font_strikeout()
2779 Super/Subscript font_script set_font_script()
2780 Outline font_outline set_font_outline()
2781 Shadow font_shadow set_font_shadow()
2782
2783 Number Numeric format num_format set_num_format()
2784
2785 Protection Lock cells locked set_locked()
2786 Hide formulas hidden set_hidden()
2787
2788 Alignment Horizontal align align set_align()
2789 Vertical align valign set_align()
2790 Rotation rotation set_rotation()
2791 Text wrap text_wrap set_text_wrap()
2792 Justify last text_justlast set_text_justlast()
2793 Center across center_across set_center_across()
2794 Indentation indent set_indent()
2795 Shrink to fit shrink set_shrink()
2796
2797 Pattern Cell pattern pattern set_pattern()
2798 Background color bg_color set_bg_color()
2799 Foreground color fg_color set_fg_color()
2800
2801 Border Cell border border set_border()
2802 Bottom border bottom set_bottom()
2803 Top border top set_top()
2804 Left border left set_left()
2805 Right border right set_right()
2806 Border color border_color set_border_color()
2807 Bottom color bottom_color set_bottom_color()
2808 Top color top_color set_top_color()
2809 Left color left_color set_left_color()
2810 Right color right_color set_right_color()
2811
2812 There are two ways of setting Format properties: by using the object
2813 method interface or by setting the property directly. For example, a
2814 typical use of the method interface would be as follows:
2815
2816 my $format = $workbook->add_format();
2817 $format->set_bold();
2818 $format->set_color('red');
2819
2820 By comparison the properties can be set directly by passing a hash of
2821 properties to the Format constructor:
2822
2823 my $format = $workbook->add_format(bold => 1, color => 'red');
2824
2825 or after the Format has been constructed by means of the "set_proper‐
2826 ties()" method as follows:
2827
2828 my $format = $workbook->add_format();
2829 $format->set_properties(bold => 1, color => 'red');
2830
2831 You can also store the properties in one or more named hashes and pass
2832 them to the required method:
2833
2834 my %font = (
2835 font => 'Arial',
2836 size => 12,
2837 color => 'blue',
2838 bold => 1,
2839 );
2840
2841 my %shading = (
2842 bg_color => 'green',
2843 pattern => 1,
2844 );
2845
2846 my $format1 = $workbook->add_format(%font); # Font only
2847 my $format2 = $workbook->add_format(%font, %shading); # Font and shading
2848
2849 The provision of two ways of setting properties might lead you to won‐
2850 der which is the best way. The answer depends on the amount of format‐
2851 ting that will be required in your program. Initially, Spread‐
2852 sheet::WriteExcel only allowed individual Format properties to be set
2853 via the appropriate method. While this was sufficient for most circum‐
2854 stances it proved very cumbersome in programs that required a large
2855 amount of formatting. In addition the mechanism for reusing properties
2856 between Format objects was complicated.
2857
2858 As a result the Perl/Tk style of adding properties was added to, hope‐
2859 fully, facilitate developers who need to define a lot of formatting. In
2860 fact the Tk style of defining properties is also supported:
2861
2862 my %font = (
2863 -font => 'Arial',
2864 -size => 12,
2865 -color => 'blue',
2866 -bold => 1,
2867 );
2868
2869 An additional advantage of working with hashes of properties is that it
2870 allows you to share formatting between workbook objects
2871
2872 You can also create a format "on the fly" and pass it directly to a
2873 write method as follows:
2874
2875 $worksheet->write('A1', "Title", $workbook->add_format(bold => 1));
2876
2877 This corresponds to an "anonymous" format in the Perl sense of anony‐
2878 mous data or subs.
2879
2880 Working with formats
2881
2882 The default format is Arial 10 with all other properties off.
2883
2884 Each unique format in Spreadsheet::WriteExcel must have a corresponding
2885 Format object. It isn't possible to use a Format with a write() method
2886 and then redefine the Format for use at a later stage. This is because
2887 a Format is applied to a cell not in its current state but in its final
2888 state. Consider the following example:
2889
2890 my $format = $workbook->add_format();
2891 $format->set_bold();
2892 $format->set_color('red');
2893 $worksheet->write('A1', "Cell A1", $format);
2894 $format->set_color('green');
2895 $worksheet->write('B1', "Cell B1", $format);
2896
2897 Cell A1 is assigned the Format $format which is initially set to the
2898 colour red. However, the colour is subsequently set to green. When
2899 Excel displays Cell A1 it will display the final state of the Format
2900 which in this case will be the colour green.
2901
2902 In general a method call without an argument will turn a property on,
2903 for example:
2904
2905 my $format1 = $workbook->add_format();
2906 $format1->set_bold(); # Turns bold on
2907 $format1->set_bold(1); # Also turns bold on
2908 $format1->set_bold(0); # Turns bold off
2909
2911 The Format object methods are described in more detail in the following
2912 sections. In addition, there is a Perl program called "formats.pl" in
2913 the "examples" directory of the WriteExcel distribution. This program
2914 creates an Excel workbook called "formats.xls" which contains examples
2915 of almost all the format types.
2916
2917 The following Format methods are available:
2918
2919 set_font()
2920 set_size()
2921 set_color()
2922 set_bold()
2923 set_italic()
2924 set_underline()
2925 set_font_strikeout()
2926 set_font_script()
2927 set_font_outline()
2928 set_font_shadow()
2929 set_num_format()
2930 set_locked()
2931 set_hidden()
2932 set_align()
2933 set_align()
2934 set_rotation()
2935 set_text_wrap()
2936 set_text_justlast()
2937 set_center_across()
2938 set_indent()
2939 set_shrink()
2940 set_pattern()
2941 set_bg_color()
2942 set_fg_color()
2943 set_border()
2944 set_bottom()
2945 set_top()
2946 set_left()
2947 set_right()
2948 set_border_color()
2949 set_bottom_color()
2950 set_top_color()
2951 set_left_color()
2952 set_right_color()
2953
2954 The above methods can also be applied directly as properties. For exam‐
2955 ple "$worksheet->set_bold()" is equivalent to "set_properties(bold =>
2956 1)".
2957
2958 set_properties(%properties)
2959
2960 The properties of an existing Format object can be set by means of
2961 "set_properties()":
2962
2963 my $format = $workbook->add_format();
2964 $format->set_properties(bold => 1, color => 'red');
2965
2966 You can also store the properties in one or more named hashes and pass
2967 them to the "set_properties()" method:
2968
2969 my %font = (
2970 font => 'Arial',
2971 size => 12,
2972 color => 'blue',
2973 bold => 1,
2974 );
2975
2976 my $format = $workbook->set_properties(%font);
2977
2978 This method can be used as an alternative to setting the properties
2979 with "add_format()" or the specific format methods that are detailed in
2980 the following sections.
2981
2982 set_font($fontname)
2983
2984 Default state: Font is Arial
2985 Default action: None
2986 Valid args: Any valid font name
2987
2988 Specify the font used:
2989
2990 $format->set_font('Times New Roman');
2991
2992 Excel can only display fonts that are installed on the system that it
2993 is running on. Therefore it is best to use the fonts that come as stan‐
2994 dard such as 'Arial', 'Times New Roman' and 'Courier New'. See also the
2995 Fonts worksheet created by formats.pl
2996
2997 set_size()
2998
2999 Default state: Font size is 10
3000 Default action: Set font size to 1
3001 Valid args: Integer values from 1 to as big as your screen.
3002
3003 Set the font size. Excel adjusts the height of a row to accommodate the
3004 largest font size in the row. You can also explicitly specify the
3005 height of a row using the set_row() worksheet method.
3006
3007 my $format = $workbook->add_format();
3008 $format->set_size(30);
3009
3010 set_color()
3011
3012 Default state: Excels default color, usually black
3013 Default action: Set the default color
3014 Valid args: Integers from 8..63 or the following strings:
3015 'black'
3016 'blue'
3017 'brown'
3018 'cyan'
3019 'gray'
3020 'green'
3021 'lime'
3022 'magenta'
3023 'navy'
3024 'orange'
3025 'pink'
3026 'purple'
3027 'red'
3028 'silver'
3029 'white'
3030 'yellow'
3031
3032 Set the font colour. The "set_color()" method is used as follows:
3033
3034 my $format = $workbook->add_format();
3035 $format->set_color('red');
3036 $worksheet->write(0, 0, "wheelbarrow", $format);
3037
3038 Note: The "set_color()" method is used to set the colour of the font in
3039 a cell. To set the colour of a cell use the "set_bg_color()" and
3040 "set_pattern()" methods.
3041
3042 For additional examples see the 'Named colors' and 'Standard colors'
3043 worksheets created by formats.pl in the examples directory.
3044
3045 See also "COLOURS IN EXCEL".
3046
3047 set_bold()
3048
3049 Default state: bold is off
3050 Default action: Turn bold on
3051 Valid args: 0, 1 [1]
3052
3053 Set the bold property of the font:
3054
3055 $format->set_bold(); # Turn bold on
3056
3057 [1] Actually, values in the range 100..1000 are also valid. 400 is nor‐
3058 mal, 700 is bold and 1000 is very bold indeed. It is probably best to
3059 set the value to 1 and use normal bold.
3060
3061 set_italic()
3062
3063 Default state: Italic is off
3064 Default action: Turn italic on
3065 Valid args: 0, 1
3066
3067 Set the italic property of the font:
3068
3069 $format->set_italic(); # Turn italic on
3070
3071 set_underline()
3072
3073 Default state: Underline is off
3074 Default action: Turn on single underline
3075 Valid args: 0 = No underline
3076 1 = Single underline
3077 2 = Double underline
3078 33 = Single accounting underline
3079 34 = Double accounting underline
3080
3081 Set the underline property of the font.
3082
3083 $format->set_underline(); # Single underline
3084
3085 set_font_strikeout()
3086
3087 Default state: Strikeout is off
3088 Default action: Turn strikeout on
3089 Valid args: 0, 1
3090
3091 Set the strikeout property of the font.
3092
3093 set_font_script()
3094
3095 Default state: Super/Subscript is off
3096 Default action: Turn Superscript on
3097 Valid args: 0 = Normal
3098 1 = Superscript
3099 2 = Subscript
3100
3101 Set the superscript/subscript property of the font. This format is cur‐
3102 rently not very useful.
3103
3104 set_font_outline()
3105
3106 Default state: Outline is off
3107 Default action: Turn outline on
3108 Valid args: 0, 1
3109
3110 Macintosh only.
3111
3112 set_font_shadow()
3113
3114 Default state: Shadow is off
3115 Default action: Turn shadow on
3116 Valid args: 0, 1
3117
3118 Macintosh only.
3119
3120 set_num_format()
3121
3122 Default state: General format
3123 Default action: Format index 1
3124 Valid args: See the following table
3125
3126 This method is used to define the numerical format of a number in
3127 Excel. It controls whether a number is displayed as an integer, a
3128 floating point number, a date, a currency value or some other user
3129 defined format.
3130
3131 The numerical format of a cell can be specified by using a format
3132 string or an index to one of Excel's built-in formats:
3133
3134 my $format1 = $workbook->add_format();
3135 my $format2 = $workbook->add_format();
3136 $format1->set_num_format('d mmm yyyy'); # Format string
3137 $format2->set_num_format(0x0f); # Format index
3138
3139 $worksheet->write(0, 0, 36892.521, $format1); # 1 Jan 2001
3140 $worksheet->write(0, 0, 36892.521, $format2); # 1-Jan-01
3141
3142 Using format strings you can define very sophisticated formatting of
3143 numbers.
3144
3145 $format01->set_num_format('0.000');
3146 $worksheet->write(0, 0, 3.1415926, $format01); # 3.142
3147
3148 $format02->set_num_format('#,##0');
3149 $worksheet->write(1, 0, 1234.56, $format02); # 1,235
3150
3151 $format03->set_num_format('#,##0.00');
3152 $worksheet->write(2, 0, 1234.56, $format03); # 1,234.56
3153
3154 $format04->set_num_format('$0.00');
3155 $worksheet->write(3, 0, 49.99, $format04); # $49.99
3156
3157 $format05->set_num_format('£0.00');
3158 $worksheet->write(4, 0, 49.99, $format05); # £49.99
3159
3160 $format06->set_num_format('¥0.00');
3161 $worksheet->write(5, 0, 49.99, $format06); # ¥49.99
3162
3163 $format07->set_num_format('mm/dd/yy');
3164 $worksheet->write(6, 0, 36892.521, $format07); # 01/01/01
3165
3166 $format08->set_num_format('mmm d yyyy');
3167 $worksheet->write(7, 0, 36892.521, $format08); # Jan 1 2001
3168
3169 $format09->set_num_format('d mmmm yyyy');
3170 $worksheet->write(8, 0, 36892.521, $format09); # 1 January 2001
3171
3172 $format10->set_num_format('dd/mm/yyyy hh:mm AM/PM');
3173 $worksheet->write(9, 0, 36892.521, $format10); # 01/01/2001 12:30 AM
3174
3175 $format11->set_num_format('0 "dollar and" .00 "cents"');
3176 $worksheet->write(10, 0, 1.87, $format11); # 1 dollar and .87 cents
3177
3178 # Conditional formatting
3179 $format12->set_num_format('[Green]General;[Red]-General;General');
3180 $worksheet->write(11, 0, 123, $format12); # > 0 Green
3181 $worksheet->write(12, 0, -45, $format12); # < 0 Red
3182 $worksheet->write(13, 0, 0, $format12); # = 0 Default colour
3183
3184 # Zip code
3185 $format13->set_num_format('00000');
3186 $worksheet->write(14, 0, '01209', $format13);
3187
3188 The number system used for dates is described in "DATES IN EXCEL".
3189
3190 The colour format should have one of the following values:
3191
3192 [Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow]
3193
3194 Alternatively you can specify the colour based on a colour index as
3195 follows: "[Color n]", where n is a standard Excel colour index - 7. See
3196 the 'Standard colors' worksheet created by formats.pl.
3197
3198 For more information refer to the documentation on formatting in the
3199 "doc" directory of the Spreadsheet::WriteExcel distro, the Excel on-
3200 line help or http://office.microsoft.com/en-gb/assis‐
3201 tance/HP051995001033.aspx
3202
3203 You should ensure that the format string is valid in Excel prior to
3204 using it in WriteExcel.
3205
3206 Excel's built-in formats are shown in the following table:
3207
3208 Index Index Format String
3209 0 0x00 General
3210 1 0x01 0
3211 2 0x02 0.00
3212 3 0x03 #,##0
3213 4 0x04 #,##0.00
3214 5 0x05 ($#,##0_);($#,##0)
3215 6 0x06 ($#,##0_);[Red]($#,##0)
3216 7 0x07 ($#,##0.00_);($#,##0.00)
3217 8 0x08 ($#,##0.00_);[Red]($#,##0.00)
3218 9 0x09 0%
3219 10 0x0a 0.00%
3220 11 0x0b 0.00E+00
3221 12 0x0c # ?/?
3222 13 0x0d # ??/??
3223 14 0x0e m/d/yy
3224 15 0x0f d-mmm-yy
3225 16 0x10 d-mmm
3226 17 0x11 mmm-yy
3227 18 0x12 h:mm AM/PM
3228 19 0x13 h:mm:ss AM/PM
3229 20 0x14 h:mm
3230 21 0x15 h:mm:ss
3231 22 0x16 m/d/yy h:mm
3232 .. .... ...........
3233 37 0x25 (#,##0_);(#,##0)
3234 38 0x26 (#,##0_);[Red](#,##0)
3235 39 0x27 (#,##0.00_);(#,##0.00)
3236 40 0x28 (#,##0.00_);[Red](#,##0.00)
3237 41 0x29 _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
3238 42 0x2a _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)
3239 43 0x2b _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)
3240 44 0x2c _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)
3241 45 0x2d mm:ss
3242 46 0x2e [h]:mm:ss
3243 47 0x2f mm:ss.0
3244 48 0x30 ##0.0E+0
3245 49 0x31 @
3246
3247 For examples of these formatting codes see the 'Numerical formats'
3248 worksheet created by formats.pl. See also the number_formats1.html and
3249 the number_formats2.html documents in the "doc" directory of the dis‐
3250 tro.
3251
3252 Note 1. Numeric formats 23 to 36 are not documented by Microsoft and
3253 may differ in international versions.
3254
3255 Note 2. In Excel 5 the dollar sign appears as a dollar sign. In Excel
3256 97-2000 it appears as the defined local currency symbol.
3257
3258 Note 3. The red negative numeric formats display slightly differently
3259 in Excel 5 and Excel 97-2000.
3260
3261 set_locked()
3262
3263 Default state: Cell locking is on
3264 Default action: Turn locking on
3265 Valid args: 0, 1
3266
3267 This property can be used to prevent modification of a cells contents.
3268 Following Excel's convention, cell locking is turned on by default.
3269 However, it only has an effect if the worksheet has been protected, see
3270 the worksheet "protect()" method.
3271
3272 my $locked = $workbook->add_format();
3273 $locked->set_locked(1); # A non-op
3274
3275 my $unlocked = $workbook->add_format();
3276 $locked->set_locked(0);
3277
3278 # Enable worksheet protection
3279 $worksheet->protect();
3280
3281 # This cell cannot be edited.
3282 $worksheet->write('A1', '=1+2', $locked);
3283
3284 # This cell can be edited.
3285 $worksheet->write('A2', '=1+2', $unlocked);
3286
3287 Note: This offers weak protection even with a password, see the note in
3288 relation to the "protect()" method.
3289
3290 set_hidden()
3291
3292 Default state: Formula hiding is off
3293 Default action: Turn hiding on
3294 Valid args: 0, 1
3295
3296 This property is used to hide a formula while still displaying its
3297 result. This is generally used to hide complex calculations from end
3298 users who are only interested in the result. It only has an effect if
3299 the worksheet has been protected, see the worksheet "protect()" method.
3300
3301 my $hidden = $workbook->add_format();
3302 $hidden->set_hidden();
3303
3304 # Enable worksheet protection
3305 $worksheet->protect();
3306
3307 # The formula in this cell isn't visible
3308 $worksheet->write('A1', '=1+2', $hidden);
3309
3310 Note: This offers weak protection even with a password, see the note in
3311 relation to the "protect()" method.
3312
3313 set_align()
3314
3315 Default state: Alignment is off
3316 Default action: Left alignment
3317 Valid args: 'left' Horizontal
3318 'center'
3319 'right'
3320 'fill'
3321 'justify'
3322 'center_across'
3323
3324 'top' Vertical
3325 'vcenter'
3326 'bottom'
3327 'vjustify'
3328
3329 This method is used to set the horizontal and vertical text alignment
3330 within a cell. Vertical and horizontal alignments can be combined. The
3331 method is used as follows:
3332
3333 my $format = $workbook->add_format();
3334 $format->set_align('center');
3335 $format->set_align('vcenter');
3336 $worksheet->set_row(0, 30);
3337 $worksheet->write(0, 0, "X", $format);
3338
3339 Text can be aligned across two or more adjacent cells using the "cen‐
3340 ter_across" property. However, for genuine merged cells it is better to
3341 use the "merge_range()" worksheet method.
3342
3343 The "vjustify" (vertical justify) option can be used to provide auto‐
3344 matic text wrapping in a cell. The height of the cell will be adjusted
3345 to accommodate the wrapped text. To specify where the text wraps use
3346 the "set_text_wrap()" method.
3347
3348 For further examples see the 'Alignment' worksheet created by for‐
3349 mats.pl.
3350
3351 set_center_across()
3352
3353 Default state: Center across selection is off
3354 Default action: Turn center across on
3355 Valid args: 1
3356
3357 Text can be aligned across two or more adjacent cells using the
3358 "set_center_across()" method. This is an alias for the "set_align('cen‐
3359 ter_across')" method call.
3360
3361 Only one cell should contain the text, the other cells should be blank:
3362
3363 my $format = $workbook->add_format();
3364 $format->set_center_across();
3365
3366 $worksheet->write(1, 1, 'Center across selection', $format);
3367 $worksheet->write_blank(1, 2, $format);
3368
3369 See also the "merge1.pl" to "merge6.pl" programs in the "examples"
3370 directory and the "merge_range()" method.
3371
3372 set_text_wrap()
3373
3374 Default state: Text wrap is off
3375 Default action: Turn text wrap on
3376 Valid args: 0, 1
3377
3378 Here is an example using the text wrap property, the escape character
3379 "\n" is used to indicate the end of line:
3380
3381 my $format = $workbook->add_format();
3382 $format->set_text_wrap();
3383 $worksheet->write(0, 0, "It's\na bum\nwrap", $format);
3384
3385 Excel will adjust the height of the row to accommodate the wrapped
3386 text. A similar effect can be obtained without newlines using the
3387 "set_align('vjustify')" method. See the "textwrap.pl" program in the
3388 "examples" directory.
3389
3390 set_rotation()
3391
3392 Default state: Text rotation is off
3393 Default action: None
3394 Valid args: Integers in the range -90 to 90 and 270
3395
3396 Set the rotation of the text in a cell. The rotation can be any angle
3397 in the range -90 to 90 degrees.
3398
3399 my $format = $workbook->add_format();
3400 $format->set_rotation(30);
3401 $worksheet->write(0, 0, "This text is rotated", $format);
3402
3403 The angle 270 is also supported. This indicates text where the letters
3404 run from top to bottom.
3405
3406 set_indent()
3407
3408 Default state: Text indentation is off
3409 Default action: Indent text 1 level
3410 Valid args: Positive integers
3411
3412 This method can be used to indent text. The argument, which should be
3413 an integer, is taken as the level of indentation:
3414
3415 my $format = $workbook->add_format();
3416 $format->set_indent(2);
3417 $worksheet->write(0, 0, "This text is indented", $format);
3418
3419 Indentation is a horizontal alignment property. It will override any
3420 other horizontal properties but it can be used in conjunction with ver‐
3421 tical properties.
3422
3423 set_shrink()
3424
3425 Default state: Text shrinking is off
3426 Default action: Turn "shrink to fit" on
3427 Valid args: 1
3428
3429 This method can be used to shrink text so that it fits in a cell.
3430
3431 my $format = $workbook->add_format();
3432 $format->set_shrink();
3433 $worksheet->write(0, 0, "Honey, I shrunk the text!", $format);
3434
3435 set_text_justlast()
3436
3437 Default state: Justify last is off
3438 Default action: Turn justify last on
3439 Valid args: 0, 1
3440
3441 Only applies to Far Eastern versions of Excel.
3442
3443 set_pattern()
3444
3445 Default state: Pattern is off
3446 Default action: Solid fill is on
3447 Valid args: 0 .. 18
3448
3449 Set the background pattern of a cell.
3450
3451 Examples of the available patterns are shown in the 'Patterns' work‐
3452 sheet created by formats.pl. However, it is unlikely that you will ever
3453 need anything other than Pattern 1 which is a solid fill of the back‐
3454 ground color.
3455
3456 set_bg_color()
3457
3458 Default state: Color is off
3459 Default action: Solid fill.
3460 Valid args: See set_color()
3461
3462 The "set_bg_color()" method can be used to set the background colour of
3463 a pattern. Patterns are defined via the "set_pattern()" method. If a
3464 pattern hasn't been defined then a solid fill pattern is used as the
3465 default.
3466
3467 Here is an example of how to set up a solid fill in a cell:
3468
3469 my $format = $workbook->add_format();
3470
3471 $format->set_pattern(); # This is optional when using a solid fill
3472
3473 $format->set_bg_color('green');
3474 $worksheet->write('A1', 'Ray', $format);
3475
3476 For further examples see the 'Patterns' worksheet created by for‐
3477 mats.pl.
3478
3479 set_fg_color()
3480
3481 Default state: Color is off
3482 Default action: Solid fill.
3483 Valid args: See set_color()
3484
3485 The "set_fg_color()" method can be used to set the foreground colour of
3486 a pattern.
3487
3488 For further examples see the 'Patterns' worksheet created by for‐
3489 mats.pl.
3490
3491 set_border()
3492
3493 Also applies to: set_bottom()
3494 set_top()
3495 set_left()
3496 set_right()
3497
3498 Default state: Border is off
3499 Default action: Set border type 1
3500 Valid args: 0 No border
3501 1 Thin single border
3502 2 Medium single border
3503 3 Dashed border
3504 4 Dotted border
3505 5 Thick single border
3506 6 Double line border
3507 7 Hair border
3508
3509 A cell border is comprised of a border on the bottom, top, left and
3510 right. These can be set to the same value using "set_border()" or indi‐
3511 vidually using the relevant method calls shown above. Examples of the
3512 available border styles are shown in the 'Borders' worksheet created by
3513 formats.pl.
3514
3515 set_border_color()
3516
3517 Also applies to: set_bottom_color()
3518 set_top_color()
3519 set_left_color()
3520 set_right_color()
3521
3522 Default state: Color is off
3523 Default action: Undefined
3524 Valid args: See set_color()
3525
3526 Set the colour of the cell borders. A cell border is comprised of a
3527 border on the bottom, top, left and right. These can be set to the same
3528 colour using "set_border_color()" or individually using the relevant
3529 method calls shown above. Examples of the border styles and colours are
3530 shown in the 'Borders' worksheet created by formats.pl.
3531
3532 copy($format)
3533
3534 This method is used to copy all of the properties from one Format
3535 object to another:
3536
3537 my $lorry1 = $workbook->add_format();
3538 $lorry1->set_bold();
3539 $lorry1->set_italic();
3540 $lorry1->set_color('red'); # lorry1 is bold, italic and red
3541
3542 my $lorry2 = $workbook->add_format();
3543 $lorry2->copy($lorry1);
3544 $lorry2->set_color('yellow'); # lorry2 is bold, italic and yellow
3545
3546 The "copy()" method is only useful if you are using the method inter‐
3547 face to Format properties. It generally isn't required if you are set‐
3548 ting Format properties directly using hashes.
3549
3550 Note: this is not a copy constructor, both objects must exist prior to
3551 copying.
3552
3554 Excel provides a colour palette of 56 colours. In Spreadsheet::WriteEx‐
3555 cel these colours are accessed via their palette index in the range
3556 8..63. This index is used to set the colour of fonts, cell patterns and
3557 cell borders. For example:
3558
3559 my $format = $workbook->add_format(
3560 color => 12, # index for blue
3561 font => 'Arial',
3562 size => 12,
3563 bold => 1,
3564 );
3565
3566 The most commonly used colours can also be accessed by name. The name
3567 acts as a simple alias for the colour index:
3568
3569 black => 8
3570 blue => 12
3571 brown => 16
3572 cyan => 15
3573 gray => 23
3574 green => 17
3575 lime => 11
3576 magenta => 14
3577 navy => 18
3578 orange => 53
3579 pink => 33
3580 purple => 20
3581 red => 10
3582 silver => 22
3583 white => 9
3584 yellow => 13
3585
3586 For example:
3587
3588 my $font = $workbook->add_format(color => 'red');
3589
3590 Users of VBA in Excel should note that the equivalent colour indices
3591 are in the range 1..56 instead of 8..63.
3592
3593 If the default palette does not provide a required colour you can over‐
3594 ride one of the built-in values. This is achieved by using the
3595 "set_custom_color()" workbook method to adjust the RGB (red green blue)
3596 components of the colour:
3597
3598 my $ferrari = $workbook->set_custom_color(40, 216, 12, 12);
3599
3600 my $format = $workbook->add_format(
3601 bg_color => $ferrari,
3602 pattern => 1,
3603 border => 1
3604 );
3605
3606 $worksheet->write_blank('A1', $format);
3607
3608 The default Excel 97 colour palette is shown in "palette.html" in the
3609 "doc" directory of the distro. You can generate an Excel version of
3610 the palette using "colors.pl" in the "examples" directory.
3611
3612 A comparison of the colour components in the Excel 5 and Excel 97+
3613 colour palettes is shown in "rgb5-97.txt" in the "doc" directory.
3614
3615 You may also find the following links helpful:
3616
3617 A detailed look at Excel's colour palette:
3618 http://www.mvps.org/dmcritchie/excel/colors.htm
3619
3620 A decimal RGB chart: http://www.hypersolutions.org/pages/rgbdec.html
3621
3622 A hex RGB chart: : http://www.hypersolutions.org/pages/rgbhex.html
3623
3625 Dates and times in Excel are represented by real numbers, for example
3626 "Jan 1 2001 12:30 AM" is represented by the number 36892.521.
3627
3628 The integer part of the number stores the number of days since the
3629 epoch and the fractional part stores the percentage of the day.
3630
3631 The epoch can be either 1900 or 1904. Excel for Windows uses 1900 and
3632 Excel for Macintosh uses 1904. The epochs are:
3633
3634 1900: 0 January 1900 i.e. 31 December 1899
3635 1904: 1 January 1904
3636
3637 By default Spreadsheet::WriteExcel uses the Windows/1900 format
3638 although it generally isn't an issue since Excel on Windows and the
3639 Macintosh will convert automatically between one system and the other.
3640 To use the 1904 epoch you must use the "set_1904()" workbook method.
3641
3642 There are two things to note about the 1900 date format. The first is
3643 that the epoch starts on 0 January 1900. The second is that the year
3644 1900 is erroneously but deliberately treated as a leap year. Therefore
3645 you must add an extra day to dates after 28 February 1900. The reason
3646 for this anomaly is explained in Microsoft Knowledge Base article
3647 Q181370.
3648
3649 A date or time in Excel is like any other number. To display the number
3650 as a date you must apply a number format to it. Refer to the
3651 "set_num_format()" method above:
3652
3653 $format->set_num_format('mmm d yyyy hh:mm AM/PM');
3654 $worksheet->write('A1', 36892.521 , $format); # Jan 1 2001 12:30 AM
3655
3656 You can also use the "write_date_time()" worksheet method to write
3657 dates in ISO8601 date format.
3658
3659 $worksheet->write_date_time('A2', '2001-01-01T12:20', format);
3660
3661 See the "write_date_time()" section of the documentation for more
3662 details.
3663
3664 See also the "Spreadsheet::WriteExcel::Utility" module that is included
3665 in the distro and which includes date handling functions and the Date‐
3666 Time::Format::Excel module, http://search.cpan.org/search?dist=Date‐
3667 Time-Format-Excel which is part of the DateTime project and which deals
3668 specifically with converting dates and times to and from Excel's for‐
3669 mat.
3670
3672 Excel allows you to group rows or columns so that they can be hidden or
3673 displayed with a single mouse click. This feature is referred to as
3674 outlines.
3675
3676 Outlines can reduce complex data down to a few salient sub-totals or
3677 summaries.
3678
3679 This feature is best viewed in Excel but the following is an ASCII rep‐
3680 resentation of what a worksheet with three outlines might look like.
3681 Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
3682 level 1. The lines at the left hand side are called outline level bars.
3683
3684 ------------------------------------------
3685 1 2 3 ⎪ ⎪ A ⎪ B ⎪ C ⎪ D ⎪ ...
3686 ------------------------------------------
3687 _ ⎪ 1 ⎪ A ⎪ ⎪ ⎪ ⎪ ...
3688 ⎪ _ ⎪ 2 ⎪ B ⎪ ⎪ ⎪ ⎪ ...
3689 ⎪ ⎪ ⎪ 3 ⎪ (C) ⎪ ⎪ ⎪ ⎪ ...
3690 ⎪ ⎪ ⎪ 4 ⎪ (D) ⎪ ⎪ ⎪ ⎪ ...
3691 ⎪ - ⎪ 5 ⎪ E ⎪ ⎪ ⎪ ⎪ ...
3692 ⎪ _ ⎪ 6 ⎪ F ⎪ ⎪ ⎪ ⎪ ...
3693 ⎪ ⎪ ⎪ 7 ⎪ (G) ⎪ ⎪ ⎪ ⎪ ...
3694 ⎪ ⎪ ⎪ 8 ⎪ (H) ⎪ ⎪ ⎪ ⎪ ...
3695 ⎪ - ⎪ 9 ⎪ I ⎪ ⎪ ⎪ ⎪ ...
3696 - ⎪ . ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
3697
3698 Clicking the minus sign on each of the level 2 outlines will collapse
3699 and hide the data as shown in the next figure. The minus sign changes
3700 to a plus sign to indicate that the data in the outline is hidden.
3701
3702 ------------------------------------------
3703 1 2 3 ⎪ ⎪ A ⎪ B ⎪ C ⎪ D ⎪ ...
3704 ------------------------------------------
3705 _ ⎪ 1 ⎪ A ⎪ ⎪ ⎪ ⎪ ...
3706 ⎪ ⎪ 2 ⎪ B ⎪ ⎪ ⎪ ⎪ ...
3707 ⎪ + ⎪ 5 ⎪ E ⎪ ⎪ ⎪ ⎪ ...
3708 ⎪ ⎪ 6 ⎪ F ⎪ ⎪ ⎪ ⎪ ...
3709 ⎪ + ⎪ 9 ⎪ I ⎪ ⎪ ⎪ ⎪ ...
3710 - ⎪ . ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
3711
3712 Clicking on the minus sign on the level 1 outline will collapse the
3713 remaining rows as follows:
3714
3715 ------------------------------------------
3716 1 2 3 ⎪ ⎪ A ⎪ B ⎪ C ⎪ D ⎪ ...
3717 ------------------------------------------
3718 ⎪ 1 ⎪ A ⎪ ⎪ ⎪ ⎪ ...
3719 + ⎪ . ⎪ ... ⎪ ... ⎪ ... ⎪ ... ⎪ ...
3720
3721 Grouping in "Spreadsheet::WriteExcel" is achieved by setting the out‐
3722 line level via the "set_row()" and "set_column()" worksheet methods:
3723
3724 set_row($row, $height, $format, $hidden, $level)
3725 set_column($first_col, $last_col, $width, $format, $hidden, $level)
3726
3727 The following example sets an outline level of 1 for rows 1 and 2
3728 (zero-indexed) and columns B to G. The parameters $height and $XF are
3729 assigned default values since they are undefined:
3730
3731 $worksheet->set_row(1, undef, undef, 0, 1);
3732 $worksheet->set_row(2, undef, undef, 0, 1);
3733 $worksheet->set_column('B:G', undef, undef, 0, 1);
3734
3735 Excel allows up to 7 outline levels. Therefore the $level parameter
3736 should be in the range "0 <= $level <= 7".
3737
3738 Rows and columns can be collapsed by setting the $hidden flag:
3739
3740 $worksheet->set_row(1, undef, undef, 1, 1);
3741 $worksheet->set_row(2, undef, undef, 1, 1);
3742 $worksheet->set_column('B:G', undef, undef, 1, 1);
3743
3744 For a more complete example see the "outline.pl" program in the exam‐
3745 ples directory of the distro.
3746
3747 Some additional outline properties can be set via the "outline_set‐
3748 tings()" worksheet method, see above.
3749
3751 Caveats
3752
3753 The first thing to note is that there are still some outstanding issues
3754 with the implementation of formulas and functions:
3755
3756 1. Writing a formula is much slower than writing the equivalent string.
3757 2. You cannot use array constants, i.e. {1;2;3}, in functions.
3758 3. Unary minus isn't supported.
3759 4. Whitespace is not preserved around operators.
3760 5. Named ranges are not supported.
3761 6. Array formulas are not supported.
3762
3763 However, these constraints will be removed in future versions. They are
3764 here because of a trade-off between features and time. Also, it is pos‐
3765 sible to work around issue 1 using the "store_formula()" and
3766 "repeat_formula()" methods as described later in this section.
3767
3768 Introduction
3769
3770 The following is a brief introduction to formulas and functions in
3771 Excel and Spreadsheet::WriteExcel.
3772
3773 A formula is a string that begins with an equals sign:
3774
3775 '=A1+B1'
3776 '=AVERAGE(1, 2, 3)'
3777
3778 The formula can contain numbers, strings, boolean values, cell refer‐
3779 ences, cell ranges and functions. Named ranges are not supported. For‐
3780 mulas should be written as they appear in Excel, that is cells and
3781 functions must be in uppercase.
3782
3783 Cells in Excel are referenced using the A1 notation system where the
3784 column is designated by a letter and the row by a number. Columns range
3785 from A to IV i.e. 0 to 255, rows range from 1 to 65536. The "Spread‐
3786 sheet::WriteExcel::Utility" module that is included in the distro con‐
3787 tains helper functions for dealing with A1 notation, for example:
3788
3789 use Spreadsheet::WriteExcel::Utility;
3790
3791 ($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
3792 $str = xl_rowcol_to_cell(1, 2); # C2
3793
3794 The Excel "$" notation in cell references is also supported. This
3795 allows you to specify whether a row or column is relative or absolute.
3796 This only has an effect if the cell is copied. The following examples
3797 show relative and absolute values.
3798
3799 '=A1' # Column and row are relative
3800 '=$A1' # Column is absolute and row is relative
3801 '=A$1' # Column is relative and row is absolute
3802 '=$A$1' # Column and row are absolute
3803
3804 Formulas can also refer to cells in other worksheets of the current
3805 workbook. For example:
3806
3807 '=Sheet2!A1'
3808 '=Sheet2!A1:A5'
3809 '=Sheet2:Sheet3!A1'
3810 '=Sheet2:Sheet3!A1:A5'
3811 q{='Test Data'!A1}
3812 q{='Test Data1:Test Data2'!A1}
3813
3814 The sheet reference and the cell reference are separated by "!" the
3815 exclamation mark symbol. If worksheet names contain spaces, commas o
3816 parentheses then Excel requires that the name is enclosed in single
3817 quotes as shown in the last two examples above. In order to avoid using
3818 a lot of escape characters you can use the quote operator "q{}" to pro‐
3819 tect the quotes. See "perlop" in the main Perl documentation. Only
3820 valid sheet names that have been added using the "add_worksheet()"
3821 method can be used in formulas. You cannot reference external work‐
3822 books.
3823
3824 The following table lists the operators that are available in Excel's
3825 formulas. The majority of the operators are the same as Perl's, differ‐
3826 ences are indicated:
3827
3828 Arithmetic operators:
3829 =====================
3830 Operator Meaning Example
3831 + Addition 1+2
3832 - Subtraction 2-1
3833 * Multiplication 2*3
3834 / Division 1/4
3835 ^ Exponentiation 2^3 # Equivalent to **
3836 - Unary minus -(1+2) # Not yet supported
3837 % Percent (Not modulus) 13% # Not supported, [1]
3838
3839 Comparison operators:
3840 =====================
3841 Operator Meaning Example
3842 = Equal to A1 = B1 # Equivalent to ==
3843 <> Not equal to A1 <> B1 # Equivalent to !=
3844 > Greater than A1 > B1
3845 < Less than A1 < B1
3846 >= Greater than or equal to A1 >= B1
3847 <= Less than or equal to A1 <= B1
3848
3849 String operator:
3850 ================
3851 Operator Meaning Example
3852 & Concatenation "Hello " & "World!" # [2]
3853
3854 Reference operators:
3855 ====================
3856 Operator Meaning Example
3857 : Range operator A1:A4 # [3]
3858 , Union operator SUM(1, 2+2, B3) # [4]
3859
3860 Notes:
3861 [1]: You can get a percentage with formatting and modulus with MOD().
3862 [2]: Equivalent to ("Hello " . "World!") in Perl.
3863 [3]: This range is equivalent to cells A1, A2, A3 and A4.
3864 [4]: The comma behaves like the list separator in Perl.
3865
3866 The range and comma operators can have different symbols in non-English
3867 versions of Excel. These will be supported in a later version of
3868 Spreadsheet::WriteExcel. European users of Excel take note:
3869
3870 $worksheet->write('A1', '=SUM(1; 2; 3)'); # Wrong!!
3871 $worksheet->write('A1', '=SUM(1, 2, 3)'); # Okay
3872
3873 The following table lists all of the core functions supported by Excel
3874 5 and Spreadsheet::WriteExcel. Any additional functions that are avail‐
3875 able through the "Analysis ToolPak" or other add-ins are not supported.
3876 These functions have all been tested to verify that they work.
3877
3878 ABS DB INDIRECT NORMINV SLN
3879 ACOS DCOUNT INFO NORMSDIST SLOPE
3880 ACOSH DCOUNTA INT NORMSINV SMALL
3881 ADDRESS DDB INTERCEPT NOT SQRT
3882 AND DEGREES IPMT NOW STANDARDIZE
3883 AREAS DEVSQ IRR NPER STDEV
3884 ASIN DGET ISBLANK NPV STDEVP
3885 ASINH DMAX ISERR ODD STEYX
3886 ATAN DMIN ISERROR OFFSET SUBSTITUTE
3887 ATAN2 DOLLAR ISLOGICAL OR SUBTOTAL
3888 ATANH DPRODUCT ISNA PEARSON SUM
3889 AVEDEV DSTDEV ISNONTEXT PERCENTILE SUMIF
3890 AVERAGE DSTDEVP ISNUMBER PERCENTRANK SUMPRODUCT
3891 BETADIST DSUM ISREF PERMUT SUMSQ
3892 BETAINV DVAR ISTEXT PI SUMX2MY2
3893 BINOMDIST DVARP KURT PMT SUMX2PY2
3894 CALL ERROR.TYPE LARGE POISSON SUMXMY2
3895 CEILING EVEN LEFT POWER SYD
3896 CELL EXACT LEN PPMT T
3897 CHAR EXP LINEST PROB TAN
3898 CHIDIST EXPONDIST LN PRODUCT TANH
3899 CHIINV FACT LOG PROPER TDIST
3900 CHITEST FALSE LOG10 PV TEXT
3901 CHOOSE FDIST LOGEST QUARTILE TIME
3902 CLEAN FIND LOGINV RADIANS TIMEVALUE
3903 CODE FINV LOGNORMDIST RAND TINV
3904 COLUMN FISHER LOOKUP RANK TODAY
3905 COLUMNS FISHERINV LOWER RATE TRANSPOSE
3906 COMBIN FIXED MATCH REGISTER.ID TREND
3907 CONCATENATE FLOOR MAX REPLACE TRIM
3908 CONFIDENCE FORECAST MDETERM REPT TRIMMEAN
3909 CORREL FREQUENCY MEDIAN RIGHT TRUE
3910 COS FTEST MID ROMAN TRUNC
3911 COSH FV MIN ROUND TTEST
3912 COUNT GAMMADIST MINUTE ROUNDDOWN TYPE
3913 COUNTA GAMMAINV MINVERSE ROUNDUP UPPER
3914 COUNTBLANK GAMMALN MIRR ROW VALUE
3915 COUNTIF GEOMEAN MMULT ROWS VAR
3916 COVAR GROWTH MOD RSQ VARP
3917 CRITBINOM HARMEAN MODE SEARCH VDB
3918 DATE HLOOKUP MONTH SECOND VLOOKUP
3919 DATEVALUE HOUR N SIGN WEEKDAY
3920 DAVERAGE HYPGEOMDIST NA SIN WEIBULL
3921 DAY IF NEGBINOMDIST SINH YEAR
3922 DAYS360 INDEX NORMDIST SKEW ZTEST
3923
3924 You can also modify the module to support function names in the follow‐
3925 ing languages: German, French, Spanish, Portuguese, Dutch, Finnish,
3926 Italian and Swedish. See the "function_locale.pl" program in the "exam‐
3927 ples" directory of the distro.
3928
3929 For a general introduction to Excel's formulas and an explanation of
3930 the syntax of the function refer to the Excel help files or the follow‐
3931 ing: http://office.microsoft.com/en-us/assistance/CH062528031033.aspx
3932
3933 If your formula doesn't work in Spreadsheet::WriteExcel try the follow‐
3934 ing:
3935
3936 1. Verify that the formula works in Excel (or Gnumeric or OpenOffice.org).
3937 2. Ensure that it isn't on the Caveats list shown above.
3938 3. Ensure that cell references and formula names are in uppercase.
3939 4. Ensure that you are using ':' as the range operator, A1:A4.
3940 5. Ensure that you are using ',' as the union operator, SUM(1,2,3).
3941 6. Ensure that the function is in the above table.
3942
3943 If you go through steps 1-6 and you still have a problem, mail me.
3944
3945 Improving performance when working with formulas
3946
3947 Writing a large number of formulas with Spreadsheet::WriteExcel can be
3948 slow. This is due to the fact that each formula has to be parsed and
3949 with the current implementation this is computationally expensive.
3950
3951 However, in a lot of cases the formulas that you write will be quite
3952 similar, for example:
3953
3954 $worksheet->write_formula('B1', '=A1 * 3 + 50', $format);
3955 $worksheet->write_formula('B2', '=A2 * 3 + 50', $format);
3956 ...
3957 ...
3958 $worksheet->write_formula('B99', '=A999 * 3 + 50', $format);
3959 $worksheet->write_formula('B1000', '=A1000 * 3 + 50', $format);
3960
3961 In this example the cell reference changes in iterations from "A1" to
3962 "A1000". The parser treats this variable as a token and arranges it
3963 according to predefined rules. However, since the parser is oblivious
3964 to the value of the token, it is essentially performing the same calcu‐
3965 lation 1000 times. This is inefficient.
3966
3967 The way to avoid this inefficiency and thereby speed up the writing of
3968 formulas is to parse the formula once and then repeatedly substitute
3969 similar tokens.
3970
3971 A formula can be parsed and stored via the "store_formula()" worksheet
3972 method. You can then use the "repeat_formula()" method to substitute
3973 $pattern, $replace pairs in the stored formula:
3974
3975 my $formula = $worksheet->store_formula('=A1 * 3 + 50');
3976
3977 for my $row (0..999) {
3978 $worksheet->repeat_formula($row, 1, $formula, $format, 'A1', 'A'.($row +1));
3979 }
3980
3981 On an arbitrary test machine this method was 10 times faster than the
3982 brute force method shown above.
3983
3984 For more information about how Spreadsheet::WriteExcel parses and
3985 stores formulas see the "Spreadsheet::WriteExcel::Formula" man page.
3986
3987 It should be noted however that the overall speed of direct formula
3988 parsing will be improved in a future version.
3989
3991 Example 1
3992
3993 The following example shows some of the basic features of Spread‐
3994 sheet::WriteExcel.
3995
3996 #!/usr/bin/perl -w
3997
3998 use strict;
3999 use Spreadsheet::WriteExcel;
4000
4001 # Create a new workbook called simple.xls and add a worksheet
4002 my $workbook = Spreadsheet::WriteExcel->new("simple.xls");
4003 my $worksheet = $workbook->add_worksheet();
4004
4005 # The general syntax is write($row, $column, $token). Note that row and
4006 # column are zero indexed
4007
4008 # Write some text
4009 $worksheet->write(0, 0, "Hi Excel!");
4010
4011 # Write some numbers
4012 $worksheet->write(2, 0, 3); # Writes 3
4013 $worksheet->write(3, 0, 3.00000); # Writes 3
4014 $worksheet->write(4, 0, 3.00001); # Writes 3.00001
4015 $worksheet->write(5, 0, 3.14159); # TeX revision no.?
4016
4017 # Write some formulas
4018 $worksheet->write(7, 0, '=A3 + A6');
4019 $worksheet->write(8, 0, '=IF(A5>3,"Yes", "No")');
4020
4021 # Write a hyperlink
4022 $worksheet->write(10, 0, 'http://www.perl.com/');
4023
4024 Example 2
4025
4026 The following is a general example which demonstrates some features of
4027 working with multiple worksheets.
4028
4029 #!/usr/bin/perl -w
4030
4031 use strict;
4032 use Spreadsheet::WriteExcel;
4033
4034 # Create a new Excel workbook
4035 my $workbook = Spreadsheet::WriteExcel->new("regions.xls");
4036
4037 # Add some worksheets
4038 my $north = $workbook->add_worksheet("North");
4039 my $south = $workbook->add_worksheet("South");
4040 my $east = $workbook->add_worksheet("East");
4041 my $west = $workbook->add_worksheet("West");
4042
4043 # Add a Format
4044 my $format = $workbook->add_format();
4045 $format->set_bold();
4046 $format->set_color('blue');
4047
4048 # Add a caption to each worksheet
4049 foreach my $worksheet ($workbook->sheets()) {
4050 $worksheet->write(0, 0, "Sales", $format);
4051 }
4052
4053 # Write some data
4054 $north->write(0, 1, 200000);
4055 $south->write(0, 1, 100000);
4056 $east->write (0, 1, 150000);
4057 $west->write (0, 1, 100000);
4058
4059 # Set the active worksheet
4060 $south->activate();
4061
4062 # Set the width of the first column
4063 $south->set_column(0, 0, 20);
4064
4065 # Set the active cell
4066 $south->set_selection(0, 1);
4067
4068 Example 3
4069
4070 This example shows how to use a conditional numerical format with
4071 colours to indicate if a share price has gone up or down.
4072
4073 use strict;
4074 use Spreadsheet::WriteExcel;
4075
4076 # Create a new workbook and add a worksheet
4077 my $workbook = Spreadsheet::WriteExcel->new("stocks.xls");
4078 my $worksheet = $workbook->add_worksheet();
4079
4080 # Set the column width for columns 1, 2, 3 and 4
4081 $worksheet->set_column(0, 3, 15);
4082
4083 # Create a format for the column headings
4084 my $header = $workbook->add_format();
4085 $header->set_bold();
4086 $header->set_size(12);
4087 $header->set_color('blue');
4088
4089 # Create a format for the stock price
4090 my $f_price = $workbook->add_format();
4091 $f_price->set_align('left');
4092 $f_price->set_num_format('$0.00');
4093
4094 # Create a format for the stock volume
4095 my $f_volume = $workbook->add_format();
4096 $f_volume->set_align('left');
4097 $f_volume->set_num_format('#,##0');
4098
4099 # Create a format for the price change. This is an example of a
4100 # conditional format. The number is formatted as a percentage. If it is
4101 # positive it is formatted in green, if it is negative it is formatted
4102 # in red and if it is zero it is formatted as the default font colour
4103 # (in this case black). Note: the [Green] format produces an unappealing
4104 # lime green. Try [Color 10] instead for a dark green.
4105 #
4106 my $f_change = $workbook->add_format();
4107 $f_change->set_align('left');
4108 $f_change->set_num_format('[Green]0.0%;[Red]-0.0%;0.0%');
4109
4110 # Write out the data
4111 $worksheet->write(0, 0, 'Company',$header);
4112 $worksheet->write(0, 1, 'Price', $header);
4113 $worksheet->write(0, 2, 'Volume', $header);
4114 $worksheet->write(0, 3, 'Change', $header);
4115
4116 $worksheet->write(1, 0, 'Damage Inc.' );
4117 $worksheet->write(1, 1, 30.25, $f_price ); # $30.25
4118 $worksheet->write(1, 2, 1234567, $f_volume); # 1,234,567
4119 $worksheet->write(1, 3, 0.085, $f_change); # 8.5% in green
4120
4121 $worksheet->write(2, 0, 'Dump Corp.' );
4122 $worksheet->write(2, 1, 1.56, $f_price ); # $1.56
4123 $worksheet->write(2, 2, 7564, $f_volume); # 7,564
4124 $worksheet->write(2, 3, -0.015, $f_change); # -1.5% in red
4125
4126 $worksheet->write(3, 0, 'Rev Ltd.' );
4127 $worksheet->write(3, 1, 0.13, $f_price ); # $0.13
4128 $worksheet->write(3, 2, 321, $f_volume); # 321
4129 $worksheet->write(3, 3, 0, $f_change); # 0 in the font color (black)
4130
4131 Example 4
4132
4133 The following is a simple example of using functions.
4134
4135 #!/usr/bin/perl -w
4136
4137 use strict;
4138 use Spreadsheet::WriteExcel;
4139
4140 # Create a new workbook and add a worksheet
4141 my $workbook = Spreadsheet::WriteExcel->new("stats.xls");
4142 my $worksheet = $workbook->add_worksheet('Test data');
4143
4144 # Set the column width for columns 1
4145 $worksheet->set_column(0, 0, 20);
4146
4147 # Create a format for the headings
4148 my $format = $workbook->add_format();
4149 $format->set_bold();
4150
4151 # Write the sample data
4152 $worksheet->write(0, 0, 'Sample', $format);
4153 $worksheet->write(0, 1, 1);
4154 $worksheet->write(0, 2, 2);
4155 $worksheet->write(0, 3, 3);
4156 $worksheet->write(0, 4, 4);
4157 $worksheet->write(0, 5, 5);
4158 $worksheet->write(0, 6, 6);
4159 $worksheet->write(0, 7, 7);
4160 $worksheet->write(0, 8, 8);
4161
4162 $worksheet->write(1, 0, 'Length', $format);
4163 $worksheet->write(1, 1, 25.4);
4164 $worksheet->write(1, 2, 25.4);
4165 $worksheet->write(1, 3, 24.8);
4166 $worksheet->write(1, 4, 25.0);
4167 $worksheet->write(1, 5, 25.3);
4168 $worksheet->write(1, 6, 24.9);
4169 $worksheet->write(1, 7, 25.2);
4170 $worksheet->write(1, 8, 24.8);
4171
4172 # Write some statistical functions
4173 $worksheet->write(4, 0, 'Count', $format);
4174 $worksheet->write(4, 1, '=COUNT(B1:I1)');
4175
4176 $worksheet->write(5, 0, 'Sum', $format);
4177 $worksheet->write(5, 1, '=SUM(B2:I2)');
4178
4179 $worksheet->write(6, 0, 'Average', $format);
4180 $worksheet->write(6, 1, '=AVERAGE(B2:I2)');
4181
4182 $worksheet->write(7, 0, 'Min', $format);
4183 $worksheet->write(7, 1, '=MIN(B2:I2)');
4184
4185 $worksheet->write(8, 0, 'Max', $format);
4186 $worksheet->write(8, 1, '=MAX(B2:I2)');
4187
4188 $worksheet->write(9, 0, 'Standard Deviation', $format);
4189 $worksheet->write(9, 1, '=STDEV(B2:I2)');
4190
4191 $worksheet->write(10, 0, 'Kurtosis', $format);
4192 $worksheet->write(10, 1, '=KURT(B2:I2)');
4193
4194 Example 5
4195
4196 The following example converts a tab separated file called "tab.txt"
4197 into an Excel file called "tab.xls".
4198
4199 #!/usr/bin/perl -w
4200
4201 use strict;
4202 use Spreadsheet::WriteExcel;
4203
4204 open (TABFILE, "tab.txt") or die "tab.txt: $!";
4205
4206 my $workbook = Spreadsheet::WriteExcel->new("tab.xls");
4207 my $worksheet = $workbook->add_worksheet();
4208
4209 # Row and column are zero indexed
4210 my $row = 0;
4211
4212 while (<TABFILE>) {
4213 chomp;
4214 # Split on single tab
4215 my @Fld = split('\t', $_);
4216
4217 my $col = 0;
4218 foreach my $token (@Fld) {
4219 $worksheet->write($row, $col, $token);
4220 $col++;
4221 }
4222 $row++;
4223 }
4224
4225 Additional Examples
4226
4227 If you performed a normal installation the following examples files
4228 should have been copied to your "~site/Spreadsheet/WriteExcel/examples"
4229 directory:
4230
4231 The following is a description of the example files that are provided
4232 with Spreadsheet::WriteExcel. They are intended to demonstrate the dif‐
4233 ferent features and options of the module.
4234
4235 Getting started
4236 ===============
4237 bug_report.pl A template for submitting bug reports.
4238 demo.pl Creates a demo of some of the features.
4239 formats.pl Creates a demo of the available formatting.
4240 regions.pl Demonstrates multiple worksheets.
4241 simple.pl An example of some of the basic features.
4242 stats.pl Basic formulas and functions.
4243
4244 Advanced
4245 ========
4246 autofit.pl Simuluate Excel's autofit for colums widths.
4247 bigfile.pl Write past the 7MB limit with OLE::Storage_Lite.
4248 cgi.pl A simple CGI program.
4249 chess.pl An example of formatting using properties.
4250 colors.pl Demo of the colour palette and named colours.
4251 comments1.pl Add comments to worksheet cells.
4252 comments2.pl Add comments with advanced options.
4253 copyformat.pl Example of copying a cell format.
4254 diag_border.pl A simple example of diagonal cell borders.
4255 easter_egg.pl Expose the Excel97 flight simulator. A must see.
4256 filehandle.pl Examples of working with filehandles.
4257 formula_result.pl Formulas with user specified results.
4258 headers.pl Examples of worksheet headers and footers.
4259 hide_sheet.pl Simple example of hiding a worksheet.
4260 hyperlink1.pl Shows how to create web hyperlinks.
4261 hyperlink2.pl Examples of internal and external hyperlinks.
4262 images.pl Adding bitmap images to worksheets.
4263 indent.pl An example of cell indentation.
4264 merge1.pl A simple example of cell merging.
4265 merge2.pl A simple example of cell merging with formatting.
4266 merge3.pl Add hyperlinks to merged cells.
4267 merge4.pl An advanced example of merging with formatting.
4268 merge5.pl An advanced example of merging with formatting.
4269 merge6.pl An example of merging with Unicode strings.
4270 mod_perl1.pl A simple mod_perl 1 program.
4271 mod_perl2.pl A simple mod_perl 2 program.
4272 outline.pl An example of outlines and grouping.
4273 panes.pl An examples of how to create panes.
4274 protection.pl Example of cell locking and formula hiding.
4275 repeat.pl Example of writing repeated formulas.
4276 right_to_left.pl Change default sheet direction to right to left.
4277 row_wrap.pl How to wrap data from one worksheet onto another.
4278 sales.pl An example of a simple sales spreadsheet.
4279 sendmail.pl Send an Excel email attachment using Mail::Sender.
4280 stats_ext.pl Same as stats.pl with external references.
4281 stocks.pl Demonstrates conditional formatting.
4282 tab_colors.pl Example of how to set worksheet tab colours.
4283 textwrap.pl Demonstrates text wrapping options.
4284 win32ole.pl A sample Win32::OLE example for comparison.
4285 write_arrays.pl Example of writing 1D or 2D arrays of data.
4286 write_handler1.pl Example of extending the write() method. Step 1.
4287 write_handler2.pl Example of extending the write() method. Step 2.
4288 write_handler3.pl Example of extending the write() method. Step 3.
4289 write_handler4.pl Example of extending the write() method. Step 4.
4290 write_to_scalar.pl Example of writing an Excel file to a Perl scalar.
4291
4292 Unicode
4293 =======
4294 unicode.pl Simple example of using Unicode UTF16 strings.
4295 unicode_japan.pl Write Japanese Unicode strings using UTF16.
4296 unicode_cyrillic.pl Write Russian cyrillic strings using UTF8.
4297 unicode_list.pl List the chars in a Unicode font.
4298 unicode_2022_jp.pl Japanese: ISO-2022-JP to utf8 in perl 5.8.
4299 unicode_8859_11.pl Thai: ISO-8859_11 to utf8 in perl 5.8.
4300 unicode_8859_7.pl Greek: ISO-8859_7 to utf8 in perl 5.8.
4301 unicode_big5.pl Chinese: BIG5 to utf8 in perl 5.8.
4302 unicode_cp1251.pl Russian: CP1251 to utf8 in perl 5.8.
4303 unicode_cp1256.pl Arabic: CP1256 to utf8 in perl 5.8.
4304 unicode_koi8r.pl Russian: KOI8-R to utf8 in perl 5.8.
4305 unicode_polish_utf8.pl Polish : UTF8 to utf8 in perl 5.8.
4306 unicode_shift_jis.pl Japanese: Shift JIS to utf8 in perl 5.8.
4307
4308 Utility
4309 =======
4310 csv2xls.pl Program to convert a CSV file to an Excel file.
4311 datecalc1.pl Convert Unix/Perl time to Excel time.
4312 datecalc2.pl Calculate an Excel date using Date::Calc.
4313 lecxe.pl Convert Excel to WriteExcel using Win32::OLE.
4314 tab2xls.pl Program to convert a tab separated file to xls.
4315
4316 Developer
4317 =========
4318 convertA1.pl Helper functions for dealing with A1 notation.
4319 function_locale.pl Add non-English function names to Formula.pm.
4320 writeA1.pl Example of how to extend the module.
4321
4323 The following limits are imposed by Excel:
4324
4325 Description Limit
4326 ----------------------------------- ------
4327 Maximum number of chars in a string 32767
4328 Maximum number of columns 256
4329 Maximum number of rows 65536
4330 Maximum chars in a sheet name 31
4331 Maximum chars in a header/footer 254
4332
4333 The minimum file size is 6K due to the OLE overhead. The maximum file
4334 size is approximately 7MB (7087104 bytes) of BIFF data. This can be
4335 extended by using Takanori Kawai's OLE::Storage_Lite module
4336 http://search.cpan.org/search?dist=OLE-Storage_Lite see the "big‐
4337 file.pl" example in the "examples" directory of the distro.
4338
4340 The latest version of this module is always available at:
4341 http://search.cpan.org/search?dist=Spreadsheet-WriteExcel/
4342
4344 This module requires Perl 5.005 (or later), Parse::RecDescent and
4345 File::Temp:
4346
4347 http://search.cpan.org/search?dist=Parse-RecDescent/
4348 http://search.cpan.org/search?dist=File-Temp/
4349
4351 See the INSTALL or install.html docs that come with the distribution
4352 or:
4353
4354 http://search.cpan.org/src/JMCNAMARA/Spreadsheet-WriteEx‐
4355 cel-2.16/INSTALL
4356
4358 Spreadsheet::WriteExcel will work on the majority of Windows, UNIX and
4359 Macintosh platforms. Specifically, the module will work on any system
4360 where perl packs floats in the 64 bit IEEE format. The float must also
4361 be in little-endian format but it will be reversed if necessary. Thus:
4362
4363 print join(" ", map { sprintf "%#02x", $_ } unpack("C*", pack "d", 1.2345)), "\n";
4364
4365 should give (or in reverse order):
4366
4367 0x8d 0x97 0x6e 0x12 0x83 0xc0 0xf3 0x3f
4368
4369 In general, if you don't know whether your system supports a 64 bit
4370 IEEE float or not, it probably does. If your system doesn't, WriteExcel
4371 will "croak()" with the message given in the DIAGNOSTICS section. You
4372 can check which platforms the module has been tested on at the CPAN
4373 testers site: http://testers.cpan.org/search?request=dist&dist=Spread‐
4374 sheet-WriteExcel
4375
4377 Filename required by Spreadsheet::WriteExcel->new()
4378 A filename must be given in the constructor.
4379
4380 Can't open filename. It may be in use or protected.
4381 The file cannot be opened for writing. The directory that you are
4382 writing to may be protected or the file may be in use by another
4383 program.
4384
4385 Unable to create tmp files via File::Temp::tempfile()...
4386 This is a "-w" warning. You will see it if you are using Spread‐
4387 sheet::WriteExcel in an environment where temporary files cannot be
4388 created, in which case all data will be stored in memory. The warn‐
4389 ing is for information only: it does not affect creation but it
4390 will affect the speed of execution for large files. See the
4391 "set_tempdir" workbook method.
4392
4393 Maximum file size, 7087104, exceeded.
4394 The current OLE implementation only supports a maximum BIFF file of
4395 this size. This limit can be extended, see the LIMITATIONS section.
4396
4397 Can't locate Parse/RecDescent.pm in @INC ...
4398 Spreadsheet::WriteExcel requires the Parse::RecDescent module.
4399 Download it from CPAN:
4400 http://search.cpan.org/search?dist=Parse-RecDescent
4401
4402 Couldn't parse formula ...
4403 There are a large number of warnings which relate to badly formed
4404 formulas and functions. See the "FORMULAS AND FUNCTIONS IN EXCEL"
4405 section for suggestions on how to avoid these errors. You should
4406 also check the formula in Excel to ensure that it is valid.
4407
4408 Required floating point format not supported on this platform.
4409 Operating system doesn't support 64 bit IEEE float or it is byte-
4410 ordered in a way unknown to WriteExcel.
4411
4412 'file.xls' cannot be accessed. The file may be read-only ...
4413 You may sometimes encounter the following error when trying to open
4414 a file in Excel: "file.xls cannot be accessed. The file may be
4415 read-only, or you may be trying to access a read-only location. Or,
4416 the server the document is stored on may not be responding."
4417
4418 This error generally means that the Excel file has been corrupted.
4419 There are two likely causes of this: the file was FTPed in ASCII
4420 mode instead of binary mode or else the file was created with UTF8
4421 data returned by an XML parser. See "WORKING WITH XML" for further
4422 details.
4423
4425 The following is some general information about the Excel binary format
4426 for anyone who may be interested.
4427
4428 Excel data is stored in the "Binary Interchange File Format" (BIFF)
4429 file format. Details of this format are given in the Excel SDK, the
4430 "Excel Developer's Kit" from Microsoft Press. It is also included in
4431 the MSDN CD library but is no longer available on the MSDN website.
4432 Versions of the BIFF documentation are available at www.wotsit.org,
4433 http://www.wotsit.org/search.asp?page=2&s=database
4434
4435 Charles Wybble has collected together almost all of the available
4436 information about the Excel file format. See "The Chicago Project" at
4437 http://chicago.sourceforge.net/devel/
4438
4439 Daniel Rentz of OpenOffice.org has also written a detailed description
4440 of the Excel workbook records, see http://sc.openoffice.org/excelfile‐
4441 format.pdf
4442
4443 The BIFF portion of the Excel file is comprised of contiguous binary
4444 records that have different functions and that hold different types of
4445 data. Each BIFF record is comprised of the following three parts:
4446
4447 Record name; Hex identifier, length = 2 bytes
4448 Record length; Length of following data, length = 2 bytes
4449 Record data; Data, length = variable
4450
4451 The BIFF data is stored along with other data in an OLE Compound File.
4452 This is a structured storage which acts like a file system within a
4453 file. A Compound File is comprised of storages and streams which, to
4454 follow the file system analogy, are like directories and files.
4455
4456 The documentation for the OLE::Storage module,
4457 http://user.cs.tu-berlin.de/~schwartz/pmh/guide.html , contains one of
4458 the few descriptions of the OLE Compound File in the public domain. The
4459 Digital Imaging Group have also detailed the OLE format in the JPEG2000
4460 specification: see Appendix A of http://www.i3a.org/pdf/wg1n1017.pdf
4461
4462 The source code for the Excel plugin of the Gnumeric spreadsheet also
4463 contains information relevant to the Excel BIFF format and the OLE con‐
4464 tainer, http://www.gnome.org/projects/gnumeric/
4465
4466 In addition the source code for OpenOffice.org is available at
4467 http://www.openoffice.org/
4468
4469 An article describing Spreadsheet::WriteExcel and how it works appeared
4470 in Issue 19 of The Perl Journal. It is reproduced, by kind permission,
4471 in the "doc" directory of the distro.
4472
4473 Please note that the provision of this information does not constitute
4474 an invitation to start hacking at the BIFF or OLE file formats. There
4475 are more interesting ways to waste your time. ;-)
4476
4478 Depending on your requirements, background and general sensibilities
4479 you may prefer one of the following methods of getting data into Excel:
4480
4481 * Win32::OLE module and office automation
4482 This requires a Windows platform and an installed copy of Excel.
4483 This is the most powerful and complete method for interfacing with
4484 Excel. See http://www.activestate.com/ASPN/Reference/Prod‐
4485 ucts/ActivePerl-5.6/faq/Windows/ActivePerl-Winfaq12.html and
4486 http://www.activestate.com/ASPN/Reference/Prod‐
4487 ucts/ActivePerl-5.6/site/lib/Win32/OLE.html If your main platform
4488 is UNIX but you have the resources to set up a separate Win32/MSOf‐
4489 fice server, you can convert office documents to text, postscript
4490 or PDF using Win32::OLE. For a demonstration of how to do this
4491 using Perl see Docserver: http://search.cpan.org/search?mode=mod‐
4492 ule&query=docserver
4493
4494 * CSV, comma separated variables or text
4495 If the file extension is "csv", Excel will open and convert this
4496 format automatically. Generating a valid CSV file isn't as easy as
4497 it seems. Have a look at the DBD::RAM, DBD::CSV, Text::xSV and
4498 Text::CSV_XS modules.
4499
4500 * DBI with DBD::ADO or DBD::ODBC
4501 Excel files contain an internal index table that allows them to act
4502 like a database file. Using one of the standard Perl database mod‐
4503 ules you can connect to an Excel file as a database.
4504
4505 * DBD::Excel
4506 You can also access Spreadsheet::WriteExcel using the standard DBI
4507 interface via Takanori Kawai's DBD::Excel module
4508 http://search.cpan.org/dist/DBD-Excel
4509
4510 * Spreadsheet::WriteExcelXML
4511 This module allows you to create an Excel XML file using the same
4512 interface as Spreadsheet::WriteExcel. See:
4513 http://search.cpan.org/dist/Spreadsheet-WriteExcelXML
4514
4515 * Excel::Template
4516 This module allows you to create an Excel file from an XML template
4517 in a manner similar to HTML::Template. See
4518 http://search.cpan.org/dist/Excel-Template/
4519
4520 * Spreadsheet::WriteExcel::FromXML
4521 This module allows you to turn a simple XML file into an Excel file
4522 using Spreadsheet::WriteExcel as a backend. The format of the XML
4523 file is defined by a supplied DTD:
4524 http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromXML
4525
4526 * Spreadsheet::WriteExcel::Simple
4527 This provides an easier interface to Spreadsheet::WriteExcel:
4528 http://search.cpan.org/dist/Spreadsheet-WriteExcel-Simple
4529
4530 * Spreadsheet::WriteExcel::FromDB
4531 This is a useful module for creating Excel files directly from a DB
4532 table: http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromDB
4533
4534 * HTML tables
4535 This is an easy way of adding formatting via a text based format.
4536
4537 * XML or HTML
4538 The Excel XML and HTML file specification are available from
4539 http://msdn.microsoft.com/library/officedev/ofxml2k/ofxml2k.htm
4540
4541 For other Perl-Excel modules try the following search:
4542 http://search.cpan.org/search?mode=module&query=excel
4543
4545 To read data from Excel files try:
4546
4547 * Spreadsheet::ParseExcel
4548 This uses the OLE::Storage-Lite module to extract data from an
4549 Excel file. http://search.cpan.org/dist/Spreadsheet-ParseExcel
4550
4551 * Spreadsheet::ParseExcel_XLHTML
4552 This module uses Spreadsheet::ParseExcel's interface but uses
4553 xlHtml (see below) to do the conversion:
4554 http://search.cpan.org/dist/Spreadsheet-ParseExcel_XLHTML Spread‐
4555 sheet::ParseExcel_XLHTML
4556
4557 * xlHtml
4558 This is an open source "Excel to HTML Converter" C/C++ project at
4559 http://chicago.sourceforge.net/xlhtml/
4560
4561 * DBD::Excel (reading)
4562 You can also access Spreadsheet::ParseExcel using the standard DBI
4563 interface via Takanori Kawai's DBD::Excel module
4564 http://search.cpan.org/dist/DBD-Excel
4565
4566 * Win32::OLE module and office automation (reading)
4567 See, the section "WRITING EXCEL FILES".
4568
4569 * HTML tables (reading)
4570 If the files are saved from Excel in a HTML format the data can be
4571 accessed using HTML::TableExtract
4572 http://search.cpan.org/dist/HTML-TableExtract
4573
4574 * DBI with DBD::ADO or DBD::ODBC.
4575 See, the section "WRITING EXCEL FILES".
4576
4577 * XML::Excel
4578 Converts Excel files to XML using Spreadsheet::ParseExcel
4579 http://search.cpan.org/dist/XML-Excel.
4580
4581 * OLE::Storage, aka LAOLA
4582 This is a Perl interface to OLE file formats. In particular, the
4583 distro contains an Excel to HTML converter called Herbert,
4584 http://user.cs.tu-berlin.de/~schwartz/pmh/ This has been superseded
4585 by the Spreadsheet::ParseExcel module.
4586
4587 For other Perl-Excel modules try the following search:
4588 http://search.cpan.org/search?mode=module&query=excel
4589
4590 If you wish to view Excel files on a UNIX/Linux platform check out the
4591 excellent Gnumeric spreadsheet application at
4592 http://www.gnome.org/projects/gnumeric/ or OpenOffice.org at
4593 http://www.openoffice.org/
4594
4595 If you wish to view Excel files on a Windows platform which doesn't
4596 have Excel installed you can use the free Microsoft Excel Viewer
4597 http://office.microsoft.com/downloads/2000/xlviewer.aspx
4598
4600 You must be careful when using Spreadsheet::WriteExcel in conjunction
4601 with Perl 5.6 and XML::Parser (and other XML parsers) due to the fact
4602 that the data returned by the parser is generally in UTF8 format.
4603
4604 When UTF8 strings are added to Spreadsheet::WriteExcel's internal data
4605 it causes the generated Excel file to become corrupt.
4606
4607 Note, this doesn't affect Perl 5.005 (which doesn't try to handle UTF8)
4608 or 5.8 (which handles it correctly).
4609
4610 To avoid this problem you should upgrade to Perl 5.8, if possible, or
4611 else you should convert the output data from XML::Parser to ASCII or
4612 ISO-8859-1 using one of the following methods:
4613
4614 $new_str = pack 'C*', unpack 'U*', $utf8_str;
4615
4616 use Unicode::MapUTF8 'from_utf8';
4617 $new_str = from_utf8({-str => $utf8_str, -charset => 'ISO-8859-1'});
4618
4620 Formulas are formulae.
4621
4622 XML and UTF8 data on Perl 5.6 can cause Excel files created by Spread‐
4623 sheet::WriteExcel to become corrupt. See "Warning about XML::Parser and
4624 Perl 5.6" for further details.
4625
4626 The format object that is used with a "merge_range()" method call is
4627 marked internally as being associated with a merged range.It is a fatal
4628 error to use a merged format in a non-merged cell. The current work‐
4629 around is to use separate formats for merged and non-merged cell. This
4630 restriction will be removed in a future release.
4631
4632 Nested formulas sometimes aren't parsed correctly and give a result of
4633 "#VALUE". If you come across a formula that parses like this, let me
4634 know.
4635
4636 Spreadsheet::ParseExcel: All formulas created by Spreadsheet::WriteEx‐
4637 cel are read as having a value of zero. This is because Spread‐
4638 sheet::WriteExcel only stores the formula and not the calculated
4639 result.
4640
4641 OpenOffice.org: Images are not displayed. Some formatting is not dis‐
4642 played correctly.
4643
4644 Gnumeric: Images are not displayed. Some formatting is not displayed
4645 correctly. URLs are not displayed as links. Page setup may cause Gnu‐
4646 meric to crash.
4647
4648 The lack of a portable way of writing a little-endian 64 bit IEEE
4649 float. There is beta code available to fix this. Let me know if you
4650 wish to test it on your platform.
4651
4652 If you wish to submit a bug report run the "bug_report.pl" program in
4653 the "examples" directory of the distro.
4654
4656 The roadmap is as follows:
4657
4658 * Fix insert_bitmap to work with write_comment(), OpenOffice.org and
4659 Gnumeric.
4660 * Add AutoFilters.
4661
4662 Also, here are some of the most requested features that probably won't
4663 get added:
4664
4665 * Macros.
4666 This would solve some other problems neatly. However, the format of
4667 Excel macros isn't documented.
4668
4669 * Some feature that you really need. ;-)
4670
4671 If there is some feature of an Excel file that you really, really need
4672 then you should use Win32::OLE with Excel on Windows. If you are on
4673 Unix you could consider connecting to a Windows server via Docserver or
4674 SOAP, see "WRITING EXCEL FILES".
4675
4677 There is a Google group for discussing and asking questions about
4678 Spreadsheet::WriteExcel: http://groups-beta.google.com/group/spread‐
4679 sheet-writeexcel/
4680
4681 Alternatively you can keep up to date with future releases by subscrib‐
4682 ing at: http://freshmeat.net/projects/writeexcel/
4683
4685 If you'd care to donate to the Spreadsheet::WriteExcel project, you can
4686 do so via PayPal: http://tinyurl.com/7ayes
4687
4689 Spreadsheet::ParseExcel: http://search.cpan.org/dist/Spreadsheet-Parse‐
4690 Excel
4691
4692 Spreadsheet-WriteExcel-FromXML: http://search.cpan.org/dist/Spread‐
4693 sheet-WriteExcel-FromXML
4694
4695 Spreadsheet::WriteExcel::FromDB: http://search.cpan.org/dist/Spread‐
4696 sheet-WriteExcel-FromDB
4697
4698 Excel::Template: http://search.cpan.org/~rkinyon/Excel-Template/
4699
4700 DateTime::Format::Excel: http://search.cpan.org/dist/DateTime-For‐
4701 mat-Excel
4702
4703 "Reading and writing Excel files with Perl" by Teodor Zlatanov, atIBM
4704 developerWorks: http://www-106.ibm.com/developerworks/library/l-pexcel/
4705
4706 "Excel-Dateien mit Perl erstellen - Controller im Glück" by Peter Din‐
4707 telmann and Christian Kirsch in the German Unix/web journal iX:
4708 http://www.heise.de/ix/artikel/2001/06/175/
4709
4710 Spreadsheet::WriteExcel documentation in Japanese by Takanori Kawai.
4711 http://member.nifty.ne.jp/hippo2000/perltips/Spreadsheet/WriteExcel.htm
4712
4713 Oesterly user brushes with fame:
4714 http://oesterly.com/releases/12102000.html
4715
4717 The following people contributed to the debugging and testing of
4718 Spreadsheet::WriteExcel:
4719
4720 Alexander Farber, Andre de Bruin, Arthur@ais, Artur Silveira da Cunha,
4721 Borgar Olsen, Brian White, Bob Mackay, Cedric Bouvier, Chad Johnson,
4722 CPAN testers, Damyan Ivanov, Daniel Berger, Daniel Gardner, Dmitry
4723 Kochurov, Eric Frazier, Ernesto Baschny, Felipe Pérez Galiana, Gordon
4724 Simpson, Hanc Pavel, Harold Bamford, James Holmes, James Wilkinson,
4725 Johan Ekenberg, Johann Hanne, Jonathan Scott Duff, J.C. Wren, Kenneth
4726 Stacey, Keith Miller, Kyle Krom, Marc Rosenthal, Markus Schmitz,
4727 Michael Braig, Michael Buschauer, Mike Blazer, Michael Erickson,
4728 Michael W J West, Ning Xie, Paul J. Falbe, Paul Medynski, Peter Dintel‐
4729 mann, Pierre Laplante, Praveen Kotha, Reto Badertscher, Rich Sorden,
4730 Shane Ashby, Shenyu Zheng, Stephan Loescher, Steve Sapovits, Sven Pas‐
4731 sig, Svetoslav Marinov, Tamas Gulacsi, Troy Daniels, Vahe Sarkissian.
4732
4733 The following people contributed patches, examples or Excel informa‐
4734 tion:
4735
4736 Andrew Benham, Bill Young, Cedric Bouvier, Charles Wybble, Daniel
4737 Rentz, David Robins, Franco Venturi, Guy Albertelli, Ian Penman, John
4738 Heitmann, Jon Guy, Kyle R. Burton, Pierre-Jean Vouette, Rubio, Marco
4739 Geri, Mark Fowler, Matisse Enzer, Sam Kington, Takanori Kawai, Tom
4740 O'Sullivan.
4741
4742 Many thanks to Ron McKelvey, Ronzo Consulting for Siemens, who spon‐
4743 sored the development of the formula caching routines.
4744
4745 Additional thanks to Takanori Kawai for translating the documentation
4746 into Japanese.
4747
4748 Gunnar Wolf maintains the Debian distro.
4749
4750 Thanks to Damian Conway for the excellent Parse::RecDescent.
4751
4752 Thanks to Tim Jenness for File::Temp.
4753
4754 Thanks to Michael Meeks and Jody Goldberg for their work on Gnumeric.
4755
4757 John McNamara jmcnamara@cpan.org
4758
4759 Cattle and Cane
4760
4761 I recall a schoolboy coming home
4762 Through fields of cane
4763 To a house of tin and timber
4764 And in the sky a rain of falling cinders.
4765 From time to time
4766 The waste memory-wastes
4767
4768 I recall a boy in bigger pants
4769 Like everyone
4770 Just waiting for a chance
4771 His father's watch
4772 He left it in the shower
4773 From time to time
4774 The waste memory-wastes
4775
4776 I recall a bigger brighter world
4777 A world of books
4778 And silent times in thought
4779 And then the railroad
4780 The railroad takes him home
4781 Through fields of cattle
4782 Through fields of cane
4783 From time to time
4784 The waste memory-wastes
4785 The waste memory-wastes
4786 Further, longer, higher, older.
4787
4788 -- Grant McLennan
4789
4791 © MM-MMVI, John McNamara.
4792
4793 All Rights Reserved. This module is free software. It may be used,
4794 redistributed and/or modified under the same terms as Perl itself.
4795
4796
4797
4798perl v5.8.8 2006-05-20 Spreadsheet::WriteExcel(3)