3
<TITLE>Spreadsheet::WriteExcel - Write to a cross-platform Excel binary file.</TITLE>
4
<LINK REV="made" HREF="mailto:">
7
<style type="text/css">
14
font-family : courier new, sans-serif;
20
font-family : courier new, sans-serif;
36
<LI><A HREF="#NAME">NAME</A></LI>
37
<LI><A HREF="#VERSION">VERSION</A></LI>
38
<LI><A HREF="#SYNOPSIS">SYNOPSIS</A></LI>
39
<LI><A HREF="#DESCRIPTION">DESCRIPTION</A></LI>
40
<LI><A HREF="#QUICK_START">QUICK START</A></LI>
41
<LI><A HREF="#WORKBOOK_METHODS">WORKBOOK METHODS</A></LI>
45
<LI><A HREF="#new_">new()</A></LI>
46
<LI><A HREF="#close_">close()</A></LI>
47
<LI><A HREF="#set_tempdir_">set_tempdir()</A></LI>
48
<LI><A HREF="#add_worksheet_sheetname_">add_worksheet($sheetname)</A></LI>
49
<LI><A HREF="#add_chart_ext_chart_data_char">add_chart_ext($chart_data, $chartname)</A></LI>
50
<LI><A HREF="#add_format_properties_">add_format(%properties)</A></LI>
51
<LI><A HREF="#set_custom_color_index_red_">set_custom_color($index, $red, $green, $blue)</A></LI>
52
<LI><A HREF="#sheets_0_1_">sheets(0, 1, ...)</A></LI>
53
<LI><A HREF="#set_1904_">set_1904()</A></LI>
54
<LI><A HREF="#set_codepage_codepage_">set_codepage($codepage)</A></LI>
58
<LI><A HREF="#WORKSHEET_METHODS">WORKSHEET METHODS</A></LI>
62
<LI><A HREF="#Cell_notation">Cell notation</A></LI>
63
<LI><A HREF="#write_row_column_token_fo">write($row, $column, $token, $format)</A></LI>
64
<LI><A HREF="#write_number_row_column_num">write_number($row, $column, $number, $format)</A></LI>
65
<LI><A HREF="#write_string_row_column_str">write_string($row, $column, $string, $format)</A></LI>
66
<LI><A HREF="#write_unicode_row_column_st">write_unicode($row, $column, $string, $format)</A></LI>
67
<LI><A HREF="#write_unicode_le_row_column_">write_unicode_le($row, $column, $string, $format)</A></LI>
68
<LI><A HREF="#keep_leading_zeros_">keep_leading_zeros()</A></LI>
69
<LI><A HREF="#write_blank_row_column_form">write_blank($row, $column, $format)</A></LI>
70
<LI><A HREF="#write_row_row_column_array_">write_row($row, $column, $array_ref, $format)</A></LI>
71
<LI><A HREF="#write_col_row_column_array_">write_col($row, $column, $array_ref, $format)</A></LI>
72
<LI><A HREF="#write_date_time_row_col_dat">write_date_time($row, $col, $date_string, $format)</A></LI>
73
<LI><A HREF="#write_url_row_col_url_str">write_url($row, $col, $url, $string, $format)</A></LI>
74
<LI><A HREF="#write_url_range_row1_col1_r">write_url_range($row1, $col1, $row2, $col2, $url, $string, $format)</A></LI>
75
<LI><A HREF="#write_formula_row_column_fo">write_formula($row, $column, $formula, $format, $value)</A></LI>
76
<LI><A HREF="#store_formula_formula_">store_formula($formula)</A></LI>
77
<LI><A HREF="#repeat_formula_row_col_form">repeat_formula($row, $col, $formula, $format, ($pattern => $replace, ...))</A></LI>
78
<LI><A HREF="#write_comment_row_column_st">write_comment($row, $column, $string)</A></LI>
79
<LI><A HREF="#add_write_handler_re_code_ref">add_write_handler($re, $code_ref)</A></LI>
80
<LI><A HREF="#insert_bitmap_row_col_filen">insert_bitmap($row, $col, $filename, $x, $y, $scale_x, $scale_y)</A></LI>
81
<LI><A HREF="#get_name_">get_name()</A></LI>
82
<LI><A HREF="#activate_">activate()</A></LI>
83
<LI><A HREF="#select_">select()</A></LI>
84
<LI><A HREF="#set_first_sheet_">set_first_sheet()</A></LI>
85
<LI><A HREF="#protect_password_">protect($password)</A></LI>
86
<LI><A HREF="#set_selection_first_row_first">set_selection($first_row, $first_col, $last_row, $last_col)</A></LI>
87
<LI><A HREF="#set_row_row_height_format_">set_row($row, $height, $format, $hidden, $level)</A></LI>
88
<LI><A HREF="#set_column_first_col_last_col">set_column($first_col, $last_col, $width, $format, $hidden, $level)</A></LI>
89
<LI><A HREF="#outline_settings_visible_symb">outline_settings($visible, $symbols_below, $symbols_right, $auto_style)</A></LI>
90
<LI><A HREF="#freeze_panes_row_col_top_ro">freeze_panes($row, $col, $top_row, $left_col)</A></LI>
91
<LI><A HREF="#thaw_panes_y_x_top_row_le">thaw_panes($y, $x, $top_row, $left_col)</A></LI>
92
<LI><A HREF="#merge_range_first_row_first_c">merge_range($first_row, $first_col, $last_row, $last_col, $token, $format)</A></LI>
93
<LI><A HREF="#set_zoom_scale_">set_zoom($scale)</A></LI>
97
<LI><A HREF="#PAGE_SET_UP_METHODS">PAGE SET-UP METHODS</A></LI>
101
<LI><A HREF="#set_landscape_">set_landscape()</A></LI>
102
<LI><A HREF="#set_portrait_">set_portrait()</A></LI>
103
<LI><A HREF="#set_paper_index_">set_paper($index)</A></LI>
104
<LI><A HREF="#center_horizontally_">center_horizontally()</A></LI>
105
<LI><A HREF="#center_vertically_">center_vertically()</A></LI>
106
<LI><A HREF="#set_margins_inches_">set_margins($inches)</A></LI>
107
<LI><A HREF="#set_header_string_margin_">set_header($string, $margin)</A></LI>
108
<LI><A HREF="#set_footer_">set_footer()</A></LI>
109
<LI><A HREF="#repeat_rows_first_row_last_ro">repeat_rows($first_row, $last_row)</A></LI>
110
<LI><A HREF="#repeat_columns_first_col_last">repeat_columns($first_col, $last_col)</A></LI>
111
<LI><A HREF="#hide_gridlines_option_">hide_gridlines($option)</A></LI>
112
<LI><A HREF="#print_row_col_headers_">print_row_col_headers()</A></LI>
113
<LI><A HREF="#print_area_first_row_first_co">print_area($first_row, $first_col, $last_row, $last_col)</A></LI>
114
<LI><A HREF="#fit_to_pages_width_height_">fit_to_pages($width, $height)</A></LI>
115
<LI><A HREF="#set_print_scale_scale_">set_print_scale($scale)</A></LI>
116
<LI><A HREF="#set_h_pagebreaks_breaks_">set_h_pagebreaks(@breaks)</A></LI>
117
<LI><A HREF="#set_v_pagebreaks_breaks_">set_v_pagebreaks(@breaks)</A></LI>
121
<LI><A HREF="#CELL_FORMATTING">CELL FORMATTING</A></LI>
124
<LI><A HREF="#Creating_and_using_a_Format_obje">Creating and using a Format object</A></LI>
125
<LI><A HREF="#Format_methods_and_Format_proper">Format methods and Format properties</A></LI>
126
<LI><A HREF="#Working_with_formats">Working with formats</A></LI>
129
<LI><A HREF="#FORMAT_METHODS">FORMAT METHODS</A></LI>
133
<LI><A HREF="#set_properties_properties_">set_properties(%properties)</A></LI>
134
<LI><A HREF="#set_font_fontname_">set_font($fontname)</A></LI>
135
<LI><A HREF="#set_size_">set_size()</A></LI>
136
<LI><A HREF="#set_color_">set_color()</A></LI>
137
<LI><A HREF="#set_bold_">set_bold()</A></LI>
138
<LI><A HREF="#set_italic_">set_italic()</A></LI>
139
<LI><A HREF="#set_underline_">set_underline()</A></LI>
140
<LI><A HREF="#set_font_strikeout_">set_font_strikeout()</A></LI>
141
<LI><A HREF="#set_font_script_">set_font_script()</A></LI>
142
<LI><A HREF="#set_font_outline_">set_font_outline()</A></LI>
143
<LI><A HREF="#set_font_shadow_">set_font_shadow()</A></LI>
144
<LI><A HREF="#set_num_format_">set_num_format()</A></LI>
145
<LI><A HREF="#set_locked_">set_locked()</A></LI>
146
<LI><A HREF="#set_hidden_">set_hidden()</A></LI>
147
<LI><A HREF="#set_align_">set_align()</A></LI>
148
<LI><A HREF="#set_center_across_">set_center_across()</A></LI>
149
<LI><A HREF="#set_text_wrap_">set_text_wrap()</A></LI>
150
<LI><A HREF="#set_rotation_">set_rotation()</A></LI>
151
<LI><A HREF="#set_indent_">set_indent()</A></LI>
152
<LI><A HREF="#set_shrink_">set_shrink()</A></LI>
153
<LI><A HREF="#set_text_justlast_">set_text_justlast()</A></LI>
154
<LI><A HREF="#set_pattern_">set_pattern()</A></LI>
155
<LI><A HREF="#set_bg_color_">set_bg_color()</A></LI>
156
<LI><A HREF="#set_fg_color_">set_fg_color()</A></LI>
157
<LI><A HREF="#set_border_">set_border()</A></LI>
158
<LI><A HREF="#set_border_color_">set_border_color()</A></LI>
159
<LI><A HREF="#copy_format_">copy($format)</A></LI>
163
<LI><A HREF="#COLOURS_IN_EXCEL">COLOURS IN EXCEL</A></LI>
164
<LI><A HREF="#DATES_IN_EXCEL">DATES IN EXCEL</A></LI>
165
<LI><A HREF="#OUTLINES_AND_GROUPING_IN_EXCEL">OUTLINES AND GROUPING IN EXCEL</A></LI>
166
<LI><A HREF="#FORMULAS_AND_FUNCTIONS_IN_EXCEL">FORMULAS AND FUNCTIONS IN EXCEL</A></LI>
169
<LI><A HREF="#Caveats">Caveats</A></LI>
170
<LI><A HREF="#Introduction">Introduction</A></LI>
171
<LI><A HREF="#Improving_performance_when_worki">Improving performance when working with formulas</A></LI>
174
<LI><A HREF="#EXAMPLES">EXAMPLES</A></LI>
178
<LI><A HREF="#Example_1">Example 1</A></LI>
179
<LI><A HREF="#Example_2">Example 2</A></LI>
180
<LI><A HREF="#Example_3">Example 3</A></LI>
181
<LI><A HREF="#Example_4">Example 4</A></LI>
182
<LI><A HREF="#Example_5">Example 5</A></LI>
183
<LI><A HREF="#Additional_Examples">Additional Examples</A></LI>
187
<LI><A HREF="#LIMITATIONS">LIMITATIONS</A></LI>
188
<LI><A HREF="#DOWNLOADING">DOWNLOADING</A></LI>
189
<LI><A HREF="#REQUIREMENTS">REQUIREMENTS</A></LI>
190
<LI><A HREF="#INSTALLATION">INSTALLATION</A></LI>
191
<LI><A HREF="#PORTABILITY">PORTABILITY</A></LI>
192
<LI><A HREF="#DIAGNOSTICS">DIAGNOSTICS</A></LI>
193
<LI><A HREF="#THE_EXCEL_BINARY_FORMAT">THE EXCEL BINARY FORMAT</A></LI>
194
<LI><A HREF="#WRITING_EXCEL_FILES">WRITING EXCEL FILES</A></LI>
195
<LI><A HREF="#READING_EXCEL_FILES">READING EXCEL FILES</A></LI>
196
<LI><A HREF="#Warning_about_XML_Parser_and_Pe">Warning about XML::Parser and Perl 5.6</A></LI>
197
<LI><A HREF="#BUGS">BUGS</A></LI>
198
<LI><A HREF="#TO_DO">TO DO</A></LI>
199
<LI><A HREF="#MAILING_LIST">MAILING LIST</A></LI>
200
<LI><A HREF="#DONATATIONS">DONATATIONS</A></LI>
201
<LI><A HREF="#ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</A></LI>
202
<LI><A HREF="#AUTHOR">AUTHOR</A></LI>
203
<LI><A HREF="#COPYRIGHT">COPYRIGHT</A></LI>
209
<H1><A NAME="NAME">NAME</A></H1>
211
Spreadsheet::WriteExcel - Write to a cross-platform Excel binary file.
216
<H1><A NAME="VERSION">VERSION</A></H1>
218
This document refers to version 2.15 of Spreadsheet::WriteExcel, released
224
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
226
To write a string, a formatted string, a number and a formula to the first
227
worksheet in an Excel workbook called perl.xls:
232
use Spreadsheet::WriteExcel;
237
# Create a new Excel workbook
238
my $workbook = Spreadsheet::WriteExcel->new("perl.xls");
244
$worksheet = $workbook->add_worksheet();
249
# Add and define a format
250
$format = $workbook->add_format(); # Add a format
251
$format->set_bold();
252
$format->set_color('red');
253
$format->set_align('center');
258
# Write a formatted and unformatted string, row and column notation.
260
$worksheet->write($row, $col, "Hi Excel!", $format);
261
$worksheet->write(1, $col, "Hi Excel!");
266
# Write a number and a formula using A1 notation
267
$worksheet->write('A3', 1.2345);
268
$worksheet->write('A4', '=SIN(PI()/4)');
273
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
275
The Spreadsheet::WriteExcel module can be used to create a cross-platform
276
Excel binary file. Multiple worksheets can be added to a workbook and
277
formatting can be applied to cells. Text, numbers, formulas, hyperlinks and
278
images can be written to the cells.
282
The Excel file produced by this module is compatible with 97, 2000, 2002
287
The module will work on the majority of Windows, UNIX and Macintosh
288
platforms. Generated files are also compatible with the Linux/UNIX
289
spreadsheet applications Gnumeric and OpenOffice.org.
293
This module cannot be used to write to an existing Excel file.
298
<H1><A NAME="QUICK_START">QUICK START</A></H1>
300
Spreadsheet::WriteExcel tries to provide an interface to as many of Excel's
301
features as possible. As a result there is a lot of documentation to
302
accompany the interface and it can be difficult at first glance to see what
303
it important and what is not. So for those of you who prefer to assemble
304
Ikea furniture first and then read the instructions, here are three easy
309
1. Create a new Excel <EM>workbook</EM> (i.e. file) using <CODE>new()</CODE>.
313
2. Add a <EM>worksheet</EM> to the new workbook using <CODE>add_worksheet()</CODE>.
317
3. Write to the worksheet using <CODE>write()</CODE>.
326
use Spreadsheet::WriteExcel; # Step 0
331
my $workbook = Spreadsheet::WriteExcel->new("perl.xls"); # Step 1
332
$worksheet = $workbook->add_worksheet(); # Step 2
333
$worksheet->write('A1', "Hi Excel!"); # Step 3
337
This will create an Excel file called <CODE>perl.xls</CODE> with a single worksheet and the text <CODE>"Hi Excel!"</CODE> in the relevant cell. And that's it. Okay, so there is actually a zeroth
338
step as well, but <CODE>use module</CODE> goes without saying. There are also more than 40 examples that come with
339
the distribution and which you can use to get you started. See <A HREF="#EXAMPLES">EXAMPLES</A>.
343
Those of you who read the instructions first and assemble the furniture
344
afterwards will know how to proceed. ;-)
349
<H1><A NAME="WORKBOOK_METHODS">WORKBOOK METHODS</A></H1>
351
The Spreadsheet::WriteExcel module provides an object oriented interface to
352
a new Excel workbook. The following methods are available through a new
370
If you are unfamiliar with object oriented interfaces or the way that they
371
are implemented in Perl have a look at <CODE>perlobj</CODE> and <CODE>perltoot</CODE> in the main Perl documentation.
376
<H2><A NAME="new_">new()</A></H2>
378
A new Excel workbook is created using the <CODE>new()</CODE> constructor which accepts either a filename or a filehandle as a parameter.
379
The following example creates a new Excel file based on a filename:
384
my $workbook = Spreadsheet::WriteExcel->new('filename.xls');
385
my $worksheet = $workbook->add_worksheet();
386
$worksheet->write(0, 0, "Hi Excel!");
390
Here are some other examples of using <CODE>new()</CODE> with filenames:
395
my $workbook1 = Spreadsheet::WriteExcel->new($filename);
396
my $workbook2 = Spreadsheet::WriteExcel->new("/tmp/filename.xls");
397
my $workbook3 = Spreadsheet::WriteExcel->new("c:\\tmp\\filename.xls");
398
my $workbook4 = Spreadsheet::WriteExcel->new('c:\tmp\filename.xls');
402
The last two examples demonstrates how to create a file on DOS or Windows
403
where it is necessary to either escape the directory separator <CODE>\</CODE> or to use single quotes to ensure that it isn't interpolated. For more
404
information see <CODE>perlfaq5: Why can't I use "C:\temp\foo" in DOS paths?</CODE>.
408
The <CODE>new()</CODE> constructor returns a Spreadsheet::WriteExcel object that you can use to
409
add worksheets and store data. It should be noted that although <CODE>my</CODE> is not specifically required it defines the scope of the new workbook
410
variable and, in the majority of cases, ensures that the workbook is closed
411
properly without explicitly calling the <CODE>close()</CODE> method.
415
If the file cannot be created, due to file permissions or some other
416
reason, <CODE>new</CODE> will return <CODE>undef</CODE>. Therefore, it is good practice to check the return value of <CODE>new</CODE> before proceeding. As usual the Perl variable <CODE>$!</CODE> will be set if there is a file creation error. You will also see one of the
417
warning messages detailed in <A HREF="#DIAGNOSTICS">DIAGNOSTICS</A>:
422
my $workbook = Spreadsheet::WriteExcel->new('protected.xls');
423
die "Problems creating new Excel file: $!" unless defined $workbook;
427
You can also pass a valid filehandle to the <CODE>new()</CODE> constructor. For example in a CGI program you could do something like this:
433
my $workbook = Spreadsheet::WriteExcel->new(\*STDOUT);
437
The requirement for <CODE>binmode()</CODE> is explained below.
441
For CGI programs you can also use the special Perl filename <CODE>'-'</CODE> which will redirect the output to STDOUT:
446
my $workbook = Spreadsheet::WriteExcel->new('-');
450
See also, the <CODE>cgi.pl</CODE> program in the <CODE>examples</CODE> directory of the distro.
454
However, this special case will not work in <CODE>mod_perl</CODE> programs where you will have to do something like the following:
463
my $workbook = Spreadsheet::WriteExcel->new(\*XLS);
471
tie *XLS => $r; # Tie to the Apache::RequestRec object
473
my $workbook = Spreadsheet::WriteExcel->new(\*XLS);
478
See also, the <CODE>mod_perl1.pl</CODE> and <CODE>mod_perl2.pl</CODE> programs in the <CODE>examples</CODE> directory of the distro.
482
Filehandles can also be useful if you want to stream an Excel file over a
483
socket or if you want to store an Excel file in a scalar.
487
For example here is a way to write an Excel file to a scalar with <CODE>perl 5.8</CODE>:
498
use Spreadsheet::WriteExcel;
503
# Requires perl 5.8 or later
504
open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
509
my $workbook = Spreadsheet::WriteExcel->new($fh);
510
my $worksheet = $workbook->add_worksheet();
515
$worksheet->write(0, 0, "Hi Excel!");
520
$workbook->close();
525
# The Excel file in now in $str. Remember to binmode() the output
526
# filehandle before printing it.
532
See also the <CODE>write_to_scalar.pl</CODE> and <CODE>filehandle.pl</CODE> programs in the <CODE>examples</CODE> directory of the distro.
536
<STRONG>Note about the requirement for</STRONG> <CODE>binmode()</CODE>: An Excel file is comprised of binary data. Therefore, if you are using a
537
filehandle you should ensure that you <CODE>binmode()</CODE> it prior to passing it to <CODE>new()</CODE>.You should do this regardless of whether you are on a Windows platform or
538
not. This applies especially to users of perl 5.8 on systems where utf8 is
539
likely to be in operation such as RedHat Linux 9. If your program, either
540
intentionally or not, writes UTF8 data to a filehandle that is passed to <CODE>new()</CODE> it will corrupt the Excel file that is created.
544
You don't have to worry about <CODE>binmode()</CODE> if you are using filenames instead of filehandles. Spreadsheet::WriteExcel
545
performs the <CODE>binmode()</CODE> internally when it converts the filename to a filehandle. For more
546
information about <CODE>binmode()</CODE> see <CODE>perlfunc</CODE> and <CODE>perlopentut</CODE> in the main Perl documentation.
551
<H2><A NAME="close_">close()</A></H2>
553
The <CODE>close()</CODE> method can be used to explicitly close an Excel file.
558
$workbook->close();
562
An explicit <CODE>close()</CODE> is required if the file must be closed prior to performing some external
563
action on it such as copying it, reading its size or attaching it to an
568
In addition, <CODE>close()</CODE> may be required to prevent perl's garbage collector from disposing of the
569
Workbook, Worksheet and Format objects in the wrong order. Situations where
576
If <CODE>my()</CODE> was not used to declare the scope of a workbook variable created using <CODE>new()</CODE>.
581
If the <CODE>new()</CODE>, <CODE>add_worksheet()</CODE> or <CODE>add_format()</CODE> methods are called in subroutines.
586
The reason for this is that Spreadsheet::WriteExcel relies on Perl's <CODE>DESTROY</CODE> mechanism to trigger destructor methods in a specific sequence. This may
587
not happen in cases where the Workbook, Worksheet and Format variables are
588
not lexically scoped or where they have different lexical scopes.
592
In general, if you create a file with a size of 0 bytes or you fail to
593
create a file you need to call <CODE>close()</CODE>.
597
The return value of <CODE>close()</CODE> is the same as that returned by perl when it closes the file created by <CODE>new()</CODE>. This allows you to handle error conditions in the usual way:
602
$workbook->close() or die "Error closing file: $!";
607
<H2><A NAME="set_tempdir_">set_tempdir()</A></H2>
609
For speed and efficiency <CODE>Spreadsheet::WriteExcel</CODE> stores worksheet data in temporary files prior to assembling the final
614
If Spreadsheet::WriteExcel is unable to create these temporary files it
615
will store the required data in memory. This can be slow for large files.
619
The problem occurs mainly with IIS on Windows although it could feasibly
620
occur on Unix systems as well. The problem generally occurs because the
621
default temp file directory is defined as <CODE>C:/</CODE> or some other directory that IIS doesn't provide write access to.
625
To check if this might be a problem on a particular system you can run a
626
simple test program with <CODE>-w</CODE> or <CODE>use warnings</CODE>. This will generate a warning if the module cannot create the required
637
use Spreadsheet::WriteExcel;
642
my $workbook = Spreadsheet::WriteExcel->new("test.xls");
643
my $worksheet = $workbook->add_worksheet();
647
To avoid this problem the <CODE>set_tempdir()</CODE> method can be used to specify a directory that is accessible for the
648
creation of temporary files.
652
The <CODE>File::Temp</CODE> module is used to create the temporary files. File::Temp uses <CODE>File::Spec</CODE> to determine an appropriate location for these files such as <CODE>/tmp</CODE> or <CODE>c:\windows\temp</CODE>. You can find out which directory is used on your system as follows:
657
perl -MFile::Spec -le "print File::Spec->tmpdir"
661
Even if the default temporary file directory is accessible you may wish to
662
specify an alternative location for security or maintenance reasons:
667
$workbook->set_tempdir('/tmp/writeexcel');
668
$workbook->set_tempdir('c:\windows\temp\writeexcel');
672
The directory for the temporary file must exist, <CODE>set_tempdir()</CODE> will not create a new directory.
676
One disadvantage of using the <CODE>set_tempdir()</CODE> method is that on some Windows systems it will limit you to approximately
677
800 concurrent tempfiles. This means that a single program running on one
678
of these systems will be limited to creating a total of 800 workbook and
679
worksheet objects. You can run multiple, non-concurrent programs to work
680
around this if necessary.
685
<H2><A NAME="add_worksheet_sheetname_">add_worksheet($sheetname)</A></H2>
687
At least one worksheet should be added to a new workbook. A worksheet is
688
used to write data into cells:
693
$worksheet1 = $workbook->add_worksheet(); # Sheet1
694
$worksheet2 = $workbook->add_worksheet('Foglio2'); # Foglio2
695
$worksheet3 = $workbook->add_worksheet('Data'); # Data
696
$worksheet4 = $workbook->add_worksheet(); # Sheet4
700
If <CODE>$sheetname</CODE> is not specified the default Excel convention will be followed, i.e.
705
The worksheet name must be a valid Excel worksheet name, i.e. it cannot
706
contain any of the following characters, <CODE>[ ] : * ? / \</CODE> and it must be less than 32 characters. In addition, you cannot use the
707
same, case insensitive, <CODE>$sheetname</CODE> for more than one worksheet.
711
On systems with <CODE>perl 5.8</CODE> and later the <CODE>add_worksheet()</CODE> method will also handle strings in Perl's <CODE>utf8</CODE> format.
716
$worksheet5 = $workbook->add_worksheet("\x{263a}"); # Smiley
721
<H2><A NAME="add_chart_ext_chart_data_char">add_chart_ext($chart_data, $chartname)</A></H2>
723
This method is use to include externally generated charts in a
724
Spreadsheet::WriteExcel file.
729
my $chart = $workbook->add_chart_ext('chart01.bin', 'Chart1');
733
This feature is new and would be best described as experimental. Read <CODE>charts.txt</CODE> in the charts directory of the distro for a full explanation.
738
<H2><A NAME="add_format_properties_">add_format(%properties)</A></H2>
740
The <CODE>add_format()</CODE> method can be used to create new Format objects which are used to apply
741
formatting to a cell. You can either define the properties at creation time
742
via a hash of property values or later via method calls.
747
$format1 = $workbook->add_format(%props); # Set properties at creation
748
$format2 = $workbook->add_format(); # Set properties later
752
See the <A HREF="#CELL_FORMATTING">CELL FORMATTING</A> section for more details about Format properties and how to set them.
757
<H2><A NAME="set_custom_color_index_red_">set_custom_color($index, $red, $green, $blue)</A></H2>
759
The <CODE>set_custom_color()</CODE> method can be used to override one of the built-in palette values with a
760
more suitable colour.
764
The value for <CODE>$index</CODE> should be in the range 8..63, see <A HREF="#COLOURS_IN_EXCEL">COLOURS IN EXCEL</A>.
768
The default named colours use the following indices:
791
A new colour is set using its RGB (red green blue) components. The <CODE>$red</CODE>, <CODE>$green</CODE> and <CODE>$blue</CODE> values must be in the range 0..255. You can determine the required values
792
in Excel using the <CODE>Tools->Options->Colors->Modify</CODE> dialog.
796
The <CODE>set_custom_color()</CODE> workbook method can also be used with a HTML style <CODE>#rrggbb</CODE> hex value:
801
$workbook->set_custom_color(40, 255, 102, 0 ); # Orange
802
$workbook->set_custom_color(40, 0xFF, 0x66, 0x00); # Same thing
803
$workbook->set_custom_color(40, '#FF6600' ); # Same thing
808
my $font = $workbook->add_format(color => 40); # Use the modified colour
812
The return value from <CODE>set_custom_color()</CODE> is the index of the colour that was changed:
817
my $ferrari = $workbook->set_custom_color(40, 216, 12, 12);
822
my $format = $workbook->add_format(
823
bg_color => $ferrari,
831
<H2><A NAME="sheets_0_1_">sheets(0, 1, ...)</A></H2>
833
The <CODE>sheets()</CODE> method returns a list, or a sliced list, of the worksheets in a workbook.
837
If no arguments are passed the method returns a list of all the worksheets
838
in the workbook. This is useful if you want to repeat an operation on each
844
foreach $worksheet ($workbook->sheets()) {
845
print $worksheet->get_name();
850
You can also specify a slice list to return one or more worksheet objects:
855
$worksheet = $workbook->sheets(0);
856
$worksheet->write('A1', "Hello");
860
Or since return value from <CODE>sheets()</CODE> is a reference to a worksheet object you can write the above example as:
865
$workbook->sheets(0)->write('A1', "Hello");
869
The following example returns the first and last worksheet in a workbook:
874
foreach $worksheet ($workbook->sheets(0, -1)) {
880
Array slices are explained in the perldata manpage.
885
<H2><A NAME="set_1904_">set_1904()</A></H2>
887
Excel stores dates as real numbers where the integer part stores the number
888
of days since the epoch and the fractional part stores the percentage of
889
the day. The epoch can be either 1900 or 1904. Excel for Windows uses 1900
890
and Excel for Macintosh uses 1904. However, Excel on either platform will
891
convert automatically between one system and the other.
895
Spreadsheet::WriteExcel stores dates in the 1900 format by default. If you
896
wish to change this you can call the <CODE>set_1904()</CODE> workbook method. You can query the current value by calling the <CODE>get_1904()</CODE> workbook method. This returns 0 for 1900 and 1 for 1904.
900
See also <A HREF="#DATES_IN_EXCEL">DATES IN EXCEL</A> for more information about working with Excel's date system.
904
In general you probably won't need to use <CODE>set_1904()</CODE>.
909
<H2><A NAME="set_codepage_codepage_">set_codepage($codepage)</A></H2>
911
The default code page or character set used by Spreadsheet::WriteExcel is
912
ANSI. This is also the default used by Excel for Windows. Occasionally
913
however it may be necessary to change the code page via the <CODE>set_codepage()</CODE> method.
917
Changing the code page may be required if your are using
918
Spreadsheet::WriteExcel on the Macintosh and you are using characters
919
outside the ASCII 128 character set:
924
$workbook->set_codepage(1); # ANSI, MS Windows
925
$workbook->set_codepage(2); # Apple Macintosh
929
The <CODE>set_codepage()</CODE> method is rarely required.
934
<H1><A NAME="WORKSHEET_METHODS">WORKSHEET METHODS</A></H1>
936
A new worksheet is created by calling the <CODE>add_worksheet()</CODE> method from a workbook object:
941
$worksheet1 = $workbook->add_worksheet();
942
$worksheet2 = $workbook->add_worksheet();
946
The following methods are available through a new worksheet:
984
<H2><A NAME="Cell_notation">Cell notation</A></H2>
986
Spreadsheet::WriteExcel supports two forms of notation to designate the
987
position of cells: Row-column notation and A1 notation.
991
Row-column notation uses a zero based index for both row and column while
992
A1 notation uses the standard Excel alphanumeric sequence of column letter
993
and 1-based row. For example:
998
(0, 0) # The top left cell in row-column notation.
999
('A1') # The top left cell in A1 notation.
1004
(1999, 29) # Row-column notation.
1005
('AD2000') # The same cell in A1 notation.
1009
Row-column notation is useful if you are referring to cells
1015
for my $i (0 .. 9) {
1016
$worksheet->write($i, 0, 'Hello'); # Cells A1 to A10
1021
A1 notation is useful for setting up a worksheet manually and for working
1027
$worksheet->write('H1', 200);
1028
$worksheet->write('H2', '=H1+1');
1032
In formulas and applicable methods you can also use the <CODE>A:A</CODE> column notation:
1037
$worksheet->write('A1', '=SUM(B:B)');
1041
The <CODE>Spreadsheet::WriteExcel::Utility</CODE> module that is included in the distro contains helper functions for dealing
1042
with A1 notation, for example:
1047
use Spreadsheet::WriteExcel::Utility;
1052
($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
1053
$str = xl_rowcol_to_cell(1, 2); # C2
1057
For simplicity, the parameter lists for the worksheet method calls in the
1058
following sections are given in terms of row-column notation. In all cases
1059
it is also possible to use A1 notation.
1063
Note: in Excel it is also possible to use a R1C1 notation. This is not
1064
supported by Spreadsheet::WriteExcel.
1069
<H2><A NAME="write_row_column_token_fo">write($row, $column, $token, $format)</A></H2>
1071
Excel makes a distinction between data types such as strings, numbers,
1072
blanks, formulas and hyperlinks. To simplify the process of writing data
1073
the <CODE>write()</CODE> method acts as a general alias for several more specific methods:
1088
The general rule is that if the data looks like a <EM>something</EM> then a <EM>something</EM> is written. Here are some examples in both row-column and A1 notation:
1094
$worksheet->write(0, 0, "Hello" ); # write_string()
1095
$worksheet->write(1, 0, 'One' ); # write_string()
1096
$worksheet->write(2, 0, 2 ); # write_number()
1097
$worksheet->write(3, 0, 3.00001 ); # write_number()
1098
$worksheet->write(4, 0, "" ); # write_blank()
1099
$worksheet->write(5, 0, '' ); # write_blank()
1100
$worksheet->write(6, 0, undef ); # write_blank()
1101
$worksheet->write(7, 0 ); # write_blank()
1102
$worksheet->write(8, 0, 'http://www.perl.com/'); # write_url()
1103
$worksheet->write('A9', 'ftp://ftp.cpan.org/' ); # write_url()
1104
$worksheet->write('A10', 'internal:Sheet1!A1' ); # write_url()
1105
$worksheet->write('A11', 'external:c:\foo.xls' ); # write_url()
1106
$worksheet->write('A12', '=A3 + 3*A4' ); # write_formula()
1107
$worksheet->write('A13', '=SIN(PI()/4)' ); # write_formula()
1108
$worksheet->write('A14', \@array ); # write_row()
1109
$worksheet->write('A15', [\@array] ); # write_col()
1114
# And if the keep_leading_zeros property is set:
1115
$worksheet->write('A16, 2 ); # write_number()
1116
$worksheet->write('A17, 02 ); # write_string()
1117
$worksheet->write('A18, 00002 ); # write_string()
1121
The "looks like" rule is defined by regular expressions:
1125
<CODE>write_number()</CODE> if <CODE>$token</CODE> is a number based on the following regex: <CODE>$token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/</CODE>.
1129
<CODE>write_string()</CODE> if <CODE>keep_leading_zeros()</CODE> is set and <CODE>$token</CODE> is an integer with leading zeros based on the following regex: <CODE>$token =~ /^0\d+$/</CODE>.
1133
<CODE>write_blank()</CODE> if <CODE>$token</CODE> is undef or a blank string: <CODE>undef</CODE>, <CODE>""</CODE> or <CODE>''</CODE>.
1137
<CODE>write_url()</CODE> if <CODE>$token</CODE> is a http, https, ftp or mailto URL based on the following regexes: <CODE>$token =~ m|^[fh]tt?ps?://|</CODE> or <CODE>$token =~ m|^mailto:|</CODE>.
1141
<CODE>write_url()</CODE> if <CODE>$token</CODE> is an internal or external sheet reference based on the following regex: <CODE>$token =~ m[^(in|ex)ternal:]</CODE>.
1145
<CODE>write_formula()</CODE> if the first character of <CODE>$token</CODE> is <CODE>"="</CODE>.
1149
<CODE>write_row()</CODE> if <CODE>$token</CODE> is an array ref.
1153
<CODE>write_col()</CODE> if <CODE>$token</CODE> is an array ref of array refs.
1157
<CODE>write_string()</CODE> if none of the previous conditions apply.
1161
The <CODE>$format</CODE> parameter is optional. It should be a valid Format object, see <A HREF="#CELL_FORMATTING">CELL FORMATTING</A>:
1166
my $format = $workbook->add_format();
1167
$format->set_bold();
1168
$format->set_color('red');
1169
$format->set_align('center');
1174
$worksheet->write(4, 0, "Hello", $format ); # Formatted string
1178
The <CODE>write()</CODE> method will ignore empty strings or <CODE>undef</CODE> tokens unless a format is also supplied. As such you needn't worry about
1179
special handling for empty or <CODE>undef</CODE> values in your data. See also the <CODE>write_blank()</CODE> method.
1183
One problem with the <CODE>write()</CODE> method is that occasionally data looks like a number but you don't want it
1184
treated as a number. For example, zip codes or ID numbers often start with
1185
a leading zero. If you write this data as a number then the leading
1186
<CODE>zero(s)</CODE> will be stripped. You can change this default
1187
behaviour by using the <CODE>keep_leading_zeros()</CODE> method. While this property is in place any integers with leading zeros
1188
will be treated as strings and the zeros will be preserved. See the <CODE>keep_leading_zeros()</CODE> section for a full discussion of this issue.
1192
You can also add your own data handlers to the <CODE>write()</CODE> method using <CODE>add_write_handler()</CODE>.
1196
On systems with <CODE>perl 5.8</CODE> and later the <CODE>write()</CODE> method will also handle Unicode strings in Perl's <CODE>utf8</CODE> format.
1200
The <CODE>write</CODE> methods return:
1206
-1 for insufficient number of arguments.
1207
-2 for row or column out of bounds.
1208
-3 for string too long.
1213
<H2><A NAME="write_number_row_column_num">write_number($row, $column, $number, $format)</A></H2>
1215
Write an integer or a float to the cell specified by <CODE>$row</CODE> and <CODE>$column</CODE>:
1220
$worksheet->write_number(0, 0, 123456);
1221
$worksheet->write_number('A2', 2.3451);
1225
See the note about <A HREF="#Cell_notation">Cell notation</A>. The <CODE>$format</CODE> parameter is optional.
1229
In general it is sufficient to use the <CODE>write()</CODE> method.
1234
<H2><A NAME="write_string_row_column_str">write_string($row, $column, $string, $format)</A></H2>
1236
Write a string to the cell specified by <CODE>$row</CODE> and <CODE>$column</CODE>:
1241
$worksheet->write_string(0, 0, "Your text here" );
1242
$worksheet->write_string('A2', "or here" );
1246
The maximum string size is 32767 characters. However the maximum string
1247
segment that Excel can display in a cell is 1000. All 32767 characters can
1248
be displayed in the formula bar.
1252
The <CODE>$format</CODE> parameter is optional.
1256
On systems with <CODE>perl 5.8</CODE> and later the <CODE>write()</CODE> method will also handle strings in Perl's <CODE>utf8</CODE> format. With older perls you can also write Unicode in <CODE>UTF16</CODE> format via the <CODE>write_unicode()</CODE> method. See also the <CODE>unicode_*.pl</CODE> programs in the examples directory of the distro.
1260
In general it is sufficient to use the <CODE>write()</CODE> method. However, you may sometimes wish to use the <CODE>write_string()</CODE> method to write data that looks like a number but that you don't want
1261
treated as a number. For example, zip codes or phone numbers:
1266
# Write as a plain string
1267
$worksheet->write_string('A1', '01209');
1271
However, if the user edits this string Excel may convert it back to a
1272
number. To get around this you can use the Excel text format <CODE>@</CODE>:
1277
# Format as a string. Doesn't change to a number when edited
1278
my $format1 = $workbook->add_format(num_format => '@');
1279
$worksheet->write_string('A2', '01209', $format1);
1283
See also the note about <A HREF="#Cell_notation">Cell notation</A>.
1288
<H2><A NAME="write_unicode_row_column_st">write_unicode($row, $column, $string, $format)</A></H2>
1290
This method is used to write Unicode strings to a cell in Excel. It is
1291
functionally the same as the <CODE>write_string()</CODE> method except that the string should be in UTF-16 Unicode format.
1295
<STRONG>Note</STRONG>: on systems with <CODE>perl 5.8</CODE> and later the <CODE>write()</CODE> and <CODE>write_string()</CODE>methods will also handle strings in Perl's <CODE>utf8</CODE> format. With older perls you must use the <CODE>write_unicode()</CODE> method.
1299
The Unicode format required by Excel is UTF-16. Additionally <CODE>Spreadsheet::WriteExcel</CODE> requires that the 16-bit characters are in big-endian order. This is
1300
generally referred to as UTF-16BE. To write UTF-16 strings in little-endian
1301
format use the <CODE>write_unicode_le()</CODE> method.
1305
The following is a simple example showing how to write some Unicode
1317
use Spreadsheet::WriteExcel;
1323
my $workbook = Spreadsheet::WriteExcel->new('unicode.xls');
1324
my $worksheet = $workbook->add_worksheet();
1329
# Increase the column width for clarity
1330
$worksheet->set_column('A:A', 25);
1335
# Write a Unicode character
1337
my $smiley = pack "n", 0x263a;
1342
# Increase the font size for legibility.
1343
my $big_font = $workbook->add_format(size => 72);
1348
$worksheet->write_unicode('A3', $smiley, $big_font);
1353
# Write a phrase in Cyrillic using a hex-encoded string
1355
my $uni_str = pack "H*", "042d0442043e0020044404400430043704300020043d" .
1356
"043000200440044304410441043a043e043c0021";
1361
$worksheet->write_unicode('A5', $uni_str);
1366
# Map a string to UTF-16BE using an external module.
1368
my $map = Unicode::Map->new("ISO-8859-1");
1369
my $utf16 = $map->to_unicode("Hello world!");
1374
$worksheet->write_unicode('A7', $utf16);
1378
The following is an example of creating an Excel file with some Japanese
1379
text. You will need to have a Unicode font installed, such as <CODE>Arial Unicode MS</CODE>, to view the results:
1390
use Spreadsheet::WriteExcel;
1395
my $workbook = Spreadsheet::WriteExcel->new('unicode.xls');
1396
my $worksheet = $workbook->add_worksheet();
1401
# It is only required to specify a Unicode font via add_format() if
1402
# you are using Excel 97. For Excel 2000+ the text will display
1403
# with the default font (if you have Unicode fonts installed).
1405
my $uni_font = $workbook->add_format(font => 'Arial Unicode MS');
1410
my $kanji = pack 'n*', 0x65e5, 0x672c;
1411
my $katakana = pack 'n*', 0xff86, 0xff8e, 0xff9d;
1412
my $hiragana = pack 'n*', 0x306b, 0x307b, 0x3093;
1417
$worksheet->write_unicode('A1', $kanji, $uni_font);
1418
$worksheet->write_unicode('A2', $katakana, $uni_font);
1419
$worksheet->write_unicode('A3', $hiragana, $uni_font);
1424
$worksheet->write('B1', 'Kanji');
1425
$worksheet->write('B2', 'Katakana');
1426
$worksheet->write('B3', 'Hiragana');
1430
Note: You can convert ascii encodings to the required UTF-16BE format using
1431
one of the many Unicode modules on CPAN. For example <CODE>Unicode::Map</CODE> and <CODE>Unicode::String</CODE>: <A
1432
HREF="http://search.cpan.org/author/MSCHWARTZ/Unicode-Map-0.112/Map.pm">http://search.cpan.org/author/MSCHWARTZ/Unicode-Map-0.112/Map.pm</A>
1434
HREF="http://search.cpan.org/author/GAAS/Unicode-String-2.06/String.pm">http://search.cpan.org/author/GAAS/Unicode-String-2.06/String.pm</A>
1439
For a full list of the Perl Unicode modules see: <A
1440
HREF="http://search.cpan.org/search?query=unicode&mode=all">http://search.cpan.org/search?query=unicode&mode=all</A>
1445
See also the <CODE>unicode_*.pl</CODE> programs in the examples directory of the distro.
1450
<H2><A NAME="write_unicode_le_row_column_">write_unicode_le($row, $column, $string, $format)</A></H2>
1452
This method is the same as <CODE>write_unicode()</CODE> except that the string should be 16-bit characters in little-endian format.
1453
This is generally referred to as UTF-16LE.
1457
UTF-16 data can be changed from little-endian to big-endian format (and
1458
vice-versa) as follows:
1463
$utf16 = pack "n*", unpack "v*", $utf16;
1467
Note, it is slightly faster to write little-endian data via
1468
<CODE>write_unicode_le()</CODE> than it is to write big-endian data via
1469
<CODE>write_unicode().</CODE>
1474
<H2><A NAME="keep_leading_zeros_">keep_leading_zeros()</A></H2>
1476
This method changes the default handling of integers with leading zeros
1477
when using the <CODE>write()</CODE> method.
1481
The <CODE>write()</CODE> method uses regular expressions to determine what type of data to write to
1482
an Excel worksheet. If the data looks like a number it writes a number
1483
using <CODE>write_number()</CODE>. One problem with this approach is that occasionally data looks like a
1484
number but you don't want it treated as a number.
1488
Zip codes and ID numbers, for example, often start with a leading zero. If
1489
you write this data as a number then the leading <CODE>zero(s)</CODE> will
1490
be stripped. This is the also the default behaviour when you enter data
1495
To get around this you can use one of three options. Write a formatted
1496
number, write the number as a string or use the <CODE>keep_leading_zeros()</CODE> method to change the default behaviour of <CODE>write()</CODE>:
1501
# Implicitly write a number, the leading zero is removed: 1209
1502
$worksheet->write('A1', '01209');
1507
# Write a zero padded number using a format: 01209
1508
my $format1 = $workbook->add_format(num_format => '00000');
1509
$worksheet->write('A2', '01209', $format1);
1514
# Write explicitly as a string: 01209
1515
$worksheet->write_string('A3', '01209');
1520
# Write implicitly as a string: 01209
1521
$worksheet->keep_leading_zeros();
1522
$worksheet->write('A4', '01209');
1526
The above code would generate a worksheet that looked like the following:
1531
-----------------------------------------------------------
1532
| | A | B | C | D | ...
1533
-----------------------------------------------------------
1534
| 1 | 1209 | | | | ...
1535
| 2 | 01209 | | | | ...
1536
| 3 | 01209 | | | | ...
1537
| 4 | 01209 | | | | ...
1541
The examples are on different sides of the cells due to the fact that Excel
1542
displays strings with a left justification and numbers with a right
1543
justification by default. You can change this by using a format to justify
1544
the data, see <A HREF="#CELL_FORMATTING">CELL FORMATTING</A>.
1548
It should be noted that if the user edits the data in examples <CODE>A3</CODE> and <CODE>A4</CODE> the strings will revert back to numbers. Again this is Excel's default
1549
behaviour. To avoid this you can use the text format <CODE>@</CODE>:
1554
# Format as a string (01209)
1555
my $format2 = $workbook->add_format(num_format => '@');
1556
$worksheet->write_string('A5', '01209', $format2);
1560
The <CODE>keep_leading_zeros()</CODE> property is off by default. The <CODE>keep_leading_zeros()</CODE> method takes 0 or 1 as an argument. It defaults to 1 if an argument isn't
1566
$worksheet->keep_leading_zeros(); # Set on
1567
$worksheet->keep_leading_zeros(1); # Set on
1568
$worksheet->keep_leading_zeros(0); # Set off
1572
See also the <CODE>add_write_handler()</CODE> method.
1577
<H2><A NAME="write_blank_row_column_form">write_blank($row, $column, $format)</A></H2>
1579
Write a blank cell specified by <CODE>$row</CODE> and <CODE>$column</CODE>:
1584
$worksheet->write_blank(0, 0, $format);
1588
This method is used to add formatting to a cell which doesn't contain a
1589
string or number value.
1593
Excel differentiates between an "Empty" cell and a "Blank" cell. An "Empty"
1594
cell is a cell which doesn't contain data whilst a "Blank" cell is a cell
1595
which doesn't contain data but does contain formatting. Excel stores
1596
"Blank" cells but ignores "Empty" cells.
1600
As such, if you write an empty cell without formatting it is ignored:
1605
$worksheet->write('A1', undef, $format); # write_blank()
1606
$worksheet->write('A2', undef ); # Ignored
1610
This seemingly uninteresting fact means that you can write arrays of data
1611
without special treatment for undef or empty string values.
1615
See the note about <A HREF="#Cell_notation">Cell notation</A>.
1620
<H2><A NAME="write_row_row_column_array_">write_row($row, $column, $array_ref, $format)</A></H2>
1622
The <CODE>write_row()</CODE> method can be used to write a 1D or 2D array of data in one go. This is
1623
useful for converting the results of a database query into an Excel
1624
worksheet. You must pass a reference to the array of data rather than the
1625
array itself. The <CODE>write()</CODE> method is then called for each element of the data. For example:
1630
@array = ('awk', 'gawk', 'mawk');
1631
$array_ref = \@array;
1636
$worksheet->write_row(0, 0, $array_ref);
1641
# The above example is equivalent to:
1642
$worksheet->write(0, 0, $array[0]);
1643
$worksheet->write(0, 1, $array[1]);
1644
$worksheet->write(0, 2, $array[2]);
1648
Note: For convenience the <CODE>write()</CODE> method behaves in the same way as <CODE>write_row()</CODE> if it is passed an array reference. Therefore the following two method
1649
calls are equivalent:
1654
$worksheet->write_row('A1', $array_ref); # Write a row of data
1655
$worksheet->write( 'A1', $array_ref); # Same thing
1659
As with all of the write methods the <CODE>$format</CODE> parameter is optional. If a format is specified it is applied to all the
1660
elements of the data array.
1664
Array references within the data will be treated as columns. This allows
1665
you to write 2D arrays of data in one go. For example:
1671
['maggie', 'milly', 'molly', 'may' ],
1673
['shell', 'star', 'crab', 'stone']
1679
$worksheet->write_row('A1', \@eec);
1683
Would produce a worksheet as follows:
1688
-----------------------------------------------------------
1689
| | A | B | C | D | E | ...
1690
-----------------------------------------------------------
1691
| 1 | maggie | 13 | shell | ... | ... | ...
1692
| 2 | milly | 14 | star | ... | ... | ...
1693
| 3 | molly | 15 | crab | ... | ... | ...
1694
| 4 | may | 16 | stone | ... | ... | ...
1695
| 5 | ... | ... | ... | ... | ... | ...
1696
| 6 | ... | ... | ... | ... | ... | ...
1700
To write the data in a row-column order refer to the <CODE>write_col()</CODE> method below.
1704
Any <CODE>undef</CODE> values in the data will be ignored unless a format is applied to the data,
1705
in which case a formatted blank cell will be written. In either case the
1706
appropriate row or column value will still be incremented.
1710
To find out more about array references refer to <CODE>perlref</CODE> and <CODE>perlreftut</CODE> in the main Perl documentation. To find out more about 2D arrays or "lists
1711
of lists" refer to <CODE>perllol</CODE>.
1715
The <CODE>write_row()</CODE> method returns the first error encountered when writing the elements of the
1716
data or zero if no errors were encountered. See the return values described
1717
for the <CODE>write()</CODE> method above.
1721
See also the <CODE>write_arrays.pl</CODE> program in the <CODE>examples</CODE> directory of the distro.
1725
The <CODE>write_row()</CODE> method allows the following idiomatic conversion of a text file to an Excel
1737
use Spreadsheet::WriteExcel;
1742
my $workbook = Spreadsheet::WriteExcel->new('file.xls');
1743
my $worksheet = $workbook->add_worksheet();
1748
open INPUT, "file.txt" or die "Couldn't open file: $!";
1753
$worksheet->write($.-1, 0, [split]) while <INPUT>;
1758
<H2><A NAME="write_col_row_column_array_">write_col($row, $column, $array_ref, $format)</A></H2>
1760
The <CODE>write_col()</CODE> method can be used to write a 1D or 2D array of data in one go. This is
1761
useful for converting the results of a database query into an Excel
1762
worksheet. You must pass a reference to the array of data rather than the
1763
array itself. The <CODE>write()</CODE> method is then called for each element of the data. For example:
1768
@array = ('awk', 'gawk', 'mawk');
1769
$array_ref = \@array;
1774
$worksheet->write_col(0, 0, $array_ref);
1779
# The above example is equivalent to:
1780
$worksheet->write(0, 0, $array[0]);
1781
$worksheet->write(1, 0, $array[1]);
1782
$worksheet->write(2, 0, $array[2]);
1786
As with all of the write methods the <CODE>$format</CODE> parameter is optional. If a format is specified it is applied to all the
1787
elements of the data array.
1791
Array references within the data will be treated as rows. This allows you
1792
to write 2D arrays of data in one go. For example:
1798
['maggie', 'milly', 'molly', 'may' ],
1800
['shell', 'star', 'crab', 'stone']
1806
$worksheet->write_col('A1', \@eec);
1810
Would produce a worksheet as follows:
1815
-----------------------------------------------------------
1816
| | A | B | C | D | E | ...
1817
-----------------------------------------------------------
1818
| 1 | maggie | milly | molly | may | ... | ...
1819
| 2 | 13 | 14 | 15 | 16 | ... | ...
1820
| 3 | shell | star | crab | stone | ... | ...
1821
| 4 | ... | ... | ... | ... | ... | ...
1822
| 5 | ... | ... | ... | ... | ... | ...
1823
| 6 | ... | ... | ... | ... | ... | ...
1827
To write the data in a column-row order refer to the <CODE>write_row()</CODE> method above.
1831
Any <CODE>undef</CODE> values in the data will be ignored unless a format is applied to the data,
1832
in which case a formatted blank cell will be written. In either case the
1833
appropriate row or column value will still be incremented.
1837
As noted above the <CODE>write()</CODE> method can be used as a synonym for <CODE>write_row()</CODE> and <CODE>write_row()</CODE> handles nested array refs as columns. Therefore, the following two method
1838
calls are equivalent although the more explicit call to <CODE>write_col()</CODE> would be preferable for maintainability:
1843
$worksheet->write_col('A1', $array_ref ); # Write a column of data
1844
$worksheet->write( 'A1', [ $array_ref ]); # Same thing
1848
To find out more about array references refer to <CODE>perlref</CODE> and <CODE>perlreftut</CODE> in the main Perl documentation. To find out more about 2D arrays or "lists
1849
of lists" refer to <CODE>perllol</CODE>.
1853
The <CODE>write_col()</CODE> method returns the first error encountered when writing the elements of the
1854
data or zero if no errors were encountered. See the return values described
1855
for the <CODE>write()</CODE> method above.
1859
See also the <CODE>write_arrays.pl</CODE> program in the <CODE>examples</CODE> directory of the distro.
1864
<H2><A NAME="write_date_time_row_col_dat">write_date_time($row, $col, $date_string, $format)</A></H2>
1866
The <CODE>write_date_time()</CODE> method can be used to write a date or time to the cell specified by <CODE>$row</CODE> and <CODE>$column</CODE>:
1871
$worksheet->write_date_time('A1', '2004-05-13T23:20', $date_format);
1875
The <CODE>$date_string</CODE> should be in the following format:
1880
yyyy-mm-ddThh:mm:ss.sss
1884
This conforms to am ISO8601 date but it should be noted that the full range
1885
of ISO8601 formats are not supported.
1889
The following variations on the <CODE>$date_string</CODE> parameter are permitted:
1894
yyyy-mm-ddThh:mm:ss.sss # Standard format
1895
yyyy-mm-ddT # No time
1896
Thh:mm:ss.sss # No date
1897
yyyy-mm-ddThh:mm:ss.sssZ # Additional Z (but not time zones)
1898
yyyy-mm-ddThh:mm:ss # No fractional seconds
1899
yyyy-mm-ddThh:mm # No seconds
1903
Note that the <CODE>T</CODE> is required in all cases.
1907
A date should always have a <CODE>$format</CODE>, otherwise it will appear as a number, see <A HREF="#DATES_IN_EXCEL">DATES IN EXCEL</A> and <A HREF="#CELL_FORMATTING">CELL FORMATTING</A>. Here is a typical example:
1912
my $date_format = $workbook->add_format(num_format => 'mm/dd/yy');
1913
$worksheet->write_date_time('A1', '2004-05-13T23:20', $date_format);
1917
Valid dates should be in the range 1900-01-01 to 9999-12-31, for the 1900
1918
epoch and 1904-01-01 to 9999-12-31, for the 1904 epoch. As with Excel,
1919
dates outside these ranges will be written as a string.
1923
See also the date_time.pl program in the <CODE>examples</CODE> directory of the distro.
1928
<H2><A NAME="write_url_row_col_url_str">write_url($row, $col, $url, $string, $format)</A></H2>
1930
Write a hyperlink to a URL in the cell specified by <CODE>$row</CODE> and <CODE>$column</CODE>. The hyperlink is comprised of two elements: the visible label and the
1931
invisible link. The visible label is the same as the link unless an
1932
alternative string is specified. The parameters <CODE>$string</CODE> and the <CODE>$format</CODE> are optional and their position is interchangeable.
1936
The label is written using the <CODE>write_string()</CODE> method. Therefore the 255 characters string limit applies to the label: the
1937
URL can be any length.
1941
There are four web style URI's supported: <CODE>http://</CODE>, <CODE>https://</CODE>, <CODE>ftp://</CODE> and <CODE>mailto:</CODE>:
1946
$worksheet->write_url(0, 0, 'ftp://www.perl.org/' );
1947
$worksheet->write_url(1, 0, 'http://www.perl.com/', 'Perl home' );
1948
$worksheet->write_url('A3', 'http://www.perl.com/', $format );
1949
$worksheet->write_url('A4', 'http://www.perl.com/', 'Perl', $format);
1950
$worksheet->write_url('A5', 'mailto:jmcnamara@cpan.org' );
1954
There are two local URIs supported: <CODE>internal:</CODE> and <CODE>external:</CODE>. These are used for hyperlinks to internal worksheet references or
1955
external workbook and worksheet references:
1960
$worksheet->write_url('A6', 'internal:Sheet2!A1' );
1961
$worksheet->write_url('A7', 'internal:Sheet2!A1', $format );
1962
$worksheet->write_url('A8', 'internal:Sheet2!A1:B2' );
1963
$worksheet->write_url('A9', q{internal:'Sales Data'!A1} );
1964
$worksheet->write_url('A10', 'external:c:\temp\foo.xls' );
1965
$worksheet->write_url('A11', 'external:c:\temp\foo.xls#Sheet2!A1' );
1966
$worksheet->write_url('A12', 'external:..\..\..\foo.xls' );
1967
$worksheet->write_url('A13', 'external:..\..\..\foo.xls#Sheet2!A1' );
1968
$worksheet->write_url('A13', 'external:\\\\NETWORK\share\foo.xls' );
1972
All of the these URI types are recognised by the <CODE>write()</CODE> method, see above.
1976
Worksheet references are typically of the form <CODE>Sheet1!A1</CODE>. You can also refer to a worksheet range using the standard Excel
1977
notation: <CODE>Sheet1!A1:B2</CODE>.
1981
In external links the workbook and worksheet name must be separated by the <CODE>#</CODE> character: <CODE>external:Workbook.xls#Sheet1!A1'</CODE>.
1985
You can also link to a named range in the target worksheet. For example say
1986
you have a named range called <CODE>my_name</CODE> in the workbook <CODE>c:\temp\foo.xls</CODE> you could link to it as follows:
1991
$worksheet->write_url('A14', 'external:c:\temp\foo.xls#my_name');
1995
Note, you cannot currently create named ranges with <CODE>Spreadsheet::WriteExcel</CODE>.
1999
Excel requires that worksheet names containing spaces or non alphanumeric
2000
characters are single quoted as follows <CODE>'Sales Data'!A1</CODE>. If you need to do this in a single quoted string then you can either
2001
escape the single quotes <CODE>\'</CODE> or use the quote operator <CODE>q{}</CODE> as described in <CODE>perlop</CODE> in the main Perl documentation.
2005
Links to network files are also supported. MS/Novell Network files normally
2006
begin with two back slashes as follows <CODE>\\NETWORK\etc</CODE>. In order to generate this in a single or double quoted string you will
2007
have to escape the backslashes, <CODE>'\\\\NETWORK\etc'</CODE>.
2011
If you are using double quote strings then you should be careful to escape
2012
anything that looks like a metacharacter. For more information see <CODE>perlfaq5: Why can't I use "C:\temp\foo" in DOS paths?</CODE>.
2016
Finally, you can avoid most of these quoting problems by using forward
2017
slashes. These are translated internally to backslashes:
2022
$worksheet->write_url('A14', "external:c:/temp/foo.xls" );
2023
$worksheet->write_url('A15', 'external://NETWORK/share/foo.xls' );
2027
See also, the note about <A HREF="#Cell_notation">Cell notation</A>.
2032
<H2><A NAME="write_url_range_row1_col1_r">write_url_range($row1, $col1, $row2, $col2, $url, $string, $format)</A></H2>
2034
This method is essentially the same as the <CODE>write_url()</CODE> method described above. The main difference is that you can specify a link
2035
for a range of cells:
2040
$worksheet->write_url(0, 0, 0, 3, 'ftp://www.perl.org/' );
2041
$worksheet->write_url(1, 0, 0, 3, 'http://www.perl.com/', 'Perl home');
2042
$worksheet->write_url('A3:D3', 'internal:Sheet2!A1' );
2043
$worksheet->write_url('A4:D4', 'external:c:\temp\foo.xls' );
2047
This method is generally only required when used in conjunction with merged
2048
cells. See the <CODE>merge_range()</CODE> method and the <CODE>merge</CODE> property of a Format object, <A HREF="#CELL_FORMATTING">CELL FORMATTING</A>.
2052
There is no way to force this behaviour through the <CODE>write()</CODE> method.
2056
The parameters <CODE>$string</CODE> and the <CODE>$format</CODE> are optional and their position is interchangeable. However, they are
2057
applied only to the first cell in the range.
2061
See also, the note about <A HREF="#Cell_notation">Cell notation</A>.
2066
<H2><A NAME="write_formula_row_column_fo">write_formula($row, $column, $formula, $format, $value)</A></H2>
2068
Write a formula or function to the cell specified by <CODE>$row</CODE> and <CODE>$column</CODE>:
2073
$worksheet->write_formula(0, 0, '=$B$3 + B4' );
2074
$worksheet->write_formula(1, 0, '=SIN(PI()/4)');
2075
$worksheet->write_formula(2, 0, '=SUM(B1:B5)' );
2076
$worksheet->write_formula('A4', '=IF(A3>1,"Yes", "No")' );
2077
$worksheet->write_formula('A5', '=AVERAGE(1, 2, 3, 4)' );
2078
$worksheet->write_formula('A6', '=DATEVALUE("1-Jan-2001")');
2082
See the note about <A HREF="#Cell_notation">Cell notation</A>. For more information about writing Excel formulas see <A HREF="#FORMULAS_AND_FUNCTIONS_IN_EXCEL">FORMULAS AND FUNCTIONS IN EXCEL</A>
2088
See also the section "Improving performance when working with formulas" and
2089
the <CODE>store_formula()</CODE> and <CODE>repeat_formula()</CODE> methods.
2093
If required, it is also possible to specify the calculated value of the
2094
formula. This is occasionally necessary when working with non-Excel
2095
applications that don't calculated the value of the formula. The calculated <CODE>$value</CODE> is added at the end of the argument list:
2100
$worksheet->write('A1', '=2+2', $format, 4);
2104
However, this probably isn't something that will ever need to do. If you do
2105
use this feature then do so with care.
2110
<H2><A NAME="store_formula_formula_">store_formula($formula)</A></H2>
2112
The <CODE>store_formula()</CODE> method is used in conjunction with <CODE>repeat_formula()</CODE> to speed up the generation of repeated formulas. See "Improving performance
2113
when working with formulas" in <A HREF="#FORMULAS_AND_FUNCTIONS_IN_EXCEL">FORMULAS AND FUNCTIONS IN EXCEL</A>.
2117
The <CODE>store_formula()</CODE> method pre-parses a textual representation of a formula and stores it for
2118
use at a later stage by the <CODE>repeat_formula()</CODE> method.
2122
<CODE>store_formula()</CODE> carries the same speed penalty as <CODE>write_formula()</CODE>. However, in practice it will be used less frequently.
2126
The return value of this method is a scalar that can be thought of as a
2127
reference to a formula.
2132
my $sin = $worksheet->store_formula('=SIN(A1)');
2133
my $cos = $worksheet->store_formula('=COS(A1)');
2138
$worksheet->repeat_formula('B1', $sin, $format, 'A1', 'A2');
2139
$worksheet->repeat_formula('C1', $cos, $format, 'A1', 'A2');
2143
Although <CODE>store_formula()</CODE> is a worksheet method the return value can be used in any worksheet:
2148
my $now = $worksheet->store_formula('=NOW()');
2153
$worksheet1->repeat_formula('B1', $now);
2154
$worksheet2->repeat_formula('B1', $now);
2155
$worksheet3->repeat_formula('B1', $now);
2160
<H2><A NAME="repeat_formula_row_col_form">repeat_formula($row, $col, $formula, $format, ($pattern => $replace, ...))</A></H2>
2162
The <CODE>repeat_formula()</CODE> method is used in conjunction with <CODE>store_formula()</CODE> to speed up the generation of repeated formulas. See "Improving performance
2163
when working with formulas" in <A HREF="#FORMULAS_AND_FUNCTIONS_IN_EXCEL">FORMULAS AND FUNCTIONS IN EXCEL</A>.
2167
In many respects <CODE>repeat_formula()</CODE> behaves like <CODE>write_formula()</CODE> except that it is significantly faster.
2171
The <CODE>repeat_formula()</CODE> method creates a new formula based on the pre-parsed tokens returned by <CODE>store_formula()</CODE>. The new formula is generated by substituting <CODE>$pattern</CODE>, <CODE>$replace</CODE> pairs in the stored formula:
2176
my $formula = $worksheet->store_formula('=A1 * 3 + 50');
2181
for my $row (0..99) {
2182
$worksheet->repeat_formula($row, 1, $formula, $format, 'A1', 'A'.($row +1));
2187
It should be noted that <CODE>repeat_formula()</CODE> doesn't modify the tokens. In the above example the substitution is always
2188
made against the original token, <CODE>A1</CODE>, which doesn't change.
2192
As usual, you can use <CODE>undef</CODE> if you don't wish to specify a <CODE>$format</CODE>:
2197
$worksheet->repeat_formula('B2', $formula, $format, 'A1', 'A2');
2198
$worksheet->repeat_formula('B3', $formula, undef, 'A1', 'A3');
2202
The substitutions are made from left to right and you can use as many <CODE>$pattern</CODE>, <CODE>$replace</CODE> pairs as you need. However, each substitution is made only once:
2207
my $formula = $worksheet->store_formula('=A1 + A1');
2213
$worksheet->repeat_formula('B1', $formula, undef, 'A1', 'B1');
2219
$worksheet->repeat_formula('B2', $formula, undef, ('A1', 'B1') x 2);
2223
Since the <CODE>$pattern</CODE> is interpolated each time that it is used it is worth using the <CODE>qr</CODE> operator to quote the pattern. The <CODE>qr</CODE> operator is explained in the <CODE>perlop</CODE> man page.
2228
$worksheet->repeat_formula('B1', $formula, $format, qr/A1/, 'A2');
2232
Care should be taken with the values that are substituted. The formula
2233
returned by <CODE>repeat_formula()</CODE> contains several other tokens in addition to those in the formula and these
2234
might also match the pattern that you are trying to replace. In particular
2235
you should avoid substituting a single 0, 1, 2 or 3.
2239
You should also be careful to avoid false matches. For example the
2240
following snippet is meant to change the stored formula in steps from <CODE>=A1 + SIN(A1)</CODE> to <CODE>=A10 + SIN(A10)</CODE>.
2245
my $formula = $worksheet->store_formula('=A1 + SIN(A1)');
2250
for my $row (1 .. 10) {
2251
$worksheet->repeat_formula($row -1, 1, $formula, undef,
2252
qw/A1/, 'A' . $row, #! Bad.
2253
qw/A1/, 'A' . $row #! Bad.
2259
However it contains a bug. In the last iteration of the loop when <CODE>$row</CODE> is 10 the following substitutions will occur:
2264
s/A1/A10/; changes =A1 + SIN(A1) to =A10 + SIN(A1)
2265
s/A1/A10/; changes =A10 + SIN(A1) to =A100 + SIN(A1) # !!
2269
The solution in this case is to use a more explicit match such as <CODE>qw/^A1$/</CODE>:
2274
$worksheet->repeat_formula($row -1, 1, $formula, undef,
2275
qw/^A1$/, 'A' . $row,
2276
qw/^A1$/, 'A' . $row
2281
Another similar problem occurs due to the fact that substitutions are made
2282
in order. For example the following snippet is meant to change the stored
2283
formula from <CODE>=A10 + A11</CODE> to <CODE>=A11 + A12</CODE>:
2288
my $formula = $worksheet->store_formula('=A10 + A11');
2293
$worksheet->repeat_formula('A1', $formula, undef,
2294
qw/A10/, 'A11', #! Bad.
2295
qw/A11/, 'A12' #! Bad.
2300
However, the actual substitution yields <CODE>=A12 + A11</CODE>:
2305
s/A10/A11/; changes =A10 + A11 to =A11 + A11
2306
s/A11/A12/; changes =A11 + A11 to =A12 + A11 # !!
2310
The solution here would be to reverse the order of the substitutions or to
2311
start with a stored formula that won't yield a false match such as <CODE>=X10 + Y11</CODE>:
2316
my $formula = $worksheet->store_formula('=X10 + Y11');
2321
$worksheet->repeat_formula('A1', $formula, undef,
2328
If you think that you have a problem related to a false match you can check
2329
the tokens that you are substituting against as follows.
2334
my $formula = $worksheet->store_formula('=A1*5+4');
2335
print "@$formula\n";
2339
See also the <CODE>repeat.pl</CODE> program in the <CODE>examples</CODE> directory of the distro.
2344
<H2><A NAME="write_comment_row_column_st">write_comment($row, $column, $string)</A></H2>
2346
<STRONG>NOTE: this method is not available in this release. Use the 1.xx versions of this module if you need this feature</STRONG>.
2350
The <CODE>write_comment()</CODE> method is used to add a comment to a cell. A cell comment is indicated in
2351
Excel by a small red triangle in the upper right-hand corner of the cell.
2352
Moving the cursor over the red triangle will cause the comment to appear.
2356
The following example shows how to add a comment to a cell:
2361
$worksheet->write("C3", "Hello");
2362
$worksheet->write_comment("C3", "This is a comment.");
2366
The cell comment can be up to 30,000 characters in length.
2371
<H2><A NAME="add_write_handler_re_code_ref">add_write_handler($re, $code_ref)</A></H2>
2373
This method is used to extend the Spreadsheet::WriteExcel
2374
<CODE>write()</CODE> method to handle user defined data.
2378
If you refer to the section on <CODE>write()</CODE> above you will see that it acts as an alias for several more specific <CODE>write_*</CODE> methods. However, it doesn't always act in exactly the way that you would
2383
One solution is to filter the input data yourself and call the appropriate <CODE>write_*</CODE> method. Another approach is to use the <CODE>add_write_handler()</CODE> method to add your own automated behaviour to <CODE>write()</CODE>.
2387
The <CODE>add_write_handler()</CODE> method take two arguments, <CODE>$re</CODE>, a regular expression to match incoming data and <CODE>$code_ref</CODE> a callback function to handle the matched data:
2392
$worksheet->add_write_handler(qr/^\d\d\d\d$/, \&my_write);
2396
(In the these examples the <CODE>qr</CODE> operator is used to quote the regular expression strings, see <EM>perlop</EM> for more details).
2400
The method is use as follows. say you wished to write 7 digit ID numbers as
2401
a string so that any leading zeros were preserved*, you could do something
2407
$worksheet->add_write_handler(qr/^\d{7}$/, \&write_my_id);
2413
my $worksheet = shift;
2414
return $worksheet->write_string(@_);
2419
* You could also use the <CODE>keep_leading_zeros()</CODE> method for this.
2423
Then if you call <CODE>write()</CODE> with an appropriate string it will be handled automatically:
2428
# Writes 0000000. It would normally be written as a number; 0.
2429
$worksheet->write('A1', '0000000');
2433
The callback function will receive a reference to the calling worksheet and
2434
all of the other arguments that were passed to <CODE>write()</CODE>. The callback will see an <CODE>@_</CODE> argument list that looks like the following:
2439
$_[0] A ref to the calling worksheet. *
2440
$_[1] Zero based row number.
2441
$_[2] Zero based column number.
2442
$_[3] A number or string or token.
2443
$_[4] A format ref if any.
2444
$_[5] Any other argruments.
2450
* It is good style to shift this off the list so the @_ is the same
2451
as the argument list seen by write().
2455
Your callback should <CODE>return()</CODE> the return value of the <CODE>write_*</CODE> method that was called or <CODE>undef</CODE> to indicate that you rejected the match and want <CODE>write()</CODE> to continue as normal.
2459
So for example if you wished to apply the previous filter only to ID values
2460
that occur in the first column you could modify your callback function as
2467
my $worksheet = shift;
2474
return $worksheet->write_string(@_);
2477
# Reject the match and return control to write()
2484
Now, you will get different behaviour for the first column and other
2490
$worksheet->write('A1', '0000000'); # Writes 0000000
2491
$worksheet->write('B1', '0000000'); # Writes 0
2495
You may add more than one handler in which case they will be called in the
2496
order that they were added.
2500
Note, the <CODE>add_write_handler()</CODE> method is particularly suited for handling dates.
2504
See the <CODE>write_handler 1-4</CODE> programs in the <CODE>examples</CODE> directory for further examples.
2509
<H2><A NAME="insert_bitmap_row_col_filen">insert_bitmap($row, $col, $filename, $x, $y, $scale_x, $scale_y)</A></H2>
2511
This method can be used to insert a bitmap into a worksheet. The bitmap
2512
must be a 24 bit, true colour, bitmap. No other format is supported. The <CODE>$x</CODE>, <CODE>$y</CODE>, <CODE>$scale_x</CODE> and <CODE>$scale_y</CODE> parameters are optional.
2517
$worksheet1->insert_bitmap('A1', 'perl.bmp');
2518
$worksheet2->insert_bitmap('A1', '../images/perl.bmp');
2519
$worksheet3->insert_bitmap('A1', '.c:\images\perl.bmp');
2523
Note: you must call <CODE>set_row()</CODE> or <CODE>set_column()</CODE> before <CODE>insert_bitmap()</CODE> if you wish to change the default dimensions of any of the rows or columns
2524
that the images occupies. The height of a row can also change if you use a
2525
font that is larger than the default. This in turn will affect the scaling
2526
of your image. To avoid this you should explicitly set the height of the
2527
row using <CODE>set_row()</CODE> if it contains a font size that will change the row height.
2531
The parameters <CODE>$x</CODE> and <CODE>$y</CODE> can be used to specify an offset from the top left hand corner of the cell
2532
specified by <CODE>$row</CODE> and <CODE>$col</CODE>. The offset values are in pixels.
2537
$worksheet1->insert_bitmap('A1', 'perl.bmp', 32, 10);
2541
The default width of a cell is 63 pixels. The default height of a cell is
2542
17 pixels. The pixels offsets can be calculated using the following
2548
Wp = int(12We) if We < 1
2549
Wp = int(7We +5) if We >= 1
2556
We is the cell width in Excels units
2557
Wp is width in pixels
2558
He is the cell height in Excels units
2559
Hp is height in pixels
2563
The offsets can be greater than the width or height of the underlying cell.
2564
This can be occasionally useful if you wish to align two or more images
2565
relative to the same cell.
2569
The parameters <CODE>$scale_x</CODE> and <CODE>$scale_y</CODE> can be used to scale the inserted image horizontally and vertically:
2574
# Scale the inserted image: width x 2.0, height x 0.8
2575
$worksheet->insert_bitmap('A1', 'perl.bmp', 0, 0, 2, 0.8);
2579
Note: although Excel allows you to import several graphics formats such as
2580
gif, jpeg, png and eps these are converted internally into a proprietary
2581
format. One of the few non-proprietary formats that Excel supports is 24
2582
bit, true colour, bitmaps. Therefore if you wish to use images in any other
2583
format you must first use an external application such as the ImageMagick <EM>convert</EM> utility to convert them to 24 bit bitmaps.
2588
convert test.png test.bmp
2592
A later release will support the use of file handles and pre-encoded bitmap
2597
See also the <CODE>images.pl</CODE> program in the <CODE>examples</CODE> directory of the distro.
2602
<H2><A NAME="get_name_">get_name()</A></H2>
2604
The <CODE>get_name()</CODE> method is used to retrieve the name of a worksheet. For example:
2609
foreach my $sheet ($workbook->sheets()) {
2610
print $sheet->get_name();
2616
<H2><A NAME="activate_">activate()</A></H2>
2618
The <CODE>activate()</CODE> method is used to specify which worksheet is initially visible in a
2619
multi-sheet workbook:
2624
$worksheet1 = $workbook->add_worksheet('To');
2625
$worksheet2 = $workbook->add_worksheet('the');
2626
$worksheet3 = $workbook->add_worksheet('wind');
2631
$worksheet3->activate();
2635
This is similar to the Excel VBA activate method. More than one worksheet
2636
can be selected via the <CODE>select()</CODE> method, however only one worksheet can be active. The default value is the
2642
<H2><A NAME="select_">select()</A></H2>
2644
The <CODE>select()</CODE> method is used to indicate that a worksheet is selected in a multi-sheet
2650
$worksheet1->activate();
2651
$worksheet2->select();
2652
$worksheet3->select();
2656
A selected worksheet has its tab highlighted. Selecting worksheets is a way
2657
of grouping them together so that, for example, several worksheets could be
2658
printed in one go. A worksheet that has been activated via the <CODE>activate()</CODE> method will also appear as selected. You probably won't need to use the <CODE>select()</CODE> method very often.
2663
<H2><A NAME="set_first_sheet_">set_first_sheet()</A></H2>
2665
The <CODE>activate()</CODE> method determines which worksheet is initially selected. However, if there
2666
are a large number of worksheets the selected worksheet may not appear on
2667
the screen. To avoid this you can select which is the leftmost visible
2668
worksheet using <CODE>set_first_sheet()</CODE>:
2674
$workbook->add_worksheet;
2680
$worksheet21 = $workbook->add_worksheet();
2681
$worksheet22 = $workbook->add_worksheet();
2686
$worksheet21->set_first_sheet();
2687
$worksheet22->activate();
2691
This method is not required very often. The default value is the first
2697
<H2><A NAME="protect_password_">protect($password)</A></H2>
2699
The <CODE>protect()</CODE> method is used to protect a worksheet from modification:
2704
$worksheet->protect();
2708
It can be turned off in Excel via the <CODE>Tools->Protection->Unprotect Sheet</CODE> menu command.
2712
The <CODE>protect()</CODE> method also has the effect of enabling a cell's <CODE>locked</CODE> and <CODE>hidden</CODE> properties if they have been set. A "locked" cell cannot be edited. A
2713
"hidden" cell will display the results of a formula but not the formula
2714
itself. In Excel a cell's locked property is on by default.
2719
# Set some format properties
2720
my $unlocked = $workbook->add_format(locked => 0);
2721
my $hidden = $workbook->add_format(hidden => 1);
2726
# Enable worksheet protection
2727
$worksheet->protect();
2732
# This cell cannot be edited, it is locked by default
2733
$worksheet->write('A1', '=1+2');
2738
# This cell can be edited
2739
$worksheet->write('A2', '=1+2', $unlocked);
2744
# The formula in this cell isn't visible
2745
$worksheet->write('A3', '=1+2', $hidden);
2749
See also the <CODE>set_locked</CODE> and <CODE>set_hidden</CODE> format methods in <A HREF="#CELL_FORMATTING">CELL FORMATTING</A>.
2753
You can optionally add a password to the worksheet protection:
2758
$worksheet->protect('drowssap');
2762
Note, the worksheet level password in Excel provides very weak protection.
2763
It does not encrypt your data in any way and it is very easy to deactivate.
2764
Therefore, do not use the above method if you wish to protect sensitive
2765
data or calculations. However, before you get worried, Excel's own workbook
2766
level password protection does provide strong encryption in Excel 97+. For
2767
technical reasons this will never be supported by <CODE>Spreadsheet::WriteExcel</CODE>.
2772
<H2><A NAME="set_selection_first_row_first">set_selection($first_row, $first_col, $last_row, $last_col)</A></H2>
2774
This method can be used to specify which cell or cells are selected in a
2775
worksheet. The most common requirement is to select a single cell, in which
2776
case <CODE>$last_row</CODE> and <CODE>$last_col</CODE> can be omitted. The active cell within a selected range is determined by
2777
the order in which <CODE>$first</CODE> and <CODE>$last</CODE> are specified. It is also possible to specify a cell or a range using A1
2778
notation. See the note about <A HREF="#Cell_notation">Cell notation</A>.
2787
$worksheet1->set_selection(3, 3); # 1. Cell D4.
2788
$worksheet2->set_selection(3, 3, 6, 6); # 2. Cells D4 to G7.
2789
$worksheet3->set_selection(6, 6, 3, 3); # 3. Cells G7 to D4.
2790
$worksheet4->set_selection('D4'); # Same as 1.
2791
$worksheet5->set_selection('D4:G7'); # Same as 2.
2792
$worksheet6->set_selection('G7:D4'); # Same as 3.
2796
The default cell selections is (0, 0), 'A1'.
2801
<H2><A NAME="set_row_row_height_format_">set_row($row, $height, $format, $hidden, $level)</A></H2>
2803
This method can be used to change the default properties of a row. All
2804
parameters apart from <CODE>$row</CODE> are optional.
2808
The most common use for this method is to change the height of a row:
2813
$worksheet->set_row(0, 20); # Row 1 height set to 20
2817
If you wish to set the format without changing the height you can pass <CODE>undef</CODE> as the height parameter:
2822
$worksheet->set_row(0, undef, $format);
2826
The <CODE>$format</CODE> parameter will be applied to any cells in the row that don't have a format.
2832
$worksheet->set_row(0, undef, $format1); # Set the format for row 1
2833
$worksheet->write('A1', "Hello"); # Defaults to $format1
2834
$worksheet->write('B1', "Hello", $format2); # Keeps $format2
2838
If you wish to define a row format in this way you should call the method
2839
before any calls to <CODE>write()</CODE>. Calling it afterwards will overwrite any format that was previously
2844
The <CODE>$hidden</CODE> parameter should be set to 1 if you wish to hide a row. This can be used,
2845
for example, to hide intermediary steps in a complicated calculation:
2850
$worksheet->set_row(0, 20, $format, 1);
2851
$worksheet->set_row(1, undef, undef, 1);
2855
The <CODE>$level</CODE> parameter is used to set the outline level of the row. Outlines are
2856
described in <A HREF="#OUTLINES_AND_GROUPING_IN_EXCEL">OUTLINES AND GROUPING IN EXCEL</A>. Adjacent rows with the same outline level are grouped together into a
2861
The following example sets an outline level of 1 for rows 1 and 2
2867
$worksheet->set_row(1, undef, undef, 0, 1);
2868
$worksheet->set_row(2, undef, undef, 0, 1);
2872
The <CODE>$hidden</CODE> parameter can also be used to collapse outlined rows when used in
2873
conjunction with the <CODE>$level</CODE> parameter.
2878
$worksheet->set_row(1, undef, undef, 1, 1);
2879
$worksheet->set_row(2, undef, undef, 1, 1);
2883
Excel allows up to 7 outline levels. Therefore the <CODE>$level</CODE> parameter should be in the range <CODE>0 <= $level <= 7</CODE>.
2888
<H2><A NAME="set_column_first_col_last_col">set_column($first_col, $last_col, $width, $format, $hidden, $level)</A></H2>
2890
This method can be used to change the default properties of a single column
2891
or a range of columns. All parameters apart from <CODE>$first_col</CODE> and <CODE>$last_col</CODE> are optional.
2895
If <CODE>set_column()</CODE> is applied to a single column the value of <CODE>$first_col</CODE> and <CODE>$last_col</CODE> should be the same. It is also possible to specify a column range using the
2896
form of A1 notation used for columns. See the note about <A HREF="#Cell_notation">Cell notation</A>.
2905
$worksheet->set_column(0, 0, 20); # Column A width set to 20
2906
$worksheet->set_column(1, 3, 30); # Columns B-D width set to 30
2907
$worksheet->set_column('E:E', 20); # Column E width set to 20
2908
$worksheet->set_column('F:H', 30); # Columns F-H width set to 30
2912
The width corresponds to the column width value that is specified in Excel.
2913
It is approximately equal to the length of a string in the default font of
2914
Arial 10. Unfortunately, there is no way to specify "AutoFit" for a column
2915
in the Excel file format. This feature is only available at runtime from
2920
As usual the <CODE>$format</CODE> parameter is optional, for additional information, see <A HREF="#CELL_FORMATTING">CELL FORMATTING</A>. If you wish to set the format without changing the width you can pass <CODE>undef</CODE> as the width parameter:
2925
$worksheet->set_column(0, 0, undef, $format);
2929
The <CODE>$format</CODE> parameter will be applied to any cells in the column that don't have a
2935
$worksheet->set_column('A:A', undef, $format1); # Set format for col 1
2936
$worksheet->write('A1', "Hello"); # Defaults to $format1
2937
$worksheet->write('A2', "Hello", $format2); # Keeps $format2
2941
If you wish to define a column format in this way you should call the
2942
method before any calls to <CODE>write()</CODE>. If you call it afterwards it won't have any effect.
2946
A default row format takes precedence over a default column format
2951
$worksheet->set_row(0, undef, $format1); # Set format for row 1
2952
$worksheet->set_column('A:A', undef, $format2); # Set format for col 1
2953
$worksheet->write('A1', "Hello"); # Defaults to $format1
2954
$worksheet->write('A2', "Hello"); # Defaults to $format2
2958
The <CODE>$hidden</CODE> parameter should be set to 1 if you wish to hide a column. This can be
2959
used, for example, to hide intermediary steps in a complicated calculation:
2964
$worksheet->set_column('D:D', 20, $format, 1);
2965
$worksheet->set_column('E:E', undef, undef, 1);
2969
The <CODE>$level</CODE> parameter is used to set the outline level of the column. Outlines are
2970
described in <A HREF="#OUTLINES_AND_GROUPING_IN_EXCEL">OUTLINES AND GROUPING IN EXCEL</A>. Adjacent columns with the same outline level are grouped together into a
2975
The following example sets an outline level of 1 for columns B to G:
2980
$worksheet->set_column('B:G', undef, undef, 0, 1);
2984
The <CODE>$hidden</CODE> parameter can also be used to collapse outlined columns when used in
2985
conjunction with the <CODE>$level</CODE> parameter.
2990
$worksheet->set_column('B:G', undef, undef, 1, 1);
2994
Excel allows up to 7 outline levels. Therefore the <CODE>$level</CODE> parameter should be in the range <CODE>0 <= $level <= 7</CODE>.
2999
<H2><A NAME="outline_settings_visible_symb">outline_settings($visible, $symbols_below, $symbols_right, $auto_style)</A></H2>
3001
The <CODE>outline_settings()</CODE> method is used to control the appearance of outlines in Excel. Outlines are
3002
described in <A HREF="#OUTLINES_AND_GROUPING_IN_EXCEL">OUTLINES AND GROUPING IN EXCEL</A>.
3006
The <CODE>$visible</CODE> parameter is used to control whether or not outlines are visible. Setting
3007
this parameter to 0 will cause all outlines on the worksheet to be hidden.
3008
They can be unhidden in Excel by means of the "Show Outline Symbols"
3009
command button. The default setting is 1 for visible outlines.
3014
$worksheet->outline_settings(0);
3018
The <CODE>$symbols_below</CODE> parameter is used to control whether the row outline symbol will appear
3019
above or below the outline level bar. The default setting is 1 for symbols
3020
to appear below the outline level bar.
3024
The <CODE>symbols_right</CODE> parameter is used to control whether the column outline symbol will appear
3025
to the left or the right of the outline level bar. The default setting is 1
3026
for symbols to appear to the right of the outline level bar.
3030
The <CODE>$auto_style</CODE> parameter is used to control whether the automatic outline generator in
3031
Excel uses automatic styles when creating an outline. This has no effect on
3032
a file generated by <CODE>Spreadsheet::WriteExcel</CODE> but it does have an effect on how the worksheet behaves after it is
3033
created. The default setting is 0 for "Automatic Styles" to be turned off.
3037
The default settings for all of these parameters correspond to Excel's
3042
The worksheet parameters controlled by <CODE>outline_settings()</CODE> are rarely used.
3047
<H2><A NAME="freeze_panes_row_col_top_ro">freeze_panes($row, $col, $top_row, $left_col)</A></H2>
3049
This method can be used to divide a worksheet into horizontal or vertical
3050
regions known as panes and to also "freeze" these panes so that the
3051
splitter bars are not visible. This is the same as the <CODE>Window->Freeze Panes</CODE> menu command in Excel
3055
The parameters <CODE>$row</CODE> and <CODE>$col</CODE> are used to specify the location of the split. It should be noted that the
3056
split is specified at the top or left of a cell and that the method uses
3057
zero based indexing. Therefore to freeze the first row of a worksheet it is
3058
necessary to specify the split at row 2 (which is 1 as the zero-based
3059
index). This might lead you to think that you are using a 1 based index but
3060
this is not the case.
3064
You can set one of the <CODE>$row</CODE> and <CODE>$col</CODE> parameters as zero if you do not want either a vertical or horizontal
3074
$worksheet->freeze_panes(1, 0); # Freeze the first row
3075
$worksheet->freeze_panes('A2'); # Same using A1 notation
3076
$worksheet->freeze_panes(0, 1); # Freeze the first column
3077
$worksheet->freeze_panes('B1'); # Same using A1 notation
3078
$worksheet->freeze_panes(1, 2); # Freeze first row and first 2 columns
3079
$worksheet->freeze_panes('C2'); # Same using A1 notation
3083
The parameters <CODE>$top_row</CODE> and <CODE>$left_col</CODE> are optional. They are used to specify the top-most or left-most visible
3084
row or column in the scrolling region of the panes. For example to freeze
3085
the first row and to have the scrolling region begin at row twenty:
3090
$worksheet->freeze_panes(1, 0, 20, 0);
3094
You cannot use A1 notation for the <CODE>$top_row</CODE> and <CODE>$left_col</CODE> parameters.
3098
See also the <CODE>panes.pl</CODE> program in the <CODE>examples</CODE> directory of the distribution.
3103
<H2><A NAME="thaw_panes_y_x_top_row_le">thaw_panes($y, $x, $top_row, $left_col)</A></H2>
3105
This method can be used to divide a worksheet into horizontal or vertical
3106
regions known as panes. This method is different from the <CODE>freeze_panes()</CODE> method in that the splits between the panes will be visible to the user and
3107
each pane will have its own scroll bars.
3111
The parameters <CODE>$y</CODE> and <CODE>$x</CODE> are used to specify the vertical and horizontal position of the split. The
3112
units for <CODE>$y</CODE> and <CODE>$x</CODE> are the same as those used by Excel to specify row height and column width.
3113
However, the vertical and horizontal units are different from each other.
3114
Therefore you must specify the <CODE>$y</CODE> and <CODE>$x</CODE> parameters in terms of the row heights and column widths that you have set
3115
or the default values which are <CODE>12.75</CODE> for a row and <CODE>8.43</CODE> for a column.
3119
You can set one of the <CODE>$y</CODE> and <CODE>$x</CODE> parameters as zero if you do not want either a vertical or horizontal
3120
split. The parameters <CODE>$top_row</CODE> and <CODE>$left_col</CODE> are optional. They are used to specify the top-most or left-most visible
3121
row or column in the bottom-right pane.
3130
$worksheet->thaw_panes(12.75, 0, 1, 0); # First row
3131
$worksheet->thaw_panes(0, 8.43, 0, 1); # First column
3132
$worksheet->thaw_panes(12.75, 8.43, 1, 1); # First row and column
3136
You cannot use A1 notation with this method.
3140
See also the <CODE>freeze_panes()</CODE> method and the <CODE>panes.pl</CODE> program in the <CODE>examples</CODE> directory of the distribution.
3145
<H2><A NAME="merge_range_first_row_first_c">merge_range($first_row, $first_col, $last_row, $last_col, $token, $format)</A></H2>
3147
Merging cells is generally achieved by setting the <CODE>merge</CODE> property of a Format object, see <A HREF="#CELL_FORMATTING">CELL FORMATTING</A>. However, this only allows simple Excel5 style horizontal merging which
3148
Excel refers to as "center across selection".
3152
The <CODE>merge_range()</CODE> method allows you to do Excel97+ style formatting where the cells can
3153
contain other types of alignment in addition to the merging:
3158
my $format = $workbook->add_format(
3160
valign => 'vcenter',
3161
align => 'center',
3167
$worksheet->merge_range('B3:D4', 'Vertical and horizontal', $format);
3171
<STRONG>WARNING</STRONG>. The format object that is used with a <CODE>merge_range()</CODE> method call is marked internally as being associated with a merged range.
3172
It is a fatal error to use a merged format in a non-merged cell. Instead
3173
you should use separate formats for merged and non-merged cells. This
3174
restriction will be removed in a future release.
3178
<CODE>merge_range()</CODE> writes its <CODE>$token</CODE> argument using the worksheet <CODE>write()</CODE> method. Therefore it will handle numbers, strings, formulas or urls as
3183
Setting the <CODE>merge</CODE> property of the format isn't required when you are using <CODE>merge_range()</CODE>. In fact using it will exclude the use of any other horizontal alignment
3188
The full possibilities of this method are shown in the <CODE>merge3.pl</CODE>, <CODE>merge4.pl</CODE> and <CODE>merge5.pl</CODE> programs in the <CODE>examples</CODE> directory of the distribution.
3193
<H2><A NAME="set_zoom_scale_">set_zoom($scale)</A></H2>
3195
Set the worksheet zoom factor in the range <CODE>10 <= $scale <= 400</CODE>:
3200
$worksheet1->set_zoom(50);
3201
$worksheet2->set_zoom(75);
3202
$worksheet3->set_zoom(300);
3203
$worksheet4->set_zoom(400);
3207
The default zoom factor is 100. You cannot zoom to "Selection" because it
3208
is calculated by Excel at run-time.
3212
Note, <CODE>set_zoom()</CODE> does not affect the scale of the printed page. For that you should use <CODE>set_print_scale()</CODE>.
3217
<H1><A NAME="PAGE_SET_UP_METHODS">PAGE SET-UP METHODS</A></H1>
3219
Page set-up methods affect the way that a worksheet looks when it is
3220
printed. They control features such as page headers and footers and
3221
margins. These methods are really just standard worksheet methods. They are
3222
documented here in a separate section for the sake of clarity.
3226
The following methods are available for page set-up:
3234
center_horizontally()
3242
print_row_col_headers()
3251
A common requirement when working with Spreadsheet::WriteExcel is to apply
3252
the same page set-up features to all of the worksheets in a workbook. To do
3253
this you can use the <CODE>sheets()</CODE> method of the <CODE>workbook</CODE> class to access the array of worksheets in a workbook:
3258
foreach $worksheet ($workbook->sheets()) {
3259
$worksheet->set_landscape();
3265
<H2><A NAME="set_landscape_">set_landscape()</A></H2>
3267
This method is used to set the orientation of a worksheet's printed page to
3273
$worksheet->set_landscape(); # Landscape mode
3278
<H2><A NAME="set_portrait_">set_portrait()</A></H2>
3280
This method is used to set the orientation of a worksheet's printed page to
3281
portrait. The default worksheet orientation is portrait, so you won't
3282
generally need to call this method.
3287
$worksheet->set_portrait(); # Portrait mode
3292
<H2><A NAME="set_paper_index_">set_paper($index)</A></H2>
3294
This method is used to set the paper format for the printed output of a
3295
worksheet. The following paper styles are available:
3300
Index Paper format Paper size
3301
===== ============ ==========
3303
1 Letter 8 1/2 x 11 in
3304
2 Letter Small 8 1/2 x 11 in
3305
3 Tabloid 11 x 17 in
3307
5 Legal 8 1/2 x 14 in
3308
6 Statement 5 1/2 x 8 1/2 in
3309
7 Executive 7 1/4 x 10 1/2 in
3312
10 A4 Small 210 x 297 mm
3316
14 Folio 8 1/2 x 13 in
3317
15 Quarto 215 x 275 mm
3320
18 Note 8 1/2 x 11 in
3321
19 Envelope 9 3 7/8 x 8 7/8
3322
20 Envelope 10 4 1/8 x 9 1/2
3323
21 Envelope 11 4 1/2 x 10 3/8
3324
22 Envelope 12 4 3/4 x 11
3325
23 Envelope 14 5 x 11 1/2
3329
27 Envelope DL 110 x 220 mm
3330
28 Envelope C3 324 x 458 mm
3331
29 Envelope C4 229 x 324 mm
3332
30 Envelope C5 162 x 229 mm
3333
31 Envelope C6 114 x 162 mm
3334
32 Envelope C65 114 x 229 mm
3335
33 Envelope B4 250 x 353 mm
3336
34 Envelope B5 176 x 250 mm
3337
35 Envelope B6 176 x 125 mm
3338
36 Envelope 110 x 230 mm
3339
37 Monarch 3.875 x 7.5 in
3340
38 Envelope 3 5/8 x 6 1/2 in
3341
39 Fanfold 14 7/8 x 11 in
3342
40 German Std Fanfold 8 1/2 x 12 in
3343
41 German Legal Fanfold 8 1/2 x 13 in
3347
Note, it is likely that not all of these paper types will be available to
3348
the end user since it will depend on the paper formats that the user's
3349
printer supports. Therefore, it is best to stick to standard paper types.
3354
$worksheet->set_paper(1); # US Letter
3355
$worksheet->set_paper(9); # A4
3359
If you do not specify a paper type the worksheet will print using the
3360
printer's default paper.
3365
<H2><A NAME="center_horizontally_">center_horizontally()</A></H2>
3367
Center the worksheet data horizontally between the margins on the printed
3373
$worksheet->center_horizontally();
3378
<H2><A NAME="center_vertically_">center_vertically()</A></H2>
3380
Center the worksheet data vertically between the margins on the printed
3386
$worksheet->center_vertically();
3391
<H2><A NAME="set_margins_inches_">set_margins($inches)</A></H2>
3393
There are several methods available for setting the worksheet margins on
3399
set_margins() # Set all margins to the same value
3400
set_margins_LR() # Set left and right margins to the same value
3401
set_margins_TB() # Set top and bottom margins to the same value
3402
set_margin_left(); # Set left margin
3403
set_margin_right(); # Set right margin
3404
set_margin_top(); # Set top margin
3405
set_margin_bottom(); # Set bottom margin
3409
All of these methods take a distance in inches as a parameter. Note: 1 inch
3410
= 25.4mm. ;-) The default left and right margin is 0.75 inch. The default
3411
top and bottom margin is 1.00 inch.
3416
<H2><A NAME="set_header_string_margin_">set_header($string, $margin)</A></H2>
3418
Headers and footers are generated using a <CODE>$string</CODE> which is a combination of plain text and control characters. The <CODE>$margin</CODE> parameter is optional.
3422
The available control character are:
3427
Control Category Description
3428
======= ======== ===========
3429
&L Justification Left
3436
&P Information Page number
3437
&N Total number of pages
3441
&A Worksheet name
3442
&Z Workbook path
3447
&fontsize Font Font size
3448
&"font,style" Font name and style
3449
&U Single underline
3450
&E Double underline
3451
&S Strikethrough
3458
&& Miscellaneous Literal ampersand &
3462
Text in headers and footers can be justified (aligned) to the left, center
3463
and right by prefixing the text with the control characters <CODE>&L</CODE>, <CODE>&C</CODE> and <CODE>&R</CODE>.
3467
For example (with ASCII art representation of the results):
3472
$worksheet->set_header('&LHello');
3477
---------------------------------------------------------------
3485
$worksheet->set_header('&CHello');
3490
---------------------------------------------------------------
3498
$worksheet->set_header('&RHello');
3503
---------------------------------------------------------------
3510
For simple text, if you do not specify any justification the text will be
3511
centred. However, you must prefix the text with <CODE>&C</CODE> if you specify a font name or any other formatting:
3516
$worksheet->set_header('Hello');
3521
---------------------------------------------------------------
3528
You can have text in each of the justification regions:
3533
$worksheet->set_header('&LCiao&CBello&RCielo');
3538
---------------------------------------------------------------
3540
| Ciao Bello Cielo |
3545
The information control characters act as variables that Excel will update
3546
as the workbook or worksheet changes. Times and dates are in the users
3552
$worksheet->set_header('&CPage &P of &N');
3557
---------------------------------------------------------------
3565
$worksheet->set_header('&CUpdated at &T');
3570
---------------------------------------------------------------
3572
| Updated at 12:30 PM |
3577
You can specify the font size of a section of the text by prefixing it with
3578
the control character <CODE>&n</CODE> where <CODE>n</CODE> is the font size:
3583
$worksheet1->set_header('&C&30Hello Big' );
3584
$worksheet2->set_header('&C&10Hello Small');
3588
You can specify the font of a section of the text by prefixing it with the
3589
control sequence <CODE>&"font,style"</CODE> where <CODE>fontname</CODE> is a font name such as "Courier New" or "Times New Roman" and <CODE>style</CODE> is one of the standard Windows font descriptions: "Regular", "Italic",
3590
"Bold" or "Bold Italic":
3595
$worksheet1->set_header('&C&"Courier New,Italic"Hello');
3596
$worksheet2->set_header('&C&"Courier New,Bold Italic"Hello');
3597
$worksheet3->set_header('&C&"Times New Roman,Regular"Hello');
3601
It is possible to combine all of these features together to create
3602
sophisticated headers and footers. As an aid to setting up complicated
3603
headers and footers you can record a page set-up as a macro in Excel and
3604
look at the format strings that VBA produces. Remember however that VBA
3605
uses two double quotes <CODE>""</CODE> to indicate a single double quote. For the last example above the
3606
equivalent VBA code looks like this:
3611
.LeftHeader = ""
3612
.CenterHeader = "&""Times New Roman,Regular""Hello"
3613
.RightHeader = ""
3617
To include a single literal ampersand <CODE>&</CODE> in a header or footer you should use a double ampersand <CODE>&&</CODE>:
3622
$worksheet1->set_header('&CCuriouser && Curiouser - Attorneys at Law');
3626
As stated above the margin parameter is optional. As with the other margins
3627
the value should be in inches. The default header and footer margin is 0.50
3628
inch. The header and footer margin size can be set as follows:
3633
$worksheet->set_header('&CHello', 0.75);
3637
The header and footer margins are independent of the top and bottom
3642
Note, the header or footer string must be less than 255 characters. Strings
3643
longer than this will not be written and a warning will be generated.
3647
On systems with <CODE>perl 5.8</CODE> and later the <CODE>set_header()</CODE> method can also handle Unicode strings in Perl's <CODE>utf8</CODE> format.
3652
$worksheet->set_header("&C\x{263a}")
3656
See, also the <CODE>headers.pl</CODE> program in the <CODE>examples</CODE> directory of the distribution.
3661
<H2><A NAME="set_footer_">set_footer()</A></H2>
3663
The syntax of the <CODE>set_footer()</CODE> method is the same as <CODE>set_header()</CODE>, see above.
3668
<H2><A NAME="repeat_rows_first_row_last_ro">repeat_rows($first_row, $last_row)</A></H2>
3670
Set the number of rows to repeat at the top of each printed page.
3674
For large Excel documents it is often desirable to have the first row or
3675
rows of the worksheet print out at the top of each page. This can be
3676
achieved by using the <CODE>repeat_rows()</CODE> method. The parameters <CODE>$first_row</CODE> and <CODE>$last_row</CODE> are zero based. The <CODE>$last_row</CODE> parameter is optional if you only wish to specify one row:
3681
$worksheet1->repeat_rows(0); # Repeat the first row
3682
$worksheet2->repeat_rows(0, 1); # Repeat the first two rows
3687
<H2><A NAME="repeat_columns_first_col_last">repeat_columns($first_col, $last_col)</A></H2>
3689
Set the columns to repeat at the left hand side of each printed page.
3693
For large Excel documents it is often desirable to have the first column or
3694
columns of the worksheet print out at the left hand side of each page. This
3695
can be achieved by using the <CODE>repeat_columns()</CODE> method. The parameters <CODE>$first_column</CODE> and <CODE>$last_column</CODE> are zero based. The <CODE>$last_column</CODE> parameter is optional if you only wish to specify one column. You can also
3696
specify the columns using A1 column notation, see the note about <A HREF="#Cell_notation">Cell notation</A>.
3701
$worksheet1->repeat_columns(0); # Repeat the first column
3702
$worksheet2->repeat_columns(0, 1); # Repeat the first two columns
3703
$worksheet3->repeat_columns('A:A'); # Repeat the first column
3704
$worksheet4->repeat_columns('A:B'); # Repeat the first two columns
3709
<H2><A NAME="hide_gridlines_option_">hide_gridlines($option)</A></H2>
3711
This method is used to hide the gridlines on the screen and printed page.
3712
Gridlines are the lines that divide the cells on a worksheet. Screen and
3713
printed gridlines are turned on by default in an Excel worksheet. If you
3714
have defined your own cell borders you may wish to hide the default
3720
$worksheet->hide_gridlines();
3724
The following values of <CODE>$option</CODE> are valid:
3729
0 : Don't hide gridlines
3730
1 : Hide printed gridlines only
3731
2 : Hide screen and printed gridlines
3735
If you don't supply an argument or use <CODE>undef</CODE> the default option is 1, i.e. only the printed gridlines are hidden.
3740
<H2><A NAME="print_row_col_headers_">print_row_col_headers()</A></H2>
3742
Set the option to print the row and column headers on the printed page.
3746
An Excel worksheet looks something like the following;
3751
------------------------------------------
3752
| | A | B | C | D | ...
3753
------------------------------------------
3758
|...| ... | ... | ... | ... | ...
3762
The headers are the letters and numbers at the top and the left of the
3763
worksheet. Since these headers serve mainly as a indication of position on
3764
the worksheet they generally do not appear on the printed page. If you wish
3765
to have them printed you can use the <CODE>print_row_col_headers()</CODE> method :
3770
$worksheet->print_row_col_headers();
3774
Do not confuse these headers with page headers as described in the <CODE>set_header()</CODE> section above.
3779
<H2><A NAME="print_area_first_row_first_co">print_area($first_row, $first_col, $last_row, $last_col)</A></H2>
3781
This method is used to specify the area of the worksheet that will be
3782
printed. All four parameters must be specified. You can also use A1
3783
notation, see the note about <A HREF="#Cell_notation">Cell notation</A>.
3788
$worksheet1->print_area("A1:H20"); # Cells A1 to H20
3789
$worksheet2->print_area(0, 0, 19, 7); # The same
3790
$worksheet2->print_area('A:H'); # Columns A to H if rows have data
3795
<H2><A NAME="fit_to_pages_width_height_">fit_to_pages($width, $height)</A></H2>
3797
The <CODE>fit_to_pages()</CODE> method is used to fit the printed area to a specific number of pages both
3798
vertically and horizontally. If the printed area exceeds the specified
3799
number of pages it will be scaled down to fit. This guarantees that the
3800
printed area will always appear on the specified number of pages even if
3801
the page size or margins change.
3806
$worksheet1->fit_to_pages(1, 1); # Fit to 1x1 pages
3807
$worksheet2->fit_to_pages(2, 1); # Fit to 2x1 pages
3808
$worksheet3->fit_to_pages(1, 2); # Fit to 1x2 pages
3812
The print area can be defined using the <CODE>print_area()</CODE> method as described above.
3816
A common requirement is to fit the printed output to <EM>n</EM> pages wide but have the height be as long as necessary. To achieve this set
3817
the <CODE>$height</CODE> to zero or leave it blank:
3822
$worksheet1->fit_to_pages(1, 0); # 1 page wide and as long as necessary
3823
$worksheet2->fit_to_pages(1); # The same
3827
Note that although it is valid to use both <CODE>fit_to_pages()</CODE> and <CODE>set_print_scale()</CODE> on the same worksheet only one of these options can be active at a time.
3828
The last method call made will set the active option.
3832
Note that <CODE>fit_to_pages()</CODE> will override any manual page breaks that are defined in the worksheet.
3837
<H2><A NAME="set_print_scale_scale_">set_print_scale($scale)</A></H2>
3839
Set the scale factor of the printed page. Scale factors in the range <CODE>10 <= $scale <= 400</CODE> are valid:
3844
$worksheet1->set_print_scale(50);
3845
$worksheet2->set_print_scale(75);
3846
$worksheet3->set_print_scale(300);
3847
$worksheet4->set_print_scale(400);
3851
The default scale factor is 100. Note, <CODE>set_print_scale()</CODE> does not affect the scale of the visible page in Excel. For that you should
3852
use <CODE>set_zoom()</CODE>.
3856
Note also that although it is valid to use both <CODE>fit_to_pages()</CODE> and <CODE>set_print_scale()</CODE> on the same worksheet only one of these options can be active at a time.
3857
The last method call made will set the active option.
3862
<H2><A NAME="set_h_pagebreaks_breaks_">set_h_pagebreaks(@breaks)</A></H2>
3864
Add horizontal page breaks to a worksheet. A page break causes all the data
3865
that follows it to be printed on the next page. Horizontal page breaks act
3866
between rows. To create a page break between rows 20 and 21 you must
3867
specify the break at row 21. However in zero index notation this is
3868
actually row 20. So you can pretend for a small while that you are using 1
3874
$worksheet1->set_h_pagebreaks(20); # Break between row 20 and 21
3878
The <CODE>set_h_pagebreaks()</CODE> method will accept a list of page breaks and you can call it more than
3884
$worksheet2->set_h_pagebreaks( 20, 40, 60, 80, 100); # Add breaks
3885
$worksheet2->set_h_pagebreaks(120, 140, 160, 180, 200); # Add some more
3889
Note: If you specify the "fit to page" option via the <CODE>fit_to_pages()</CODE> method it will override all manual page breaks.
3893
There is a silent limitation of about 1000 horizontal page breaks per
3894
worksheet in line with an Excel internal limitation.
3899
<H2><A NAME="set_v_pagebreaks_breaks_">set_v_pagebreaks(@breaks)</A></H2>
3901
Add vertical page breaks to a worksheet. A page break causes all the data
3902
that follows it to be printed on the next page. Vertical page breaks act
3903
between columns. To create a page break between columns 20 and 21 you must
3904
specify the break at column 21. However in zero index notation this is
3905
actually column 20. So you can pretend for a small while that you are using
3911
$worksheet1->set_v_pagebreaks(20); # Break between column 20 and 21
3915
The <CODE>set_v_pagebreaks()</CODE> method will accept a list of page breaks and you can call it more than
3921
$worksheet2->set_v_pagebreaks( 20, 40, 60, 80, 100); # Add breaks
3922
$worksheet2->set_v_pagebreaks(120, 140, 160, 180, 200); # Add some more
3926
Note: If you specify the "fit to page" option via the <CODE>fit_to_pages()</CODE> method it will override all manual page breaks.
3931
<H1><A NAME="CELL_FORMATTING">CELL FORMATTING</A></H1>
3933
This section describes the methods and properties that are available for
3934
formatting cells in Excel. The properties of a cell that can be formatted
3935
include: fonts, colours, patterns, borders, alignment and number
3941
<H2><A NAME="Creating_and_using_a_Format_obje">Creating and using a Format object</A></H2>
3943
Cell formatting is defined through a Format object. Format objects are
3944
created by calling the workbook <CODE>add_format()</CODE> method as follows:
3949
my $format1 = $workbook->add_format(); # Set properties later
3950
my $format2 = $workbook->add_format(%props); # Set at creation
3954
The format object holds all the formatting properties that can be applied
3955
to a cell, a row or a column. The process of setting these properties is
3956
discussed in the next section.
3960
Once a Format object has been constructed and it properties have been set
3961
it can be passed as an argument to the worksheet <CODE>write</CODE> methods as follows:
3966
$worksheet->write(0, 0, "One", $format);
3967
$worksheet->write_string(1, 0, "Two", $format);
3968
$worksheet->write_number(2, 0, 3, $format);
3969
$worksheet->write_blank(3, 0, $format);
3973
Formats can also be passed to the worksheet <CODE>set_row()</CODE> and <CODE>set_column()</CODE> methods to define the default property for a row or column.
3978
$worksheet->set_row(0, 15, $format);
3979
$worksheet->set_column(0, 0, 15, $format);
3984
<H2><A NAME="Format_methods_and_Format_proper">Format methods and Format properties</A></H2>
3986
The following table shows the Excel format categories, the formatting
3987
properties that can be applied and the equivalent object method:
3992
Category Description Property Method Name
3993
-------- ----------- -------- -----------
3994
Font Font type font set_font()
3995
Font size size set_size()
3996
Font color color set_color()
3997
Bold bold set_bold()
3998
Italic italic set_italic()
3999
Underline underline set_underline()
4000
Strikeout font_strikeout set_font_strikeout()
4001
Super/Subscript font_script set_font_script()
4002
Outline font_outline set_font_outline()
4003
Shadow font_shadow set_font_shadow()
4008
Number Numeric format num_format set_num_format()
4013
Protection Lock cells locked set_locked()
4014
Hide formulas hidden set_hidden()
4019
Alignment Horizontal align align set_align()
4020
Vertical align valign set_align()
4021
Rotation rotation set_rotation()
4022
Text wrap text_wrap set_text_wrap()
4023
Justify last text_justlast set_text_justlast()
4024
Center across center_across set_center_across()
4025
Indentation indent set_indent()
4026
Shrink to fit shrink set_shrink()
4031
Pattern Cell pattern pattern set_pattern()
4032
Background color bg_color set_bg_color()
4033
Foreground color fg_color set_fg_color()
4038
Border Cell border border set_border()
4039
Bottom border bottom set_bottom()
4040
Top border top set_top()
4041
Left border left set_left()
4042
Right border right set_right()
4043
Border color border_color set_border_color()
4044
Bottom color bottom_color set_bottom_color()
4045
Top color top_color set_top_color()
4046
Left color left_color set_left_color()
4047
Right color right_color set_right_color()
4051
There are two ways of setting Format properties: by using the object method
4052
interface or by setting the property directly. For example, a typical use
4053
of the method interface would be as follows:
4058
my $format = $workbook->add_format();
4059
$format->set_bold();
4060
$format->set_color('red');
4064
By comparison the properties can be set directly by passing a hash of
4065
properties to the Format constructor:
4070
my $format = $workbook->add_format(bold => 1, color => 'red');
4074
or after the Format has been constructed by means of the <CODE>set_properties()</CODE> method as follows:
4079
my $format = $workbook->add_format();
4080
$format->set_properties(bold => 1, color => 'red');
4084
You can also store the properties in one or more named hashes and pass them
4085
to the required method:
4101
bg_color => 'green',
4108
my $format1 = $workbook->add_format(%font); # Font only
4109
my $format2 = $workbook->add_format(%font, %shading); # Font and shading
4113
The provision of two ways of setting properties might lead you to wonder
4114
which is the best way. The answer depends on the amount of formatting that
4115
will be required in your program. Initially, Spreadsheet::WriteExcel only
4116
allowed individual Format properties to be set via the appropriate method.
4117
While this was sufficient for most circumstances it proved very cumbersome
4118
in programs that required a large amount of formatting. In addition the
4119
mechanism for reusing properties between Format objects was complicated.
4123
As a result the Perl/Tk style of adding properties was added to, hopefully,
4124
facilitate developers who need to define a lot of formatting. In fact the
4125
Tk style of defining properties is also supported:
4131
-font => 'Arial',
4133
-color => 'blue',
4139
An additional advantage of working with hashes of properties is that it
4140
allows you to share formatting between workbook objects
4144
You can also create a format "on the fly" and pass it directly to a write
4150
$worksheet->write('A1', "Title", $workbook->add_format(bold => 1));
4154
This corresponds to an "anonymous" format in the Perl sense of anonymous
4160
<H2><A NAME="Working_with_formats">Working with formats</A></H2>
4162
The default format is Arial 10 with all other properties off.
4166
Each unique format in Spreadsheet::WriteExcel must have a corresponding
4167
Format object. It isn't possible to use a Format with a
4168
<CODE>write()</CODE> method and then redefine the Format for use at a later
4169
stage. This is because a Format is applied to a cell not in its current
4170
state but in its final state. Consider the following example:
4175
my $format = $workbook->add_format();
4176
$format->set_bold();
4177
$format->set_color('red');
4178
$worksheet->write('A1', "Cell A1", $format);
4179
$format->set_color('green');
4180
$worksheet->write('B1', "Cell B1", $format);
4184
Cell A1 is assigned the Format <CODE>$format</CODE> which is initially set to the colour red. However, the colour is
4185
subsequently set to green. When Excel displays Cell A1 it will display the
4186
final state of the Format which in this case will be the colour green.
4190
In general a method call without an argument will turn a property on, for
4196
my $format1 = $workbook->add_format();
4197
$format1->set_bold(); # Turns bold on
4198
$format1->set_bold(1); # Also turns bold on
4199
$format1->set_bold(0); # Turns bold off
4204
<H1><A NAME="FORMAT_METHODS">FORMAT METHODS</A></H1>
4206
The Format object methods are described in more detail in the following
4207
sections. In addition, there is a Perl program called <CODE>formats.pl</CODE> in the <CODE>examples</CODE> directory of the WriteExcel distribution. This program creates an Excel
4208
workbook called <CODE>formats.xls</CODE> which contains examples of almost all the format types.
4212
The following Format methods are available:
4223
set_font_strikeout()
4254
The above methods can also be applied directly as properties. For example <CODE>$worksheet->set_bold()</CODE> is equivalent to <CODE>set_properties(bold => 1)</CODE>.
4259
<H2><A NAME="set_properties_properties_">set_properties(%properties)</A></H2>
4261
The properties of an existing Format object can be set by means of <CODE>set_properties()</CODE>:
4266
my $format = $workbook->add_format();
4267
$format->set_properties(bold => 1, color => 'red');
4271
You can also store the properties in one or more named hashes and pass them
4272
to the <CODE>set_properties()</CODE> method:
4287
my $format = $workbook->set_properties(%font);
4291
This method can be used as an alternative to setting the properties with <CODE>add_format()</CODE> or the specific format methods that are detailed in the following sections.
4296
<H2><A NAME="set_font_fontname_">set_font($fontname)</A></H2>
4299
Default state: Font is Arial
4300
Default action: None
4301
Valid args: Any valid font name
4305
Specify the font used:
4310
$format->set_font('Times New Roman');
4314
Excel can only display fonts that are installed on the system that it is
4315
running on. Therefore it is best to use the fonts that come as standard
4316
such as 'Arial', 'Times New Roman' and 'Courier New'. See also the Fonts
4317
worksheet created by formats.pl
4322
<H2><A NAME="set_size_">set_size()</A></H2>
4325
Default state: Font size is 10
4326
Default action: Set font size to 1
4327
Valid args: Integer values from 1 to as big as your screen.
4331
Set the font size. Excel adjusts the height of a row to accommodate the
4332
largest font size in the row. You can also explicitly specify the height of
4333
a row using the <CODE>set_row()</CODE> worksheet method.
4338
my $format = $workbook->add_format();
4339
$format->set_size(30);
4344
<H2><A NAME="set_color_">set_color()</A></H2>
4347
Default state: Excels default color, usually black
4348
Default action: Set the default color
4349
Valid args: Integers from 8..63 or the following strings:
4368
Set the font colour. The <CODE>set_color()</CODE> method is used as follows:
4373
my $format = $workbook->add_format();
4374
$format->set_color('red');
4375
$worksheet->write(0, 0, "wheelbarrow", $format);
4379
Note: The <CODE>set_color()</CODE> method is used to set the colour of the font in a cell. To set the colour
4380
of a cell use the <CODE>set_bg_color()</CODE> and <CODE>set_pattern()</CODE> methods.
4384
For additional examples see the 'Named colors' and 'Standard colors'
4385
worksheets created by formats.pl in the examples directory.
4389
See also <A HREF="#COLOURS_IN_EXCEL">COLOURS IN EXCEL</A>.
4394
<H2><A NAME="set_bold_">set_bold()</A></H2>
4397
Default state: bold is off
4398
Default action: Turn bold on
4399
Valid args: 0, 1 [1]
4403
Set the bold property of the font:
4408
$format->set_bold(); # Turn bold on
4412
[1] Actually, values in the range 100..1000 are also valid. 400 is normal,
4413
700 is bold and 1000 is very bold indeed. It is probably best to set the
4414
value to 1 and use normal bold.
4419
<H2><A NAME="set_italic_">set_italic()</A></H2>
4422
Default state: Italic is off
4423
Default action: Turn italic on
4428
Set the italic property of the font:
4433
$format->set_italic(); # Turn italic on
4438
<H2><A NAME="set_underline_">set_underline()</A></H2>
4441
Default state: Underline is off
4442
Default action: Turn on single underline
4443
Valid args: 0 = No underline
4444
1 = Single underline
4445
2 = Double underline
4446
33 = Single accounting underline
4447
34 = Double accounting underline
4451
Set the underline property of the font.
4456
$format->set_underline(); # Single underline
4461
<H2><A NAME="set_font_strikeout_">set_font_strikeout()</A></H2>
4464
Default state: Strikeout is off
4465
Default action: Turn strikeout on
4470
Set the strikeout property of the font.
4475
<H2><A NAME="set_font_script_">set_font_script()</A></H2>
4478
Default state: Super/Subscript is off
4479
Default action: Turn Superscript on
4480
Valid args: 0 = Normal
4486
Set the superscript/subscript property of the font. This format is
4487
currently not very useful.
4492
<H2><A NAME="set_font_outline_">set_font_outline()</A></H2>
4495
Default state: Outline is off
4496
Default action: Turn outline on
4506
<H2><A NAME="set_font_shadow_">set_font_shadow()</A></H2>
4509
Default state: Shadow is off
4510
Default action: Turn shadow on
4520
<H2><A NAME="set_num_format_">set_num_format()</A></H2>
4523
Default state: General format
4524
Default action: Format index 1
4525
Valid args: See the following table
4529
This method is used to define the numerical format of a number in Excel. It
4530
controls whether a number is displayed as an integer, a floating point
4531
number, a date, a currency value or some other user defined format.
4535
The numerical format of a cell can be specified by using a format string or
4536
an index to one of Excel's built-in formats:
4541
my $format1 = $workbook->add_format();
4542
my $format2 = $workbook->add_format();
4543
$format1->set_num_format('d mmm yyyy'); # Format string
4544
$format2->set_num_format(0x0f); # Format index
4549
$worksheet->write(0, 0, 36892.521, $format1); # 1 Jan 2001
4550
$worksheet->write(0, 0, 36892.521, $format2); # 1-Jan-01
4554
Using format strings you can define very sophisticated formatting of
4560
$format01->set_num_format('0.000');
4561
$worksheet->write(0, 0, 3.1415926, $format01); # 3.142
4566
$format02->set_num_format('#,##0');
4567
$worksheet->write(1, 0, 1234.56, $format02); # 1,235
4572
$format03->set_num_format('#,##0.00');
4573
$worksheet->write(2, 0, 1234.56, $format03); # 1,234.56
4578
$format04->set_num_format('$0.00');
4579
$worksheet->write(3, 0, 49.99, $format04); # $49.99
4584
$format05->set_num_format('�0.00');
4585
$worksheet->write(4, 0, 49.99, $format05); # �49.99
4590
$format06->set_num_format('�0.00');
4591
$worksheet->write(5, 0, 49.99, $format06); # �49.99
4596
$format07->set_num_format('mm/dd/yy');
4597
$worksheet->write(6, 0, 36892.521, $format07); # 01/01/01
4602
$format08->set_num_format('mmm d yyyy');
4603
$worksheet->write(7, 0, 36892.521, $format08); # Jan 1 2001
4608
$format09->set_num_format('d mmmm yyyy');
4609
$worksheet->write(8, 0, 36892.521, $format09); # 1 January 2001
4614
$format10->set_num_format('dd/mm/yyyy hh:mm AM/PM');
4615
$worksheet->write(9, 0, 36892.521, $format10); # 01/01/2001 12:30 AM
4620
$format11->set_num_format('0 "dollar and" .00 "cents"');
4621
$worksheet->write(10, 0, 1.87, $format11); # 1 dollar and .87 cents
4626
# Conditional formatting
4627
$format12->set_num_format('[Green]General;[Red]-General;General');
4628
$worksheet->write(11, 0, 123, $format12); # > 0 Green
4629
$worksheet->write(12, 0, -45, $format12); # < 0 Red
4630
$worksheet->write(13, 0, 0, $format12); # = 0 Default colour
4636
$format13->set_num_format('00000');
4637
$worksheet->write(14, 0, '01209', $format13);
4641
The number system used for dates is described in <A HREF="#DATES_IN_EXCEL">DATES IN EXCEL</A>.
4645
The colour format should have one of the following values:
4650
[Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow]
4654
Alternatively you can specify the colour based on a colour index as
4655
follows: <CODE>[Color n]</CODE>, where n is a standard Excel colour index - 7. See the 'Standard colors'
4656
worksheet created by formats.pl.
4660
For more information refer to the documentation on formatting in the <CODE>doc</CODE> directory of the Spreadsheet::WriteExcel distro, the Excel on-line help or
4662
HREF="http://office.microsoft.com/en-gb/assistance/HP051995001033.aspx">http://office.microsoft.com/en-gb/assistance/HP051995001033.aspx</A>
4667
You should ensure that the format string is valid in Excel prior to using
4672
Excel's built-in formats are shown in the following table:
4677
Index Index Format String
4683
5 0x05 ($#,##0_);($#,##0)
4684
6 0x06 ($#,##0_);[Red]($#,##0)
4685
7 0x07 ($#,##0.00_);($#,##0.00)
4686
8 0x08 ($#,##0.00_);[Red]($#,##0.00)
4697
19 0x13 h:mm:ss AM/PM
4702
37 0x25 (#,##0_);(#,##0)
4703
38 0x26 (#,##0_);[Red](#,##0)
4704
39 0x27 (#,##0.00_);(#,##0.00)
4705
40 0x28 (#,##0.00_);[Red](#,##0.00)
4706
41 0x29 _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
4707
42 0x2a _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)
4708
43 0x2b _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)
4709
44 0x2c _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)
4718
For examples of these formatting codes see the 'Numerical formats'
4719
worksheet created by formats.pl. See also the number_formats1.html and the
4720
number_formats2.html documents in the <CODE>doc</CODE> directory of the distro.
4724
Note 1. Numeric formats 23 to 36 are not documented by Microsoft and may
4725
differ in international versions.
4729
Note 2. In Excel 5 the dollar sign appears as a dollar sign. In Excel
4730
97-2000 it appears as the defined local currency symbol.
4734
Note 3. The red negative numeric formats display slightly differently in
4735
Excel 5 and Excel 97-2000.
4740
<H2><A NAME="set_locked_">set_locked()</A></H2>
4743
Default state: Cell locking is on
4744
Default action: Turn locking on
4749
This property can be used to prevent modification of a cells contents.
4750
Following Excel's convention, cell locking is turned on by default.
4751
However, it only has an effect if the worksheet has been protected, see the
4752
worksheet <CODE>protect()</CODE> method.
4757
my $locked = $workbook->add_format();
4758
$locked->set_locked(1); # A non-op
4763
my $unlocked = $workbook->add_format();
4764
$locked->set_locked(0);
4769
# Enable worksheet protection
4770
$worksheet->protect();
4775
# This cell cannot be edited.
4776
$worksheet->write('A1', '=1+2', $locked);
4781
# This cell can be edited.
4782
$worksheet->write('A2', '=1+2', $unlocked);
4786
Note: This offers weak protection even with a password, see the note in
4787
relation to the <CODE>protect()</CODE> method.
4792
<H2><A NAME="set_hidden_">set_hidden()</A></H2>
4795
Default state: Formula hiding is off
4796
Default action: Turn hiding on
4801
This property is used to hide a formula while still displaying its result.
4802
This is generally used to hide complex calculations from end users who are
4803
only interested in the result. It only has an effect if the worksheet has
4804
been protected, see the worksheet <CODE>protect()</CODE> method.
4809
my $hidden = $workbook->add_format();
4810
$hidden->set_hidden();
4815
# Enable worksheet protection
4816
$worksheet->protect();
4821
# The formula in this cell isn't visible
4822
$worksheet->write('A1', '=1+2', $hidden);
4826
Note: This offers weak protection even with a password, see the note in
4827
relation to the <CODE>protect()</CODE> method.
4832
<H2><A NAME="set_align_">set_align()</A></H2>
4835
Default state: Alignment is off
4836
Default action: Left alignment
4837
Valid args: 'left' Horizontal
4854
This method is used to set the horizontal and vertical text alignment
4855
within a cell. Vertical and horizontal alignments can be combined. The
4856
method is used as follows:
4861
my $format = $workbook->add_format();
4862
$format->set_align('center');
4863
$format->set_align('vcenter');
4864
$worksheet->set_row(0, 30);
4865
$worksheet->write(0, 0, "X", $format);
4869
Text can be aligned across two or more adjacent cells using the <CODE>center_across</CODE> property. However, for genuine merged cells it is better to use the <CODE>merge_range()</CODE> worksheet method.
4873
The <CODE>vjustify</CODE> (vertical justify) option can be used to provide automatic text wrapping in
4874
a cell. The height of the cell will be adjusted to accommodate the wrapped
4875
text. To specify where the text wraps use the <CODE>set_text_wrap()</CODE> method.
4879
For further examples see the 'Alignment' worksheet created by formats.pl.
4884
<H2><A NAME="set_center_across_">set_center_across()</A></H2>
4887
Default state: Center across selection is off
4888
Default action: Turn center across on
4893
Text can be aligned across two or more adjacent cells using the <CODE>set_center_across()</CODE> method. This is an alias for the <CODE>set_align('center_across')</CODE> method call.
4897
Only one cell should contain the text, the other cells should be blank:
4902
my $format = $workbook->add_format();
4903
$format->set_center_across();
4908
$worksheet->write(1, 1, 'Center across selection', $format);
4909
$worksheet->write_blank(1, 2, $format);
4913
See also the <CODE>merge1.pl</CODE> to <CODE>merge5.pl</CODE> programs in the <CODE>examples</CODE> directory and the <CODE>merge_range()</CODE> method.
4918
<H2><A NAME="set_text_wrap_">set_text_wrap()</A></H2>
4921
Default state: Text wrap is off
4922
Default action: Turn text wrap on
4927
Here is an example using the text wrap property, the escape character <CODE>\n</CODE> is used to indicate the end of line:
4932
my $format = $workbook->add_format();
4933
$format->set_text_wrap();
4934
$worksheet->write(0, 0, "It's\na bum\nwrap", $format);
4938
Excel will adjust the height of the row to accommodate the wrapped text. A
4939
similar effect can be obtained without newlines using the <CODE>set_align('vjustify')</CODE> method. See the <CODE>textwrap.pl</CODE> program in the <CODE>examples</CODE> directory.
4944
<H2><A NAME="set_rotation_">set_rotation()</A></H2>
4947
Default state: Text rotation is off
4948
Default action: None
4949
Valid args: Integers in the range -90 to 90 and 270
4953
Set the rotation of the text in a cell. The rotation can be any angle in
4954
the range -90 to 90 degrees.
4959
my $format = $workbook->add_format();
4960
$format->set_rotation(30);
4961
$worksheet->write(0, 0, "This text is rotated", $format);
4965
The angle 270 is also supported. This indicates text where the letters run
4971
<H2><A NAME="set_indent_">set_indent()</A></H2>
4974
Default state: Text indentation is off
4975
Default action: Indent text 1 level
4976
Valid args: Positive integers
4980
This method can be used to indent text. The argument, which should be an
4981
integer, is taken as the level of indentation:
4986
my $format = $workbook->add_format();
4987
$format->set_indent(2);
4988
$worksheet->write(0, 0, "This text is indented", $format);
4992
Indentation is a horizontal alignment property. It will override any other
4993
horizontal properties but it can be used in conjunction with vertical
4999
<H2><A NAME="set_shrink_">set_shrink()</A></H2>
5002
Default state: Text shrinking is off
5003
Default action: Turn "shrink to fit" on
5008
This method can be used to shrink text so that it fits in a cell.
5013
my $format = $workbook->add_format();
5014
$format->set_shrink();
5015
$worksheet->write(0, 0, "Honey, I shrunk the text!", $format);
5020
<H2><A NAME="set_text_justlast_">set_text_justlast()</A></H2>
5023
Default state: Justify last is off
5024
Default action: Turn justify last on
5029
Only applies to Far Eastern versions of Excel.
5034
<H2><A NAME="set_pattern_">set_pattern()</A></H2>
5037
Default state: Pattern is off
5038
Default action: Solid fill is on
5043
Set the background pattern of a cell.
5047
Examples of the available patterns are shown in the 'Patterns' worksheet
5048
created by formats.pl. However, it is unlikely that you will ever need
5049
anything other than Pattern 1 which is a solid fill of the background
5055
<H2><A NAME="set_bg_color_">set_bg_color()</A></H2>
5058
Default state: Color is off
5059
Default action: Solid fill.
5060
Valid args: See set_color()
5064
The <CODE>set_bg_color()</CODE> method can be used to set the background colour of a pattern. Patterns are
5065
defined via the <CODE>set_pattern()</CODE> method. If a pattern hasn't been defined then a solid fill pattern is used
5070
Here is an example of how to set up a solid fill in a cell:
5075
my $format = $workbook->add_format();
5080
$format->set_pattern(); # This is optional when using a solid fill
5085
$format->set_bg_color('green');
5086
$worksheet->write('A1', 'Ray', $format);
5090
For further examples see the 'Patterns' worksheet created by formats.pl.
5095
<H2><A NAME="set_fg_color_">set_fg_color()</A></H2>
5098
Default state: Color is off
5099
Default action: Solid fill.
5100
Valid args: See set_color()
5104
The <CODE>set_fg_color()</CODE> method can be used to set the foreground colour of a pattern.
5108
For further examples see the 'Patterns' worksheet created by formats.pl.
5113
<H2><A NAME="set_border_">set_border()</A></H2>
5116
Also applies to: set_bottom()
5124
Default state: Border is off
5125
Default action: Set border type 1
5126
Valid args: 0 No border
5127
1 Thin single border
5128
2 Medium single border
5131
5 Thick single border
5132
6 Double line border
5137
A cell border is comprised of a border on the bottom, top, left and right.
5138
These can be set to the same value using <CODE>set_border()</CODE> or individually using the relevant method calls shown above. Examples of
5139
the available border styles are shown in the 'Borders' worksheet created by
5145
<H2><A NAME="set_border_color_">set_border_color()</A></H2>
5148
Also applies to: set_bottom_color()
5156
Default state: Color is off
5157
Default action: Undefined
5158
Valid args: See set_color()
5162
Set the colour of the cell borders. A cell border is comprised of a border
5163
on the bottom, top, left and right. These can be set to the same colour
5164
using <CODE>set_border_color()</CODE> or individually using the relevant method calls shown above. Examples of
5165
the border styles and colours are shown in the 'Borders' worksheet created
5171
<H2><A NAME="copy_format_">copy($format)</A></H2>
5173
This method is used to copy all of the properties from one Format object to
5179
my $lorry1 = $workbook->add_format();
5180
$lorry1->set_bold();
5181
$lorry1->set_italic();
5182
$lorry1->set_color('red'); # lorry1 is bold, italic and red
5187
my $lorry2 = $workbook->add_format();
5188
$lorry2->copy($lorry1);
5189
$lorry2->set_color('yellow'); # lorry2 is bold, italic and yellow
5193
The <CODE>copy()</CODE> method is only useful if you are using the method interface to Format
5194
properties. It generally isn't required if you are setting Format
5195
properties directly using hashes.
5199
Note: this is not a copy constructor, both objects must exist prior to
5205
<H1><A NAME="COLOURS_IN_EXCEL">COLOURS IN EXCEL</A></H1>
5207
Excel provides a colour palette of 56 colours. In Spreadsheet::WriteExcel
5208
these colours are accessed via their palette index in the range 8..63. This
5209
index is used to set the colour of fonts, cell patterns and cell borders.
5215
my $format = $workbook->add_format(
5216
color => 12, # index for blue
5224
The most commonly used colours can also be accessed by name. The name acts
5225
as a simple alias for the colour index:
5253
my $font = $workbook->add_format(color => 'red');
5257
Users of VBA in Excel should note that the equivalent colour indices are in
5258
the range 1..56 instead of 8..63.
5262
If the default palette does not provide a required colour you can override
5263
one of the built-in values. This is achieved by using the <CODE>set_custom_color()</CODE> workbook method to adjust the RGB (red green blue) components of the
5269
my $ferrari = $workbook->set_custom_color(40, 216, 12, 12);
5274
my $format = $workbook->add_format(
5275
bg_color => $ferrari,
5283
$worksheet->write_blank('A1', $format);
5287
The default Excel 97 colour palette is shown in <CODE>palette.html</CODE> in the <CODE>doc</CODE> directory of the distro. You can generate an Excel version of the palette
5288
using <CODE>colors.pl</CODE> in the <CODE>examples</CODE> directory.
5292
A comparison of the colour components in the Excel 5 and Excel 97+ colour
5293
palettes is shown in <CODE>rgb5-97.txt</CODE> in the <CODE>doc</CODE> directory.
5297
You may also find the following links helpful:
5301
A detailed look at Excel's colour palette: <A
5302
HREF="http://www.geocities.com/davemcritchie/excel/colors.htm">http://www.geocities.com/davemcritchie/excel/colors.htm</A>
5307
A decimal RGB chart: <A
5308
HREF="http://www.hypersolutions.org/pages/rgbdec.html">http://www.hypersolutions.org/pages/rgbdec.html</A>
5313
A hex RGB chart: : <A
5314
HREF="http://www.hypersolutions.org/pages/rgbhex.html">http://www.hypersolutions.org/pages/rgbhex.html</A>
5320
<H1><A NAME="DATES_IN_EXCEL">DATES IN EXCEL</A></H1>
5322
Dates and times in Excel are represented by real numbers, for example "Jan
5323
1 2001 12:30 AM" is represented by the number 36892.521.
5327
The integer part of the number stores the number of days since the epoch
5328
and the fractional part stores the percentage of the day.
5332
The epoch can be either 1900 or 1904. Excel for Windows uses 1900 and Excel
5333
for Macintosh uses 1904. The epochs are:
5338
1900: 0 January 1900 i.e. 31 December 1899
5339
1904: 1 January 1904
5343
By default Spreadsheet::WriteExcel uses the Windows/1900 format although it
5344
generally isn't an issue since Excel on Windows and the Macintosh will
5345
convert automatically between one system and the other. To use the 1904
5346
epoch you must use the <CODE>set_1904()</CODE> workbook method.
5350
There are two things to note about the 1900 date format. The first is that
5351
the epoch starts on 0 January 1900. The second is that the year 1900 is
5352
erroneously but deliberately treated as a leap year. Therefore you must add
5353
an extra day to dates after 28 February 1900. The reason for this anomaly
5355
HREF="http://support.microsoft.com/support/kb/articles/Q181/3/70.asp">http://support.microsoft.com/support/kb/articles/Q181/3/70.asp</A>
5360
A date or time in Excel is like any other number. To display the number as
5361
a date you must apply a number format to it. Refer to the <CODE>set_num_format()</CODE> method above:
5366
$format->set_num_format('mmm d yyyy hh:mm AM/PM');
5367
$worksheet->write('A1', 36892.521 , $format); # Jan 1 2001 12:30 AM
5371
You can also use the <CODE>write_date_time()</CODE> worksheet method to write dates in ISO8601 date format.
5376
$worksheet->write_date_time('A2', '2001-01-01T12:20', format);
5380
See the <CODE>write_date_time()</CODE> section of the documentation for more details.
5384
See also the <CODE>Spreadsheet::WriteExcel::Utility</CODE> module that is included in the distro and which includes date handling
5385
functions and the DateTime::Format::Excel module, <A
5386
HREF="http://search.cpan.org/search?dist=DateTime-Format-Excel">http://search.cpan.org/search?dist=DateTime-Format-Excel</A>
5387
which is part of the DateTime project and which deals specifically with
5388
converting dates and times to and from Excel's format.
5393
<H1><A NAME="OUTLINES_AND_GROUPING_IN_EXCEL">OUTLINES AND GROUPING IN EXCEL</A></H1>
5395
Excel allows you to group rows or columns so that they can be hidden or
5396
displayed with a single mouse click. This feature is referred to as
5401
Outlines can reduce complex data down to a few salient sub-totals or
5406
This feature is best viewed in Excel but the following is an ASCII
5407
representation of what a worksheet with three outlines might look like.
5408
Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at level
5409
1. The lines at the left hand side are called outline level bars.
5414
------------------------------------------
5415
1 2 3 | | A | B | C | D | ...
5416
------------------------------------------
5417
_ | 1 | A | | | | ...
5418
| _ | 2 | B | | | | ...
5419
| | | 3 | (C) | | | | ...
5420
| | | 4 | (D) | | | | ...
5421
| - | 5 | E | | | | ...
5422
| _ | 6 | F | | | | ...
5423
| | | 7 | (G) | | | | ...
5424
| | | 8 | (H) | | | | ...
5425
| - | 9 | I | | | | ...
5426
- | . | ... | ... | ... | ... | ...
5430
Clicking the minus sign on each of the level 2 outlines will collapse and
5431
hide the data as shown in the next figure. The minus sign changes to a plus
5432
sign to indicate that the data in the outline is hidden.
5437
------------------------------------------
5438
1 2 3 | | A | B | C | D | ...
5439
------------------------------------------
5440
_ | 1 | A | | | | ...
5441
| | 2 | B | | | | ...
5442
| + | 5 | E | | | | ...
5443
| | 6 | F | | | | ...
5444
| + | 9 | I | | | | ...
5445
- | . | ... | ... | ... | ... | ...
5449
Clicking on the minus sign on the level 1 outline will collapse the
5450
remaining rows as follows:
5455
------------------------------------------
5456
1 2 3 | | A | B | C | D | ...
5457
------------------------------------------
5459
+ | . | ... | ... | ... | ... | ...
5463
Grouping in <CODE>Spreadsheet::WriteExcel</CODE> is achieved by setting the outline level via the <CODE>set_row()</CODE> and <CODE>set_column()</CODE> worksheet methods:
5468
set_row($row, $height, $format, $hidden, $level)
5469
set_column($first_col, $last_col, $width, $format, $hidden, $level)
5473
The following example sets an outline level of 1 for rows 1 and 2
5474
(zero-indexed) and columns B to G. The parameters <CODE>$height</CODE> and <CODE>$XF</CODE> are assigned default values since they are undefined:
5479
$worksheet->set_row(1, undef, undef, 0, 1);
5480
$worksheet->set_row(2, undef, undef, 0, 1);
5481
$worksheet->set_column('B:G', undef, undef, 0, 1);
5485
Excel allows up to 7 outline levels. Therefore the <CODE>$level</CODE> parameter should be in the range <CODE>0 <= $level <= 7</CODE>.
5489
Rows and columns can be collapsed by setting the <CODE>$hidden</CODE> flag:
5494
$worksheet->set_row(1, undef, undef, 1, 1);
5495
$worksheet->set_row(2, undef, undef, 1, 1);
5496
$worksheet->set_column('B:G', undef, undef, 1, 1);
5500
For a more complete example see the <CODE>outline.pl</CODE> program in the examples directory of the distro.
5504
Some additional outline properties can be set via the <CODE>outline_settings()</CODE> worksheet method, see above.
5509
<H1><A NAME="FORMULAS_AND_FUNCTIONS_IN_EXCEL">FORMULAS AND FUNCTIONS IN EXCEL</A></H1>
5512
<H2><A NAME="Caveats">Caveats</A></H2>
5514
The first thing to note is that there are still some outstanding issues
5515
with the implementation of formulas and functions:
5520
1. Writing a formula is much slower than writing the equivalent string.
5521
2. You cannot use array constants, i.e. {1;2;3}, in functions.
5522
3. Unary minus isn't supported.
5523
4. Whitespace is not preserved around operators.
5524
5. Named ranges are not supported.
5525
6. Array formulas are not supported.
5529
However, these constraints will be removed in future versions. They are
5530
here because of a trade-off between features and time. Also, it is possible
5531
to work around issue 1 using the <CODE>store_formula()</CODE> and <CODE>repeat_formula()</CODE> methods as described later in this section.
5536
<H2><A NAME="Introduction">Introduction</A></H2>
5538
The following is a brief introduction to formulas and functions in Excel
5539
and Spreadsheet::WriteExcel.
5543
A formula is a string that begins with an equals sign:
5553
The formula can contain numbers, strings, boolean values, cell references,
5554
cell ranges and functions. Named ranges are not supported. Formulas should
5555
be written as they appear in Excel, that is cells and functions must be in
5560
Cells in Excel are referenced using the A1 notation system where the column
5561
is designated by a letter and the row by a number. Columns range from A to
5562
IV i.e. 0 to 255, rows range from 1 to 65536. The <CODE>Spreadsheet::WriteExcel::Utility</CODE> module that is included in the distro contains helper functions for dealing
5563
with A1 notation, for example:
5568
use Spreadsheet::WriteExcel::Utility;
5573
($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
5574
$str = xl_rowcol_to_cell(1, 2); # C2
5578
The Excel <CODE>$</CODE> notation in cell references is also supported. This allows you to specify
5579
whether a row or column is relative or absolute. This only has an effect if
5580
the cell is copied. The following examples show relative and absolute
5586
'=A1' # Column and row are relative
5587
'=$A1' # Column is absolute and row is relative
5588
'=A$1' # Column is relative and row is absolute
5589
'=$A$1' # Column and row are absolute
5593
Formulas can also refer to cells in other worksheets of the current
5594
workbook. For example:
5602
'=Sheet2:Sheet3!A1:A5'
5604
q{='Test Data1:Test Data2'!A1}
5608
The sheet reference and the cell reference are separated by <CODE>!</CODE> the exclamation mark symbol. If worksheet names contain spaces, commas o
5609
parentheses then Excel requires that the name is enclosed in single quotes
5610
as shown in the last two examples above. In order to avoid using a lot of
5611
escape characters you can use the quote operator <CODE>q{}</CODE> to protect the quotes. See <CODE>perlop</CODE> in the main Perl documentation. Only valid sheet names that have been added
5612
using the <CODE>add_worksheet()</CODE> method can be used in formulas. You cannot reference external workbooks.
5616
The following table lists the operators that are available in Excel's
5617
formulas. The majority of the operators are the same as Perl's, differences
5623
Arithmetic operators:
5624
=====================
5625
Operator Meaning Example
5628
* Multiplication 2*3
5630
^ Exponentiation 2^3 # Equivalent to **
5631
- Unary minus -(1+2) # Not yet supported
5632
% Percent (Not modulus) 13% # Not supported, [1]
5637
Comparison operators:
5638
=====================
5639
Operator Meaning Example
5640
= Equal to A1 = B1 # Equivalent to ==
5641
<> Not equal to A1 <> B1 # Equivalent to !=
5642
> Greater than A1 > B1
5643
< Less than A1 < B1
5644
>= Greater than or equal to A1 >= B1
5645
<= Less than or equal to A1 <= B1
5652
Operator Meaning Example
5653
& Concatenation "Hello " & "World!" # [2]
5658
Reference operators:
5659
====================
5660
Operator Meaning Example
5661
: Range operator A1:A4 # [3]
5662
, Union operator SUM(1, 2+2, B3) # [4]
5668
[1]: You can get a percentage with formatting and modulus with MOD().
5669
[2]: Equivalent to ("Hello " . "World!") in Perl.
5670
[3]: This range is equivalent to cells A1, A2, A3 and A4.
5671
[4]: The comma behaves like the list separator in Perl.
5675
The range and comma operators can have different symbols in non-English
5676
versions of Excel. These will be supported in a later version of
5677
Spreadsheet::WriteExcel. European users of Excel take note:
5682
$worksheet->write('A1', '=SUM(1; 2; 3)'); # Wrong!!
5683
$worksheet->write('A1', '=SUM(1, 2, 3)'); # Okay
5687
The following table lists all of the core functions supported by Excel 5
5688
and Spreadsheet::WriteExcel. Any additional functions that are available
5689
through the "Analysis ToolPak" or other add-ins are not supported. These
5690
functions have all been tested to verify that they work.
5695
ABS DB INDIRECT NORMINV SLN
5696
ACOS DCOUNT INFO NORMSDIST SLOPE
5697
ACOSH DCOUNTA INT NORMSINV SMALL
5698
ADDRESS DDB INTERCEPT NOT SQRT
5699
AND DEGREES IPMT NOW STANDARDIZE
5700
AREAS DEVSQ IRR NPER STDEV
5701
ASIN DGET ISBLANK NPV STDEVP
5702
ASINH DMAX ISERR ODD STEYX
5703
ATAN DMIN ISERROR OFFSET SUBSTITUTE
5704
ATAN2 DOLLAR ISLOGICAL OR SUBTOTAL
5705
ATANH DPRODUCT ISNA PEARSON SUM
5706
AVEDEV DSTDEV ISNONTEXT PERCENTILE SUMIF
5707
AVERAGE DSTDEVP ISNUMBER PERCENTRANK SUMPRODUCT
5708
BETADIST DSUM ISREF PERMUT SUMSQ
5709
BETAINV DVAR ISTEXT PI SUMX2MY2
5710
BINOMDIST DVARP KURT PMT SUMX2PY2
5711
CALL ERROR.TYPE LARGE POISSON SUMXMY2
5712
CEILING EVEN LEFT POWER SYD
5713
CELL EXACT LEN PPMT T
5714
CHAR EXP LINEST PROB TAN
5715
CHIDIST EXPONDIST LN PRODUCT TANH
5716
CHIINV FACT LOG PROPER TDIST
5717
CHITEST FALSE LOG10 PV TEXT
5718
CHOOSE FDIST LOGEST QUARTILE TIME
5719
CLEAN FIND LOGINV RADIANS TIMEVALUE
5720
CODE FINV LOGNORMDIST RAND TINV
5721
COLUMN FISHER LOOKUP RANK TODAY
5722
COLUMNS FISHERINV LOWER RATE TRANSPOSE
5723
COMBIN FIXED MATCH REGISTER.ID TREND
5724
CONCATENATE FLOOR MAX REPLACE TRIM
5725
CONFIDENCE FORECAST MDETERM REPT TRIMMEAN
5726
CORREL FREQUENCY MEDIAN RIGHT TRUE
5727
COS FTEST MID ROMAN TRUNC
5728
COSH FV MIN ROUND TTEST
5729
COUNT GAMMADIST MINUTE ROUNDDOWN TYPE
5730
COUNTA GAMMAINV MINVERSE ROUNDUP UPPER
5731
COUNTBLANK GAMMALN MIRR ROW VALUE
5732
COUNTIF GEOMEAN MMULT ROWS VAR
5733
COVAR GROWTH MOD RSQ VARP
5734
CRITBINOM HARMEAN MODE SEARCH VDB
5735
DATE HLOOKUP MONTH SECOND VLOOKUP
5736
DATEVALUE HOUR N SIGN WEEKDAY
5737
DAVERAGE HYPGEOMDIST NA SIN WEIBULL
5738
DAY IF NEGBINOMDIST SINH YEAR
5739
DAYS360 INDEX NORMDIST SKEW ZTEST
5743
You can also modify the module to support function names in the following
5744
languages: German, French, Spanish, Portuguese, Dutch, Finnish, Italian and
5745
Swedish. See the <CODE>function_locale.pl</CODE> program in the <CODE>examples</CODE> directory of the distro.
5749
For a general introduction to Excel's formulas and an explanation of the
5750
syntax of the function refer to the Excel help files or the following
5752
HREF="http://msdn.microsoft.com/library/default.asp?URL=/library/officedev/office97/s88f2.htm">http://msdn.microsoft.com/library/default.asp?URL=/library/officedev/office97/s88f2.htm</A>
5754
HREF="http://msdn.microsoft.com/library/default.asp?URL=/library/en-us/office97/s992f.htm">http://msdn.microsoft.com/library/default.asp?URL=/library/en-us/office97/s992f.htm</A>
5759
If your formula doesn't work in Spreadsheet::WriteExcel try the following:
5764
1. Verify that the formula works in Excel (or Gnumeric or OpenOffice.org).
5765
2. Ensure that it isn't on the Caveats list shown above.
5766
3. Ensure that cell references and formula names are in uppercase.
5767
4. Ensure that you are using ':' as the range operator, A1:A4.
5768
5. Ensure that you are using ',' as the union operator, SUM(1,2,3).
5769
6. Ensure that the function is in the above table.
5773
If you go through steps 1-6 and you still have a problem, mail me.
5778
<H2><A NAME="Improving_performance_when_worki">Improving performance when working with formulas</A></H2>
5780
Writing a large number of formulas with Spreadsheet::WriteExcel can be
5781
slow. This is due to the fact that each formula has to be parsed and with
5782
the current implementation this is computationally expensive.
5786
However, in a lot of cases the formulas that you write will be quite
5787
similar, for example:
5792
$worksheet->write_formula('B1', '=A1 * 3 + 50', $format);
5793
$worksheet->write_formula('B2', '=A2 * 3 + 50', $format);
5796
$worksheet->write_formula('B99', '=A999 * 3 + 50', $format);
5797
$worksheet->write_formula('B1000', '=A1000 * 3 + 50', $format);
5801
In this example the cell reference changes in iterations from <CODE>A1</CODE> to <CODE>A1000</CODE>. The parser treats this variable as a <EM>token</EM> and arranges it according to predefined rules. However, since the parser is
5802
oblivious to the value of the token, it is essentially performing the same
5803
calculation 1000 times. This is inefficient.
5807
The way to avoid this inefficiency and thereby speed up the writing of
5808
formulas is to parse the formula once and then repeatedly substitute
5813
A formula can be parsed and stored via the <CODE>store_formula()</CODE> worksheet method. You can then use the <CODE>repeat_formula()</CODE> method to substitute <CODE>$pattern</CODE>, <CODE>$replace</CODE> pairs in the stored formula:
5818
my $formula = $worksheet->store_formula('=A1 * 3 + 50');
5823
for my $row (0..999) {
5824
$worksheet->repeat_formula($row, 1, $formula, $format, 'A1', 'A'.($row +1));
5829
On an arbitrary test machine this method was 10 times faster than the brute
5830
force method shown above.
5834
For more information about how Spreadsheet::WriteExcel parses and stores
5835
formulas see the <CODE>Spreadsheet::WriteExcel::Formula</CODE> man page.
5839
It should be noted however that the overall speed of direct formula parsing
5840
will be improved in a future version.
5845
<H1><A NAME="EXAMPLES">EXAMPLES</A></H1>
5848
<H2><A NAME="Example_1">Example 1</A></H2>
5850
The following example shows some of the basic features of
5851
Spreadsheet::WriteExcel.
5862
use Spreadsheet::WriteExcel;
5867
# Create a new workbook called simple.xls and add a worksheet
5868
my $workbook = Spreadsheet::WriteExcel->new("simple.xls");
5869
my $worksheet = $workbook->add_worksheet();
5874
# The general syntax is write($row, $column, $token). Note that row and
5875
# column are zero indexed
5881
$worksheet->write(0, 0, "Hi Excel!");
5886
# Write some numbers
5887
$worksheet->write(2, 0, 3); # Writes 3
5888
$worksheet->write(3, 0, 3.00000); # Writes 3
5889
$worksheet->write(4, 0, 3.00001); # Writes 3.00001
5890
$worksheet->write(5, 0, 3.14159); # TeX revision no.?
5895
# Write some formulas
5896
$worksheet->write(7, 0, '=A3 + A6');
5897
$worksheet->write(8, 0, '=IF(A5>3,"Yes", "No")');
5903
$worksheet->write(10, 0, '<A HREF="http://www.perl.com/">http://www.perl.com/</A>');
5907
<br><center><img SRC="simple.gif" ALT="The output from simple.pl"></center>
5911
<H2><A NAME="Example_2">Example 2</A></H2>
5913
The following is a general example which demonstrates some features of
5914
working with multiple worksheets.
5925
use Spreadsheet::WriteExcel;
5930
# Create a new Excel workbook
5931
my $workbook = Spreadsheet::WriteExcel->new("regions.xls");
5936
# Add some worksheets
5937
my $north = $workbook->add_worksheet("North");
5938
my $south = $workbook->add_worksheet("South");
5939
my $east = $workbook->add_worksheet("East");
5940
my $west = $workbook->add_worksheet("West");
5946
my $format = $workbook->add_format();
5947
$format->set_bold();
5948
$format->set_color('blue');
5953
# Add a caption to each worksheet
5954
foreach my $worksheet ($workbook->sheets()) {
5955
$worksheet->write(0, 0, "Sales", $format);
5962
$north->write(0, 1, 200000);
5963
$south->write(0, 1, 100000);
5964
$east->write (0, 1, 150000);
5965
$west->write (0, 1, 100000);
5970
# Set the active worksheet
5971
$south->activate();
5976
# Set the width of the first column
5977
$south->set_column(0, 0, 20);
5982
# Set the active cell
5983
$south->set_selection(0, 1);
5987
<br><center><img SRC="regions.gif" ALT="The output from regions.pl"></center>
5991
<H2><A NAME="Example_3">Example 3</A></H2>
5993
This example shows how to use a conditional numerical format with colours
5994
to indicate if a share price has gone up or down.
6000
use Spreadsheet::WriteExcel;
6005
# Create a new workbook and add a worksheet
6006
my $workbook = Spreadsheet::WriteExcel->new("stocks.xls");
6007
my $worksheet = $workbook->add_worksheet();
6012
# Set the column width for columns 1, 2, 3 and 4
6013
$worksheet->set_column(0, 3, 15);
6018
# Create a format for the column headings
6019
my $header = $workbook->add_format();
6020
$header->set_bold();
6021
$header->set_size(12);
6022
$header->set_color('blue');
6027
# Create a format for the stock price
6028
my $f_price = $workbook->add_format();
6029
$f_price->set_align('left');
6030
$f_price->set_num_format('$0.00');
6035
# Create a format for the stock volume
6036
my $f_volume = $workbook->add_format();
6037
$f_volume->set_align('left');
6038
$f_volume->set_num_format('#,##0');
6043
# Create a format for the price change. This is an example of a
6044
# conditional format. The number is formatted as a percentage. If it is
6045
# positive it is formatted in green, if it is negative it is formatted
6046
# in red and if it is zero it is formatted as the default font colour
6047
# (in this case black). Note: the [Green] format produces an unappealing
6048
# lime green. Try [Color 10] instead for a dark green.
6050
my $f_change = $workbook->add_format();
6051
$f_change->set_align('left');
6052
$f_change->set_num_format('[Green]0.0%;[Red]-0.0%;0.0%');
6057
# Write out the data
6058
$worksheet->write(0, 0, 'Company',$header);
6059
$worksheet->write(0, 1, 'Price', $header);
6060
$worksheet->write(0, 2, 'Volume', $header);
6061
$worksheet->write(0, 3, 'Change', $header);
6066
$worksheet->write(1, 0, 'Damage Inc.' );
6067
$worksheet->write(1, 1, 30.25, $f_price ); # $30.25
6068
$worksheet->write(1, 2, 1234567, $f_volume); # 1,234,567
6069
$worksheet->write(1, 3, 0.085, $f_change); # 8.5% in green
6074
$worksheet->write(2, 0, 'Dump Corp.' );
6075
$worksheet->write(2, 1, 1.56, $f_price ); # $1.56
6076
$worksheet->write(2, 2, 7564, $f_volume); # 7,564
6077
$worksheet->write(2, 3, -0.015, $f_change); # -1.5% in red
6082
$worksheet->write(3, 0, 'Rev Ltd.' );
6083
$worksheet->write(3, 1, 0.13, $f_price ); # $0.13
6084
$worksheet->write(3, 2, 321, $f_volume); # 321
6085
$worksheet->write(3, 3, 0, $f_change); # 0 in the font color (black)
6089
<br><center><img SRC="stocks.gif" ALT="The output from stocks.pl"></center>
6093
<H2><A NAME="Example_4">Example 4</A></H2>
6095
The following is a simple example of using functions.
6106
use Spreadsheet::WriteExcel;
6111
# Create a new workbook and add a worksheet
6112
my $workbook = Spreadsheet::WriteExcel->new("stats.xls");
6113
my $worksheet = $workbook->add_worksheet('Test data');
6118
# Set the column width for columns 1
6119
$worksheet->set_column(0, 0, 20);
6124
# Create a format for the headings
6125
my $format = $workbook->add_format();
6126
$format->set_bold();
6131
# Write the sample data
6132
$worksheet->write(0, 0, 'Sample', $format);
6133
$worksheet->write(0, 1, 1);
6134
$worksheet->write(0, 2, 2);
6135
$worksheet->write(0, 3, 3);
6136
$worksheet->write(0, 4, 4);
6137
$worksheet->write(0, 5, 5);
6138
$worksheet->write(0, 6, 6);
6139
$worksheet->write(0, 7, 7);
6140
$worksheet->write(0, 8, 8);
6145
$worksheet->write(1, 0, 'Length', $format);
6146
$worksheet->write(1, 1, 25.4);
6147
$worksheet->write(1, 2, 25.4);
6148
$worksheet->write(1, 3, 24.8);
6149
$worksheet->write(1, 4, 25.0);
6150
$worksheet->write(1, 5, 25.3);
6151
$worksheet->write(1, 6, 24.9);
6152
$worksheet->write(1, 7, 25.2);
6153
$worksheet->write(1, 8, 24.8);
6158
# Write some statistical functions
6159
$worksheet->write(4, 0, 'Count', $format);
6160
$worksheet->write(4, 1, '=COUNT(B1:I1)');
6165
$worksheet->write(5, 0, 'Sum', $format);
6166
$worksheet->write(5, 1, '=SUM(B2:I2)');
6171
$worksheet->write(6, 0, 'Average', $format);
6172
$worksheet->write(6, 1, '=AVERAGE(B2:I2)');
6177
$worksheet->write(7, 0, 'Min', $format);
6178
$worksheet->write(7, 1, '=MIN(B2:I2)');
6183
$worksheet->write(8, 0, 'Max', $format);
6184
$worksheet->write(8, 1, '=MAX(B2:I2)');
6189
$worksheet->write(9, 0, 'Standard Deviation', $format);
6190
$worksheet->write(9, 1, '=STDEV(B2:I2)');
6195
$worksheet->write(10, 0, 'Kurtosis', $format);
6196
$worksheet->write(10, 1, '=KURT(B2:I2)');
6200
<br><center><img SRC="stats.gif" ALT="The output from stats.pl"></center>
6204
<H2><A NAME="Example_5">Example 5</A></H2>
6206
The following example converts a tab separated file called <CODE>tab.txt</CODE> into an Excel file called <CODE>tab.xls</CODE>.
6217
use Spreadsheet::WriteExcel;
6222
open (TABFILE, "tab.txt") or die "tab.txt: $!";
6227
my $workbook = Spreadsheet::WriteExcel->new("tab.xls");
6228
my $worksheet = $workbook->add_worksheet();
6233
# Row and column are zero indexed
6239
while (<TABFILE>) {
6241
# Split on single tab
6242
my @Fld = split('\t', $_);
6248
foreach my $token (@Fld) {
6249
$worksheet->write($row, $col, $token);
6258
<H2><A NAME="Additional_Examples">Additional Examples</A></H2>
6260
If you performed a normal installation the following examples files should
6261
have been copied to your <CODE>~site/Spreadsheet/WriteExcel/examples</CODE> directory:
6265
The following is a description of the example files that are provided with
6266
Spreadsheet::WriteExcel. They are intended to demonstrate the different
6267
features and options of the module.
6274
bug_report.pl A template for submitting bug reports.
6275
demo.pl Creates a demo of some of the features.
6276
formats.pl Creates a demo of the available formatting.
6277
regions.pl Demonstrates multiple worksheets.
6278
simple.pl An example of some of the basic features.
6279
stats.pl Basic formulas and functions.
6286
bigfile.pl Write past the 7MB limit with OLE::Storage_Lite.
6287
cgi.pl A simple CGI program.
6288
chess.pl An example of formatting using properties.
6289
colors.pl Demo of the colour palette and named colours.
6290
copyformat.pl Example of copying a cell format.
6291
diag_border.pl A simple example of diagonal cell borders.
6292
easter_egg.pl Expose the Excel97 flight simulator. A must see.
6293
filehandle.pl Examples of working with filehandles.
6294
formula_result.pl Formulas with user specified results.
6295
headers.pl Examples of worksheet headers and footers.
6296
hyperlink1.pl Shows how to create web hyperlinks.
6297
hyperlink2.pl Examples of internal and external hyperlinks.
6298
images.pl Adding bitmap images to worksheets.
6299
indent.pl An example of cell indentation.
6300
merge1.pl A simple example of cell merging.
6301
merge2.pl A simple example of cell merging with formatting.
6302
merge3.pl Add hyperlinks to merged cells.
6303
merge4.pl An advanced example of merging with formatting.
6304
merge5.pl An advanced example of merging with formatting.
6305
mod_perl1.pl A simple mod_perl 1 program.
6306
mod_perl2.pl A simple mod_perl 2 program.
6307
outline.pl An example of outlines and grouping.
6308
panes.pl An examples of how to create panes.
6309
protection.pl Example of cell locking and formula hiding.
6310
hide_sheet.pl Simple example of hiding a worksheet.
6311
repeat.pl Example of writing repeated formulas.
6312
sales.pl An example of a simple sales spreadsheet.
6313
sendmail.pl Send an Excel email attachment using Mail::Sender.
6314
stats_ext.pl Same as stats.pl with external references.
6315
stocks.pl Demonstrates conditional formatting.
6316
textwrap.pl Demonstrates text wrapping options.
6317
win32ole.pl A sample Win32::OLE example for comparison.
6318
write_arrays.pl Example of writing 1D or 2D arrays of data.
6319
write_to_scalar.pl Example of writing an Excel file to a Perl scalar.
6320
write_handler1.pl Example of extending the write() method. Step 1.
6321
write_handler2.pl Example of extending the write() method. Step 2.
6322
write_handler3.pl Example of extending the write() method. Step 3.
6323
write_handler4.pl Example of extending the write() method. Step 4.
6330
unicode.pl Simple example of using Unicode UTF16 strings.
6331
unicode_japan.pl Write Japanese Unicode strings using UTF16.
6332
unicode_cyrillic.pl Write Russian cyrillic strings using UTF8.
6333
unicode_list.pl List the chars in a Unicode font.
6334
unicode_2022_jp.pl Japanese: ISO-2022-JP to utf8 in perl 5.8.
6335
unicode_8859_11.pl Thai: ISO-8859_11 to utf8 in perl 5.8.
6336
unicode_8859_7.pl Greek: ISO-8859_7 to utf8 in perl 5.8.
6337
unicode_big5.pl Chinese: BIG5 to utf8 in perl 5.8.
6338
unicode_cp1251.pl Russian: CP1251 to utf8 in perl 5.8.
6339
unicode_cp1256.pl Arabic: CP1256 to utf8 in perl 5.8.
6340
unicode_koi8r.pl Russian: KOI8-R to utf8 in perl 5.8.
6341
unicode_polish_utf8.pl Polish : UTF8 to utf8 in perl 5.8.
6342
unicode_shift_jis.pl Japanese: Shift JIS to utf8 in perl 5.8.
6349
csv2xls.pl Program to convert a CSV file to an Excel file.
6350
datecalc1.pl Convert Unix/Perl time to Excel time.
6351
datecalc2.pl Calculate an Excel date using Date::Calc.
6352
lecxe.pl Convert Excel to WriteExcel using Win32::OLE.
6353
tab2xls.pl Program to convert a tab separated file to xls.
6360
convertA1.pl Helper functions for dealing with A1 notation.
6361
function_locale.pl Add non-English function names to Formula.pm.
6362
writeA1.pl Example of how to extend the module.
6367
<H1><A NAME="LIMITATIONS">LIMITATIONS</A></H1>
6369
The following limits are imposed by Excel:
6375
----------------------------------- ------
6376
Maximum number of chars in a string 32767
6377
Maximum number of columns 256
6378
Maximum number of rows 65536
6379
Maximum chars in a sheet name 31
6380
Maximum chars in a header/footer 254
6384
The minimum file size is 6K due to the OLE overhead. The maximum file size
6385
is approximately 7MB (7087104 bytes) of BIFF data. This can be extended by
6386
using Takanori Kawai's OLE::Storage_Lite module <A
6387
HREF="http://search.cpan.org/search?dist=OLE-Storage_Lite">http://search.cpan.org/search?dist=OLE-Storage_Lite</A>
6388
see the <CODE>bigfile.pl</CODE> example in the <CODE>examples</CODE> directory of the distro.
6393
<H1><A NAME="DOWNLOADING">DOWNLOADING</A></H1>
6395
The latest version of this module is always available at: <A
6396
HREF="http://search.cpan.org/search?dist=Spreadsheet-WriteExcel/">http://search.cpan.org/search?dist=Spreadsheet-WriteExcel/</A>
6402
<H1><A NAME="REQUIREMENTS">REQUIREMENTS</A></H1>
6404
This module requires Perl 5.005 (or later), Parse::RecDescent and
6410
<A HREF="http://search.cpan.org/search?dist=Parse-RecDescent/">http://search.cpan.org/search?dist=Parse-RecDescent/</A>
6411
<A HREF="http://search.cpan.org/search?dist=File-Temp/">http://search.cpan.org/search?dist=File-Temp/</A>
6416
<H1><A NAME="INSTALLATION">INSTALLATION</A></H1>
6418
See the INSTALL or install.html docs that come with the distribution or:
6423
HREF="http://search.cpan.org/doc/JMCNAMARA/Spreadsheet-WriteExcel-2.11/WriteExcel/doc/install.html">http://search.cpan.org/doc/JMCNAMARA/Spreadsheet-WriteExcel-2.11/WriteExcel/doc/install.html</A>
6429
<H1><A NAME="PORTABILITY">PORTABILITY</A></H1>
6431
Spreadsheet::WriteExcel will work on the majority of Windows, UNIX and
6432
Macintosh platforms. Specifically, the module will work on any system where
6433
perl packs floats in the 64 bit IEEE format. The float must also be in
6434
little-endian format but it will be reversed if necessary. Thus:
6439
print join(" ", map { sprintf "%#02x", $_ } unpack("C*", pack "d", 1.2345)), "\n";
6443
should give (or in reverse order):
6448
0x8d 0x97 0x6e 0x12 0x83 0xc0 0xf3 0x3f
6452
In general, if you don't know whether your system supports a 64 bit IEEE
6453
float or not, it probably does. If your system doesn't, WriteExcel will <CODE>croak()</CODE> with the message given in the <A HREF="#DIAGNOSTICS">DIAGNOSTICS</A> section. You can check which platforms the module has been tested on at the
6454
CPAN testers site: <A
6455
HREF="http://testers.cpan.org/search?request=dist&dist=Spreadsheet-WriteExcel">http://testers.cpan.org/search?request=dist&dist=Spreadsheet-WriteExcel</A>
6461
<H1><A NAME="DIAGNOSTICS">DIAGNOSTICS</A></H1>
6463
<DT><A NAME="item_Filename">Filename required by Spreadsheet::WriteExcel->new()</A><DD>
6465
A filename must be given in the constructor.
6468
<DT><A NAME="item_Can">Can't open filename. It may be in use or protected.</A><DD>
6470
The file cannot be opened for writing. The directory that you are writing
6471
to may be protected or the file may be in use by another program.
6474
<DT><A NAME="item_Unable">Unable to create tmp files via File::Temp::tempfile()...</A><DD>
6476
This is a <CODE>-w</CODE> warning. You will see it if you are using Spreadsheet::WriteExcel in an
6477
environment where temporary files cannot be created, in which case all data
6478
will be stored in memory. The warning is for information only: it does not
6479
affect creation but it will affect the speed of execution for large files.
6480
See the <CODE>set_tempdir</CODE> workbook method.
6483
<DT><A NAME="item_Maximum">Maximum file size, 7087104, exceeded.</A><DD>
6485
The current OLE implementation only supports a maximum BIFF file of this
6486
size. This limit can be extended, see the <A HREF="#LIMITATIONS">LIMITATIONS</A> section.
6489
<DT>Can't locate Parse/RecDescent.pm in @INC ...<DD>
6491
Spreadsheet::WriteExcel requires the Parse::RecDescent module. Download it
6493
HREF="http://search.cpan.org/search?dist=Parse-RecDescent">http://search.cpan.org/search?dist=Parse-RecDescent</A>
6497
<DT><A NAME="item_Couldn">Couldn't parse formula ...</A><DD>
6499
There are a large number of warnings which relate to badly formed formulas
6500
and functions. See the <A HREF="#FORMULAS_AND_FUNCTIONS_IN_EXCEL">FORMULAS AND FUNCTIONS IN EXCEL</A> section for suggestions on how to avoid these errors. You should also check
6501
the formula in Excel to ensure that it is valid.
6504
<DT><A NAME="item_Required">Required floating point format not supported on this platform.</A><DD>
6506
Operating system doesn't support 64 bit IEEE float or it is byte-ordered in
6507
a way unknown to WriteExcel.
6510
<DT><A NAME="item__file_xls_">'file.xls' cannot be accessed. The file may be read-only ...</A><DD>
6512
You may sometimes encounter the following error when trying to open a file
6513
in Excel: "file.xls cannot be accessed. The file may be read-only, or you
6514
may be trying to access a read-only location. Or, the server the document
6515
is stored on may not be responding."
6519
This error generally means that the Excel file has been corrupted. There
6520
are two likely causes of this: the file was FTPed in ASCII mode instead of
6521
binary mode or else the file was created with UTF8 data returned by an XML
6522
parser. See <A HREF="#WORKING_WITH_XML">WORKING WITH XML</A> for further details.
6528
<H1><A NAME="THE_EXCEL_BINARY_FORMAT">THE EXCEL BINARY FORMAT</A></H1>
6530
The following is some general information about the Excel binary format for
6531
anyone who may be interested.
6535
Excel data is stored in the "Binary Interchange File Format" (BIFF) file
6536
format. Details of this format are given in the Excel SDK, the "Excel
6537
Developer's Kit" from Microsoft Press. It is also included in the MSDN CD
6538
library but is no longer available on the MSDN website. Versions of the
6539
BIFF documentation are available at www.wotsit.org, <A
6540
HREF="http://www.wotsit.org/search.asp?page=2&s=database">http://www.wotsit.org/search.asp?page=2&s=database</A>
6545
Charles Wybble has collected together almost all of the available
6546
information about the Excel file format. See "The Chicago Project" at <A
6547
HREF="http://chicago.sourceforge.net/devel/">http://chicago.sourceforge.net/devel/</A>
6552
Daniel Rentz of OpenOffice.org has also written a detailed description of
6553
the Excel workbook records, see <A
6554
HREF="http://sc.openoffice.org/excelfileformat.pdf">http://sc.openoffice.org/excelfileformat.pdf</A>
6559
The BIFF portion of the Excel file is comprised of contiguous binary
6560
records that have different functions and that hold different types of
6561
data. Each BIFF record is comprised of the following three parts:
6566
Record name; Hex identifier, length = 2 bytes
6567
Record length; Length of following data, length = 2 bytes
6568
Record data; Data, length = variable
6572
The BIFF data is stored along with other data in an OLE Compound File. This
6573
is a structured storage which acts like a file system within a file. A
6574
Compound File is comprised of storages and streams which, to follow the
6575
file system analogy, are like directories and files.
6579
The documentation for the OLE::Storage module, <A
6580
HREF="http://user.cs.tu-berlin.de/~schwartz/pmh/guide.html">http://user.cs.tu-berlin.de/~schwartz/pmh/guide.html</A>
6581
, contains one of the few descriptions of the OLE Compound File in the
6582
public domain. The Digital Imaging Group have also detailed the OLE format
6583
in the JPEG2000 specification: see Appendix A of <A
6584
HREF="http://www.i3a.org/pdf/wg1n1017.pdf">http://www.i3a.org/pdf/wg1n1017.pdf</A>
6589
For a open source implementation of the OLE library see the 'cole' library
6590
at <A HREF="http://atena.com/libole2.php">http://atena.com/libole2.php</A>
6594
The source code for the Excel plugin of the Gnumeric spreadsheet also
6595
contains information relevant to the Excel BIFF format and the OLE
6597
HREF="http://www.gnome.org/projects/gnumeric/">http://www.gnome.org/projects/gnumeric/</A>
6599
HREF="ftp://ftp.ximian.com/pub/ximian-source/">ftp://ftp.ximian.com/pub/ximian-source/</A>
6604
In addition the source code for OpenOffice.org is available at <A
6605
HREF="http://www.openoffice.org/">http://www.openoffice.org/</A>
6609
An article describing Spreadsheet::WriteExcel and how it works appears in
6610
Issue #19 of The Perl Journal, <A
6611
HREF="http://www.samag.com/documents/s=1272/sam05030004/">http://www.samag.com/documents/s=1272/sam05030004/</A>
6612
It is reproduced, by kind permission, in the <CODE>doc</CODE> directory of the distro.
6616
Please note that the provision of this information does not constitute an
6617
invitation to start hacking at the BIFF or OLE file formats. There are more
6618
interesting ways to waste your time. ;-)
6623
<H1><A NAME="WRITING_EXCEL_FILES">WRITING EXCEL FILES</A></H1>
6625
Depending on your requirements, background and general sensibilities you
6626
may prefer one of the following methods of getting data into Excel:
6630
<LI><A NAME="item_Win32">Win32::OLE module and office automation</A>
6632
This requires a Windows platform and an installed copy of Excel. This is
6633
the most powerful and complete method for interfacing with Excel. See <A
6634
HREF="http://www.activestate.com/ASPN/Reference/Products/ActivePerl-5.6/faq/Windows/ActivePerl-Winfaq12.html">http://www.activestate.com/ASPN/Reference/Products/ActivePerl-5.6/faq/Windows/ActivePerl-Winfaq12.html</A>
6636
HREF="http://www.activestate.com/ASPN/Reference/Products/ActivePerl-5.6/site/lib/Win32/OLE.html">http://www.activestate.com/ASPN/Reference/Products/ActivePerl-5.6/site/lib/Win32/OLE.html</A>
6637
If your main platform is UNIX but you have the resources to set up a
6638
separate Win32/MSOffice server, you can convert office documents to text,
6639
postscript or PDF using Win32::OLE. For a demonstration of how to do this
6640
using Perl see Docserver: <A
6641
HREF="http://search.cpan.org/search?mode=module&query=docserver">http://search.cpan.org/search?mode=module&query=docserver</A>
6645
<LI><A NAME="item_CSV">CSV, comma separated variables or text</A>
6647
If the file extension is <CODE>csv</CODE>, Excel will open and convert this format automatically. Generating a valid
6648
CSV file isn't as easy as it seems. Have a look at the DBD::RAM, DBD::CSV,
6649
Text::xSV and Text::CSV_XS modules.
6652
<LI><A NAME="item_DBI">DBI with DBD::ADO or DBD::ODBC</A>
6654
Excel files contain an internal index table that allows them to act like a
6655
database file. Using one of the standard Perl database modules you can
6656
connect to an Excel file as a database.
6659
<LI><A NAME="item_DBD">DBD::Excel</A>
6661
You can also access Spreadsheet::WriteExcel using the standard DBI
6662
interface via Takanori Kawai's DBD::Excel module <A
6663
HREF="http://search.cpan.org/dist/DBD-Excel">http://search.cpan.org/dist/DBD-Excel</A>
6667
<LI><A NAME="item_Spreadsheet">Spreadsheet::WriteExcelXML</A>
6669
This module allows you to create an Excel XML file using the same interface
6670
as Spreadsheet::WriteExcel. See: <A
6671
HREF="http://search.cpan.org/dist/Spreadsheet-WriteExcelXML">http://search.cpan.org/dist/Spreadsheet-WriteExcelXML</A>
6675
<LI><A NAME="item_Excel">Excel::Template</A>
6677
This module allows you to create an Excel file from an XML template in a
6678
manner similar to HTML::Template. See <A
6679
HREF="http://search.cpan.org/dist/Excel-Template/">http://search.cpan.org/dist/Excel-Template/</A>
6683
<LI><A NAME="item_Spreadsheet">Spreadsheet::WriteExcel::FromXML</A>
6685
This module allows you to turn a simple XML file into an Excel file using
6686
Spreadsheet::WriteExcel as a backend. The format of the XML file is defined
6687
by a supplied DTD: <A
6688
HREF="http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromXML">http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromXML</A>
6692
<LI><A NAME="item_Spreadsheet">Spreadsheet::WriteExcel::Simple</A>
6694
This provides an easier interface to Spreadsheet::WriteExcel: <A
6695
HREF="http://search.cpan.org/dist/Spreadsheet-WriteExcel-Simple">http://search.cpan.org/dist/Spreadsheet-WriteExcel-Simple</A>
6699
<LI><A NAME="item_Spreadsheet">Spreadsheet::WriteExcel::FromDB</A>
6701
This is a useful module for creating Excel files directly from a DB table:
6703
HREF="http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromDB">http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromDB</A>
6707
<LI><A NAME="item_HTML">HTML tables</A>
6709
This is an easy way of adding formatting via a text based format.
6712
<LI><A NAME="item_XML">XML or HTML</A>
6714
The Excel XML and HTML file specification are available from <A
6715
HREF="http://msdn.microsoft.com/library/officedev/ofxml2k/ofxml2k.htm">http://msdn.microsoft.com/library/officedev/ofxml2k/ofxml2k.htm</A>
6721
For other Perl-Excel modules try the following search: <A
6722
HREF="http://search.cpan.org/search?mode=module&query=excel">http://search.cpan.org/search?mode=module&query=excel</A>
6728
<H1><A NAME="READING_EXCEL_FILES">READING EXCEL FILES</A></H1>
6730
To read data from Excel files try:
6734
<LI><A NAME="item_Spreadsheet">Spreadsheet::ParseExcel</A>
6736
This uses the OLE::Storage-Lite module to extract data from an Excel file.
6738
HREF="http://search.cpan.org/dist/Spreadsheet-ParseExcel">http://search.cpan.org/dist/Spreadsheet-ParseExcel</A>
6742
<LI><A NAME="item_Spreadsheet">Spreadsheet::ParseExcel_XLHTML</A>
6744
This module uses Spreadsheet::ParseExcel's interface but uses xlHtml (see
6745
below) to do the conversion: <A
6746
HREF="http://search.cpan.org/dist/Spreadsheet-ParseExcel_XLHTML">http://search.cpan.org/dist/Spreadsheet-ParseExcel_XLHTML</A>
6747
Spreadsheet::ParseExcel_XLHTML
6750
<LI><A NAME="item_xlHtml">xlHtml</A>
6752
This is an open source "Excel to HTML Converter" C/C++ project at <A
6753
HREF="http://www.xlhtml.org/">http://www.xlhtml.org/</A> See also, the OLE
6754
Filters Project at <A
6755
HREF="http://atena.com/libole2.php">http://atena.com/libole2.php</A>
6758
<LI><A NAME="item_DBD">DBD::Excel (reading)</A>
6760
You can also access Spreadsheet::ParseExcel using the standard DBI
6761
interface via Takanori Kawai's DBD::Excel module <A
6762
HREF="http://search.cpan.org/dist/DBD-Excel">http://search.cpan.org/dist/DBD-Excel</A>
6766
<LI><A NAME="item_Win32">Win32::OLE module and office automation (reading)</A>
6768
See, the section <A HREF="#WRITING_EXCEL_FILES">WRITING EXCEL FILES</A>.
6771
<LI><A NAME="item_HTML">HTML tables (reading)</A>
6773
If the files are saved from Excel in a HTML format the data can be accessed
6774
using HTML::TableExtract <A
6775
HREF="http://search.cpan.org/dist/HTML-TableExtract">http://search.cpan.org/dist/HTML-TableExtract</A>
6779
<LI><A NAME="item_DBI">DBI with DBD::ADO or DBD::ODBC.</A>
6781
See, the section <A HREF="#WRITING_EXCEL_FILES">WRITING EXCEL FILES</A>.
6784
<LI><A NAME="item_XML">XML::Excel</A>
6786
Converts Excel files to XML using Spreadsheet::ParseExcel <A
6787
HREF="http://search.cpan.org/dist/XML-Excel.">http://search.cpan.org/dist/XML-Excel.</A>
6791
<LI><A NAME="item_OLE">OLE::Storage, aka LAOLA</A>
6793
This is a Perl interface to OLE file formats. In particular, the distro
6794
contains an Excel to HTML converter called Herbert, <A
6795
HREF="http://user.cs.tu-berlin.de/~schwartz/pmh/">http://user.cs.tu-berlin.de/~schwartz/pmh/</A>
6796
This has been superseded by the Spreadsheet::ParseExcel module.
6801
For other Perl-Excel modules try the following search: <A
6802
HREF="http://search.cpan.org/search?mode=module&query=excel">http://search.cpan.org/search?mode=module&query=excel</A>
6807
If you wish to view Excel files on a UNIX/Linux platform check out the
6808
excellent Gnumeric spreadsheet application at <A
6809
HREF="http://www.gnome.org/projects/gnumeric/">http://www.gnome.org/projects/gnumeric/</A>
6810
or OpenOffice.org at <A
6811
HREF="http://www.openoffice.org/">http://www.openoffice.org/</A>
6815
If you wish to view Excel files on a Windows platform which doesn't have
6816
Excel installed you can use the free Microsoft Excel Viewer <A
6817
HREF="http://office.microsoft.com/downloads/2000/xlviewer.aspx">http://office.microsoft.com/downloads/2000/xlviewer.aspx</A>
6823
<H1><A NAME="Warning_about_XML_Parser_and_Pe">Warning about XML::Parser and Perl 5.6</A></H1>
6825
You must be careful when using Spreadsheet::WriteExcel in conjunction with
6826
Perl 5.6 and XML::Parser (and other XML parsers) due to the fact that the
6827
data returned by the parser is generally in UTF8 format.
6831
When UTF8 strings are added to Spreadsheet::WriteExcel's internal data it
6832
causes the generated Excel file to become corrupt.
6836
Note, this doesn't affect Perl 5.005 (which doesn't try to handle UTF8) or
6837
5.8 (which handles it correctly).
6841
To avoid this problem you should upgrade to Perl 5.8, if possible, or else
6842
you should convert the output data from XML::Parser to ASCII or ISO-8859-1
6843
using one of the following methods:
6848
$new_str = pack 'C*', unpack 'U*', $utf8_str;
6853
use Unicode::MapUTF8 'from_utf8';
6854
$new_str = from_utf8({-str => $utf8_str, -charset => 'ISO-8859-1'});
6859
<H1><A NAME="BUGS">BUGS</A></H1>
6861
Formulas are formulae.
6865
This version of the module doesn't support the <CODE>write_comment()</CODE>
6866
method. This will be fixed soon.
6870
XML and UTF8 data on Perl 5.6 can cause Excel files created by
6871
Spreadsheet::WriteExcel to become corrupt. See <A HREF="#Warning_about_XML_Parser_and_Pe">Warning about XML::Parser and Perl 5.6</A> for further details.
6875
The format object that is used with a <CODE>merge_range()</CODE> method call is marked internally as being associated with a merged range.It
6876
is a fatal error to use a merged format in a non-merged cell. The current
6877
workaround is to use separate formats for merged and non-merged cell. This
6878
restriction will be removed in a future release.
6882
Nested formulas sometimes aren't parsed correctly and give a result of
6883
"#VALUE". If you come across a formula that parses like this, let
6888
Spreadsheet::ParseExcel: All formulas created by Spreadsheet::WriteExcel
6889
are read as having a value of zero. This is because Spreadsheet::WriteExcel
6890
only stores the formula and not the calculated result.
6894
OpenOffice.org: Some formatting is not displayed correctly.
6898
Gnumeric: Some formatting is not displayed correctly. URLs are not
6899
displayed as links. Page setup can cause Gnumeric to crash.
6903
The lack of a portable way of writing a little-endian 64 bit IEEE float.
6904
There is beta code available to fix this. Let me know if you wish to test
6905
it on your platform.
6909
If you wish to submit a bug report run the <CODE>bug_report.pl</CODE> program in the <CODE>examples</CODE> directory of the distro.
6914
<H1><A NAME="TO_DO">TO DO</A></H1>
6916
The roadmap is as follows:
6920
<LI><A NAME="item_Add">Add write_comment().</A>
6921
<LI><A NAME="item_Add">Add AutoFilters.</A>
6924
Also, here are some of the most requested features that probably won't get
6929
<LI><A NAME="item_Macros">Macros.</A>
6931
This would solve some other problems neatly. However, the format of Excel
6932
macros isn't documented.
6935
<LI><A NAME="item_Some">Some feature that you really need. ;-)</A>
6938
If there is some feature of an Excel file that you really, really need then
6939
you should use Win32::OLE with Excel on Windows. If you are on Unix you
6940
could consider connecting to a Windows server via Docserver or SOAP, see <A HREF="#WRITING_EXCEL_FILES">WRITING EXCEL FILES</A>.
6945
<H1><A NAME="MAILING_LIST">MAILING LIST</A></H1>
6947
There is a Google group for discussing and asking questions about
6948
Spreadsheet::WriteExcel: <A
6949
HREF="http://groups-beta.google.com/group/spreadsheet-writeexcel/">http://groups-beta.google.com/group/spreadsheet-writeexcel/</A>
6951
<table style="border:1px solid #aa0033; font-size:small" align=center>
6954
<img src="http://groups-beta.google.com/groups/img/groups_medium.gif" height=58 width=150 alt="Google Groups">
6956
<td colspan=2 align=center><b>Subscribe to Spreadsheet::WriteExcel</b></td>
6958
<form action="http://groups-beta.google.com/group/spreadsheet-writeexcel/boxsubscribe">
6960
<td>Email: <input type=text name=email></td>
6963
style="background-color:#ffcc33;padding:2px;border:2px outset #ffcc33;">
6966
<input type=submit name="sub" value="Subscribe">
6973
<tr><td colspan=2 align=center>
6974
<a href="http://groups-beta.google.com/group/spreadsheet-writeexcel">Browse Archives</a> at
6975
<a href="http://groups-beta.google.com/">groups-beta.google.com</a>
6980
Alternatively you can keep up to date with future releases by subscribing
6982
HREF="http://freshmeat.net/projects/writeexcel/">http://freshmeat.net/projects/writeexcel/</A>
6988
<H1><A NAME="DONATATIONS">DONATATIONS</A></H1>
6990
If you'd care to donate to the Spreadsheet::WriteExcel project, you can do
6994
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
6995
<input type="hidden" name="cmd" value="_xclick">
6996
<input type="hidden" name="business" value="jmcnamara@cpan.org">
6997
<input type="hidden" name="item_name" value="Spreadsheet::WriteExcel">
6998
<input type="hidden" name="no_shipping" value="1">
6999
<input type="hidden" name="cn" value="Optional note with donation">
7000
<input type="hidden" name="currency_code" value="USD">
7001
<input type="hidden" name="tax" value="0">
7002
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
7009
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
7011
Spreadsheet::ParseExcel: <A
7012
HREF="http://search.cpan.org/dist/Spreadsheet-ParseExcel">http://search.cpan.org/dist/Spreadsheet-ParseExcel</A>
7017
Spreadsheet-WriteExcel-FromXML: <A
7018
HREF="http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromXML">http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromXML</A>
7023
Spreadsheet::WriteExcel::FromDB: <A
7024
HREF="http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromDB">http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromDB</A>
7030
HREF="http://search.cpan.org/~rkinyon/Excel-Template/">http://search.cpan.org/~rkinyon/Excel-Template/</A>
7035
DateTime::Format::Excel: <A
7036
HREF="http://search.cpan.org/dist/DateTime-Format-Excel">http://search.cpan.org/dist/DateTime-Format-Excel</A>
7041
"Reading and writing Excel files with Perl" by Teodor Zlatanov, atIBM
7043
HREF="http://www-106.ibm.com/developerworks/library/l-pexcel/">http://www-106.ibm.com/developerworks/library/l-pexcel/</A>
7048
"Excel-Dateien mit Perl erstellen - Controller im Gl�ck" by Peter
7049
Dintelmann and Christian Kirsch in the German Unix/web journal iX: <A
7050
HREF="http://www.heise.de/ix/artikel/2001/06/175/">http://www.heise.de/ix/artikel/2001/06/175/</A>
7055
"Spreadsheet::WriteExcel" in The Perl Journal: <A
7056
HREF="http://www.samag.com/documents/s=1272/sam05030004/">http://www.samag.com/documents/s=1272/sam05030004/</A>
7061
Spreadsheet::WriteExcel documentation in Japanese by Takanori Kawai. <A
7062
HREF="http://member.nifty.ne.jp/hippo2000/perltips/Spreadsheet/WriteExcel.htm">http://member.nifty.ne.jp/hippo2000/perltips/Spreadsheet/WriteExcel.htm</A>
7067
Oesterly user brushes with fame: <A
7068
HREF="http://oesterly.com/releases/12102000.html">http://oesterly.com/releases/12102000.html</A>
7074
<H1><A NAME="ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</A></H1>
7076
The following people contributed to the debugging and testing of
7077
Spreadsheet::WriteExcel:
7081
Alexander Farber, Andre de Bruin, Arthur@ais, Artur Silveira da Cunha,
7082
Borgar Olsen, Brian White, Bob Mackay, Cedric Bouvier, Chad Johnson, CPAN
7083
testers, Damyan Ivanov, Daniel Berger, Daniel Gardner, Dmitry Kochurov,
7084
Eric Frazier, Ernesto Baschny, Felipe P�rez Galiana, Gordon Simpson, Hanc
7085
Pavel, Harold Bamford, James Holmes, James Wilkinson, Johan Ekenberg,
7086
Johann Hanne, Jonathan Scott Duff, J.C. Wren, Kenneth Stacey, Keith Miller,
7087
Kyle Krom, Marc Rosenthal, Markus Schmitz, Michael Braig, Michael
7088
Buschauer, Mike Blazer, Michael Erickson, Michael W J West, Ning Xie, Paul
7089
J. Falbe, Paul Medynski, Peter Dintelmann, Pierre Laplante, Praveen Kotha,
7090
Reto Badertscher, Rich Sorden, Shane Ashby, Shenyu Zheng, Stephan Loescher,
7091
Steve Sapovits, Sven Passig, Svetoslav Marinov, Tamas Gulacsi, Troy
7092
Daniels, Vahe Sarkissian.
7096
The following people contributed patches, examples or Excel information:
7100
Andrew Benham, Bill Young, Cedric Bouvier, Charles Wybble, Daniel Rentz,
7101
David Robins, Franco Venturi, Guy Albertelli, Ian Penman, John Heitmann,
7102
Jon Guy, Kyle R. Burton, Pierre-Jean Vouette, Rubio, Marco Geri, Mark
7103
Fowler, Matisse Enzer, Sam Kington, Takanori Kawai, Tom O'Sullivan.
7107
Many thanks to Ron McKelvey, Ronzo Consulting for Siemens, who sponsored
7108
the development of the formula caching routines.
7112
Additional thanks to Takanori Kawai for translating the documentation into
7117
Gunnar Wolf maintains the Debian distro.
7121
Thanks to Damian Conway for the excellent Parse::RecDescent.
7125
Thanks to Tim Jenness for File::Temp.
7129
Thanks to Michael Meeks and Jody Goldberg for their work on Gnumeric.
7134
<H1><A NAME="AUTHOR">AUTHOR</A></H1>
7136
John McNamara <A HREF="mailto:jmcnamara@cpan.org">jmcnamara@cpan.org</A>
7141
Olha que coisa mais linda
7142
Mais cheia de gra�a. � ela menina
7144
Num doce balan�o a caminho do mar
7149
-- Vinicius de Moraes
7154
<H1><A NAME="COPYRIGHT">COPYRIGHT</A></H1>
7156
� MM-MMV, John McNamara.
7160
All Rights Reserved. This module is free software. It may be used,
7161
redistributed and/or modified under the same terms as Perl itself.