Server IP : 103.119.228.120 / Your IP : 18.116.24.111 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/local/ssl/local/share/man/man3/ |
Upload File : |
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Spreadsheet::WriteExcel 3" .TH Spreadsheet::WriteExcel 3 "2013-11-07" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Spreadsheet::WriteExcel \- Write to a cross\-platform Excel binary file. .SH "VERSION" .IX Header "VERSION" This document refers to version 2.40 of Spreadsheet::WriteExcel, released November 6, 2013. .SH "SYNOPSIS" .IX Header "SYNOPSIS" To write a string, a formatted string, a number and a formula to the first worksheet in an Excel workbook called perl.xls: .PP .Vb 1 \& use Spreadsheet::WriteExcel; \& \& # Create a new Excel workbook \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqperl.xls\*(Aq); \& \& # Add a worksheet \& $worksheet = $workbook\->add_worksheet(); \& \& # Add and define a format \& $format = $workbook\->add_format(); # Add a format \& $format\->set_bold(); \& $format\->set_color(\*(Aqred\*(Aq); \& $format\->set_align(\*(Aqcenter\*(Aq); \& \& # Write a formatted and unformatted string, row and column notation. \& $col = $row = 0; \& $worksheet\->write($row, $col, \*(AqHi Excel!\*(Aq, $format); \& $worksheet\->write(1, $col, \*(AqHi Excel!\*(Aq); \& \& # Write a number and a formula using A1 notation \& $worksheet\->write(\*(AqA3\*(Aq, 1.2345); \& $worksheet\->write(\*(AqA4\*(Aq, \*(Aq=SIN(PI()/4)\*(Aq); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The Spreadsheet::WriteExcel Perl module can be used to create a cross-platform Excel binary file. Multiple worksheets can be added to a workbook and formatting can be applied to cells. Text, numbers, formulas, hyperlinks, images and charts can be written to the cells. .PP The file produced by this module is compatible with Excel 97, 2000, 2002, 2003 and 2007. .PP The module will work on the majority of Windows, \s-1UNIX\s0 and Mac platforms. Generated files are also compatible with the Linux/UNIX spreadsheet applications Gnumeric and OpenOffice.org. .PP This module cannot be used to write to an existing Excel file (See \*(L"\s-1MODIFYING AND REWRITING EXCEL FILES\*(R"\s0). .PP \&\fBNote: This module is in maintenance only mode\fR and in future will only be updated with bug fixes. The newer, more feature rich and \s-1API\s0 compatible Excel::Writer::XLSX module is recommended instead. See, \*(L"Migrating to Excel::Writer::XLSX\*(R". .SH "QUICK START" .IX Header "QUICK START" Spreadsheet::WriteExcel tries to provide an interface to as many of Excel's features as possible. As a result there is a lot of documentation to accompany the interface and it can be difficult at first glance to see what it important and what is not. So for those of you who prefer to assemble Ikea furniture first and then read the instructions, here are three easy steps: .PP 1. Create a new Excel \fIworkbook\fR (i.e. file) using \f(CW\*(C`new()\*(C'\fR. .PP 2. Add a \fIworksheet\fR to the new workbook using \f(CW\*(C`add_worksheet()\*(C'\fR. .PP 3. Write to the worksheet using \f(CW\*(C`write()\*(C'\fR. .PP Like this: .PP .Vb 1 \& use Spreadsheet::WriteExcel; # Step 0 \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqperl.xls\*(Aq); # Step 1 \& $worksheet = $workbook\->add_worksheet(); # Step 2 \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqHi Excel!\*(Aq); # Step 3 .Ve .PP This will create an Excel file called \f(CW\*(C`perl.xls\*(C'\fR with a single worksheet and the text \f(CW\*(AqHi Excel!\*(Aq\fR in the relevant cell. And that's it. Okay, so there is actually a zeroth step as well, but \f(CW\*(C`use module\*(C'\fR goes without saying. There are also more than 80 examples that come with the distribution and which you can use to get you started. See \*(L"\s-1EXAMPLES\*(R"\s0. .PP Those of you who read the instructions first and assemble the furniture afterwards will know how to proceed. ;\-) .SH "WORKBOOK METHODS" .IX Header "WORKBOOK METHODS" The Spreadsheet::WriteExcel module provides an object oriented interface to a new Excel workbook. The following methods are available through a new workbook. .PP .Vb 10 \& new() \& add_worksheet() \& add_format() \& add_chart() \& add_chart_ext() \& close() \& compatibility_mode() \& set_properties() \& define_name() \& set_tempdir() \& set_custom_color() \& sheets() \& set_1904() \& set_codepage() .Ve .PP If you are unfamiliar with object oriented interfaces or the way that they are implemented in Perl have a look at \f(CW\*(C`perlobj\*(C'\fR and \f(CW\*(C`perltoot\*(C'\fR in the main Perl documentation. .SS "\fInew()\fP" .IX Subsection "new()" A new Excel workbook is created using the \f(CW\*(C`new()\*(C'\fR constructor which accepts either a filename or a filehandle as a parameter. The following example creates a new Excel file based on a filename: .PP .Vb 3 \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqfilename.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& $worksheet\->write(0, 0, \*(AqHi Excel!\*(Aq); .Ve .PP Here are some other examples of using \f(CW\*(C`new()\*(C'\fR with filenames: .PP .Vb 4 \& my $workbook1 = Spreadsheet::WriteExcel\->new($filename); \& my $workbook2 = Spreadsheet::WriteExcel\->new(\*(Aq/tmp/filename.xls\*(Aq); \& my $workbook3 = Spreadsheet::WriteExcel\->new("c:\e\etmp\e\efilename.xls"); \& my $workbook4 = Spreadsheet::WriteExcel\->new(\*(Aqc:\etmp\efilename.xls\*(Aq); .Ve .PP The last two examples demonstrates how to create a file on \s-1DOS\s0 or Windows where it is necessary to either escape the directory separator \f(CW\*(C`\e\*(C'\fR or to use single quotes to ensure that it isn't interpolated. For more information see \f(CW\*(C`perlfaq5: Why can\*(Aqt I use "C:\etemp\efoo" in DOS paths?\*(C'\fR. .PP The \f(CW\*(C`new()\*(C'\fR constructor returns a Spreadsheet::WriteExcel object that you can use to add worksheets and store data. It should be noted that although \f(CW\*(C`my\*(C'\fR is not specifically required it defines the scope of the new workbook variable and, in the majority of cases, ensures that the workbook is closed properly without explicitly calling the \f(CW\*(C`close()\*(C'\fR method. .PP If the file cannot be created, due to file permissions or some other reason, \f(CW\*(C`new\*(C'\fR will return \f(CW\*(C`undef\*(C'\fR. Therefore, it is good practice to check the return value of \f(CW\*(C`new\*(C'\fR before proceeding. As usual the Perl variable \f(CW$!\fR will be set if there is a file creation error. You will also see one of the warning messages detailed in \*(L"\s-1DIAGNOSTICS\*(R"\s0: .PP .Vb 2 \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqprotected.xls\*(Aq); \& die "Problems creating new Excel file: $!" unless defined $workbook; .Ve .PP You can also pass a valid filehandle to the \f(CW\*(C`new()\*(C'\fR constructor. For example in a \s-1CGI\s0 program you could do something like this: .PP .Vb 2 \& binmode(STDOUT); \& my $workbook = Spreadsheet::WriteExcel\->new(\e*STDOUT); .Ve .PP The requirement for \f(CW\*(C`binmode()\*(C'\fR is explained below. .PP See also, the \f(CW\*(C`cgi.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .PP However, this special case will not work in \f(CW\*(C`mod_perl\*(C'\fR programs where you will have to do something like the following: .PP .Vb 6 \& # mod_perl 1 \& ... \& tie *XLS, \*(AqApache\*(Aq; \& binmode(XLS); \& my $workbook = Spreadsheet::WriteExcel\->new(\e*XLS); \& ... \& \& # mod_perl 2 \& ... \& tie *XLS => $r; # Tie to the Apache::RequestRec object \& binmode(*XLS); \& my $workbook = Spreadsheet::WriteExcel\->new(\e*XLS); \& ... .Ve .PP See also, the \f(CW\*(C`mod_perl1.pl\*(C'\fR and \f(CW\*(C`mod_perl2.pl\*(C'\fR programs in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .PP Filehandles can also be useful if you want to stream an Excel file over a socket or if you want to store an Excel file in a scalar. .PP For example here is a way to write an Excel file to a scalar with \f(CW\*(C`perl 5.8\*(C'\fR: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Requires perl 5.8 or later \& open my $fh, \*(Aq>\*(Aq, \emy $str or die "Failed to open filehandle: $!"; \& \& my $workbook = Spreadsheet::WriteExcel\->new($fh); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->write(0, 0, \*(AqHi Excel!\*(Aq); \& \& $workbook\->close(); \& \& # The Excel file in now in $str. Remember to binmode() the output \& # filehandle before printing it. \& binmode STDOUT; \& print $str; .Ve .PP See also the \f(CW\*(C`write_to_scalar.pl\*(C'\fR and \f(CW\*(C`filehandle.pl\*(C'\fR programs in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .PP \&\fBNote about the requirement for\fR \f(CW\*(C`binmode()\*(C'\fR. An Excel file is comprised of binary data. Therefore, if you are using a filehandle you should ensure that you \f(CW\*(C`binmode()\*(C'\fR it prior to passing it to \f(CW\*(C`new()\*(C'\fR.You should do this regardless of whether you are on a Windows platform or not. This applies especially to users of perl 5.8 on systems where \f(CW\*(C`UTF\-8\*(C'\fR is likely to be in operation such as RedHat Linux 9. If your program, either intentionally or not, writes \f(CW\*(C`UTF\-8\*(C'\fR data to a filehandle that is passed to \f(CW\*(C`new()\*(C'\fR it will corrupt the Excel file that is created. .PP You don't have to worry about \f(CW\*(C`binmode()\*(C'\fR if you are using filenames instead of filehandles. Spreadsheet::WriteExcel performs the \f(CW\*(C`binmode()\*(C'\fR internally when it converts the filename to a filehandle. For more information about \f(CW\*(C`binmode()\*(C'\fR see \f(CW\*(C`perlfunc\*(C'\fR and \f(CW\*(C`perlopentut\*(C'\fR in the main Perl documentation. .ie n .SS "add_worksheet($sheetname, $utf_16_be)" .el .SS "add_worksheet($sheetname, \f(CW$utf_16_be\fP)" .IX Subsection "add_worksheet($sheetname, $utf_16_be)" At least one worksheet should be added to a new workbook. A worksheet is used to write data into cells: .PP .Vb 4 \& $worksheet1 = $workbook\->add_worksheet(); # Sheet1 \& $worksheet2 = $workbook\->add_worksheet(\*(AqFoglio2\*(Aq); # Foglio2 \& $worksheet3 = $workbook\->add_worksheet(\*(AqData\*(Aq); # Data \& $worksheet4 = $workbook\->add_worksheet(); # Sheet4 .Ve .PP If \f(CW$sheetname\fR is not specified the default Excel convention will be followed, i.e. Sheet1, Sheet2, etc. The \f(CW$utf_16_be\fR parameter is optional, see below. .PP The worksheet name must be a valid Excel worksheet name, i.e. it cannot contain any of the following characters, \f(CW\*(C`[ ] : * ? / \e\*(C'\fR and it must be less than 32 characters. In addition, you cannot use the same, case insensitive, \f(CW$sheetname\fR for more than one worksheet. .PP On systems with \f(CW\*(C`perl 5.8\*(C'\fR and later the \f(CW\*(C`add_worksheet()\*(C'\fR method will also handle strings in \f(CW\*(C`UTF\-8\*(C'\fR format. .PP .Vb 1 \& $worksheet = $workbook\->add_worksheet("\ex{263a}"); # Smiley .Ve .PP On earlier Perl systems your can specify \f(CW\*(C`UTF\-16BE\*(C'\fR worksheet names using an additional optional parameter: .PP .Vb 2 \& my $name = pack \*(Aqn\*(Aq, 0x263a; \& $worksheet = $workbook\->add_worksheet($name, 1); # Smiley .Ve .SS "add_format(%properties)" .IX Subsection "add_format(%properties)" The \f(CW\*(C`add_format()\*(C'\fR method can be used to create new Format objects which are used to apply formatting to a cell. You can either define the properties at creation time via a hash of property values or later via method calls. .PP .Vb 2 \& $format1 = $workbook\->add_format(%props); # Set properties at creation \& $format2 = $workbook\->add_format(); # Set properties later .Ve .PP See the \*(L"\s-1CELL FORMATTING\*(R"\s0 section for more details about Format properties and how to set them. .SS "add_chart(%properties)" .IX Subsection "add_chart(%properties)" This method is use to create a new chart either as a standalone worksheet (the default) or as an embeddable object that can be inserted into a worksheet via the \f(CW\*(C`insert_chart()\*(C'\fR Worksheet method. .PP .Vb 1 \& my $chart = $workbook\->add_chart( type => \*(Aqcolumn\*(Aq ); .Ve .PP The properties that can be set are: .PP .Vb 3 \& type (required) \& name (optional) \& embedded (optional) .Ve .IP "\(bu" 4 \&\f(CW\*(C`type\*(C'\fR .Sp This is a required parameter. It defines the type of chart that will be created. .Sp .Vb 1 \& my $chart = $workbook\->add_chart( type => \*(Aqline\*(Aq ); .Ve .Sp The available types are: .Sp .Vb 7 \& area \& bar \& column \& line \& pie \& scatter \& stock .Ve .IP "\(bu" 4 \&\f(CW\*(C`name\*(C'\fR .Sp Set the name for the chart sheet. The name property is optional and if it isn't supplied will default to \f(CW\*(C`Chart1 .. n\*(C'\fR. The name must be a valid Excel worksheet name. See \f(CW\*(C`add_worksheet()\*(C'\fR for more details on valid sheet names. The \f(CW\*(C`name\*(C'\fR property can be omitted for embedded charts. .Sp .Vb 1 \& my $chart = $workbook\->add_chart( type => \*(Aqline\*(Aq, name => \*(AqResults Chart\*(Aq ); .Ve .IP "\(bu" 4 \&\f(CW\*(C`embedded\*(C'\fR .Sp Specifies that the Chart object will be inserted in a worksheet via the \f(CW\*(C`insert_chart()\*(C'\fR Worksheet method. It is an error to try insert a Chart that doesn't have this flag set. .Sp .Vb 1 \& my $chart = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the chart. \& ... \& \& # Insert the chart into the a worksheet. \& $worksheet\->insert_chart( \*(AqE2\*(Aq, $chart ); .Ve .PP See Spreadsheet::WriteExcel::Chart for details on how to configure the chart object once it is created. See also the \f(CW\*(C`chart_*.pl\*(C'\fR programs in the examples directory of the distro. .ie n .SS "add_chart_ext($chart_data, $chartname)" .el .SS "add_chart_ext($chart_data, \f(CW$chartname\fP)" .IX Subsection "add_chart_ext($chart_data, $chartname)" This method is use to include externally generated charts in a Spreadsheet::WriteExcel file. .PP .Vb 1 \& my $chart = $workbook\->add_chart_ext(\*(Aqchart01.bin\*(Aq, \*(AqChart1\*(Aq); .Ve .PP This feature is semi-deprecated in favour of the \*(L"native\*(R" charts created using \f(CW\*(C`add_chart()\*(C'\fR. Read \f(CW\*(C`external_charts.txt\*(C'\fR (or \f(CW\*(C`.pod\*(C'\fR) in the external_charts directory of the distro for a full explanation. .SS "\fIclose()\fP" .IX Subsection "close()" In general your Excel file will be closed automatically when your program ends or when the Workbook object goes out of scope, however the \f(CW\*(C`close()\*(C'\fR method can be used to explicitly close an Excel file. .PP .Vb 1 \& $workbook\->close(); .Ve .PP An explicit \f(CW\*(C`close()\*(C'\fR is required if the file must be closed prior to performing some external action on it such as copying it, reading its size or attaching it to an email. .PP In addition, \f(CW\*(C`close()\*(C'\fR may be required to prevent perl's garbage collector from disposing of the Workbook, Worksheet and Format objects in the wrong order. Situations where this can occur are: .IP "\(bu" 4 If \f(CW\*(C`my()\*(C'\fR was not used to declare the scope of a workbook variable created using \f(CW\*(C`new()\*(C'\fR. .IP "\(bu" 4 If the \f(CW\*(C`new()\*(C'\fR, \f(CW\*(C`add_worksheet()\*(C'\fR or \f(CW\*(C`add_format()\*(C'\fR methods are called in subroutines. .PP The reason for this is that Spreadsheet::WriteExcel relies on Perl's \f(CW\*(C`DESTROY\*(C'\fR mechanism to trigger destructor methods in a specific sequence. This may not happen in cases where the Workbook, Worksheet and Format variables are not lexically scoped or where they have different lexical scopes. .PP In general, if you create a file with a size of 0 bytes or you fail to create a file you need to call \f(CW\*(C`close()\*(C'\fR. .PP The return value of \f(CW\*(C`close()\*(C'\fR is the same as that returned by perl when it closes the file created by \f(CW\*(C`new()\*(C'\fR. This allows you to handle error conditions in the usual way: .PP .Vb 1 \& $workbook\->close() or die "Error closing file: $!"; .Ve .SS "\fIcompatibility_mode()\fP" .IX Subsection "compatibility_mode()" This method is used to improve compatibility with third party applications that read Excel files. .PP .Vb 1 \& $workbook\->compatibility_mode(); .Ve .PP An Excel file is comprised of binary records that describe properties of a spreadsheet. Excel is reasonably liberal about this and, outside of a core subset, it doesn't require every possible record to be present when it reads a file. This is also true of Gnumeric and OpenOffice.Org Calc. .PP Spreadsheet::WriteExcel takes advantage of this fact to omit some records in order to minimise the amount of data stored in memory and to simplify and speed up the writing of files. However, some third party applications that read Excel files often expect certain records to be present. In \*(L"compatibility mode\*(R" Spreadsheet::WriteExcel writes these records and tries to be as close to an Excel generated file as possible. .PP Applications that require \f(CW\*(C`compatibility_mode()\*(C'\fR are Apache \s-1POI,\s0 Apple Numbers, and Quickoffice on Nokia, Palm and other devices. You should also use \f(CW\*(C`compatibility_mode()\*(C'\fR if your Excel file will be used as an external data source by another Excel file. .PP If you encounter other situations that require \f(CW\*(C`compatibility_mode()\*(C'\fR, please let me know. .PP It should be noted that \f(CW\*(C`compatibility_mode()\*(C'\fR requires additional data to be stored in memory and additional processing. This incurs a memory and speed penalty and may not be suitable for very large files (>20MB). .PP You must call \f(CW\*(C`compatibility_mode()\*(C'\fR before calling \f(CW\*(C`add_worksheet()\*(C'\fR. .SS "\fIset_properties()\fP" .IX Subsection "set_properties()" The \f(CW\*(C`set_properties\*(C'\fR method can be used to set the document properties of the Excel file created by \f(CW\*(C`Spreadsheet::WriteExcel\*(C'\fR. These properties are visible when you use the \f(CW\*(C`File\->Properties\*(C'\fR menu option in Excel and are also available to external applications that read or index windows files. .PP The properties should be passed as a hash of values as follows: .PP .Vb 5 \& $workbook\->set_properties( \& title => \*(AqThis is an example spreadsheet\*(Aq, \& author => \*(AqJohn McNamara\*(Aq, \& comments => \*(AqCreated with Perl and Spreadsheet::WriteExcel\*(Aq, \& ); .Ve .PP The properties that can be set are: .PP .Vb 8 \& title \& subject \& author \& manager \& company \& category \& keywords \& comments .Ve .PP User defined properties are not supported due to effort required. .PP In perl 5.8+ you can also pass \s-1UTF\-8\s0 strings as properties. See \*(L"\s-1UNICODE IN EXCEL\*(R"\s0. .PP .Vb 1 \& my $smiley = chr 0x263A; \& \& $workbook\->set_properties( \& subject => "Happy now? $smiley", \& ); .Ve .PP With older versions of perl you can use a module to convert a non-ASCII string to a binary representation of \s-1UTF\-8\s0 and then pass an additional \f(CW\*(C`utf8\*(C'\fR flag to \f(CW\*(C`set_properties()\*(C'\fR: .PP .Vb 1 \& my $smiley = pack \*(AqH*\*(Aq, \*(AqE298BA\*(Aq; \& \& $workbook\->set_properties( \& subject => "Happy now? $smiley", \& utf8 => 1, \& ); .Ve .PP Usually Spreadsheet::WriteExcel allows you to use \s-1UTF\-16\s0 with pre 5.8 versions of perl. However, document properties don't support \s-1UTF\-16\s0 for these type of strings. .PP In order to promote the usefulness of Perl and the Spreadsheet::WriteExcel module consider adding a comment such as the following when using document properties: .PP .Vb 5 \& $workbook\->set_properties( \& ..., \& comments => \*(AqCreated with Perl and Spreadsheet::WriteExcel\*(Aq, \& ..., \& ); .Ve .PP This feature requires that the \f(CW\*(C`OLE::Storage_Lite\*(C'\fR module is installed (which is usually the case for a standard Spreadsheet::WriteExcel installation). However, this also means that the resulting \s-1OLE\s0 document may \fBpossibly\fR be buggy for files less than 7MB since it hasn't been as rigorously tested in that domain. As a result of this \f(CW\*(C`set_properties\*(C'\fR is currently incompatible with Gnumeric for files less than 7MB. This is being investigated. If you encounter any problems with this features let me know. .PP For convenience it is possible to pass either a hash or hash ref of arguments to this method. .PP See also the \f(CW\*(C`properties.pl\*(C'\fR program in the examples directory of the distro. .SS "\fIdefine_name()\fP" .IX Subsection "define_name()" This method is used to defined a name that can be used to represent a value, a single cell or a range of cells in a workbook. .PP .Vb 3 \& $workbook\->define_name(\*(AqExchange_rate\*(Aq, \*(Aq=0.96\*(Aq); \& $workbook\->define_name(\*(AqSales\*(Aq, \*(Aq=Sheet1!$G$1:$H$10\*(Aq); \& $workbook\->define_name(\*(AqSheet2!Sales\*(Aq, \*(Aq=Sheet2!$G$1:$G$10\*(Aq); .Ve .PP See the defined_name.pl program in the examples dir of the distro. .PP Note: This currently a beta feature. More documentation and examples will be added. .SS "\fIset_tempdir()\fP" .IX Subsection "set_tempdir()" For speed and efficiency \f(CW\*(C`Spreadsheet::WriteExcel\*(C'\fR stores worksheet data in temporary files prior to assembling the final workbook. .PP If Spreadsheet::WriteExcel is unable to create these temporary files it will store the required data in memory. This can be slow for large files. .PP The problem occurs mainly with \s-1IIS\s0 on Windows although it could feasibly occur on Unix systems as well. The problem generally occurs because the default temp file directory is defined as \f(CW\*(C`C:/\*(C'\fR or some other directory that \s-1IIS\s0 doesn't provide write access to. .PP To check if this might be a problem on a particular system you can run a simple test program with \f(CW\*(C`\-w\*(C'\fR or \f(CW\*(C`use warnings\*(C'\fR. This will generate a warning if the module cannot create the required temporary files: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqtest.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); .Ve .PP To avoid this problem the \f(CW\*(C`set_tempdir()\*(C'\fR method can be used to specify a directory that is accessible for the creation of temporary files. .PP The \f(CW\*(C`File::Temp\*(C'\fR module is used to create the temporary files. File::Temp uses \f(CW\*(C`File::Spec\*(C'\fR to determine an appropriate location for these files such as \f(CW\*(C`/tmp\*(C'\fR or \f(CW\*(C`c:\ewindows\etemp\*(C'\fR. You can find out which directory is used on your system as follows: .PP .Vb 1 \& perl \-MFile::Spec \-le "print File::Spec\->tmpdir" .Ve .PP Even if the default temporary file directory is accessible you may wish to specify an alternative location for security or maintenance reasons: .PP .Vb 2 \& $workbook\->set_tempdir(\*(Aq/tmp/writeexcel\*(Aq); \& $workbook\->set_tempdir(\*(Aqc:\ewindows\etemp\ewriteexcel\*(Aq); .Ve .PP The directory for the temporary file must exist, \f(CW\*(C`set_tempdir()\*(C'\fR will not create a new directory. .PP One disadvantage of using the \f(CW\*(C`set_tempdir()\*(C'\fR method is that on some Windows systems it will limit you to approximately 800 concurrent tempfiles. This means that a single program running on one of these systems will be limited to creating a total of 800 workbook and worksheet objects. You can run multiple, non-concurrent programs to work around this if necessary. .ie n .SS "set_custom_color($index, $red, $green, $blue)" .el .SS "set_custom_color($index, \f(CW$red\fP, \f(CW$green\fP, \f(CW$blue\fP)" .IX Subsection "set_custom_color($index, $red, $green, $blue)" The \f(CW\*(C`set_custom_color()\*(C'\fR method can be used to override one of the built-in palette values with a more suitable colour. .PP The value for \f(CW$index\fR should be in the range 8..63, see \*(L"\s-1COLOURS IN EXCEL\*(R"\s0. .PP The default named colours use the following indices: .PP .Vb 10 \& 8 => black \& 9 => white \& 10 => red \& 11 => lime \& 12 => blue \& 13 => yellow \& 14 => magenta \& 15 => cyan \& 16 => brown \& 17 => green \& 18 => navy \& 20 => purple \& 22 => silver \& 23 => gray \& 33 => pink \& 53 => orange .Ve .PP A new colour is set using its \s-1RGB \s0(red green blue) components. The \f(CW$red\fR, \f(CW$green\fR and \f(CW$blue\fR values must be in the range 0..255. You can determine the required values in Excel using the \f(CW\*(C`Tools\->Options\->Colors\->Modify\*(C'\fR dialog. .PP The \f(CW\*(C`set_custom_color()\*(C'\fR workbook method can also be used with a \s-1HTML\s0 style \f(CW\*(C`#rrggbb\*(C'\fR hex value: .PP .Vb 3 \& $workbook\->set_custom_color(40, 255, 102, 0 ); # Orange \& $workbook\->set_custom_color(40, 0xFF, 0x66, 0x00); # Same thing \& $workbook\->set_custom_color(40, \*(Aq#FF6600\*(Aq ); # Same thing \& \& my $font = $workbook\->add_format(color => 40); # Use the modified colour .Ve .PP The return value from \f(CW\*(C`set_custom_color()\*(C'\fR is the index of the colour that was changed: .PP .Vb 1 \& my $ferrari = $workbook\->set_custom_color(40, 216, 12, 12); \& \& my $format = $workbook\->add_format( \& bg_color => $ferrari, \& pattern => 1, \& border => 1 \& ); .Ve .SS "sheets(0, 1, ...)" .IX Subsection "sheets(0, 1, ...)" The \f(CW\*(C`sheets()\*(C'\fR method returns a list, or a sliced list, of the worksheets in a workbook. .PP If no arguments are passed the method returns a list of all the worksheets in the workbook. This is useful if you want to repeat an operation on each worksheet: .PP .Vb 3 \& foreach $worksheet ($workbook\->sheets()) { \& print $worksheet\->get_name(); \& } .Ve .PP You can also specify a slice list to return one or more worksheet objects: .PP .Vb 2 \& $worksheet = $workbook\->sheets(0); \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqHello\*(Aq); .Ve .PP Or since return value from \f(CW\*(C`sheets()\*(C'\fR is a reference to a worksheet object you can write the above example as: .PP .Vb 1 \& $workbook\->sheets(0)\->write(\*(AqA1\*(Aq, \*(AqHello\*(Aq); .Ve .PP The following example returns the first and last worksheet in a workbook: .PP .Vb 3 \& foreach $worksheet ($workbook\->sheets(0, \-1)) { \& # Do something \& } .Ve .PP Array slices are explained in the perldata manpage. .SS "\fIset_1904()\fP" .IX Subsection "set_1904()" Excel stores dates as real numbers where the integer part stores the number of days since the epoch and the fractional part stores the percentage of the day. The epoch can be either 1900 or 1904. Excel for Windows uses 1900 and Excel for Macintosh uses 1904. However, Excel on either platform will convert automatically between one system and the other. .PP Spreadsheet::WriteExcel stores dates in the 1900 format by default. If you wish to change this you can call the \f(CW\*(C`set_1904()\*(C'\fR workbook method. You can query the current value by calling the \f(CW\*(C`get_1904()\*(C'\fR workbook method. This returns 0 for 1900 and 1 for 1904. .PP See also \*(L"\s-1DATES AND TIME IN EXCEL\*(R"\s0 for more information about working with Excel's date system. .PP In general you probably won't need to use \f(CW\*(C`set_1904()\*(C'\fR. .SS "set_codepage($codepage)" .IX Subsection "set_codepage($codepage)" The default code page or character set used by Spreadsheet::WriteExcel is \s-1ANSI.\s0 This is also the default used by Excel for Windows. Occasionally however it may be necessary to change the code page via the \f(CW\*(C`set_codepage()\*(C'\fR method. .PP Changing the code page may be required if your are using Spreadsheet::WriteExcel on the Macintosh and you are using characters outside the \s-1ASCII 128\s0 character set: .PP .Vb 2 \& $workbook\->set_codepage(1); # ANSI, MS Windows \& $workbook\->set_codepage(2); # Apple Macintosh .Ve .PP The \f(CW\*(C`set_codepage()\*(C'\fR method is rarely required. .SH "WORKSHEET METHODS" .IX Header "WORKSHEET METHODS" A new worksheet is created by calling the \f(CW\*(C`add_worksheet()\*(C'\fR method from a workbook object: .PP .Vb 2 \& $worksheet1 = $workbook\->add_worksheet(); \& $worksheet2 = $workbook\->add_worksheet(); .Ve .PP The following methods are available through a new worksheet: .PP .Vb 10 \& write() \& write_number() \& write_string() \& write_utf16be_string() \& write_utf16le_string() \& keep_leading_zeros() \& write_blank() \& write_row() \& write_col() \& write_date_time() \& write_url() \& write_url_range() \& write_formula() \& store_formula() \& repeat_formula() \& write_comment() \& show_comments() \& add_write_handler() \& insert_image() \& insert_chart() \& data_validation() \& get_name() \& activate() \& select() \& hide() \& set_first_sheet() \& protect() \& set_selection() \& set_row() \& set_column() \& outline_settings() \& freeze_panes() \& split_panes() \& merge_range() \& set_zoom() \& right_to_left() \& hide_zero() \& set_tab_color() \& autofilter() .Ve .SS "Cell notation" .IX Subsection "Cell notation" Spreadsheet::WriteExcel supports two forms of notation to designate the position of cells: Row-column notation and A1 notation. .PP Row-column notation uses a zero based index for both row and column while A1 notation uses the standard Excel alphanumeric sequence of column letter and 1\-based row. For example: .PP .Vb 2 \& (0, 0) # The top left cell in row\-column notation. \& (\*(AqA1\*(Aq) # The top left cell in A1 notation. \& \& (1999, 29) # Row\-column notation. \& (\*(AqAD2000\*(Aq) # The same cell in A1 notation. .Ve .PP Row-column notation is useful if you are referring to cells programmatically: .PP .Vb 3 \& for my $i (0 .. 9) { \& $worksheet\->write($i, 0, \*(AqHello\*(Aq); # Cells A1 to A10 \& } .Ve .PP A1 notation is useful for setting up a worksheet manually and for working with formulas: .PP .Vb 2 \& $worksheet\->write(\*(AqH1\*(Aq, 200); \& $worksheet\->write(\*(AqH2\*(Aq, \*(Aq=H1+1\*(Aq); .Ve .PP In formulas and applicable methods you can also use the \f(CW\*(C`A:A\*(C'\fR column notation: .PP .Vb 1 \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq=SUM(B:B)\*(Aq); .Ve .PP The \f(CW\*(C`Spreadsheet::WriteExcel::Utility\*(C'\fR module that is included in the distro contains helper functions for dealing with A1 notation, for example: .PP .Vb 1 \& use Spreadsheet::WriteExcel::Utility; \& \& ($row, $col) = xl_cell_to_rowcol(\*(AqC2\*(Aq); # (1, 2) \& $str = xl_rowcol_to_cell(1, 2); # C2 .Ve .PP For simplicity, the parameter lists for the worksheet method calls in the following sections are given in terms of row-column notation. In all cases it is also possible to use A1 notation. .PP Note: in Excel it is also possible to use a R1C1 notation. This is not supported by Spreadsheet::WriteExcel. .ie n .SS "write($row, $column, $token, $format)" .el .SS "write($row, \f(CW$column\fP, \f(CW$token\fP, \f(CW$format\fP)" .IX Subsection "write($row, $column, $token, $format)" Excel makes a distinction between data types such as strings, numbers, blanks, formulas and hyperlinks. To simplify the process of writing data the \f(CW\*(C`write()\*(C'\fR method acts as a general alias for several more specific methods: .PP .Vb 7 \& write_string() \& write_number() \& write_blank() \& write_formula() \& write_url() \& write_row() \& write_col() .Ve .PP The general rule is that if the data looks like a \fIsomething\fR then a \fIsomething\fR is written. Here are some examples in both row-column and A1 notation: .PP .Vb 10 \& # Same as: \& $worksheet\->write(0, 0, \*(AqHello\*(Aq ); # write_string() \& $worksheet\->write(1, 0, \*(AqOne\*(Aq ); # write_string() \& $worksheet\->write(2, 0, 2 ); # write_number() \& $worksheet\->write(3, 0, 3.00001 ); # write_number() \& $worksheet\->write(4, 0, "" ); # write_blank() \& $worksheet\->write(5, 0, \*(Aq\*(Aq ); # write_blank() \& $worksheet\->write(6, 0, undef ); # write_blank() \& $worksheet\->write(7, 0 ); # write_blank() \& $worksheet\->write(8, 0, \*(Aqhttp://www.perl.com/\*(Aq); # write_url() \& $worksheet\->write(\*(AqA9\*(Aq, \*(Aqftp://ftp.cpan.org/\*(Aq ); # write_url() \& $worksheet\->write(\*(AqA10\*(Aq, \*(Aqinternal:Sheet1!A1\*(Aq ); # write_url() \& $worksheet\->write(\*(AqA11\*(Aq, \*(Aqexternal:c:\efoo.xls\*(Aq ); # write_url() \& $worksheet\->write(\*(AqA12\*(Aq, \*(Aq=A3 + 3*A4\*(Aq ); # write_formula() \& $worksheet\->write(\*(AqA13\*(Aq, \*(Aq=SIN(PI()/4)\*(Aq ); # write_formula() \& $worksheet\->write(\*(AqA14\*(Aq, \e@array ); # write_row() \& $worksheet\->write(\*(AqA15\*(Aq, [\e@array] ); # write_col() \& \& # And if the keep_leading_zeros property is set: \& $worksheet\->write(\*(AqA16\*(Aq, \*(Aq2\*(Aq ); # write_number() \& $worksheet\->write(\*(AqA17\*(Aq, \*(Aq02\*(Aq ); # write_string() \& $worksheet\->write(\*(AqA18\*(Aq, \*(Aq00002\*(Aq ); # write_string() .Ve .PP The \*(L"looks like\*(R" rule is defined by regular expressions: .PP \&\f(CW\*(C`write_number()\*(C'\fR if \f(CW$token\fR is a number based on the following regex: \f(CW\*(C`$token =~ /^([+\-]?)(?=\ed|\e.\ed)\ed*(\e.\ed*)?([Ee]([+\-]?\ed+))?$/\*(C'\fR. .PP \&\f(CW\*(C`write_string()\*(C'\fR if \f(CW\*(C`keep_leading_zeros()\*(C'\fR is set and \f(CW$token\fR is an integer with leading zeros based on the following regex: \f(CW\*(C`$token =~ /^0\ed+$/\*(C'\fR. .PP \&\f(CW\*(C`write_blank()\*(C'\fR if \f(CW$token\fR is undef or a blank string: \f(CW\*(C`undef\*(C'\fR, \f(CW""\fR or \f(CW\*(Aq\*(Aq\fR. .PP \&\f(CW\*(C`write_url()\*(C'\fR if \f(CW$token\fR is a http, https, ftp or mailto \s-1URL\s0 based on the following regexes: \f(CW\*(C`$token =~ m|^[fh]tt?ps?://|\*(C'\fR or \f(CW\*(C`$token =~ m|^mailto:|\*(C'\fR. .PP \&\f(CW\*(C`write_url()\*(C'\fR if \f(CW$token\fR is an internal or external sheet reference based on the following regex: \f(CW\*(C`$token =~ m[^(in|ex)ternal:]\*(C'\fR. .PP \&\f(CW\*(C`write_formula()\*(C'\fR if the first character of \f(CW$token\fR is \f(CW"="\fR. .PP \&\f(CW\*(C`write_row()\*(C'\fR if \f(CW$token\fR is an array ref. .PP \&\f(CW\*(C`write_col()\*(C'\fR if \f(CW$token\fR is an array ref of array refs. .PP \&\f(CW\*(C`write_string()\*(C'\fR if none of the previous conditions apply. .PP The \f(CW$format\fR parameter is optional. It should be a valid Format object, see \*(L"\s-1CELL FORMATTING\*(R"\s0: .PP .Vb 4 \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_color(\*(Aqred\*(Aq); \& $format\->set_align(\*(Aqcenter\*(Aq); \& \& $worksheet\->write(4, 0, \*(AqHello\*(Aq, $format); # Formatted string .Ve .PP The \fIwrite()\fR method will ignore empty strings or \f(CW\*(C`undef\*(C'\fR tokens unless a format is also supplied. As such you needn't worry about special handling for empty or \f(CW\*(C`undef\*(C'\fR values in your data. See also the \f(CW\*(C`write_blank()\*(C'\fR method. .PP One problem with the \f(CW\*(C`write()\*(C'\fR method is that occasionally data looks like a number but you don't want it treated as a number. For example, zip codes or \s-1ID\s0 numbers often start with a leading zero. If you write this data as a number then the leading zero(s) will be stripped. You can change this default behaviour by using the \f(CW\*(C`keep_leading_zeros()\*(C'\fR method. While this property is in place any integers with leading zeros will be treated as strings and the zeros will be preserved. See the \f(CW\*(C`keep_leading_zeros()\*(C'\fR section for a full discussion of this issue. .PP You can also add your own data handlers to the \f(CW\*(C`write()\*(C'\fR method using \f(CW\*(C`add_write_handler()\*(C'\fR. .PP On systems with \f(CW\*(C`perl 5.8\*(C'\fR and later the \f(CW\*(C`write()\*(C'\fR method will also handle Unicode strings in \f(CW\*(C`UTF\-8\*(C'\fR format. .PP The \f(CW\*(C`write\*(C'\fR methods return: .PP .Vb 4 \& 0 for success. \& \-1 for insufficient number of arguments. \& \-2 for row or column out of bounds. \& \-3 for string too long. .Ve .ie n .SS "write_number($row, $column, $number, $format)" .el .SS "write_number($row, \f(CW$column\fP, \f(CW$number\fP, \f(CW$format\fP)" .IX Subsection "write_number($row, $column, $number, $format)" Write an integer or a float to the cell specified by \f(CW$row\fR and \f(CW$column\fR: .PP .Vb 2 \& $worksheet\->write_number(0, 0, 123456); \& $worksheet\->write_number(\*(AqA2\*(Aq, 2.3451); .Ve .PP See the note about \*(L"Cell notation\*(R". The \f(CW$format\fR parameter is optional. .PP In general it is sufficient to use the \f(CW\*(C`write()\*(C'\fR method. .ie n .SS "write_string($row, $column, $string, $format)" .el .SS "write_string($row, \f(CW$column\fP, \f(CW$string\fP, \f(CW$format\fP)" .IX Subsection "write_string($row, $column, $string, $format)" Write a string to the cell specified by \f(CW$row\fR and \f(CW$column\fR: .PP .Vb 2 \& $worksheet\->write_string(0, 0, \*(AqYour text here\*(Aq ); \& $worksheet\->write_string(\*(AqA2\*(Aq, \*(Aqor here\*(Aq ); .Ve .PP The maximum string size is 32767 characters. However the maximum string segment that Excel can display in a cell is 1000. All 32767 characters can be displayed in the formula bar. .PP The \f(CW$format\fR parameter is optional. .PP On systems with \f(CW\*(C`perl 5.8\*(C'\fR and later the \f(CW\*(C`write()\*(C'\fR method will also handle strings in \f(CW\*(C`UTF\-8\*(C'\fR format. With older perls you can also write Unicode in \f(CW\*(C`UTF16\*(C'\fR format via the \f(CW\*(C`write_utf16be_string()\*(C'\fR method. See also the \f(CW\*(C`unicode_*.pl\*(C'\fR programs in the examples directory of the distro. .PP In general it is sufficient to use the \f(CW\*(C`write()\*(C'\fR method. However, you may sometimes wish to use the \f(CW\*(C`write_string()\*(C'\fR method to write data that looks like a number but that you don't want treated as a number. For example, zip codes or phone numbers: .PP .Vb 2 \& # Write as a plain string \& $worksheet\->write_string(\*(AqA1\*(Aq, \*(Aq01209\*(Aq); .Ve .PP However, if the user edits this string Excel may convert it back to a number. To get around this you can use the Excel text format \f(CW\*(C`@\*(C'\fR: .PP .Vb 3 \& # Format as a string. Doesn\*(Aqt change to a number when edited \& my $format1 = $workbook\->add_format(num_format => \*(Aq@\*(Aq); \& $worksheet\->write_string(\*(AqA2\*(Aq, \*(Aq01209\*(Aq, $format1); .Ve .PP See also the note about \*(L"Cell notation\*(R". .ie n .SS "write_utf16be_string($row, $column, $string, $format)" .el .SS "write_utf16be_string($row, \f(CW$column\fP, \f(CW$string\fP, \f(CW$format\fP)" .IX Subsection "write_utf16be_string($row, $column, $string, $format)" This method is used to write \f(CW\*(C`UTF\-16BE\*(C'\fR strings to a cell in Excel. It is functionally the same as the \f(CW\*(C`write_string()\*(C'\fR method except that the string should be in \f(CW\*(C`UTF\-16BE\*(C'\fR Unicode format. It is generally easier, when using Spreadsheet::WriteExcel, to write unicode strings in \f(CW\*(C`UTF\-8\*(C'\fR format, see \*(L"\s-1UNICODE IN EXCEL\*(R"\s0. The \f(CW\*(C`write_utf16be_string()\*(C'\fR method is mainly of use in versions of perl prior to 5.8. .PP The following is a simple example showing how to write some Unicode strings in \f(CW\*(C`UTF\-16BE\*(C'\fR format: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& \& use strict; \& use Spreadsheet::WriteExcel; \& use Unicode::Map(); \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqutf_16_be.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Increase the column width for clarity \& $worksheet\->set_column(\*(AqA:A\*(Aq, 25); \& \& \& # Write a Unicode character \& # \& my $smiley = pack \*(Aqn\*(Aq, 0x263a; \& \& # Increase the font size for legibility. \& my $big_font = $workbook\->add_format(size => 72); \& \& $worksheet\->write_utf16be_string(\*(AqA3\*(Aq, $smiley, $big_font); \& \& \& \& # Write a phrase in Cyrillic using a hex\-encoded string \& # \& my $str = pack \*(AqH*\*(Aq, \*(Aq042d0442043e0020044404400430043704300020043d\*(Aq . \& \*(Aq043000200440044304410441043a043e043c0021\*(Aq; \& \& $worksheet\->write_utf16be_string(\*(AqA5\*(Aq, $str); \& \& \& \& # Map a string to UTF\-16BE using an external module. \& # \& my $map = Unicode::Map\->new(\*(AqISO\-8859\-1\*(Aq); \& my $utf16 = $map\->to_unicode(\*(AqHello world!\*(Aq); \& \& $worksheet\->write_utf16be_string(\*(AqA7\*(Aq, $utf16); .Ve .PP You can convert \s-1ASCII\s0 encodings to the required \f(CW\*(C`UTF\-16BE\*(C'\fR format using one of the many Unicode modules on \s-1CPAN.\s0 For example \f(CW\*(C`Unicode::Map\*(C'\fR and \f(CW\*(C`Unicode::String\*(C'\fR: <http://search.cpan.org/author/MSCHWARTZ/Unicode\-Map/Map.pm> and <http://search.cpan.org/author/GAAS/Unicode\-String/String.pm>. .PP For a full list of the Perl Unicode modules see: <http://search.cpan.org/search?query=unicode&mode=all>. .PP \&\f(CW\*(C`UTF\-16BE\*(C'\fR is the format most often returned by \f(CW\*(C`Perl\*(C'\fR modules that generate \f(CW\*(C`UTF\-16\*(C'\fR. To write \f(CW\*(C`UTF\-16\*(C'\fR strings in little-endian format use the \f(CW\*(C`write_utf16be_string_le()\*(C'\fR method below. .PP The \f(CW\*(C`write_utf16be_string()\*(C'\fR method was previously called \f(CW\*(C`write_unicode()\*(C'\fR. That, overly general, name is still supported but deprecated. .PP See also the \f(CW\*(C`unicode_*.pl\*(C'\fR programs in the examples directory of the distro. .ie n .SS "write_utf16le_string($row, $column, $string, $format)" .el .SS "write_utf16le_string($row, \f(CW$column\fP, \f(CW$string\fP, \f(CW$format\fP)" .IX Subsection "write_utf16le_string($row, $column, $string, $format)" This method is the same as \f(CW\*(C`write_utf16be()\*(C'\fR except that the string should be 16\-bit characters in little-endian format. This is generally referred to as \f(CW\*(C`UTF\-16LE\*(C'\fR. See \*(L"\s-1UNICODE IN EXCEL\*(R"\s0. .PP \&\f(CW\*(C`UTF\-16\*(C'\fR data can be changed from little-endian to big-endian format (and vice-versa) as follows: .PP .Vb 1 \& $utf16be = pack \*(Aqn*\*(Aq, unpack \*(Aqv*\*(Aq, $utf16le; .Ve .SS "\fIkeep_leading_zeros()\fP" .IX Subsection "keep_leading_zeros()" This method changes the default handling of integers with leading zeros when using the \f(CW\*(C`write()\*(C'\fR method. .PP The \f(CW\*(C`write()\*(C'\fR method uses regular expressions to determine what type of data to write to an Excel worksheet. If the data looks like a number it writes a number using \f(CW\*(C`write_number()\*(C'\fR. One problem with this approach is that occasionally data looks like a number but you don't want it treated as a number. .PP Zip codes and \s-1ID\s0 numbers, for example, often start with a leading zero. If you write this data as a number then the leading zero(s) will be stripped. This is the also the default behaviour when you enter data manually in Excel. .PP To get around this you can use one of three options. Write a formatted number, write the number as a string or use the \f(CW\*(C`keep_leading_zeros()\*(C'\fR method to change the default behaviour of \f(CW\*(C`write()\*(C'\fR: .PP .Vb 2 \& # Implicitly write a number, the leading zero is removed: 1209 \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq01209\*(Aq); \& \& # Write a zero padded number using a format: 01209 \& my $format1 = $workbook\->add_format(num_format => \*(Aq00000\*(Aq); \& $worksheet\->write(\*(AqA2\*(Aq, \*(Aq01209\*(Aq, $format1); \& \& # Write explicitly as a string: 01209 \& $worksheet\->write_string(\*(AqA3\*(Aq, \*(Aq01209\*(Aq); \& \& # Write implicitly as a string: 01209 \& $worksheet\->keep_leading_zeros(); \& $worksheet\->write(\*(AqA4\*(Aq, \*(Aq01209\*(Aq); .Ve .PP The above code would generate a worksheet that looked like the following: .PP .Vb 7 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 1 | 1209 | | | | ... \& | 2 | 01209 | | | | ... \& | 3 | 01209 | | | | ... \& | 4 | 01209 | | | | ... .Ve .PP The examples are on different sides of the cells due to the fact that Excel displays strings with a left justification and numbers with a right justification by default. You can change this by using a format to justify the data, see \*(L"\s-1CELL FORMATTING\*(R"\s0. .PP It should be noted that if the user edits the data in examples \f(CW\*(C`A3\*(C'\fR and \f(CW\*(C`A4\*(C'\fR the strings will revert back to numbers. Again this is Excel's default behaviour. To avoid this you can use the text format \f(CW\*(C`@\*(C'\fR: .PP .Vb 3 \& # Format as a string (01209) \& my $format2 = $workbook\->add_format(num_format => \*(Aq@\*(Aq); \& $worksheet\->write_string(\*(AqA5\*(Aq, \*(Aq01209\*(Aq, $format2); .Ve .PP The \f(CW\*(C`keep_leading_zeros()\*(C'\fR property is off by default. The \f(CW\*(C`keep_leading_zeros()\*(C'\fR method takes 0 or 1 as an argument. It defaults to 1 if an argument isn't specified: .PP .Vb 3 \& $worksheet\->keep_leading_zeros(); # Set on \& $worksheet\->keep_leading_zeros(1); # Set on \& $worksheet\->keep_leading_zeros(0); # Set off .Ve .PP See also the \f(CW\*(C`add_write_handler()\*(C'\fR method. .ie n .SS "write_blank($row, $column, $format)" .el .SS "write_blank($row, \f(CW$column\fP, \f(CW$format\fP)" .IX Subsection "write_blank($row, $column, $format)" Write a blank cell specified by \f(CW$row\fR and \f(CW$column\fR: .PP .Vb 1 \& $worksheet\->write_blank(0, 0, $format); .Ve .PP This method is used to add formatting to a cell which doesn't contain a string or number value. .PP Excel differentiates between an \*(L"Empty\*(R" cell and a \*(L"Blank\*(R" cell. An \*(L"Empty\*(R" cell is a cell which doesn't contain data whilst a \*(L"Blank\*(R" cell is a cell which doesn't contain data but does contain formatting. Excel stores \*(L"Blank\*(R" cells but ignores \*(L"Empty\*(R" cells. .PP As such, if you write an empty cell without formatting it is ignored: .PP .Vb 2 \& $worksheet\->write(\*(AqA1\*(Aq, undef, $format); # write_blank() \& $worksheet\->write(\*(AqA2\*(Aq, undef ); # Ignored .Ve .PP This seemingly uninteresting fact means that you can write arrays of data without special treatment for undef or empty string values. .PP See the note about \*(L"Cell notation\*(R". .ie n .SS "write_row($row, $column, $array_ref, $format)" .el .SS "write_row($row, \f(CW$column\fP, \f(CW$array_ref\fP, \f(CW$format\fP)" .IX Subsection "write_row($row, $column, $array_ref, $format)" The \f(CW\*(C`write_row()\*(C'\fR method can be used to write a 1D or 2D array of data in one go. This is useful for converting the results of a database query into an Excel worksheet. You must pass a reference to the array of data rather than the array itself. The \f(CW\*(C`write()\*(C'\fR method is then called for each element of the data. For example: .PP .Vb 2 \& @array = (\*(Aqawk\*(Aq, \*(Aqgawk\*(Aq, \*(Aqmawk\*(Aq); \& $array_ref = \e@array; \& \& $worksheet\->write_row(0, 0, $array_ref); \& \& # The above example is equivalent to: \& $worksheet\->write(0, 0, $array[0]); \& $worksheet\->write(0, 1, $array[1]); \& $worksheet\->write(0, 2, $array[2]); .Ve .PP Note: For convenience the \f(CW\*(C`write()\*(C'\fR method behaves in the same way as \f(CW\*(C`write_row()\*(C'\fR if it is passed an array reference. Therefore the following two method calls are equivalent: .PP .Vb 2 \& $worksheet\->write_row(\*(AqA1\*(Aq, $array_ref); # Write a row of data \& $worksheet\->write( \*(AqA1\*(Aq, $array_ref); # Same thing .Ve .PP As with all of the write methods the \f(CW$format\fR parameter is optional. If a format is specified it is applied to all the elements of the data array. .PP Array references within the data will be treated as columns. This allows you to write 2D arrays of data in one go. For example: .PP .Vb 5 \& @eec = ( \& [\*(Aqmaggie\*(Aq, \*(Aqmilly\*(Aq, \*(Aqmolly\*(Aq, \*(Aqmay\*(Aq ], \& [13, 14, 15, 16 ], \& [\*(Aqshell\*(Aq, \*(Aqstar\*(Aq, \*(Aqcrab\*(Aq, \*(Aqstone\*(Aq] \& ); \& \& $worksheet\->write_row(\*(AqA1\*(Aq, \e@eec); .Ve .PP Would produce a worksheet as follows: .PP .Vb 9 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | A | B | C | D | E | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 1 | maggie | 13 | shell | ... | ... | ... \& | 2 | milly | 14 | star | ... | ... | ... \& | 3 | molly | 15 | crab | ... | ... | ... \& | 4 | may | 16 | stone | ... | ... | ... \& | 5 | ... | ... | ... | ... | ... | ... \& | 6 | ... | ... | ... | ... | ... | ... .Ve .PP To write the data in a row-column order refer to the \f(CW\*(C`write_col()\*(C'\fR method below. .PP Any \f(CW\*(C`undef\*(C'\fR values in the data will be ignored unless a format is applied to the data, in which case a formatted blank cell will be written. In either case the appropriate row or column value will still be incremented. .PP To find out more about array references refer to \f(CW\*(C`perlref\*(C'\fR and \f(CW\*(C`perlreftut\*(C'\fR in the main Perl documentation. To find out more about 2D arrays or \*(L"lists of lists\*(R" refer to \f(CW\*(C`perllol\*(C'\fR. .PP The \f(CW\*(C`write_row()\*(C'\fR method returns the first error encountered when writing the elements of the data or zero if no errors were encountered. See the return values described for the \f(CW\*(C`write()\*(C'\fR method above. .PP See also the \f(CW\*(C`write_arrays.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .PP The \f(CW\*(C`write_row()\*(C'\fR method allows the following idiomatic conversion of a text file to an Excel file: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqfile.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& open INPUT, \*(Aqfile.txt\*(Aq or die "Couldn\*(Aqt open file: $!"; \& \& $worksheet\->write($.\-1, 0, [split]) while <INPUT>; .Ve .ie n .SS "write_col($row, $column, $array_ref, $format)" .el .SS "write_col($row, \f(CW$column\fP, \f(CW$array_ref\fP, \f(CW$format\fP)" .IX Subsection "write_col($row, $column, $array_ref, $format)" The \f(CW\*(C`write_col()\*(C'\fR method can be used to write a 1D or 2D array of data in one go. This is useful for converting the results of a database query into an Excel worksheet. You must pass a reference to the array of data rather than the array itself. The \f(CW\*(C`write()\*(C'\fR method is then called for each element of the data. For example: .PP .Vb 2 \& @array = (\*(Aqawk\*(Aq, \*(Aqgawk\*(Aq, \*(Aqmawk\*(Aq); \& $array_ref = \e@array; \& \& $worksheet\->write_col(0, 0, $array_ref); \& \& # The above example is equivalent to: \& $worksheet\->write(0, 0, $array[0]); \& $worksheet\->write(1, 0, $array[1]); \& $worksheet\->write(2, 0, $array[2]); .Ve .PP As with all of the write methods the \f(CW$format\fR parameter is optional. If a format is specified it is applied to all the elements of the data array. .PP Array references within the data will be treated as rows. This allows you to write 2D arrays of data in one go. For example: .PP .Vb 5 \& @eec = ( \& [\*(Aqmaggie\*(Aq, \*(Aqmilly\*(Aq, \*(Aqmolly\*(Aq, \*(Aqmay\*(Aq ], \& [13, 14, 15, 16 ], \& [\*(Aqshell\*(Aq, \*(Aqstar\*(Aq, \*(Aqcrab\*(Aq, \*(Aqstone\*(Aq] \& ); \& \& $worksheet\->write_col(\*(AqA1\*(Aq, \e@eec); .Ve .PP Would produce a worksheet as follows: .PP .Vb 9 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | A | B | C | D | E | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 1 | maggie | milly | molly | may | ... | ... \& | 2 | 13 | 14 | 15 | 16 | ... | ... \& | 3 | shell | star | crab | stone | ... | ... \& | 4 | ... | ... | ... | ... | ... | ... \& | 5 | ... | ... | ... | ... | ... | ... \& | 6 | ... | ... | ... | ... | ... | ... .Ve .PP To write the data in a column-row order refer to the \f(CW\*(C`write_row()\*(C'\fR method above. .PP Any \f(CW\*(C`undef\*(C'\fR values in the data will be ignored unless a format is applied to the data, in which case a formatted blank cell will be written. In either case the appropriate row or column value will still be incremented. .PP As noted above the \f(CW\*(C`write()\*(C'\fR method can be used as a synonym for \f(CW\*(C`write_row()\*(C'\fR and \f(CW\*(C`write_row()\*(C'\fR handles nested array refs as columns. Therefore, the following two method calls are equivalent although the more explicit call to \f(CW\*(C`write_col()\*(C'\fR would be preferable for maintainability: .PP .Vb 2 \& $worksheet\->write_col(\*(AqA1\*(Aq, $array_ref ); # Write a column of data \& $worksheet\->write( \*(AqA1\*(Aq, [ $array_ref ]); # Same thing .Ve .PP To find out more about array references refer to \f(CW\*(C`perlref\*(C'\fR and \f(CW\*(C`perlreftut\*(C'\fR in the main Perl documentation. To find out more about 2D arrays or \*(L"lists of lists\*(R" refer to \f(CW\*(C`perllol\*(C'\fR. .PP The \f(CW\*(C`write_col()\*(C'\fR method returns the first error encountered when writing the elements of the data or zero if no errors were encountered. See the return values described for the \f(CW\*(C`write()\*(C'\fR method above. .PP See also the \f(CW\*(C`write_arrays.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .ie n .SS "write_date_time($row, $col, $date_string, $format)" .el .SS "write_date_time($row, \f(CW$col\fP, \f(CW$date_string\fP, \f(CW$format\fP)" .IX Subsection "write_date_time($row, $col, $date_string, $format)" The \f(CW\*(C`write_date_time()\*(C'\fR method can be used to write a date or time to the cell specified by \f(CW$row\fR and \f(CW$column\fR: .PP .Vb 1 \& $worksheet\->write_date_time(\*(AqA1\*(Aq, \*(Aq2004\-05\-13T23:20\*(Aq, $date_format); .Ve .PP The \f(CW$date_string\fR should be in the following format: .PP .Vb 1 \& yyyy\-mm\-ddThh:mm:ss.sss .Ve .PP This conforms to an \s-1ISO8601\s0 date but it should be noted that the full range of \s-1ISO8601\s0 formats are not supported. .PP The following variations on the \f(CW$date_string\fR parameter are permitted: .PP .Vb 6 \& yyyy\-mm\-ddThh:mm:ss.sss # Standard format \& yyyy\-mm\-ddT # No time \& Thh:mm:ss.sss # No date \& yyyy\-mm\-ddThh:mm:ss.sssZ # Additional Z (but not time zones) \& yyyy\-mm\-ddThh:mm:ss # No fractional seconds \& yyyy\-mm\-ddThh:mm # No seconds .Ve .PP Note that the \f(CW\*(C`T\*(C'\fR is required in all cases. .PP A date should always have a \f(CW$format\fR, otherwise it will appear as a number, see \*(L"\s-1DATES AND TIME IN EXCEL\*(R"\s0 and \*(L"\s-1CELL FORMATTING\*(R"\s0. Here is a typical example: .PP .Vb 2 \& my $date_format = $workbook\->add_format(num_format => \*(Aqmm/dd/yy\*(Aq); \& $worksheet\->write_date_time(\*(AqA1\*(Aq, \*(Aq2004\-05\-13T23:20\*(Aq, $date_format); .Ve .PP Valid dates should be in the range 1900\-01\-01 to 9999\-12\-31, for the 1900 epoch and 1904\-01\-01 to 9999\-12\-31, for the 1904 epoch. As with Excel, dates outside these ranges will be written as a string. .PP See also the date_time.pl program in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .ie n .SS "write_url($row, $col, $url, $label, $format)" .el .SS "write_url($row, \f(CW$col\fP, \f(CW$url\fP, \f(CW$label\fP, \f(CW$format\fP)" .IX Subsection "write_url($row, $col, $url, $label, $format)" Write a hyperlink to a \s-1URL\s0 in the cell specified by \f(CW$row\fR and \f(CW$column\fR. The hyperlink is comprised of two elements: the visible label and the invisible link. The visible label is the same as the link unless an alternative label is specified. The parameters \f(CW$label\fR and the \f(CW$format\fR are optional and their position is interchangeable. .PP The label is written using the \f(CW\*(C`write()\*(C'\fR method. Therefore it is possible to write strings, numbers or formulas as labels. .PP There are four web style \s-1URI\s0's supported: \f(CW\*(C`http://\*(C'\fR, \f(CW\*(C`https://\*(C'\fR, \f(CW\*(C`ftp://\*(C'\fR and \f(CW\*(C`mailto:\*(C'\fR: .PP .Vb 5 \& $worksheet\->write_url(0, 0, \*(Aqftp://www.perl.org/\*(Aq ); \& $worksheet\->write_url(1, 0, \*(Aqhttp://www.perl.com/\*(Aq, \*(AqPerl home\*(Aq ); \& $worksheet\->write_url(\*(AqA3\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq, $format ); \& $worksheet\->write_url(\*(AqA4\*(Aq, \*(Aqhttp://www.perl.com/\*(Aq, \*(AqPerl\*(Aq, $format); \& $worksheet\->write_url(\*(AqA5\*(Aq, \*(Aqmailto:jmcnamara@cpan.org\*(Aq ); .Ve .PP There are two local URIs supported: \f(CW\*(C`internal:\*(C'\fR and \f(CW\*(C`external:\*(C'\fR. These are used for hyperlinks to internal worksheet references or external workbook and worksheet references: .PP .Vb 9 \& $worksheet\->write_url(\*(AqA6\*(Aq, \*(Aqinternal:Sheet2!A1\*(Aq ); \& $worksheet\->write_url(\*(AqA7\*(Aq, \*(Aqinternal:Sheet2!A1\*(Aq, $format ); \& $worksheet\->write_url(\*(AqA8\*(Aq, \*(Aqinternal:Sheet2!A1:B2\*(Aq ); \& $worksheet\->write_url(\*(AqA9\*(Aq, q{internal:\*(AqSales Data\*(Aq!A1} ); \& $worksheet\->write_url(\*(AqA10\*(Aq, \*(Aqexternal:c:\etemp\efoo.xls\*(Aq ); \& $worksheet\->write_url(\*(AqA11\*(Aq, \*(Aqexternal:c:\etemp\efoo.xls#Sheet2!A1\*(Aq ); \& $worksheet\->write_url(\*(AqA12\*(Aq, \*(Aqexternal:..\e..\e..\efoo.xls\*(Aq ); \& $worksheet\->write_url(\*(AqA13\*(Aq, \*(Aqexternal:..\e..\e..\efoo.xls#Sheet2!A1\*(Aq ); \& $worksheet\->write_url(\*(AqA13\*(Aq, \*(Aqexternal:\e\e\e\eNETWORK\eshare\efoo.xls\*(Aq ); .Ve .PP All of the these \s-1URI\s0 types are recognised by the \f(CW\*(C`write()\*(C'\fR method, see above. .PP Worksheet references are typically of the form \f(CW\*(C`Sheet1!A1\*(C'\fR. You can also refer to a worksheet range using the standard Excel notation: \f(CW\*(C`Sheet1!A1:B2\*(C'\fR. .PP In external links the workbook and worksheet name must be separated by the \f(CW\*(C`#\*(C'\fR character: \f(CW\*(C`external:Workbook.xls#Sheet1!A1\*(Aq\*(C'\fR. .PP You can also link to a named range in the target worksheet. For example say you have a named range called \f(CW\*(C`my_name\*(C'\fR in the workbook \f(CW\*(C`c:\etemp\efoo.xls\*(C'\fR you could link to it as follows: .PP .Vb 1 \& $worksheet\->write_url(\*(AqA14\*(Aq, \*(Aqexternal:c:\etemp\efoo.xls#my_name\*(Aq); .Ve .PP Note, you cannot currently create named ranges with \f(CW\*(C`Spreadsheet::WriteExcel\*(C'\fR. .PP Excel requires that worksheet names containing spaces or non alphanumeric characters are single quoted as follows \f(CW\*(C`\*(AqSales Data\*(Aq!A1\*(C'\fR. If you need to do this in a single quoted string then you can either escape the single quotes \f(CW\*(C`\e\*(Aq\*(C'\fR or use the quote operator \f(CW\*(C`q{}\*(C'\fR as described in \f(CW\*(C`perlop\*(C'\fR in the main Perl documentation. .PP Links to network files are also supported. MS/Novell Network files normally begin with two back slashes as follows \f(CW\*(C`\e\eNETWORK\eetc\*(C'\fR. In order to generate this in a single or double quoted string you will have to escape the backslashes, \f(CW\*(Aq\e\e\e\eNETWORK\eetc\*(Aq\fR. .PP If you are using double quote strings then you should be careful to escape anything that looks like a metacharacter. For more information see \f(CW\*(C`perlfaq5: Why can\*(Aqt I use "C:\etemp\efoo" in DOS paths?\*(C'\fR. .PP Finally, you can avoid most of these quoting problems by using forward slashes. These are translated internally to backslashes: .PP .Vb 2 \& $worksheet\->write_url(\*(AqA14\*(Aq, "external:c:/temp/foo.xls" ); \& $worksheet\->write_url(\*(AqA15\*(Aq, \*(Aqexternal://NETWORK/share/foo.xls\*(Aq ); .Ve .PP See also, the note about \*(L"Cell notation\*(R". .ie n .SS "write_url_range($row1, $col1, $row2, $col2, $url, $string, $format)" .el .SS "write_url_range($row1, \f(CW$col1\fP, \f(CW$row2\fP, \f(CW$col2\fP, \f(CW$url\fP, \f(CW$string\fP, \f(CW$format\fP)" .IX Subsection "write_url_range($row1, $col1, $row2, $col2, $url, $string, $format)" This method is essentially the same as the \f(CW\*(C`write_url()\*(C'\fR method described above. The main difference is that you can specify a link for a range of cells: .PP .Vb 4 \& $worksheet\->write_url(0, 0, 0, 3, \*(Aqftp://www.perl.org/\*(Aq ); \& $worksheet\->write_url(1, 0, 0, 3, \*(Aqhttp://www.perl.com/\*(Aq, \*(AqPerl home\*(Aq); \& $worksheet\->write_url(\*(AqA3:D3\*(Aq, \*(Aqinternal:Sheet2!A1\*(Aq ); \& $worksheet\->write_url(\*(AqA4:D4\*(Aq, \*(Aqexternal:c:\etemp\efoo.xls\*(Aq ); .Ve .PP This method is generally only required when used in conjunction with merged cells. See the \f(CW\*(C`merge_range()\*(C'\fR method and the \f(CW\*(C`merge\*(C'\fR property of a Format object, \*(L"\s-1CELL FORMATTING\*(R"\s0. .PP There is no way to force this behaviour through the \f(CW\*(C`write()\*(C'\fR method. .PP The parameters \f(CW$string\fR and the \f(CW$format\fR are optional and their position is interchangeable. However, they are applied only to the first cell in the range. .PP See also, the note about \*(L"Cell notation\*(R". .ie n .SS "write_formula($row, $column, $formula, $format, $value)" .el .SS "write_formula($row, \f(CW$column\fP, \f(CW$formula\fP, \f(CW$format\fP, \f(CW$value\fP)" .IX Subsection "write_formula($row, $column, $formula, $format, $value)" Write a formula or function to the cell specified by \f(CW$row\fR and \f(CW$column\fR: .PP .Vb 6 \& $worksheet\->write_formula(0, 0, \*(Aq=$B$3 + B4\*(Aq ); \& $worksheet\->write_formula(1, 0, \*(Aq=SIN(PI()/4)\*(Aq); \& $worksheet\->write_formula(2, 0, \*(Aq=SUM(B1:B5)\*(Aq ); \& $worksheet\->write_formula(\*(AqA4\*(Aq, \*(Aq=IF(A3>1,"Yes", "No")\*(Aq ); \& $worksheet\->write_formula(\*(AqA5\*(Aq, \*(Aq=AVERAGE(1, 2, 3, 4)\*(Aq ); \& $worksheet\->write_formula(\*(AqA6\*(Aq, \*(Aq=DATEVALUE("1\-Jan\-2001")\*(Aq); .Ve .PP See the note about \*(L"Cell notation\*(R". For more information about writing Excel formulas see \*(L"\s-1FORMULAS AND FUNCTIONS IN EXCEL\*(R"\s0 .PP See also the section \*(L"Improving performance when working with formulas\*(R" and the \f(CW\*(C`store_formula()\*(C'\fR and \f(CW\*(C`repeat_formula()\*(C'\fR methods. .PP If required, it is also possible to specify the calculated value of the formula. This is occasionally necessary when working with non-Excel applications that don't calculate the value of the formula. The calculated \f(CW$value\fR is added at the end of the argument list: .PP .Vb 1 \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq=2+2\*(Aq, $format, 4); .Ve .PP However, this probably isn't something that will ever need to do. If you do use this feature then do so with care. .SS "store_formula($formula)" .IX Subsection "store_formula($formula)" The \f(CW\*(C`store_formula()\*(C'\fR method is used in conjunction with \f(CW\*(C`repeat_formula()\*(C'\fR to speed up the generation of repeated formulas. See \*(L"Improving performance when working with formulas\*(R" in \*(L"\s-1FORMULAS AND FUNCTIONS IN EXCEL\*(R"\s0. .PP The \f(CW\*(C`store_formula()\*(C'\fR method pre-parses a textual representation of a formula and stores it for use at a later stage by the \f(CW\*(C`repeat_formula()\*(C'\fR method. .PP \&\f(CW\*(C`store_formula()\*(C'\fR carries the same speed penalty as \f(CW\*(C`write_formula()\*(C'\fR. However, in practice it will be used less frequently. .PP The return value of this method is a scalar that can be thought of as a reference to a formula. .PP .Vb 2 \& my $sin = $worksheet\->store_formula(\*(Aq=SIN(A1)\*(Aq); \& my $cos = $worksheet\->store_formula(\*(Aq=COS(A1)\*(Aq); \& \& $worksheet\->repeat_formula(\*(AqB1\*(Aq, $sin, $format, \*(AqA1\*(Aq, \*(AqA2\*(Aq); \& $worksheet\->repeat_formula(\*(AqC1\*(Aq, $cos, $format, \*(AqA1\*(Aq, \*(AqA2\*(Aq); .Ve .PP Although \f(CW\*(C`store_formula()\*(C'\fR is a worksheet method the return value can be used in any worksheet: .PP .Vb 1 \& my $now = $worksheet\->store_formula(\*(Aq=NOW()\*(Aq); \& \& $worksheet1\->repeat_formula(\*(AqB1\*(Aq, $now); \& $worksheet2\->repeat_formula(\*(AqB1\*(Aq, $now); \& $worksheet3\->repeat_formula(\*(AqB1\*(Aq, $now); .Ve .ie n .SS "repeat_formula($row, $col, $formula, $format, ($pattern => $replace, ...))" .el .SS "repeat_formula($row, \f(CW$col\fP, \f(CW$formula\fP, \f(CW$format\fP, ($pattern => \f(CW$replace\fP, ...))" .IX Subsection "repeat_formula($row, $col, $formula, $format, ($pattern => $replace, ...))" The \f(CW\*(C`repeat_formula()\*(C'\fR method is used in conjunction with \f(CW\*(C`store_formula()\*(C'\fR to speed up the generation of repeated formulas. See \*(L"Improving performance when working with formulas\*(R" in \*(L"\s-1FORMULAS AND FUNCTIONS IN EXCEL\*(R"\s0. .PP In many respects \f(CW\*(C`repeat_formula()\*(C'\fR behaves like \f(CW\*(C`write_formula()\*(C'\fR except that it is significantly faster. .PP The \f(CW\*(C`repeat_formula()\*(C'\fR method creates a new formula based on the pre-parsed tokens returned by \f(CW\*(C`store_formula()\*(C'\fR. The new formula is generated by substituting \f(CW$pattern\fR, \f(CW$replace\fR pairs in the stored formula: .PP .Vb 1 \& my $formula = $worksheet\->store_formula(\*(Aq=A1 * 3 + 50\*(Aq); \& \& for my $row (0..99) { \& $worksheet\->repeat_formula($row, 1, $formula, $format, \*(AqA1\*(Aq, \*(AqA\*(Aq.($row +1)); \& } .Ve .PP It should be noted that \f(CW\*(C`repeat_formula()\*(C'\fR doesn't modify the tokens. In the above example the substitution is always made against the original token, \f(CW\*(C`A1\*(C'\fR, which doesn't change. .PP As usual, you can use \f(CW\*(C`undef\*(C'\fR if you don't wish to specify a \f(CW$format\fR: .PP .Vb 2 \& $worksheet\->repeat_formula(\*(AqB2\*(Aq, $formula, $format, \*(AqA1\*(Aq, \*(AqA2\*(Aq); \& $worksheet\->repeat_formula(\*(AqB3\*(Aq, $formula, undef, \*(AqA1\*(Aq, \*(AqA3\*(Aq); .Ve .PP The substitutions are made from left to right and you can use as many \f(CW$pattern\fR, \f(CW$replace\fR pairs as you need. However, each substitution is made only once: .PP .Vb 1 \& my $formula = $worksheet\->store_formula(\*(Aq=A1 + A1\*(Aq); \& \& # Gives \*(Aq=B1 + A1\*(Aq \& $worksheet\->repeat_formula(\*(AqB1\*(Aq, $formula, undef, \*(AqA1\*(Aq, \*(AqB1\*(Aq); \& \& # Gives \*(Aq=B1 + B1\*(Aq \& $worksheet\->repeat_formula(\*(AqB2\*(Aq, $formula, undef, (\*(AqA1\*(Aq, \*(AqB1\*(Aq) x 2); .Ve .PP Since the \f(CW$pattern\fR is interpolated each time that it is used it is worth using the \f(CW\*(C`qr\*(C'\fR operator to quote the pattern. The \f(CW\*(C`qr\*(C'\fR operator is explained in the \f(CW\*(C`perlop\*(C'\fR man page. .PP .Vb 1 \& $worksheet\->repeat_formula(\*(AqB1\*(Aq, $formula, $format, qr/A1/, \*(AqA2\*(Aq); .Ve .PP Care should be taken with the values that are substituted. The formula returned by \f(CW\*(C`repeat_formula()\*(C'\fR contains several other tokens in addition to those in the formula and these might also match the pattern that you are trying to replace. In particular you should avoid substituting a single 0, 1, 2 or 3. .PP You should also be careful to avoid false matches. For example the following snippet is meant to change the stored formula in steps from \f(CW\*(C`=A1 + SIN(A1)\*(C'\fR to \f(CW\*(C`=A10 + SIN(A10)\*(C'\fR. .PP .Vb 1 \& my $formula = $worksheet\->store_formula(\*(Aq=A1 + SIN(A1)\*(Aq); \& \& for my $row (1 .. 10) { \& $worksheet\->repeat_formula($row \-1, 1, $formula, undef, \& qw/A1/, \*(AqA\*(Aq . $row, #! Bad. \& qw/A1/, \*(AqA\*(Aq . $row #! Bad. \& ); \& } .Ve .PP However it contains a bug. In the last iteration of the loop when \f(CW$row\fR is 10 the following substitutions will occur: .PP .Vb 2 \& s/A1/A10/; changes =A1 + SIN(A1) to =A10 + SIN(A1) \& s/A1/A10/; changes =A10 + SIN(A1) to =A100 + SIN(A1) # !! .Ve .PP The solution in this case is to use a more explicit match such as \f(CW\*(C`qw/^A1$/\*(C'\fR: .PP .Vb 4 \& $worksheet\->repeat_formula($row \-1, 1, $formula, undef, \& qw/^A1$/, \*(AqA\*(Aq . $row, \& qw/^A1$/, \*(AqA\*(Aq . $row \& ); .Ve .PP Another similar problem occurs due to the fact that substitutions are made in order. For example the following snippet is meant to change the stored formula from \f(CW\*(C`=A10 + A11\*(C'\fR to \f(CW\*(C`=A11 + A12\*(C'\fR: .PP .Vb 1 \& my $formula = $worksheet\->store_formula(\*(Aq=A10 + A11\*(Aq); \& \& $worksheet\->repeat_formula(\*(AqA1\*(Aq, $formula, undef, \& qw/A10/, \*(AqA11\*(Aq, #! Bad. \& qw/A11/, \*(AqA12\*(Aq #! Bad. \& ); .Ve .PP However, the actual substitution yields \f(CW\*(C`=A12 + A11\*(C'\fR: .PP .Vb 2 \& s/A10/A11/; changes =A10 + A11 to =A11 + A11 \& s/A11/A12/; changes =A11 + A11 to =A12 + A11 # !! .Ve .PP The solution here would be to reverse the order of the substitutions or to start with a stored formula that won't yield a false match such as \f(CW\*(C`=X10 + Y11\*(C'\fR: .PP .Vb 1 \& my $formula = $worksheet\->store_formula(\*(Aq=X10 + Y11\*(Aq); \& \& $worksheet\->repeat_formula(\*(AqA1\*(Aq, $formula, undef, \& qw/X10/, \*(AqA11\*(Aq, \& qw/Y11/, \*(AqA12\*(Aq \& ); .Ve .PP If you think that you have a problem related to a false match you can check the tokens that you are substituting against as follows. .PP .Vb 2 \& my $formula = $worksheet\->store_formula(\*(Aq=A1*5+4\*(Aq); \& print "@$formula\en"; .Ve .PP See also the \f(CW\*(C`repeat.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .ie n .SS "write_comment($row, $column, $string, ...)" .el .SS "write_comment($row, \f(CW$column\fP, \f(CW$string\fP, ...)" .IX Subsection "write_comment($row, $column, $string, ...)" The \f(CW\*(C`write_comment()\*(C'\fR method is used to add a comment to a cell. A cell comment is indicated in Excel by a small red triangle in the upper right-hand corner of the cell. Moving the cursor over the red triangle will reveal the comment. .PP The following example shows how to add a comment to a cell: .PP .Vb 2 \& $worksheet\->write (2, 2, \*(AqHello\*(Aq); \& $worksheet\->write_comment(2, 2, \*(AqThis is a comment.\*(Aq); .Ve .PP As usual you can replace the \f(CW$row\fR and \f(CW$column\fR parameters with an \f(CW\*(C`A1\*(C'\fR cell reference. See the note about \*(L"Cell notation\*(R". .PP .Vb 2 \& $worksheet\->write (\*(AqC3\*(Aq, \*(AqHello\*(Aq); \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqThis is a comment.\*(Aq); .Ve .PP On systems with \f(CW\*(C`perl 5.8\*(C'\fR and later the \f(CW\*(C`write_comment()\*(C'\fR method will also handle strings in \f(CW\*(C`UTF\-8\*(C'\fR format. .PP .Vb 2 \& $worksheet\->write_comment(\*(AqC3\*(Aq, "\ex{263a}"); # Smiley \& $worksheet\->write_comment(\*(AqC4\*(Aq, \*(AqComment ca va?\*(Aq); .Ve .PP In addition to the basic 3 argument form of \f(CW\*(C`write_comment()\*(C'\fR you can pass in several optional key/value pairs to control the format of the comment. For example: .PP .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, visible => 1, author => \*(AqPerl\*(Aq); .Ve .PP Most of these options are quite specific and in general the default comment behaviour will be all that you need. However, should you need greater control over the format of the cell comment the following options are available: .PP .Vb 10 \& encoding \& author \& author_encoding \& visible \& x_scale \& width \& y_scale \& height \& color \& start_cell \& start_row \& start_col \& x_offset \& y_offset .Ve .IP "Option: encoding" 4 .IX Item "Option: encoding" This option is used to indicate that the comment string is encoded as \f(CW\*(C`UTF\-16BE\*(C'\fR. .Sp .Vb 1 \& my $comment = pack \*(Aqn\*(Aq, 0x263a; # UTF\-16BE Smiley symbol \& \& $worksheet\->write_comment(\*(AqC3\*(Aq, $comment, encoding => 1); .Ve .Sp If you wish to use Unicode characters in the comment string then the preferred method is to use perl 5.8 and \f(CW\*(C`UTF\-8\*(C'\fR strings, see \*(L"\s-1UNICODE IN EXCEL\*(R"\s0. .IP "Option: author" 4 .IX Item "Option: author" This option is used to indicate who the author of the comment is. Excel displays the author of the comment in the status bar at the bottom of the worksheet. This is usually of interest in corporate environments where several people might review and provide comments to a workbook. .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqAtonement\*(Aq, author => \*(AqIan McEwan\*(Aq); .Ve .IP "Option: author_encoding" 4 .IX Item "Option: author_encoding" This option is used to indicate that the author string is encoded as \f(CW\*(C`UTF\-16BE\*(C'\fR. .IP "Option: visible" 4 .IX Item "Option: visible" This option is used to make a cell comment visible when the worksheet is opened. The default behaviour in Excel is that comments are initially hidden. However, it is also possible in Excel to make individual or all comments visible. In Spreadsheet::WriteExcel individual comments can be made visible as follows: .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, visible => 1); .Ve .Sp It is possible to make all comments in a worksheet visible using the \f(CW\*(C`show_comments()\*(C'\fR worksheet method (see below). Alternatively, if all of the cell comments have been made visible you can hide individual comments: .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, visible => 0); .Ve .IP "Option: x_scale" 4 .IX Item "Option: x_scale" This option is used to set the width of the cell comment box as a factor of the default width. .Sp .Vb 2 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, x_scale => 2); \& $worksheet\->write_comment(\*(AqC4\*(Aq, \*(AqHello\*(Aq, x_scale => 4.2); .Ve .IP "Option: width" 4 .IX Item "Option: width" This option is used to set the width of the cell comment box explicitly in pixels. .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, width => 200); .Ve .IP "Option: y_scale" 4 .IX Item "Option: y_scale" This option is used to set the height of the cell comment box as a factor of the default height. .Sp .Vb 2 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, y_scale => 2); \& $worksheet\->write_comment(\*(AqC4\*(Aq, \*(AqHello\*(Aq, y_scale => 4.2); .Ve .IP "Option: height" 4 .IX Item "Option: height" This option is used to set the height of the cell comment box explicitly in pixels. .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, height => 200); .Ve .IP "Option: color" 4 .IX Item "Option: color" This option is used to set the background colour of cell comment box. You can use one of the named colours recognised by Spreadsheet::WriteExcel or a colour index. See \*(L"\s-1COLOURS IN EXCEL\*(R"\s0. .Sp .Vb 2 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, color => \*(Aqgreen\*(Aq); \& $worksheet\->write_comment(\*(AqC4\*(Aq, \*(AqHello\*(Aq, color => 0x35); # Orange .Ve .IP "Option: start_cell" 4 .IX Item "Option: start_cell" This option is used to set the cell in which the comment will appear. By default Excel displays comments one cell to the right and one cell above the cell to which the comment relates. However, you can change this behaviour if you wish. In the following example the comment which would appear by default in cell \f(CW\*(C`D2\*(C'\fR is moved to \f(CW\*(C`E2\*(C'\fR. .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, start_cell => \*(AqE2\*(Aq); .Ve .IP "Option: start_row" 4 .IX Item "Option: start_row" This option is used to set the row in which the comment will appear. See the \f(CW\*(C`start_cell\*(C'\fR option above. The row is zero indexed. .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, start_row => 0); .Ve .IP "Option: start_col" 4 .IX Item "Option: start_col" This option is used to set the column in which the comment will appear. See the \f(CW\*(C`start_cell\*(C'\fR option above. The column is zero indexed. .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, start_col => 4); .Ve .IP "Option: x_offset" 4 .IX Item "Option: x_offset" This option is used to change the x offset, in pixels, of a comment within a cell: .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, $comment, x_offset => 30); .Ve .IP "Option: y_offset" 4 .IX Item "Option: y_offset" This option is used to change the y offset, in pixels, of a comment within a cell: .Sp .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, $comment, x_offset => 30); .Ve .PP You can apply as many of these options as you require. .PP See also \*(L"\s-1ROW HEIGHTS AND WORKSHEET OBJECTS\*(R"\s0. .SS "\fIshow_comments()\fP" .IX Subsection "show_comments()" This method is used to make all cell comments visible when a worksheet is opened. .PP Individual comments can be made visible using the \f(CW\*(C`visible\*(C'\fR parameter of the \f(CW\*(C`write_comment\*(C'\fR method (see above): .PP .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, visible => 1); .Ve .PP If all of the cell comments have been made visible you can hide individual comments as follows: .PP .Vb 1 \& $worksheet\->write_comment(\*(AqC3\*(Aq, \*(AqHello\*(Aq, visible => 0); .Ve .ie n .SS "add_write_handler($re, $code_ref)" .el .SS "add_write_handler($re, \f(CW$code_ref\fP)" .IX Subsection "add_write_handler($re, $code_ref)" This method is used to extend the Spreadsheet::WriteExcel \fIwrite()\fR method to handle user defined data. .PP If you refer to the section on \f(CW\*(C`write()\*(C'\fR above you will see that it acts as an alias for several more specific \f(CW\*(C`write_*\*(C'\fR methods. However, it doesn't always act in exactly the way that you would like it to. .PP One solution is to filter the input data yourself and call the appropriate \f(CW\*(C`write_*\*(C'\fR method. Another approach is to use the \f(CW\*(C`add_write_handler()\*(C'\fR method to add your own automated behaviour to \f(CW\*(C`write()\*(C'\fR. .PP The \f(CW\*(C`add_write_handler()\*(C'\fR method take two arguments, \f(CW$re\fR, a regular expression to match incoming data and \f(CW$code_ref\fR a callback function to handle the matched data: .PP .Vb 1 \& $worksheet\->add_write_handler(qr/^\ed\ed\ed\ed$/, \e&my_write); .Ve .PP (In the these examples the \f(CW\*(C`qr\*(C'\fR operator is used to quote the regular expression strings, see perlop for more details). .PP The method is used as follows. say you wished to write 7 digit \s-1ID\s0 numbers as a string so that any leading zeros were preserved*, you could do something like the following: .PP .Vb 1 \& $worksheet\->add_write_handler(qr/^\ed{7}$/, \e&write_my_id); \& \& \& sub write_my_id { \& my $worksheet = shift; \& return $worksheet\->write_string(@_); \& } .Ve .PP * You could also use the \f(CW\*(C`keep_leading_zeros()\*(C'\fR method for this. .PP Then if you call \f(CW\*(C`write()\*(C'\fR with an appropriate string it will be handled automatically: .PP .Vb 2 \& # Writes 0000000. It would normally be written as a number; 0. \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq0000000\*(Aq); .Ve .PP The callback function will receive a reference to the calling worksheet and all of the other arguments that were passed to \f(CW\*(C`write()\*(C'\fR. The callback will see an \f(CW@_\fR argument list that looks like the following: .PP .Vb 7 \& $_[0] A ref to the calling worksheet. * \& $_[1] Zero based row number. \& $_[2] Zero based column number. \& $_[3] A number or string or token. \& $_[4] A format ref if any. \& $_[5] Any other arguments. \& ... \& \& * It is good style to shift this off the list so the @_ is the same \& as the argument list seen by write(). .Ve .PP Your callback should \f(CW\*(C`return()\*(C'\fR the return value of the \f(CW\*(C`write_*\*(C'\fR method that was called or \f(CW\*(C`undef\*(C'\fR to indicate that you rejected the match and want \f(CW\*(C`write()\*(C'\fR to continue as normal. .PP So for example if you wished to apply the previous filter only to \s-1ID\s0 values that occur in the first column you could modify your callback function as follows: .PP .Vb 3 \& sub write_my_id { \& my $worksheet = shift; \& my $col = $_[1]; \& \& if ($col == 0) { \& return $worksheet\->write_string(@_); \& } \& else { \& # Reject the match and return control to write() \& return undef; \& } \& } .Ve .PP Now, you will get different behaviour for the first column and other columns: .PP .Vb 2 \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq0000000\*(Aq); # Writes 0000000 \& $worksheet\->write(\*(AqB1\*(Aq, \*(Aq0000000\*(Aq); # Writes 0 .Ve .PP You may add more than one handler in which case they will be called in the order that they were added. .PP Note, the \f(CW\*(C`add_write_handler()\*(C'\fR method is particularly suited for handling dates. .PP See the \f(CW\*(C`write_handler 1\-4\*(C'\fR programs in the \f(CW\*(C`examples\*(C'\fR directory for further examples. .ie n .SS "insert_image($row, $col, $filename, $x, $y, $scale_x, $scale_y)" .el .SS "insert_image($row, \f(CW$col\fP, \f(CW$filename\fP, \f(CW$x\fP, \f(CW$y\fP, \f(CW$scale_x\fP, \f(CW$scale_y\fP)" .IX Subsection "insert_image($row, $col, $filename, $x, $y, $scale_x, $scale_y)" This method can be used to insert a image into a worksheet. The image can be in \s-1PNG, JPEG\s0 or \s-1BMP\s0 format. The \f(CW$x\fR, \f(CW$y\fR, \f(CW$scale_x\fR and \f(CW$scale_y\fR parameters are optional. .PP .Vb 3 \& $worksheet1\->insert_image(\*(AqA1\*(Aq, \*(Aqperl.bmp\*(Aq); \& $worksheet2\->insert_image(\*(AqA1\*(Aq, \*(Aq../images/perl.bmp\*(Aq); \& $worksheet3\->insert_image(\*(AqA1\*(Aq, \*(Aq.c:\eimages\eperl.bmp\*(Aq); .Ve .PP The parameters \f(CW$x\fR and \f(CW$y\fR can be used to specify an offset from the top left hand corner of the cell specified by \f(CW$row\fR and \f(CW$col\fR. The offset values are in pixels. .PP .Vb 1 \& $worksheet1\->insert_image(\*(AqA1\*(Aq, \*(Aqperl.bmp\*(Aq, 32, 10); .Ve .PP The default width of a cell is 63 pixels. The default height of a cell is 17 pixels. The pixels offsets can be calculated using the following relationships: .PP .Vb 3 \& Wp = int(12We) if We < 1 \& Wp = int(7We +5) if We >= 1 \& Hp = int(4/3He) \& \& where: \& We is the cell width in Excels units \& Wp is width in pixels \& He is the cell height in Excels units \& Hp is height in pixels .Ve .PP The offsets can be greater than the width or height of the underlying cell. This can be occasionally useful if you wish to align two or more images relative to the same cell. .PP The parameters \f(CW$scale_x\fR and \f(CW$scale_y\fR can be used to scale the inserted image horizontally and vertically: .PP .Vb 2 \& # Scale the inserted image: width x 2.0, height x 0.8 \& $worksheet\->insert_image(\*(AqA1\*(Aq, \*(Aqperl.bmp\*(Aq, 0, 0, 2, 0.8); .Ve .PP See also the \f(CW\*(C`images.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .PP \&\s-1BMP\s0 images must be 24 bit, true colour, bitmaps. In general it is best to avoid \s-1BMP\s0 images since they aren't compressed. The older \f(CW\*(C`insert_bitmap()\*(C'\fR method is still supported but deprecated. .PP See also \*(L"\s-1ROW HEIGHTS AND WORKSHEET OBJECTS\*(R"\s0. .ie n .SS "insert_chart($row, $col, $chart, $x, $y, $scale_x, $scale_y)" .el .SS "insert_chart($row, \f(CW$col\fP, \f(CW$chart\fP, \f(CW$x\fP, \f(CW$y\fP, \f(CW$scale_x\fP, \f(CW$scale_y\fP)" .IX Subsection "insert_chart($row, $col, $chart, $x, $y, $scale_x, $scale_y)" This method can be used to insert a Chart object into a worksheet. The Chart must be created by the \f(CW\*(C`add_chart()\*(C'\fR Workbook method and it must have the \f(CW\*(C`embedded\*(C'\fR option set. .PP .Vb 1 \& my $chart = $workbook\->add_chart( type => \*(Aqline\*(Aq, embedded => 1 ); \& \& # Configure the chart. \& ... \& \& # Insert the chart into the a worksheet. \& $worksheet\->insert_chart(\*(AqE2\*(Aq, $chart); .Ve .PP See \f(CW\*(C`add_chart()\*(C'\fR for details on how to create the Chart object and Spreadsheet::WriteExcel::Chart for details on how to configure it. See also the \f(CW\*(C`chart_*.pl\*(C'\fR programs in the examples directory of the distro. .PP The \f(CW$x\fR, \f(CW$y\fR, \f(CW$scale_x\fR and \f(CW$scale_y\fR parameters are optional. .PP The parameters \f(CW$x\fR and \f(CW$y\fR can be used to specify an offset from the top left hand corner of the cell specified by \f(CW$row\fR and \f(CW$col\fR. The offset values are in pixels. See the \f(CW\*(C`insert_image\*(C'\fR method above for more information on sizes. .PP .Vb 1 \& $worksheet1\->insert_chart(\*(AqE2\*(Aq, $chart, 3, 3); .Ve .PP The parameters \f(CW$scale_x\fR and \f(CW$scale_y\fR can be used to scale the inserted image horizontally and vertically: .PP .Vb 2 \& # Scale the width by 120% and the height by 150% \& $worksheet\->insert_chart(\*(AqE2\*(Aq, $chart, 0, 0, 1.2, 1.5); .Ve .PP The easiest way to calculate the required scaling is to create a test chart worksheet with Spreadsheet::WriteExcel. Then open the file, select the chart and drag the corner to get the required size. While holding down the mouse the scale of the resized chart is shown to the left of the formula bar. .PP See also \*(L"\s-1ROW HEIGHTS AND WORKSHEET OBJECTS\*(R"\s0. .ie n .SS "embed_chart($row, $col, $filename, $x, $y, $scale_x, $scale_y)" .el .SS "embed_chart($row, \f(CW$col\fP, \f(CW$filename\fP, \f(CW$x\fP, \f(CW$y\fP, \f(CW$scale_x\fP, \f(CW$scale_y\fP)" .IX Subsection "embed_chart($row, $col, $filename, $x, $y, $scale_x, $scale_y)" This method can be used to insert a externally generated chart into a worksheet. The chart must first be extracted from an existing Excel file. This feature is semi-deprecated in favour of the \*(L"native\*(R" charts created using \f(CW\*(C`add_chart()\*(C'\fR. Read \f(CW\*(C`external_charts.txt\*(C'\fR (or \f(CW\*(C`.pod\*(C'\fR) in the external_charts directory of the distro for a full explanation. .PP Here is an example: .PP .Vb 1 \& $worksheet\->embed_chart(\*(AqB2\*(Aq, \*(Aqsales_chart.bin\*(Aq); .Ve .PP The \f(CW$x\fR, \f(CW$y\fR, \f(CW$scale_x\fR and \f(CW$scale_y\fR parameters are optional. See \f(CW\*(C`insert_chart()\*(C'\fR above for details. .SS "\fIdata_validation()\fP" .IX Subsection "data_validation()" The \f(CW\*(C`data_validation()\*(C'\fR method is used to construct an Excel data validation or to limit the user input to a dropdown list of values. .PP .Vb 6 \& $worksheet\->data_validation(\*(AqB3\*(Aq, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 100, \& }); \& \& $worksheet\->data_validation(\*(AqB5:B9\*(Aq, \& { \& validate => \*(Aqlist\*(Aq, \& value => [\*(Aqopen\*(Aq, \*(Aqhigh\*(Aq, \*(Aqclose\*(Aq], \& }); .Ve .PP This method contains a lot of parameters and is described in detail in a separate section \*(L"\s-1DATA VALIDATION IN EXCEL\*(R"\s0. .PP See also the \f(CW\*(C`data_validate.pl\*(C'\fR program in the examples directory of the distro .SS "\fIget_name()\fP" .IX Subsection "get_name()" The \f(CW\*(C`get_name()\*(C'\fR method is used to retrieve the name of a worksheet. For example: .PP .Vb 3 \& foreach my $sheet ($workbook\->sheets()) { \& print $sheet\->get_name(); \& } .Ve .PP For reasons related to the design of Spreadsheet::WriteExcel and to the internals of Excel there is no \f(CW\*(C`set_name()\*(C'\fR method. The only way to set the worksheet name is via the \f(CW\*(C`add_worksheet()\*(C'\fR method. .SS "\fIactivate()\fP" .IX Subsection "activate()" The \f(CW\*(C`activate()\*(C'\fR method is used to specify which worksheet is initially visible in a multi-sheet workbook: .PP .Vb 3 \& $worksheet1 = $workbook\->add_worksheet(\*(AqTo\*(Aq); \& $worksheet2 = $workbook\->add_worksheet(\*(Aqthe\*(Aq); \& $worksheet3 = $workbook\->add_worksheet(\*(Aqwind\*(Aq); \& \& $worksheet3\->activate(); .Ve .PP This is similar to the Excel \s-1VBA\s0 activate method. More than one worksheet can be selected via the \f(CW\*(C`select()\*(C'\fR method, see below, however only one worksheet can be active. .PP The default active worksheet is the first worksheet. .SS "\fIselect()\fP" .IX Subsection "select()" The \f(CW\*(C`select()\*(C'\fR method is used to indicate that a worksheet is selected in a multi-sheet workbook: .PP .Vb 3 \& $worksheet1\->activate(); \& $worksheet2\->select(); \& $worksheet3\->select(); .Ve .PP A selected worksheet has its tab highlighted. Selecting worksheets is a way of grouping them together so that, for example, several worksheets could be printed in one go. A worksheet that has been activated via the \f(CW\*(C`activate()\*(C'\fR method will also appear as selected. .SS "\fIhide()\fP" .IX Subsection "hide()" The \f(CW\*(C`hide()\*(C'\fR method is used to hide a worksheet: .PP .Vb 1 \& $worksheet2\->hide(); .Ve .PP You may wish to hide a worksheet in order to avoid confusing a user with intermediate data or calculations. .PP A hidden worksheet can not be activated or selected so this method is mutually exclusive with the \f(CW\*(C`activate()\*(C'\fR and \f(CW\*(C`select()\*(C'\fR methods. In addition, since the first worksheet will default to being the active worksheet, you cannot hide the first worksheet without activating another sheet: .PP .Vb 2 \& $worksheet2\->activate(); \& $worksheet1\->hide(); .Ve .SS "\fIset_first_sheet()\fP" .IX Subsection "set_first_sheet()" The \f(CW\*(C`activate()\*(C'\fR method determines which worksheet is initially selected. However, if there are a large number of worksheets the selected worksheet may not appear on the screen. To avoid this you can select which is the leftmost visible worksheet using \f(CW\*(C`set_first_sheet()\*(C'\fR: .PP .Vb 3 \& for (1..20) { \& $workbook\->add_worksheet; \& } \& \& $worksheet21 = $workbook\->add_worksheet(); \& $worksheet22 = $workbook\->add_worksheet(); \& \& $worksheet21\->set_first_sheet(); \& $worksheet22\->activate(); .Ve .PP This method is not required very often. The default value is the first worksheet. .SS "protect($password)" .IX Subsection "protect($password)" The \f(CW\*(C`protect()\*(C'\fR method is used to protect a worksheet from modification: .PP .Vb 1 \& $worksheet\->protect(); .Ve .PP It can be turned off in Excel via the \f(CW\*(C`Tools\->Protection\->Unprotect Sheet\*(C'\fR menu command. .PP The \f(CW\*(C`protect()\*(C'\fR method also has the effect of enabling a cell's \f(CW\*(C`locked\*(C'\fR and \f(CW\*(C`hidden\*(C'\fR properties if they have been set. A \*(L"locked\*(R" cell cannot be edited. A \*(L"hidden\*(R" cell will display the results of a formula but not the formula itself. In Excel a cell's locked property is on by default. .PP .Vb 3 \& # Set some format properties \& my $unlocked = $workbook\->add_format(locked => 0); \& my $hidden = $workbook\->add_format(hidden => 1); \& \& # Enable worksheet protection \& $worksheet\->protect(); \& \& # This cell cannot be edited, it is locked by default \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq=1+2\*(Aq); \& \& # This cell can be edited \& $worksheet\->write(\*(AqA2\*(Aq, \*(Aq=1+2\*(Aq, $unlocked); \& \& # The formula in this cell isn\*(Aqt visible \& $worksheet\->write(\*(AqA3\*(Aq, \*(Aq=1+2\*(Aq, $hidden); .Ve .PP See also the \f(CW\*(C`set_locked\*(C'\fR and \f(CW\*(C`set_hidden\*(C'\fR format methods in \*(L"\s-1CELL FORMATTING\*(R"\s0. .PP You can optionally add a password to the worksheet protection: .PP .Vb 1 \& $worksheet\->protect(\*(Aqdrowssap\*(Aq); .Ve .PP Note, the worksheet level password in Excel provides very weak protection. It does not encrypt your data in any way and it is very easy to deactivate. Therefore, do not use the above method if you wish to protect sensitive data or calculations. However, before you get worried, Excel's own workbook level password protection does provide strong encryption in Excel 97+. For technical reasons this will never be supported by \f(CW\*(C`Spreadsheet::WriteExcel\*(C'\fR. .ie n .SS "set_selection($first_row, $first_col, $last_row, $last_col)" .el .SS "set_selection($first_row, \f(CW$first_col\fP, \f(CW$last_row\fP, \f(CW$last_col\fP)" .IX Subsection "set_selection($first_row, $first_col, $last_row, $last_col)" This method can be used to specify which cell or cells are selected in a worksheet. The most common requirement is to select a single cell, in which case \f(CW$last_row\fR and \f(CW$last_col\fR can be omitted. The active cell within a selected range is determined by the order in which \f(CW$first\fR and \f(CW$last\fR are specified. It is also possible to specify a cell or a range using A1 notation. See the note about \*(L"Cell notation\*(R". .PP Examples: .PP .Vb 6 \& $worksheet1\->set_selection(3, 3); # 1. Cell D4. \& $worksheet2\->set_selection(3, 3, 6, 6); # 2. Cells D4 to G7. \& $worksheet3\->set_selection(6, 6, 3, 3); # 3. Cells G7 to D4. \& $worksheet4\->set_selection(\*(AqD4\*(Aq); # Same as 1. \& $worksheet5\->set_selection(\*(AqD4:G7\*(Aq); # Same as 2. \& $worksheet6\->set_selection(\*(AqG7:D4\*(Aq); # Same as 3. .Ve .PP The default cell selections is (0, 0), 'A1'. .ie n .SS "set_row($row, $height, $format, $hidden, $level, $collapsed)" .el .SS "set_row($row, \f(CW$height\fP, \f(CW$format\fP, \f(CW$hidden\fP, \f(CW$level\fP, \f(CW$collapsed\fP)" .IX Subsection "set_row($row, $height, $format, $hidden, $level, $collapsed)" This method can be used to change the default properties of a row. All parameters apart from \f(CW$row\fR are optional. .PP The most common use for this method is to change the height of a row: .PP .Vb 1 \& $worksheet\->set_row(0, 20); # Row 1 height set to 20 .Ve .PP If you wish to set the format without changing the height you can pass \f(CW\*(C`undef\*(C'\fR as the height parameter: .PP .Vb 1 \& $worksheet\->set_row(0, undef, $format); .Ve .PP The \f(CW$format\fR parameter will be applied to any cells in the row that don't have a format. For example .PP .Vb 3 \& $worksheet\->set_row(0, undef, $format1); # Set the format for row 1 \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqHello\*(Aq); # Defaults to $format1 \& $worksheet\->write(\*(AqB1\*(Aq, \*(AqHello\*(Aq, $format2); # Keeps $format2 .Ve .PP If you wish to define a row format in this way you should call the method before any calls to \f(CW\*(C`write()\*(C'\fR. Calling it afterwards will overwrite any format that was previously specified. .PP The \f(CW$hidden\fR parameter should be set to 1 if you wish to hide a row. This can be used, for example, to hide intermediary steps in a complicated calculation: .PP .Vb 2 \& $worksheet\->set_row(0, 20, $format, 1); \& $worksheet\->set_row(1, undef, undef, 1); .Ve .PP The \f(CW$level\fR parameter is used to set the outline level of the row. Outlines are described in \*(L"\s-1OUTLINES AND GROUPING IN EXCEL\*(R"\s0. Adjacent rows with the same outline level are grouped together into a single outline. .PP The following example sets an outline level of 1 for rows 1 and 2 (zero-indexed): .PP .Vb 2 \& $worksheet\->set_row(1, undef, undef, 0, 1); \& $worksheet\->set_row(2, undef, undef, 0, 1); .Ve .PP The \f(CW$hidden\fR parameter can also be used to hide collapsed outlined rows when used in conjunction with the \f(CW$level\fR parameter. .PP .Vb 2 \& $worksheet\->set_row(1, undef, undef, 1, 1); \& $worksheet\->set_row(2, undef, undef, 1, 1); .Ve .PP For collapsed outlines you should also indicate which row has the collapsed \f(CW\*(C`+\*(C'\fR symbol using the optional \f(CW$collapsed\fR parameter. .PP .Vb 1 \& $worksheet\->set_row(3, undef, undef, 0, 0, 1); .Ve .PP For a more complete example see the \f(CW\*(C`outline.pl\*(C'\fR and \f(CW\*(C`outline_collapsed.pl\*(C'\fR programs in the examples directory of the distro. .PP Excel allows up to 7 outline levels. Therefore the \f(CW$level\fR parameter should be in the range \f(CW\*(C`0 <= $level <= 7\*(C'\fR. .ie n .SS "set_column($first_col, $last_col, $width, $format, $hidden, $level, $collapsed)" .el .SS "set_column($first_col, \f(CW$last_col\fP, \f(CW$width\fP, \f(CW$format\fP, \f(CW$hidden\fP, \f(CW$level\fP, \f(CW$collapsed\fP)" .IX Subsection "set_column($first_col, $last_col, $width, $format, $hidden, $level, $collapsed)" This method can be used to change the default properties of a single column or a range of columns. All parameters apart from \f(CW$first_col\fR and \f(CW$last_col\fR are optional. .PP If \f(CW\*(C`set_column()\*(C'\fR is applied to a single column the value of \f(CW$first_col\fR and \f(CW$last_col\fR should be the same. In the case where \f(CW$last_col\fR is zero it is set to the same value as \f(CW$first_col\fR. .PP It is also possible, and generally clearer, to specify a column range using the form of A1 notation used for columns. See the note about \*(L"Cell notation\*(R". .PP Examples: .PP .Vb 4 \& $worksheet\->set_column(0, 0, 20); # Column A width set to 20 \& $worksheet\->set_column(1, 3, 30); # Columns B\-D width set to 30 \& $worksheet\->set_column(\*(AqE:E\*(Aq, 20); # Column E width set to 20 \& $worksheet\->set_column(\*(AqF:H\*(Aq, 30); # Columns F\-H width set to 30 .Ve .PP The width corresponds to the column width value that is specified in Excel. It is approximately equal to the length of a string in the default font of Arial 10. Unfortunately, there is no way to specify \*(L"AutoFit\*(R" for a column in the Excel file format. This feature is only available at runtime from within Excel. .PP As usual the \f(CW$format\fR parameter is optional, for additional information, see \*(L"\s-1CELL FORMATTING\*(R"\s0. If you wish to set the format without changing the width you can pass \f(CW\*(C`undef\*(C'\fR as the width parameter: .PP .Vb 1 \& $worksheet\->set_column(0, 0, undef, $format); .Ve .PP The \f(CW$format\fR parameter will be applied to any cells in the column that don't have a format. For example .PP .Vb 3 \& $worksheet\->set_column(\*(AqA:A\*(Aq, undef, $format1); # Set format for col 1 \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqHello\*(Aq); # Defaults to $format1 \& $worksheet\->write(\*(AqA2\*(Aq, \*(AqHello\*(Aq, $format2); # Keeps $format2 .Ve .PP If you wish to define a column format in this way you should call the method before any calls to \f(CW\*(C`write()\*(C'\fR. If you call it afterwards it won't have any effect. .PP A default row format takes precedence over a default column format .PP .Vb 4 \& $worksheet\->set_row(0, undef, $format1); # Set format for row 1 \& $worksheet\->set_column(\*(AqA:A\*(Aq, undef, $format2); # Set format for col 1 \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqHello\*(Aq); # Defaults to $format1 \& $worksheet\->write(\*(AqA2\*(Aq, \*(AqHello\*(Aq); # Defaults to $format2 .Ve .PP The \f(CW$hidden\fR parameter should be set to 1 if you wish to hide a column. This can be used, for example, to hide intermediary steps in a complicated calculation: .PP .Vb 2 \& $worksheet\->set_column(\*(AqD:D\*(Aq, 20, $format, 1); \& $worksheet\->set_column(\*(AqE:E\*(Aq, undef, undef, 1); .Ve .PP The \f(CW$level\fR parameter is used to set the outline level of the column. Outlines are described in \*(L"\s-1OUTLINES AND GROUPING IN EXCEL\*(R"\s0. Adjacent columns with the same outline level are grouped together into a single outline. .PP The following example sets an outline level of 1 for columns B to G: .PP .Vb 1 \& $worksheet\->set_column(\*(AqB:G\*(Aq, undef, undef, 0, 1); .Ve .PP The \f(CW$hidden\fR parameter can also be used to hide collapsed outlined columns when used in conjunction with the \f(CW$level\fR parameter. .PP .Vb 1 \& $worksheet\->set_column(\*(AqB:G\*(Aq, undef, undef, 1, 1); .Ve .PP For collapsed outlines you should also indicate which row has the collapsed \f(CW\*(C`+\*(C'\fR symbol using the optional \f(CW$collapsed\fR parameter. .PP .Vb 1 \& $worksheet\->set_column(\*(AqH:H\*(Aq, undef, undef, 0, 0, 1); .Ve .PP For a more complete example see the \f(CW\*(C`outline.pl\*(C'\fR and \f(CW\*(C`outline_collapsed.pl\*(C'\fR programs in the examples directory of the distro. .PP Excel allows up to 7 outline levels. Therefore the \f(CW$level\fR parameter should be in the range \f(CW\*(C`0 <= $level <= 7\*(C'\fR. .ie n .SS "outline_settings($visible, $symbols_below, $symbols_right, $auto_style)" .el .SS "outline_settings($visible, \f(CW$symbols_below\fP, \f(CW$symbols_right\fP, \f(CW$auto_style\fP)" .IX Subsection "outline_settings($visible, $symbols_below, $symbols_right, $auto_style)" The \f(CW\*(C`outline_settings()\*(C'\fR method is used to control the appearance of outlines in Excel. Outlines are described in \*(L"\s-1OUTLINES AND GROUPING IN EXCEL\*(R"\s0. .PP The \f(CW$visible\fR parameter is used to control whether or not outlines are visible. Setting this parameter to 0 will cause all outlines on the worksheet to be hidden. They can be unhidden in Excel by means of the \*(L"Show Outline Symbols\*(R" command button. The default setting is 1 for visible outlines. .PP .Vb 1 \& $worksheet\->outline_settings(0); .Ve .PP The \f(CW$symbols_below\fR parameter is used to control whether the row outline symbol will appear above or below the outline level bar. The default setting is 1 for symbols to appear below the outline level bar. .PP The \f(CW\*(C`symbols_right\*(C'\fR parameter is used to control whether the column outline symbol will appear to the left or the right of the outline level bar. The default setting is 1 for symbols to appear to the right of the outline level bar. .PP The \f(CW$auto_style\fR parameter is used to control whether the automatic outline generator in Excel uses automatic styles when creating an outline. This has no effect on a file generated by \f(CW\*(C`Spreadsheet::WriteExcel\*(C'\fR but it does have an effect on how the worksheet behaves after it is created. The default setting is 0 for \*(L"Automatic Styles\*(R" to be turned off. .PP The default settings for all of these parameters correspond to Excel's default parameters. .PP The worksheet parameters controlled by \f(CW\*(C`outline_settings()\*(C'\fR are rarely used. .ie n .SS "freeze_panes($row, $col, $top_row, $left_col)" .el .SS "freeze_panes($row, \f(CW$col\fP, \f(CW$top_row\fP, \f(CW$left_col\fP)" .IX Subsection "freeze_panes($row, $col, $top_row, $left_col)" This method can be used to divide a worksheet into horizontal or vertical regions known as panes and to also \*(L"freeze\*(R" these panes so that the splitter bars are not visible. This is the same as the \f(CW\*(C`Window\->Freeze Panes\*(C'\fR menu command in Excel .PP The parameters \f(CW$row\fR and \f(CW$col\fR are used to specify the location of the split. It should be noted that the split is specified at the top or left of a cell and that the method uses zero based indexing. Therefore to freeze the first row of a worksheet it is necessary to specify the split at row 2 (which is 1 as the zero-based index). This might lead you to think that you are using a 1 based index but this is not the case. .PP You can set one of the \f(CW$row\fR and \f(CW$col\fR parameters as zero if you do not want either a vertical or horizontal split. .PP Examples: .PP .Vb 6 \& $worksheet\->freeze_panes(1, 0); # Freeze the first row \& $worksheet\->freeze_panes(\*(AqA2\*(Aq); # Same using A1 notation \& $worksheet\->freeze_panes(0, 1); # Freeze the first column \& $worksheet\->freeze_panes(\*(AqB1\*(Aq); # Same using A1 notation \& $worksheet\->freeze_panes(1, 2); # Freeze first row and first 2 columns \& $worksheet\->freeze_panes(\*(AqC2\*(Aq); # Same using A1 notation .Ve .PP The parameters \f(CW$top_row\fR and \f(CW$left_col\fR are optional. They are used to specify the top-most or left-most visible row or column in the scrolling region of the panes. For example to freeze the first row and to have the scrolling region begin at row twenty: .PP .Vb 1 \& $worksheet\->freeze_panes(1, 0, 20, 0); .Ve .PP You cannot use A1 notation for the \f(CW$top_row\fR and \f(CW$left_col\fR parameters. .PP See also the \f(CW\*(C`panes.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distribution. .ie n .SS "split_panes($y, $x, $top_row, $left_col)" .el .SS "split_panes($y, \f(CW$x\fP, \f(CW$top_row\fP, \f(CW$left_col\fP)" .IX Subsection "split_panes($y, $x, $top_row, $left_col)" This method can be used to divide a worksheet into horizontal or vertical regions known as panes. This method is different from the \f(CW\*(C`freeze_panes()\*(C'\fR method in that the splits between the panes will be visible to the user and each pane will have its own scroll bars. .PP The parameters \f(CW$y\fR and \f(CW$x\fR are used to specify the vertical and horizontal position of the split. The units for \f(CW$y\fR and \f(CW$x\fR are the same as those used by Excel to specify row height and column width. However, the vertical and horizontal units are different from each other. Therefore you must specify the \f(CW$y\fR and \f(CW$x\fR parameters in terms of the row heights and column widths that you have set or the default values which are \f(CW12.75\fR for a row and \f(CW8.43\fR for a column. .PP You can set one of the \f(CW$y\fR and \f(CW$x\fR parameters as zero if you do not want either a vertical or horizontal split. The parameters \f(CW$top_row\fR and \f(CW$left_col\fR are optional. They are used to specify the top-most or left-most visible row or column in the bottom-right pane. .PP Example: .PP .Vb 3 \& $worksheet\->split_panes(12.75, 0, 1, 0); # First row \& $worksheet\->split_panes(0, 8.43, 0, 1); # First column \& $worksheet\->split_panes(12.75, 8.43, 1, 1); # First row and column .Ve .PP You cannot use A1 notation with this method. .PP See also the \f(CW\*(C`freeze_panes()\*(C'\fR method and the \f(CW\*(C`panes.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distribution. .PP Note: This \f(CW\*(C`split_panes()\*(C'\fR method was called \f(CW\*(C`thaw_panes()\*(C'\fR in older versions. The older name is still available for backwards compatibility. .ie n .SS "merge_range($first_row, $first_col, $last_row, $last_col, $token, $format, $utf_16_be)" .el .SS "merge_range($first_row, \f(CW$first_col\fP, \f(CW$last_row\fP, \f(CW$last_col\fP, \f(CW$token\fP, \f(CW$format\fP, \f(CW$utf_16_be\fP)" .IX Subsection "merge_range($first_row, $first_col, $last_row, $last_col, $token, $format, $utf_16_be)" Merging cells can be achieved by setting the \f(CW\*(C`merge\*(C'\fR property of a Format object, see \*(L"\s-1CELL FORMATTING\*(R"\s0. However, this only allows simple Excel5 style horizontal merging which Excel refers to as \*(L"center across selection\*(R". .PP The \f(CW\*(C`merge_range()\*(C'\fR method allows you to do Excel97+ style formatting where the cells can contain other types of alignment in addition to the merging: .PP .Vb 5 \& my $format = $workbook\->add_format( \& border => 6, \& valign => \*(Aqvcenter\*(Aq, \& align => \*(Aqcenter\*(Aq, \& ); \& \& $worksheet\->merge_range(\*(AqB3:D4\*(Aq, \*(AqVertical and horizontal\*(Aq, $format); .Ve .PP \&\fB\s-1WARNING\s0\fR. The format object that is used with a \f(CW\*(C`merge_range()\*(C'\fR method call is marked internally as being associated with a merged range. It is a fatal error to use a merged format in a non-merged cell. Instead you should use separate formats for merged and non-merged cells. This restriction will be removed in a future release. .PP The \f(CW$utf_16_be\fR parameter is optional, see below. .PP \&\f(CW\*(C`merge_range()\*(C'\fR writes its \f(CW$token\fR argument using the worksheet \f(CW\*(C`write()\*(C'\fR method. Therefore it will handle numbers, strings, formulas or urls as required. .PP Setting the \f(CW\*(C`merge\*(C'\fR property of the format isn't required when you are using \f(CW\*(C`merge_range()\*(C'\fR. In fact using it will exclude the use of any other horizontal alignment option. .PP On systems with \f(CW\*(C`perl 5.8\*(C'\fR and later the \f(CW\*(C`merge_range()\*(C'\fR method will also handle strings in \f(CW\*(C`UTF\-8\*(C'\fR format. .PP .Vb 1 \& $worksheet\->merge_range(\*(AqB3:D4\*(Aq, "\ex{263a}", $format); # Smiley .Ve .PP On earlier Perl systems your can specify \f(CW\*(C`UTF\-16BE\*(C'\fR worksheet names using an additional optional parameter: .PP .Vb 2 \& my $str = pack \*(Aqn\*(Aq, 0x263a; \& $worksheet\->merge_range(\*(AqB3:D4\*(Aq, $str, $format, 1); # Smiley .Ve .PP The full possibilities of this method are shown in the \f(CW\*(C`merge3.pl\*(C'\fR to \f(CW\*(C`merge6.pl\*(C'\fR programs in the \f(CW\*(C`examples\*(C'\fR directory of the distribution. .SS "set_zoom($scale)" .IX Subsection "set_zoom($scale)" Set the worksheet zoom factor in the range \f(CW\*(C`10 <= $scale <= 400\*(C'\fR: .PP .Vb 4 \& $worksheet1\->set_zoom(50); \& $worksheet2\->set_zoom(75); \& $worksheet3\->set_zoom(300); \& $worksheet4\->set_zoom(400); .Ve .PP The default zoom factor is 100. You cannot zoom to \*(L"Selection\*(R" because it is calculated by Excel at run-time. .PP Note, \f(CW\*(C`set_zoom()\*(C'\fR does not affect the scale of the printed page. For that you should use \f(CW\*(C`set_print_scale()\*(C'\fR. .SS "\fIright_to_left()\fP" .IX Subsection "right_to_left()" The \f(CW\*(C`right_to_left()\*(C'\fR method is used to change the default direction of the worksheet from left-to-right, with the A1 cell in the top left, to right-to-left, with the he A1 cell in the top right. .PP .Vb 1 \& $worksheet\->right_to_left(); .Ve .PP This is useful when creating Arabic, Hebrew or other near or far eastern worksheets that use right-to-left as the default direction. .SS "\fIhide_zero()\fP" .IX Subsection "hide_zero()" The \f(CW\*(C`hide_zero()\*(C'\fR method is used to hide any zero values that appear in cells. .PP .Vb 1 \& $worksheet\->hide_zero(); .Ve .PP In Excel this option is found under Tools\->Options\->View. .SS "\fIset_tab_color()\fP" .IX Subsection "set_tab_color()" The \f(CW\*(C`set_tab_color()\*(C'\fR method is used to change the colour of the worksheet tab. This feature is only available in Excel 2002 and later. You can use one of the standard colour names provided by the Format object or a colour index. See \*(L"\s-1COLOURS IN EXCEL\*(R"\s0 and the \f(CW\*(C`set_custom_color()\*(C'\fR method. .PP .Vb 2 \& $worksheet1\->set_tab_color(\*(Aqred\*(Aq); \& $worksheet2\->set_tab_color(0x0C); .Ve .PP See the \f(CW\*(C`tab_colors.pl\*(C'\fR program in the examples directory of the distro. .ie n .SS "autofilter($first_row, $first_col, $last_row, $last_col)" .el .SS "autofilter($first_row, \f(CW$first_col\fP, \f(CW$last_row\fP, \f(CW$last_col\fP)" .IX Subsection "autofilter($first_row, $first_col, $last_row, $last_col)" This method allows an autofilter to be added to a worksheet. An autofilter is a way of adding drop down lists to the headers of a 2D range of worksheet data. This in turn allow users to filter the data based on simple criteria so that some data is shown and some is hidden. .PP To add an autofilter to a worksheet: .PP .Vb 2 \& $worksheet\->autofilter(0, 0, 10, 3); \& $worksheet\->autofilter(\*(AqA1:D11\*(Aq); # Same as above in A1 notation. .Ve .PP Filter conditions can be applied using the \f(CW\*(C`filter_column()\*(C'\fR method. .PP See the \f(CW\*(C`autofilter.pl\*(C'\fR program in the examples directory of the distro for a more detailed example. .ie n .SS "filter_column($column, $expression)" .el .SS "filter_column($column, \f(CW$expression\fP)" .IX Subsection "filter_column($column, $expression)" The \f(CW\*(C`filter_column\*(C'\fR method can be used to filter columns in a autofilter range based on simple conditions. .PP \&\fB\s-1NOTE:\s0\fR It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. Rows are hidden using the \f(CW\*(C`set_row()\*(C'\fR \f(CW\*(C`visible\*(C'\fR parameter. \f(CW\*(C`Spreadsheet::WriteExcel\*(C'\fR cannot do this automatically since it isn't part of the file format. See the \f(CW\*(C`autofilter.pl\*(C'\fR program in the examples directory of the distro for an example. .PP The conditions for the filter are specified using simple expressions: .PP .Vb 2 \& $worksheet\->filter_column(\*(AqA\*(Aq, \*(Aqx > 2000\*(Aq); \& $worksheet\->filter_column(\*(AqB\*(Aq, \*(Aqx > 2000 and x < 5000\*(Aq); .Ve .PP The \f(CW$column\fR parameter can either be a zero indexed column number or a string column name. .PP The following operators are available: .PP .Vb 7 \& Operator Synonyms \& == = eq =~ \& != <> ne != \& > \& < \& >= \& <= \& \& and && \& or || .Ve .PP The operator synonyms are just syntactic sugar to make you more comfortable using the expressions. It is important to remember that the expressions will be interpreted by Excel and not by perl. .PP An expression can comprise a single statement or two statements separated by the \f(CW\*(C`and\*(C'\fR and \f(CW\*(C`or\*(C'\fR operators. For example: .PP .Vb 5 \& \*(Aqx < 2000\*(Aq \& \*(Aqx > 2000\*(Aq \& \*(Aqx == 2000\*(Aq \& \*(Aqx > 2000 and x < 5000\*(Aq \& \*(Aqx == 2000 or x == 5000\*(Aq .Ve .PP Filtering of blank or non-blank data can be achieved by using a value of \f(CW\*(C`Blanks\*(C'\fR or \f(CW\*(C`NonBlanks\*(C'\fR in the expression: .PP .Vb 2 \& \*(Aqx == Blanks\*(Aq \& \*(Aqx == NonBlanks\*(Aq .Ve .PP Top 10 style filters can be specified using a expression like the following: .PP .Vb 1 \& Top|Bottom 1\-500 Items|% .Ve .PP For example: .PP .Vb 4 \& \*(AqTop 10 Items\*(Aq \& \*(AqBottom 5 Items\*(Aq \& \*(AqTop 25 %\*(Aq \& \*(AqBottom 50 %\*(Aq .Ve .PP Excel also allows some simple string matching operations: .PP .Vb 6 \& \*(Aqx =~ b*\*(Aq # begins with b \& \*(Aqx !~ b*\*(Aq # doesn\*(Aqt begin with b \& \*(Aqx =~ *b\*(Aq # ends with b \& \*(Aqx !~ *b\*(Aq # doesn\*(Aqt end with b \& \*(Aqx =~ *b*\*(Aq # contains b \& \*(Aqx !~ *b*\*(Aq # doesn\*(Aqt contains b .Ve .PP You can also use \f(CW\*(C`*\*(C'\fR to match any character or number and \f(CW\*(C`?\*(C'\fR to match any single character or number. No other regular expression quantifier is supported by Excel's filters. Excel's regular expression characters can be escaped using \f(CW\*(C`~\*(C'\fR. .PP The placeholder variable \f(CW\*(C`x\*(C'\fR in the above examples can be replaced by any simple string. The actual placeholder name is ignored internally so the following are all equivalent: .PP .Vb 3 \& \*(Aqx < 2000\*(Aq \& \*(Aqcol < 2000\*(Aq \& \*(AqPrice < 2000\*(Aq .Ve .PP Also, note that a filter condition can only be applied to a column in a range specified by the \f(CW\*(C`autofilter()\*(C'\fR Worksheet method. .PP See the \f(CW\*(C`autofilter.pl\*(C'\fR program in the examples directory of the distro for a more detailed example. .SH "PAGE SET-UP METHODS" .IX Header "PAGE SET-UP METHODS" Page set-up methods affect the way that a worksheet looks when it is printed. They control features such as page headers and footers and margins. These methods are really just standard worksheet methods. They are documented here in a separate section for the sake of clarity. .PP The following methods are available for page set-up: .PP .Vb 10 \& set_landscape() \& set_portrait() \& set_page_view() \& set_paper() \& center_horizontally() \& center_vertically() \& set_margins() \& set_header() \& set_footer() \& repeat_rows() \& repeat_columns() \& hide_gridlines() \& print_row_col_headers() \& print_area() \& print_across() \& fit_to_pages() \& set_start_page() \& set_print_scale() \& set_h_pagebreaks() \& set_v_pagebreaks() .Ve .PP A common requirement when working with Spreadsheet::WriteExcel is to apply the same page set-up features to all of the worksheets in a workbook. To do this you can use the \f(CW\*(C`sheets()\*(C'\fR method of the \f(CW\*(C`workbook\*(C'\fR class to access the array of worksheets in a workbook: .PP .Vb 3 \& foreach $worksheet ($workbook\->sheets()) { \& $worksheet\->set_landscape(); \& } .Ve .SS "\fIset_landscape()\fP" .IX Subsection "set_landscape()" This method is used to set the orientation of a worksheet's printed page to landscape: .PP .Vb 1 \& $worksheet\->set_landscape(); # Landscape mode .Ve .SS "\fIset_portrait()\fP" .IX Subsection "set_portrait()" This method is used to set the orientation of a worksheet's printed page to portrait. The default worksheet orientation is portrait, so you won't generally need to call this method. .PP .Vb 1 \& $worksheet\->set_portrait(); # Portrait mode .Ve .SS "\fIset_page_view()\fP" .IX Subsection "set_page_view()" This method is used to display the worksheet in \*(L"Page View\*(R" mode. This is currently only supported by Mac Excel, where it is the default. .PP .Vb 1 \& $worksheet\->set_page_view(); .Ve .SS "set_paper($index)" .IX Subsection "set_paper($index)" This method is used to set the paper format for the printed output of a worksheet. The following paper styles are available: .PP .Vb 10 \& Index Paper format Paper size \& ===== ============ ========== \& 0 Printer default \- \& 1 Letter 8 1/2 x 11 in \& 2 Letter Small 8 1/2 x 11 in \& 3 Tabloid 11 x 17 in \& 4 Ledger 17 x 11 in \& 5 Legal 8 1/2 x 14 in \& 6 Statement 5 1/2 x 8 1/2 in \& 7 Executive 7 1/4 x 10 1/2 in \& 8 A3 297 x 420 mm \& 9 A4 210 x 297 mm \& 10 A4 Small 210 x 297 mm \& 11 A5 148 x 210 mm \& 12 B4 250 x 354 mm \& 13 B5 182 x 257 mm \& 14 Folio 8 1/2 x 13 in \& 15 Quarto 215 x 275 mm \& 16 \- 10x14 in \& 17 \- 11x17 in \& 18 Note 8 1/2 x 11 in \& 19 Envelope 9 3 7/8 x 8 7/8 \& 20 Envelope 10 4 1/8 x 9 1/2 \& 21 Envelope 11 4 1/2 x 10 3/8 \& 22 Envelope 12 4 3/4 x 11 \& 23 Envelope 14 5 x 11 1/2 \& 24 C size sheet \- \& 25 D size sheet \- \& 26 E size sheet \- \& 27 Envelope DL 110 x 220 mm \& 28 Envelope C3 324 x 458 mm \& 29 Envelope C4 229 x 324 mm \& 30 Envelope C5 162 x 229 mm \& 31 Envelope C6 114 x 162 mm \& 32 Envelope C65 114 x 229 mm \& 33 Envelope B4 250 x 353 mm \& 34 Envelope B5 176 x 250 mm \& 35 Envelope B6 176 x 125 mm \& 36 Envelope 110 x 230 mm \& 37 Monarch 3.875 x 7.5 in \& 38 Envelope 3 5/8 x 6 1/2 in \& 39 Fanfold 14 7/8 x 11 in \& 40 German Std Fanfold 8 1/2 x 12 in \& 41 German Legal Fanfold 8 1/2 x 13 in .Ve .PP Note, it is likely that not all of these paper types will be available to the end user since it will depend on the paper formats that the user's printer supports. Therefore, it is best to stick to standard paper types. .PP .Vb 2 \& $worksheet\->set_paper(1); # US Letter \& $worksheet\->set_paper(9); # A4 .Ve .PP If you do not specify a paper type the worksheet will print using the printer's default paper. .SS "\fIcenter_horizontally()\fP" .IX Subsection "center_horizontally()" Center the worksheet data horizontally between the margins on the printed page: .PP .Vb 1 \& $worksheet\->center_horizontally(); .Ve .SS "\fIcenter_vertically()\fP" .IX Subsection "center_vertically()" Center the worksheet data vertically between the margins on the printed page: .PP .Vb 1 \& $worksheet\->center_vertically(); .Ve .SS "set_margins($inches)" .IX Subsection "set_margins($inches)" There are several methods available for setting the worksheet margins on the printed page: .PP .Vb 7 \& set_margins() # Set all margins to the same value \& set_margins_LR() # Set left and right margins to the same value \& set_margins_TB() # Set top and bottom margins to the same value \& set_margin_left(); # Set left margin \& set_margin_right(); # Set right margin \& set_margin_top(); # Set top margin \& set_margin_bottom(); # Set bottom margin .Ve .PP All of these methods take a distance in inches as a parameter. Note: 1 inch = 25.4mm. ;\-) The default left and right margin is 0.75 inch. The default top and bottom margin is 1.00 inch. .ie n .SS "set_header($string, $margin)" .el .SS "set_header($string, \f(CW$margin\fP)" .IX Subsection "set_header($string, $margin)" Headers and footers are generated using a \f(CW$string\fR which is a combination of plain text and control characters. The \f(CW$margin\fR parameter is optional. .PP The available control character are: .PP .Vb 5 \& Control Category Description \& ======= ======== =========== \& &L Justification Left \& &C Center \& &R Right \& \& &P Information Page number \& &N Total number of pages \& &D Date \& &T Time \& &F File name \& &A Worksheet name \& &Z Workbook path \& \& &fontsize Font Font size \& &"font,style" Font name and style \& &U Single underline \& &E Double underline \& &S Strikethrough \& &X Superscript \& &Y Subscript \& \& && Miscellaneous Literal ampersand & .Ve .PP Text in headers and footers can be justified (aligned) to the left, center and right by prefixing the text with the control characters \f(CW&L\fR, \f(CW&C\fR and \f(CW&R\fR. .PP For example (with \s-1ASCII\s0 art representation of the results): .PP .Vb 1 \& $worksheet\->set_header(\*(Aq&LHello\*(Aq); \& \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | \& | Hello | \& | | \& \& \& $worksheet\->set_header(\*(Aq&CHello\*(Aq); \& \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | \& | Hello | \& | | \& \& \& $worksheet\->set_header(\*(Aq&RHello\*(Aq); \& \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | \& | Hello | \& | | .Ve .PP For simple text, if you do not specify any justification the text will be centred. However, you must prefix the text with \f(CW&C\fR if you specify a font name or any other formatting: .PP .Vb 1 \& $worksheet\->set_header(\*(AqHello\*(Aq); \& \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | \& | Hello | \& | | .Ve .PP You can have text in each of the justification regions: .PP .Vb 1 \& $worksheet\->set_header(\*(Aq&LCiao&CBello&RCielo\*(Aq); \& \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | \& | Ciao Bello Cielo | \& | | .Ve .PP The information control characters act as variables that Excel will update as the workbook or worksheet changes. Times and dates are in the users default format: .PP .Vb 1 \& $worksheet\->set_header(\*(Aq&CPage &P of &N\*(Aq); \& \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | \& | Page 1 of 6 | \& | | \& \& \& $worksheet\->set_header(\*(Aq&CUpdated at &T\*(Aq); \& \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | \& | Updated at 12:30 PM | \& | | .Ve .PP You can specify the font size of a section of the text by prefixing it with the control character \f(CW&n\fR where \f(CW\*(C`n\*(C'\fR is the font size: .PP .Vb 2 \& $worksheet1\->set_header(\*(Aq&C&30Hello Big\*(Aq ); \& $worksheet2\->set_header(\*(Aq&C&10Hello Small\*(Aq); .Ve .PP You can specify the font of a section of the text by prefixing it with the control sequence \f(CW\*(C`&"font,style"\*(C'\fR where \f(CW\*(C`fontname\*(C'\fR is a font name such as \*(L"Courier New\*(R" or \*(L"Times New Roman\*(R" and \f(CW\*(C`style\*(C'\fR is one of the standard Windows font descriptions: \*(L"Regular\*(R", \*(L"Italic\*(R", \*(L"Bold\*(R" or \*(L"Bold Italic\*(R": .PP .Vb 3 \& $worksheet1\->set_header(\*(Aq&C&"Courier New,Italic"Hello\*(Aq); \& $worksheet2\->set_header(\*(Aq&C&"Courier New,Bold Italic"Hello\*(Aq); \& $worksheet3\->set_header(\*(Aq&C&"Times New Roman,Regular"Hello\*(Aq); .Ve .PP It is possible to combine all of these features together to create sophisticated headers and footers. As an aid to setting up complicated headers and footers you can record a page set-up as a macro in Excel and look at the format strings that \s-1VBA\s0 produces. Remember however that \s-1VBA\s0 uses two double quotes \f(CW""\fR to indicate a single double quote. For the last example above the equivalent \s-1VBA\s0 code looks like this: .PP .Vb 3 \& .LeftHeader = "" \& .CenterHeader = "&""Times New Roman,Regular""Hello" \& .RightHeader = "" .Ve .PP To include a single literal ampersand \f(CW\*(C`&\*(C'\fR in a header or footer you should use a double ampersand \f(CW\*(C`&&\*(C'\fR: .PP .Vb 1 \& $worksheet1\->set_header(\*(Aq&CCuriouser && Curiouser \- Attorneys at Law\*(Aq); .Ve .PP As stated above the margin parameter is optional. As with the other margins the value should be in inches. The default header and footer margin is 0.50 inch. The header and footer margin size can be set as follows: .PP .Vb 1 \& $worksheet\->set_header(\*(Aq&CHello\*(Aq, 0.75); .Ve .PP The header and footer margins are independent of the top and bottom margins. .PP Note, the header or footer string must be less than 255 characters. Strings longer than this will not be written and a warning will be generated. .PP On systems with \f(CW\*(C`perl 5.8\*(C'\fR and later the \f(CW\*(C`set_header()\*(C'\fR method can also handle Unicode strings in \f(CW\*(C`UTF\-8\*(C'\fR format. .PP .Vb 1 \& $worksheet\->set_header("&C\ex{263a}") .Ve .PP See, also the \f(CW\*(C`headers.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distribution. .SS "\fIset_footer()\fP" .IX Subsection "set_footer()" The syntax of the \f(CW\*(C`set_footer()\*(C'\fR method is the same as \f(CW\*(C`set_header()\*(C'\fR, see above. .ie n .SS "repeat_rows($first_row, $last_row)" .el .SS "repeat_rows($first_row, \f(CW$last_row\fP)" .IX Subsection "repeat_rows($first_row, $last_row)" Set the number of rows to repeat at the top of each printed page. .PP For large Excel documents it is often desirable to have the first row or rows of the worksheet print out at the top of each page. This can be achieved by using the \f(CW\*(C`repeat_rows()\*(C'\fR method. The parameters \f(CW$first_row\fR and \f(CW$last_row\fR are zero based. The \f(CW$last_row\fR parameter is optional if you only wish to specify one row: .PP .Vb 2 \& $worksheet1\->repeat_rows(0); # Repeat the first row \& $worksheet2\->repeat_rows(0, 1); # Repeat the first two rows .Ve .ie n .SS "repeat_columns($first_col, $last_col)" .el .SS "repeat_columns($first_col, \f(CW$last_col\fP)" .IX Subsection "repeat_columns($first_col, $last_col)" Set the columns to repeat at the left hand side of each printed page. .PP For large Excel documents it is often desirable to have the first column or columns of the worksheet print out at the left hand side of each page. This can be achieved by using the \f(CW\*(C`repeat_columns()\*(C'\fR method. The parameters \f(CW$first_column\fR and \f(CW$last_column\fR are zero based. The \f(CW$last_column\fR parameter is optional if you only wish to specify one column. You can also specify the columns using A1 column notation, see the note about \*(L"Cell notation\*(R". .PP .Vb 4 \& $worksheet1\->repeat_columns(0); # Repeat the first column \& $worksheet2\->repeat_columns(0, 1); # Repeat the first two columns \& $worksheet3\->repeat_columns(\*(AqA:A\*(Aq); # Repeat the first column \& $worksheet4\->repeat_columns(\*(AqA:B\*(Aq); # Repeat the first two columns .Ve .SS "hide_gridlines($option)" .IX Subsection "hide_gridlines($option)" This method is used to hide the gridlines on the screen and printed page. Gridlines are the lines that divide the cells on a worksheet. Screen and printed gridlines are turned on by default in an Excel worksheet. If you have defined your own cell borders you may wish to hide the default gridlines. .PP .Vb 1 \& $worksheet\->hide_gridlines(); .Ve .PP The following values of \f(CW$option\fR are valid: .PP .Vb 3 \& 0 : Don\*(Aqt hide gridlines \& 1 : Hide printed gridlines only \& 2 : Hide screen and printed gridlines .Ve .PP If you don't supply an argument or use \f(CW\*(C`undef\*(C'\fR the default option is 1, i.e. only the printed gridlines are hidden. .SS "\fIprint_row_col_headers()\fP" .IX Subsection "print_row_col_headers()" Set the option to print the row and column headers on the printed page. .PP An Excel worksheet looks something like the following; .PP .Vb 8 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 1 | | | | | ... \& | 2 | | | | | ... \& | 3 | | | | | ... \& | 4 | | | | | ... \& |...| ... | ... | ... | ... | ... .Ve .PP The headers are the letters and numbers at the top and the left of the worksheet. Since these headers serve mainly as a indication of position on the worksheet they generally do not appear on the printed page. If you wish to have them printed you can use the \f(CW\*(C`print_row_col_headers()\*(C'\fR method : .PP .Vb 1 \& $worksheet\->print_row_col_headers(); .Ve .PP Do not confuse these headers with page headers as described in the \f(CW\*(C`set_header()\*(C'\fR section above. .ie n .SS "print_area($first_row, $first_col, $last_row, $last_col)" .el .SS "print_area($first_row, \f(CW$first_col\fP, \f(CW$last_row\fP, \f(CW$last_col\fP)" .IX Subsection "print_area($first_row, $first_col, $last_row, $last_col)" This method is used to specify the area of the worksheet that will be printed. All four parameters must be specified. You can also use A1 notation, see the note about \*(L"Cell notation\*(R". .PP .Vb 3 \& $worksheet1\->print_area(\*(AqA1:H20\*(Aq); # Cells A1 to H20 \& $worksheet2\->print_area(0, 0, 19, 7); # The same \& $worksheet2\->print_area(\*(AqA:H\*(Aq); # Columns A to H if rows have data .Ve .SS "\fIprint_across()\fP" .IX Subsection "print_across()" The \f(CW\*(C`print_across\*(C'\fR method is used to change the default print direction. This is referred to by Excel as the sheet \*(L"page order\*(R". .PP .Vb 1 \& $worksheet\->print_across(); .Ve .PP The default page order is shown below for a worksheet that extends over 4 pages. The order is called \*(L"down then across\*(R": .PP .Vb 2 \& [1] [3] \& [2] [4] .Ve .PP However, by using the \f(CW\*(C`print_across\*(C'\fR method the print order will be changed to \*(L"across then down\*(R": .PP .Vb 2 \& [1] [2] \& [3] [4] .Ve .ie n .SS "fit_to_pages($width, $height)" .el .SS "fit_to_pages($width, \f(CW$height\fP)" .IX Subsection "fit_to_pages($width, $height)" The \f(CW\*(C`fit_to_pages()\*(C'\fR method is used to fit the printed area to a specific number of pages both vertically and horizontally. If the printed area exceeds the specified number of pages it will be scaled down to fit. This guarantees that the printed area will always appear on the specified number of pages even if the page size or margins change. .PP .Vb 3 \& $worksheet1\->fit_to_pages(1, 1); # Fit to 1x1 pages \& $worksheet2\->fit_to_pages(2, 1); # Fit to 2x1 pages \& $worksheet3\->fit_to_pages(1, 2); # Fit to 1x2 pages .Ve .PP The print area can be defined using the \f(CW\*(C`print_area()\*(C'\fR method as described above. .PP A common requirement is to fit the printed output to \fIn\fR pages wide but have the height be as long as necessary. To achieve this set the \f(CW$height\fR to zero or leave it blank: .PP .Vb 2 \& $worksheet1\->fit_to_pages(1, 0); # 1 page wide and as long as necessary \& $worksheet2\->fit_to_pages(1); # The same .Ve .PP Note that although it is valid to use both \f(CW\*(C`fit_to_pages()\*(C'\fR and \f(CW\*(C`set_print_scale()\*(C'\fR on the same worksheet only one of these options can be active at a time. The last method call made will set the active option. .PP Note that \f(CW\*(C`fit_to_pages()\*(C'\fR will override any manual page breaks that are defined in the worksheet. .SS "set_start_page($start_page)" .IX Subsection "set_start_page($start_page)" The \f(CW\*(C`set_start_page()\*(C'\fR method is used to set the number of the starting page when the worksheet is printed out. The default value is 1. .PP .Vb 1 \& $worksheet\->set_start_page(2); .Ve .SS "set_print_scale($scale)" .IX Subsection "set_print_scale($scale)" Set the scale factor of the printed page. Scale factors in the range \f(CW\*(C`10 <= $scale <= 400\*(C'\fR are valid: .PP .Vb 4 \& $worksheet1\->set_print_scale(50); \& $worksheet2\->set_print_scale(75); \& $worksheet3\->set_print_scale(300); \& $worksheet4\->set_print_scale(400); .Ve .PP The default scale factor is 100. Note, \f(CW\*(C`set_print_scale()\*(C'\fR does not affect the scale of the visible page in Excel. For that you should use \f(CW\*(C`set_zoom()\*(C'\fR. .PP Note also that although it is valid to use both \f(CW\*(C`fit_to_pages()\*(C'\fR and \f(CW\*(C`set_print_scale()\*(C'\fR on the same worksheet only one of these options can be active at a time. The last method call made will set the active option. .SS "set_h_pagebreaks(@breaks)" .IX Subsection "set_h_pagebreaks(@breaks)" Add horizontal page breaks to a worksheet. A page break causes all the data that follows it to be printed on the next page. Horizontal page breaks act between rows. To create a page break between rows 20 and 21 you must specify the break at row 21. However in zero index notation this is actually row 20. So you can pretend for a small while that you are using 1 index notation: .PP .Vb 1 \& $worksheet1\->set_h_pagebreaks(20); # Break between row 20 and 21 .Ve .PP The \f(CW\*(C`set_h_pagebreaks()\*(C'\fR method will accept a list of page breaks and you can call it more than once: .PP .Vb 2 \& $worksheet2\->set_h_pagebreaks( 20, 40, 60, 80, 100); # Add breaks \& $worksheet2\->set_h_pagebreaks(120, 140, 160, 180, 200); # Add some more .Ve .PP Note: If you specify the \*(L"fit to page\*(R" option via the \f(CW\*(C`fit_to_pages()\*(C'\fR method it will override all manual page breaks. .PP There is a silent limitation of about 1000 horizontal page breaks per worksheet in line with an Excel internal limitation. .SS "set_v_pagebreaks(@breaks)" .IX Subsection "set_v_pagebreaks(@breaks)" Add vertical page breaks to a worksheet. A page break causes all the data that follows it to be printed on the next page. Vertical page breaks act between columns. To create a page break between columns 20 and 21 you must specify the break at column 21. However in zero index notation this is actually column 20. So you can pretend for a small while that you are using 1 index notation: .PP .Vb 1 \& $worksheet1\->set_v_pagebreaks(20); # Break between column 20 and 21 .Ve .PP The \f(CW\*(C`set_v_pagebreaks()\*(C'\fR method will accept a list of page breaks and you can call it more than once: .PP .Vb 2 \& $worksheet2\->set_v_pagebreaks( 20, 40, 60, 80, 100); # Add breaks \& $worksheet2\->set_v_pagebreaks(120, 140, 160, 180, 200); # Add some more .Ve .PP Note: If you specify the \*(L"fit to page\*(R" option via the \f(CW\*(C`fit_to_pages()\*(C'\fR method it will override all manual page breaks. .SH "CELL FORMATTING" .IX Header "CELL FORMATTING" This section describes the methods and properties that are available for formatting cells in Excel. The properties of a cell that can be formatted include: fonts, colours, patterns, borders, alignment and number formatting. .SS "Creating and using a Format object" .IX Subsection "Creating and using a Format object" Cell formatting is defined through a Format object. Format objects are created by calling the workbook \f(CW\*(C`add_format()\*(C'\fR method as follows: .PP .Vb 2 \& my $format1 = $workbook\->add_format(); # Set properties later \& my $format2 = $workbook\->add_format(%props); # Set at creation .Ve .PP The format object holds all the formatting properties that can be applied to a cell, a row or a column. The process of setting these properties is discussed in the next section. .PP Once a Format object has been constructed and its properties have been set it can be passed as an argument to the worksheet \f(CW\*(C`write\*(C'\fR methods as follows: .PP .Vb 4 \& $worksheet\->write(0, 0, \*(AqOne\*(Aq, $format); \& $worksheet\->write_string(1, 0, \*(AqTwo\*(Aq, $format); \& $worksheet\->write_number(2, 0, 3, $format); \& $worksheet\->write_blank(3, 0, $format); .Ve .PP Formats can also be passed to the worksheet \f(CW\*(C`set_row()\*(C'\fR and \f(CW\*(C`set_column()\*(C'\fR methods to define the default property for a row or column. .PP .Vb 2 \& $worksheet\->set_row(0, 15, $format); \& $worksheet\->set_column(0, 0, 15, $format); .Ve .SS "Format methods and Format properties" .IX Subsection "Format methods and Format properties" The following table shows the Excel format categories, the formatting properties that can be applied and the equivalent object method: .PP .Vb 12 \& Category Description Property Method Name \& \-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\- \& Font Font type font set_font() \& Font size size set_size() \& Font color color set_color() \& Bold bold set_bold() \& Italic italic set_italic() \& Underline underline set_underline() \& Strikeout font_strikeout set_font_strikeout() \& Super/Subscript font_script set_font_script() \& Outline font_outline set_font_outline() \& Shadow font_shadow set_font_shadow() \& \& Number Numeric format num_format set_num_format() \& \& Protection Lock cells locked set_locked() \& Hide formulas hidden set_hidden() \& \& Alignment Horizontal align align set_align() \& Vertical align valign set_align() \& Rotation rotation set_rotation() \& Text wrap text_wrap set_text_wrap() \& Justify last text_justlast set_text_justlast() \& Center across center_across set_center_across() \& Indentation indent set_indent() \& Shrink to fit shrink set_shrink() \& \& Pattern Cell pattern pattern set_pattern() \& Background color bg_color set_bg_color() \& Foreground color fg_color set_fg_color() \& \& Border Cell border border set_border() \& Bottom border bottom set_bottom() \& Top border top set_top() \& Left border left set_left() \& Right border right set_right() \& Border color border_color set_border_color() \& Bottom color bottom_color set_bottom_color() \& Top color top_color set_top_color() \& Left color left_color set_left_color() \& Right color right_color set_right_color() .Ve .PP There are two ways of setting Format properties: by using the object method interface or by setting the property directly. For example, a typical use of the method interface would be as follows: .PP .Vb 3 \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_color(\*(Aqred\*(Aq); .Ve .PP By comparison the properties can be set directly by passing a hash of properties to the Format constructor: .PP .Vb 1 \& my $format = $workbook\->add_format(bold => 1, color => \*(Aqred\*(Aq); .Ve .PP or after the Format has been constructed by means of the \f(CW\*(C`set_format_properties()\*(C'\fR method as follows: .PP .Vb 2 \& my $format = $workbook\->add_format(); \& $format\->set_format_properties(bold => 1, color => \*(Aqred\*(Aq); .Ve .PP You can also store the properties in one or more named hashes and pass them to the required method: .PP .Vb 6 \& my %font = ( \& font => \*(AqArial\*(Aq, \& size => 12, \& color => \*(Aqblue\*(Aq, \& bold => 1, \& ); \& \& my %shading = ( \& bg_color => \*(Aqgreen\*(Aq, \& pattern => 1, \& ); \& \& \& my $format1 = $workbook\->add_format(%font); # Font only \& my $format2 = $workbook\->add_format(%font, %shading); # Font and shading .Ve .PP The provision of two ways of setting properties might lead you to wonder which is the best way. The method mechanism may be better is you prefer setting properties via method calls (which the author did when the code was first written) otherwise passing properties to the constructor has proved to be a little more flexible and self documenting in practice. An additional advantage of working with property hashes is that it allows you to share formatting between workbook objects as shown in the example above. .PP The Perl/Tk style of adding properties is also supported: .PP .Vb 6 \& my %font = ( \& \-font => \*(AqArial\*(Aq, \& \-size => 12, \& \-color => \*(Aqblue\*(Aq, \& \-bold => 1, \& ); .Ve .SS "Working with formats" .IX Subsection "Working with formats" The default format is Arial 10 with all other properties off. .PP Each unique format in Spreadsheet::WriteExcel must have a corresponding Format object. It isn't possible to use a Format with a \fIwrite()\fR method and then redefine the Format for use at a later stage. This is because a Format is applied to a cell not in its current state but in its final state. Consider the following example: .PP .Vb 6 \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_color(\*(Aqred\*(Aq); \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqCell A1\*(Aq, $format); \& $format\->set_color(\*(Aqgreen\*(Aq); \& $worksheet\->write(\*(AqB1\*(Aq, \*(AqCell B1\*(Aq, $format); .Ve .PP Cell A1 is assigned the Format \f(CW$format\fR which is initially set to the colour red. However, the colour is subsequently set to green. When Excel displays Cell A1 it will display the final state of the Format which in this case will be the colour green. .PP In general a method call without an argument will turn a property on, for example: .PP .Vb 4 \& my $format1 = $workbook\->add_format(); \& $format1\->set_bold(); # Turns bold on \& $format1\->set_bold(1); # Also turns bold on \& $format1\->set_bold(0); # Turns bold off .Ve .SH "FORMAT METHODS" .IX Header "FORMAT METHODS" The Format object methods are described in more detail in the following sections. In addition, there is a Perl program called \f(CW\*(C`formats.pl\*(C'\fR in the \f(CW\*(C`examples\*(C'\fR directory of the WriteExcel distribution. This program creates an Excel workbook called \f(CW\*(C`formats.xls\*(C'\fR which contains examples of almost all the format types. .PP The following Format methods are available: .PP .Vb 10 \& set_font() \& set_size() \& set_color() \& set_bold() \& set_italic() \& set_underline() \& set_font_strikeout() \& set_font_script() \& set_font_outline() \& set_font_shadow() \& set_num_format() \& set_locked() \& set_hidden() \& set_align() \& set_rotation() \& set_text_wrap() \& set_text_justlast() \& set_center_across() \& set_indent() \& set_shrink() \& set_pattern() \& set_bg_color() \& set_fg_color() \& set_border() \& set_bottom() \& set_top() \& set_left() \& set_right() \& set_border_color() \& set_bottom_color() \& set_top_color() \& set_left_color() \& set_right_color() .Ve .PP The above methods can also be applied directly as properties. For example \f(CW\*(C`$format\->set_bold()\*(C'\fR is equivalent to \f(CW\*(C`$workbook\->add_format(bold => 1)\*(C'\fR. .SS "set_format_properties(%properties)" .IX Subsection "set_format_properties(%properties)" The properties of an existing Format object can be also be set by means of \f(CW\*(C`set_format_properties()\*(C'\fR: .PP .Vb 2 \& my $format = $workbook\->add_format(); \& $format\->set_format_properties(bold => 1, color => \*(Aqred\*(Aq); .Ve .PP However, this method is here mainly for legacy reasons. It is preferable to set the properties in the format constructor: .PP .Vb 1 \& my $format = $workbook\->add_format(bold => 1, color => \*(Aqred\*(Aq); .Ve .SS "set_font($fontname)" .IX Subsection "set_font($fontname)" .Vb 3 \& Default state: Font is Arial \& Default action: None \& Valid args: Any valid font name .Ve .PP Specify the font used: .PP .Vb 1 \& $format\->set_font(\*(AqTimes New Roman\*(Aq); .Ve .PP Excel can only display fonts that are installed on the system that it is running on. Therefore it is best to use the fonts that come as standard such as 'Arial', 'Times New Roman' and 'Courier New'. See also the Fonts worksheet created by formats.pl .SS "\fIset_size()\fP" .IX Subsection "set_size()" .Vb 3 \& Default state: Font size is 10 \& Default action: Set font size to 1 \& Valid args: Integer values from 1 to as big as your screen. .Ve .PP Set the font size. Excel adjusts the height of a row to accommodate the largest font size in the row. You can also explicitly specify the height of a row using the \fIset_row()\fR worksheet method. .PP .Vb 2 \& my $format = $workbook\->add_format(); \& $format\->set_size(30); .Ve .SS "\fIset_color()\fP" .IX Subsection "set_color()" .Vb 10 \& Default state: Excels default color, usually black \& Default action: Set the default color \& Valid args: Integers from 8..63 or the following strings: \& \*(Aqblack\*(Aq \& \*(Aqblue\*(Aq \& \*(Aqbrown\*(Aq \& \*(Aqcyan\*(Aq \& \*(Aqgray\*(Aq \& \*(Aqgreen\*(Aq \& \*(Aqlime\*(Aq \& \*(Aqmagenta\*(Aq \& \*(Aqnavy\*(Aq \& \*(Aqorange\*(Aq \& \*(Aqpink\*(Aq \& \*(Aqpurple\*(Aq \& \*(Aqred\*(Aq \& \*(Aqsilver\*(Aq \& \*(Aqwhite\*(Aq \& \*(Aqyellow\*(Aq .Ve .PP Set the font colour. The \f(CW\*(C`set_color()\*(C'\fR method is used as follows: .PP .Vb 3 \& my $format = $workbook\->add_format(); \& $format\->set_color(\*(Aqred\*(Aq); \& $worksheet\->write(0, 0, \*(Aqwheelbarrow\*(Aq, $format); .Ve .PP Note: The \f(CW\*(C`set_color()\*(C'\fR method is used to set the colour of the font in a cell. To set the colour of a cell use the \f(CW\*(C`set_bg_color()\*(C'\fR and \f(CW\*(C`set_pattern()\*(C'\fR methods. .PP For additional examples see the 'Named colors' and 'Standard colors' worksheets created by formats.pl in the examples directory. .PP See also \*(L"\s-1COLOURS IN EXCEL\*(R"\s0. .SS "\fIset_bold()\fP" .IX Subsection "set_bold()" .Vb 3 \& Default state: bold is off \& Default action: Turn bold on \& Valid args: 0, 1 [1] .Ve .PP Set the bold property of the font: .PP .Vb 1 \& $format\->set_bold(); # Turn bold on .Ve .PP [1] Actually, values in the range 100..1000 are also valid. 400 is normal, 700 is bold and 1000 is very bold indeed. It is probably best to set the value to 1 and use normal bold. .SS "\fIset_italic()\fP" .IX Subsection "set_italic()" .Vb 3 \& Default state: Italic is off \& Default action: Turn italic on \& Valid args: 0, 1 .Ve .PP Set the italic property of the font: .PP .Vb 1 \& $format\->set_italic(); # Turn italic on .Ve .SS "\fIset_underline()\fP" .IX Subsection "set_underline()" .Vb 7 \& Default state: Underline is off \& Default action: Turn on single underline \& Valid args: 0 = No underline \& 1 = Single underline \& 2 = Double underline \& 33 = Single accounting underline \& 34 = Double accounting underline .Ve .PP Set the underline property of the font. .PP .Vb 1 \& $format\->set_underline(); # Single underline .Ve .SS "\fIset_font_strikeout()\fP" .IX Subsection "set_font_strikeout()" .Vb 3 \& Default state: Strikeout is off \& Default action: Turn strikeout on \& Valid args: 0, 1 .Ve .PP Set the strikeout property of the font. .SS "\fIset_font_script()\fP" .IX Subsection "set_font_script()" .Vb 5 \& Default state: Super/Subscript is off \& Default action: Turn Superscript on \& Valid args: 0 = Normal \& 1 = Superscript \& 2 = Subscript .Ve .PP Set the superscript/subscript property of the font. This format is currently not very useful. .SS "\fIset_font_outline()\fP" .IX Subsection "set_font_outline()" .Vb 3 \& Default state: Outline is off \& Default action: Turn outline on \& Valid args: 0, 1 .Ve .PP Macintosh only. .SS "\fIset_font_shadow()\fP" .IX Subsection "set_font_shadow()" .Vb 3 \& Default state: Shadow is off \& Default action: Turn shadow on \& Valid args: 0, 1 .Ve .PP Macintosh only. .SS "\fIset_num_format()\fP" .IX Subsection "set_num_format()" .Vb 3 \& Default state: General format \& Default action: Format index 1 \& Valid args: See the following table .Ve .PP This method is used to define the numerical format of a number in Excel. It controls whether a number is displayed as an integer, a floating point number, a date, a currency value or some other user defined format. .PP The numerical format of a cell can be specified by using a format string or an index to one of Excel's built-in formats: .PP .Vb 4 \& my $format1 = $workbook\->add_format(); \& my $format2 = $workbook\->add_format(); \& $format1\->set_num_format(\*(Aqd mmm yyyy\*(Aq); # Format string \& $format2\->set_num_format(0x0f); # Format index \& \& $worksheet\->write(0, 0, 36892.521, $format1); # 1 Jan 2001 \& $worksheet\->write(0, 0, 36892.521, $format2); # 1\-Jan\-01 .Ve .PP Using format strings you can define very sophisticated formatting of numbers. .PP .Vb 2 \& $format01\->set_num_format(\*(Aq0.000\*(Aq); \& $worksheet\->write(0, 0, 3.1415926, $format01); # 3.142 \& \& $format02\->set_num_format(\*(Aq#,##0\*(Aq); \& $worksheet\->write(1, 0, 1234.56, $format02); # 1,235 \& \& $format03\->set_num_format(\*(Aq#,##0.00\*(Aq); \& $worksheet\->write(2, 0, 1234.56, $format03); # 1,234.56 \& \& $format04\->set_num_format(\*(Aq$0.00\*(Aq); \& $worksheet\->write(3, 0, 49.99, $format04); # $49.99 \& \& # Note you can use other currency symbols such as the pound or yen as well. \& # Other currencies may require the use of Unicode. \& \& $format07\->set_num_format(\*(Aqmm/dd/yy\*(Aq); \& $worksheet\->write(6, 0, 36892.521, $format07); # 01/01/01 \& \& $format08\->set_num_format(\*(Aqmmm d yyyy\*(Aq); \& $worksheet\->write(7, 0, 36892.521, $format08); # Jan 1 2001 \& \& $format09\->set_num_format(\*(Aqd mmmm yyyy\*(Aq); \& $worksheet\->write(8, 0, 36892.521, $format09); # 1 January 2001 \& \& $format10\->set_num_format(\*(Aqdd/mm/yyyy hh:mm AM/PM\*(Aq); \& $worksheet\->write(9, 0, 36892.521, $format10); # 01/01/2001 12:30 AM \& \& $format11\->set_num_format(\*(Aq0 "dollar and" .00 "cents"\*(Aq); \& $worksheet\->write(10, 0, 1.87, $format11); # 1 dollar and .87 cents \& \& # Conditional formatting \& $format12\->set_num_format(\*(Aq[Green]General;[Red]\-General;General\*(Aq); \& $worksheet\->write(11, 0, 123, $format12); # > 0 Green \& $worksheet\->write(12, 0, \-45, $format12); # < 0 Red \& $worksheet\->write(13, 0, 0, $format12); # = 0 Default colour \& \& # Zip code \& $format13\->set_num_format(\*(Aq00000\*(Aq); \& $worksheet\->write(14, 0, \*(Aq01209\*(Aq, $format13); .Ve .PP The number system used for dates is described in \*(L"\s-1DATES AND TIME IN EXCEL\*(R"\s0. .PP The colour format should have one of the following values: .PP .Vb 1 \& [Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow] .Ve .PP Alternatively you can specify the colour based on a colour index as follows: \f(CW\*(C`[Color n]\*(C'\fR, where n is a standard Excel colour index \- 7. See the 'Standard colors' worksheet created by formats.pl. .PP For more information refer to the documentation on formatting in the \f(CW\*(C`docs\*(C'\fR directory of the Spreadsheet::WriteExcel distro, the Excel on-line help or <http://office.microsoft.com/en\-gb/assistance/HP051995001033.aspx>. .PP You should ensure that the format string is valid in Excel prior to using it in WriteExcel. .PP Excel's built-in formats are shown in the following table: .PP .Vb 10 \& Index Index Format String \& 0 0x00 General \& 1 0x01 0 \& 2 0x02 0.00 \& 3 0x03 #,##0 \& 4 0x04 #,##0.00 \& 5 0x05 ($#,##0_);($#,##0) \& 6 0x06 ($#,##0_);[Red]($#,##0) \& 7 0x07 ($#,##0.00_);($#,##0.00) \& 8 0x08 ($#,##0.00_);[Red]($#,##0.00) \& 9 0x09 0% \& 10 0x0a 0.00% \& 11 0x0b 0.00E+00 \& 12 0x0c # ?/? \& 13 0x0d # ??/?? \& 14 0x0e m/d/yy \& 15 0x0f d\-mmm\-yy \& 16 0x10 d\-mmm \& 17 0x11 mmm\-yy \& 18 0x12 h:mm AM/PM \& 19 0x13 h:mm:ss AM/PM \& 20 0x14 h:mm \& 21 0x15 h:mm:ss \& 22 0x16 m/d/yy h:mm \& .. .... ........... \& 37 0x25 (#,##0_);(#,##0) \& 38 0x26 (#,##0_);[Red](#,##0) \& 39 0x27 (#,##0.00_);(#,##0.00) \& 40 0x28 (#,##0.00_);[Red](#,##0.00) \& 41 0x29 _(* #,##0_);_(* (#,##0);_(* "\-"_);_(@_) \& 42 0x2a _($* #,##0_);_($* (#,##0);_($* "\-"_);_(@_) \& 43 0x2b _(* #,##0.00_);_(* (#,##0.00);_(* "\-"??_);_(@_) \& 44 0x2c _($* #,##0.00_);_($* (#,##0.00);_($* "\-"??_);_(@_) \& 45 0x2d mm:ss \& 46 0x2e [h]:mm:ss \& 47 0x2f mm:ss.0 \& 48 0x30 ##0.0E+0 \& 49 0x31 @ .Ve .PP For examples of these formatting codes see the 'Numerical formats' worksheet created by formats.pl. See also the number_formats1.html and the number_formats2.html documents in the \f(CW\*(C`docs\*(C'\fR directory of the distro. .PP Note 1. Numeric formats 23 to 36 are not documented by Microsoft and may differ in international versions. .PP Note 2. In Excel 5 the dollar sign appears as a dollar sign. In Excel 97\-2000 it appears as the defined local currency symbol. .PP Note 3. The red negative numeric formats display slightly differently in Excel 5 and Excel 97\-2000. .SS "\fIset_locked()\fP" .IX Subsection "set_locked()" .Vb 3 \& Default state: Cell locking is on \& Default action: Turn locking on \& Valid args: 0, 1 .Ve .PP This property can be used to prevent modification of a cells contents. Following Excel's convention, cell locking is turned on by default. However, it only has an effect if the worksheet has been protected, see the worksheet \f(CW\*(C`protect()\*(C'\fR method. .PP .Vb 2 \& my $locked = $workbook\->add_format(); \& $locked\->set_locked(1); # A non\-op \& \& my $unlocked = $workbook\->add_format(); \& $locked\->set_locked(0); \& \& # Enable worksheet protection \& $worksheet\->protect(); \& \& # This cell cannot be edited. \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq=1+2\*(Aq, $locked); \& \& # This cell can be edited. \& $worksheet\->write(\*(AqA2\*(Aq, \*(Aq=1+2\*(Aq, $unlocked); .Ve .PP Note: This offers weak protection even with a password, see the note in relation to the \f(CW\*(C`protect()\*(C'\fR method. .SS "\fIset_hidden()\fP" .IX Subsection "set_hidden()" .Vb 3 \& Default state: Formula hiding is off \& Default action: Turn hiding on \& Valid args: 0, 1 .Ve .PP This property is used to hide a formula while still displaying its result. This is generally used to hide complex calculations from end users who are only interested in the result. It only has an effect if the worksheet has been protected, see the worksheet \f(CW\*(C`protect()\*(C'\fR method. .PP .Vb 2 \& my $hidden = $workbook\->add_format(); \& $hidden\->set_hidden(); \& \& # Enable worksheet protection \& $worksheet\->protect(); \& \& # The formula in this cell isn\*(Aqt visible \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq=1+2\*(Aq, $hidden); .Ve .PP Note: This offers weak protection even with a password, see the note in relation to the \f(CW\*(C`protect()\*(C'\fR method. .SS "\fIset_align()\fP" .IX Subsection "set_align()" .Vb 8 \& Default state: Alignment is off \& Default action: Left alignment \& Valid args: \*(Aqleft\*(Aq Horizontal \& \*(Aqcenter\*(Aq \& \*(Aqright\*(Aq \& \*(Aqfill\*(Aq \& \*(Aqjustify\*(Aq \& \*(Aqcenter_across\*(Aq \& \& \*(Aqtop\*(Aq Vertical \& \*(Aqvcenter\*(Aq \& \*(Aqbottom\*(Aq \& \*(Aqvjustify\*(Aq .Ve .PP This method is used to set the horizontal and vertical text alignment within a cell. Vertical and horizontal alignments can be combined. The method is used as follows: .PP .Vb 5 \& my $format = $workbook\->add_format(); \& $format\->set_align(\*(Aqcenter\*(Aq); \& $format\->set_align(\*(Aqvcenter\*(Aq); \& $worksheet\->set_row(0, 30); \& $worksheet\->write(0, 0, \*(AqX\*(Aq, $format); .Ve .PP Text can be aligned across two or more adjacent cells using the \f(CW\*(C`center_across\*(C'\fR property. However, for genuine merged cells it is better to use the \f(CW\*(C`merge_range()\*(C'\fR worksheet method. .PP The \f(CW\*(C`vjustify\*(C'\fR (vertical justify) option can be used to provide automatic text wrapping in a cell. The height of the cell will be adjusted to accommodate the wrapped text. To specify where the text wraps use the \f(CW\*(C`set_text_wrap()\*(C'\fR method. .PP For further examples see the 'Alignment' worksheet created by formats.pl. .SS "\fIset_center_across()\fP" .IX Subsection "set_center_across()" .Vb 3 \& Default state: Center across selection is off \& Default action: Turn center across on \& Valid args: 1 .Ve .PP Text can be aligned across two or more adjacent cells using the \f(CW\*(C`set_center_across()\*(C'\fR method. This is an alias for the \f(CW\*(C`set_align(\*(Aqcenter_across\*(Aq)\*(C'\fR method call. .PP Only one cell should contain the text, the other cells should be blank: .PP .Vb 2 \& my $format = $workbook\->add_format(); \& $format\->set_center_across(); \& \& $worksheet\->write(1, 1, \*(AqCenter across selection\*(Aq, $format); \& $worksheet\->write_blank(1, 2, $format); .Ve .PP See also the \f(CW\*(C`merge1.pl\*(C'\fR to \f(CW\*(C`merge6.pl\*(C'\fR programs in the \f(CW\*(C`examples\*(C'\fR directory and the \f(CW\*(C`merge_range()\*(C'\fR method. .SS "\fIset_text_wrap()\fP" .IX Subsection "set_text_wrap()" .Vb 3 \& Default state: Text wrap is off \& Default action: Turn text wrap on \& Valid args: 0, 1 .Ve .PP Here is an example using the text wrap property, the escape character \f(CW\*(C`\en\*(C'\fR is used to indicate the end of line: .PP .Vb 3 \& my $format = $workbook\->add_format(); \& $format\->set_text_wrap(); \& $worksheet\->write(0, 0, "It\*(Aqs\ena bum\enwrap", $format); .Ve .PP Excel will adjust the height of the row to accommodate the wrapped text. A similar effect can be obtained without newlines using the \f(CW\*(C`set_align(\*(Aqvjustify\*(Aq)\*(C'\fR method. See the \f(CW\*(C`textwrap.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory. .SS "\fIset_rotation()\fP" .IX Subsection "set_rotation()" .Vb 3 \& Default state: Text rotation is off \& Default action: None \& Valid args: Integers in the range \-90 to 90 and 270 .Ve .PP Set the rotation of the text in a cell. The rotation can be any angle in the range \-90 to 90 degrees. .PP .Vb 3 \& my $format = $workbook\->add_format(); \& $format\->set_rotation(30); \& $worksheet\->write(0, 0, \*(AqThis text is rotated\*(Aq, $format); .Ve .PP The angle 270 is also supported. This indicates text where the letters run from top to bottom. .SS "\fIset_indent()\fP" .IX Subsection "set_indent()" .Vb 3 \& Default state: Text indentation is off \& Default action: Indent text 1 level \& Valid args: Positive integers .Ve .PP This method can be used to indent text. The argument, which should be an integer, is taken as the level of indentation: .PP .Vb 3 \& my $format = $workbook\->add_format(); \& $format\->set_indent(2); \& $worksheet\->write(0, 0, \*(AqThis text is indented\*(Aq, $format); .Ve .PP Indentation is a horizontal alignment property. It will override any other horizontal properties but it can be used in conjunction with vertical properties. .SS "\fIset_shrink()\fP" .IX Subsection "set_shrink()" .Vb 3 \& Default state: Text shrinking is off \& Default action: Turn "shrink to fit" on \& Valid args: 1 .Ve .PP This method can be used to shrink text so that it fits in a cell. .PP .Vb 3 \& my $format = $workbook\->add_format(); \& $format\->set_shrink(); \& $worksheet\->write(0, 0, \*(AqHoney, I shrunk the text!\*(Aq, $format); .Ve .SS "\fIset_text_justlast()\fP" .IX Subsection "set_text_justlast()" .Vb 3 \& Default state: Justify last is off \& Default action: Turn justify last on \& Valid args: 0, 1 .Ve .PP Only applies to Far Eastern versions of Excel. .SS "\fIset_pattern()\fP" .IX Subsection "set_pattern()" .Vb 3 \& Default state: Pattern is off \& Default action: Solid fill is on \& Valid args: 0 .. 18 .Ve .PP Set the background pattern of a cell. .PP Examples of the available patterns are shown in the 'Patterns' worksheet created by formats.pl. However, it is unlikely that you will ever need anything other than Pattern 1 which is a solid fill of the background color. .SS "\fIset_bg_color()\fP" .IX Subsection "set_bg_color()" .Vb 3 \& Default state: Color is off \& Default action: Solid fill. \& Valid args: See set_color() .Ve .PP The \f(CW\*(C`set_bg_color()\*(C'\fR method can be used to set the background colour of a pattern. Patterns are defined via the \f(CW\*(C`set_pattern()\*(C'\fR method. If a pattern hasn't been defined then a solid fill pattern is used as the default. .PP Here is an example of how to set up a solid fill in a cell: .PP .Vb 1 \& my $format = $workbook\->add_format(); \& \& $format\->set_pattern(); # This is optional when using a solid fill \& \& $format\->set_bg_color(\*(Aqgreen\*(Aq); \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqRay\*(Aq, $format); .Ve .PP For further examples see the 'Patterns' worksheet created by formats.pl. .SS "\fIset_fg_color()\fP" .IX Subsection "set_fg_color()" .Vb 3 \& Default state: Color is off \& Default action: Solid fill. \& Valid args: See set_color() .Ve .PP The \f(CW\*(C`set_fg_color()\*(C'\fR method can be used to set the foreground colour of a pattern. .PP For further examples see the 'Patterns' worksheet created by formats.pl. .SS "\fIset_border()\fP" .IX Subsection "set_border()" .Vb 4 \& Also applies to: set_bottom() \& set_top() \& set_left() \& set_right() \& \& Default state: Border is off \& Default action: Set border type 1 \& Valid args: 0\-13, See below. .Ve .PP A cell border is comprised of a border on the bottom, top, left and right. These can be set to the same value using \f(CW\*(C`set_border()\*(C'\fR or individually using the relevant method calls shown above. .PP The following shows the border styles sorted by Spreadsheet::WriteExcel index number: .PP .Vb 10 \& Index Name Weight Style \& ===== ============= ====== =========== \& 0 None 0 \& 1 Continuous 1 \-\-\-\-\-\-\-\-\-\-\- \& 2 Continuous 2 \-\-\-\-\-\-\-\-\-\-\- \& 3 Dash 1 \- \- \- \- \- \- \& 4 Dot 1 . . . . . . \& 5 Continuous 3 \-\-\-\-\-\-\-\-\-\-\- \& 6 Double 3 =========== \& 7 Continuous 0 \-\-\-\-\-\-\-\-\-\-\- \& 8 Dash 2 \- \- \- \- \- \- \& 9 Dash Dot 1 \- . \- . \- . \& 10 Dash Dot 2 \- . \- . \- . \& 11 Dash Dot Dot 1 \- . . \- . . \& 12 Dash Dot Dot 2 \- . . \- . . \& 13 SlantDash Dot 2 / \- . / \- . .Ve .PP The following shows the borders sorted by style: .PP .Vb 10 \& Name Weight Style Index \& ============= ====== =========== ===== \& Continuous 0 \-\-\-\-\-\-\-\-\-\-\- 7 \& Continuous 1 \-\-\-\-\-\-\-\-\-\-\- 1 \& Continuous 2 \-\-\-\-\-\-\-\-\-\-\- 2 \& Continuous 3 \-\-\-\-\-\-\-\-\-\-\- 5 \& Dash 1 \- \- \- \- \- \- 3 \& Dash 2 \- \- \- \- \- \- 8 \& Dash Dot 1 \- . \- . \- . 9 \& Dash Dot 2 \- . \- . \- . 10 \& Dash Dot Dot 1 \- . . \- . . 11 \& Dash Dot Dot 2 \- . . \- . . 12 \& Dot 1 . . . . . . 4 \& Double 3 =========== 6 \& None 0 0 \& SlantDash Dot 2 / \- . / \- . 13 .Ve .PP The following shows the borders in the order shown in the Excel Dialog. .PP .Vb 9 \& Index Style Index Style \& ===== ===== ===== ===== \& 0 None 12 \- . . \- . . \& 7 \-\-\-\-\-\-\-\-\-\-\- 13 / \- . / \- . \& 4 . . . . . . 10 \- . \- . \- . \& 11 \- . . \- . . 8 \- \- \- \- \- \- \& 9 \- . \- . \- . 2 \-\-\-\-\-\-\-\-\-\-\- \& 3 \- \- \- \- \- \- 5 \-\-\-\-\-\-\-\-\-\-\- \& 1 \-\-\-\-\-\-\-\-\-\-\- 6 =========== .Ve .PP Examples of the available border styles are shown in the 'Borders' worksheet created by formats.pl. .SS "\fIset_border_color()\fP" .IX Subsection "set_border_color()" .Vb 4 \& Also applies to: set_bottom_color() \& set_top_color() \& set_left_color() \& set_right_color() \& \& Default state: Color is off \& Default action: Undefined \& Valid args: See set_color() .Ve .PP Set the colour of the cell borders. A cell border is comprised of a border on the bottom, top, left and right. These can be set to the same colour using \f(CW\*(C`set_border_color()\*(C'\fR or individually using the relevant method calls shown above. Examples of the border styles and colours are shown in the 'Borders' worksheet created by formats.pl. .SS "copy($format)" .IX Subsection "copy($format)" This method is used to copy all of the properties from one Format object to another: .PP .Vb 4 \& my $lorry1 = $workbook\->add_format(); \& $lorry1\->set_bold(); \& $lorry1\->set_italic(); \& $lorry1\->set_color(\*(Aqred\*(Aq); # lorry1 is bold, italic and red \& \& my $lorry2 = $workbook\->add_format(); \& $lorry2\->copy($lorry1); \& $lorry2\->set_color(\*(Aqyellow\*(Aq); # lorry2 is bold, italic and yellow .Ve .PP The \f(CW\*(C`copy()\*(C'\fR method is only useful if you are using the method interface to Format properties. It generally isn't required if you are setting Format properties directly using hashes. .PP Note: this is not a copy constructor, both objects must exist prior to copying. .SH "UNICODE IN EXCEL" .IX Header "UNICODE IN EXCEL" The following is a brief introduction to handling Unicode in \f(CW\*(C`Spreadsheet::WriteExcel\*(C'\fR. .PP \&\fIFor a more general introduction to Unicode handling in Perl see\fR perlunitut and perluniintro. .PP When using Spreadsheet::WriteExcel the best and easiest way to write unicode strings to an Excel file is to use \f(CW\*(C`UTF\-8\*(C'\fR encoded strings and perl 5.8 (or later). Spreadsheet::WriteExcel also allows you to write unicode strings using older perls but it generally requires more work, as explained below. .PP Internally, Excel encodes unicode data as \f(CW\*(C`UTF\-16LE\*(C'\fR (where \s-1LE\s0 means little-endian). If you are using perl 5.8+ then Spreadsheet::WriteExcel will convert \f(CW\*(C`UTF\-8\*(C'\fR strings to \f(CW\*(C`UTF\-16LE\*(C'\fR when required. No further intervention is required from the programmer, for example: .PP .Vb 2 \& # perl 5.8+ example: \& my $smiley = "\ex{263A}"; \& \& $worksheet\->write(\*(AqA1\*(Aq, \*(AqHello world\*(Aq); # ASCII \& $worksheet\->write(\*(AqA2\*(Aq, $smiley); # UTF\-8 .Ve .PP Spreadsheet::WriteExcel also lets you write unicode data as \f(CW\*(C`UTF\-16\*(C'\fR. Since the majority of \s-1CPAN\s0 modules default to \f(CW\*(C`UTF\-16BE\*(C'\fR (big-endian) Spreadsheet::WriteExcel also uses \f(CW\*(C`UTF\-16BE\*(C'\fR and converts it internally to \f(CW\*(C`UTF\-16LE\*(C'\fR: .PP .Vb 2 \& # perl 5.005 example: \& my $smiley = pack \*(Aqn\*(Aq, 0x263A; \& \& $worksheet\->write (\*(AqA3\*(Aq, \*(AqHello world\*(Aq); # ASCII \& $worksheet\->write_utf16be_string(\*(AqA4\*(Aq, $smiley); # UTF\-16 .Ve .PP Although the above examples look similar there is an important difference. With \f(CW\*(C`uft8\*(C'\fR and perl 5.8+ Spreadsheet::WriteExcel treats \f(CW\*(C`UTF\-8\*(C'\fR strings in exactly the same way as any other string. However, with \f(CW\*(C`UTF16\*(C'\fR data we need to distinguish it from other strings either by calling a separate function or by passing an additional flag to indicate the data type. .PP If you are dealing with non-ASCII characters that aren't in \f(CW\*(C`UTF\-8\*(C'\fR then perl 5.8+ provides useful tools in the guise of the \f(CW\*(C`Encode\*(C'\fR module to help you to convert to the required format. For example: .PP .Vb 1 \& use Encode \*(Aqdecode\*(Aq; \& \& my $string = \*(Aqsome string with koi8\-r characters\*(Aq; \& $string = decode(\*(Aqkoi8\-r\*(Aq, $string); # koi8\-r to utf8 .Ve .PP Alternatively you can read data from an encoded file and convert it to \f(CW\*(C`UTF\-8\*(C'\fR as you read it in: .PP .Vb 2 \& my $file = \*(Aqunicode_koi8r.txt\*(Aq; \& open FH, \*(Aq<:encoding(koi8\-r)\*(Aq, $file or die "Couldn\*(Aqt open $file: $!\en"; \& \& my $row = 0; \& while (<FH>) { \& # Data read in is now in utf8 format. \& chomp; \& $worksheet\->write($row++, 0, $_); \& } .Ve .PP These methodologies are explained in more detail in perlunitut, perluniintro and perlunicode. .PP See also the \f(CW\*(C`unicode_*.pl\*(C'\fR programs in the examples directory of the distro. .SH "COLOURS IN EXCEL" .IX Header "COLOURS IN EXCEL" Excel provides a colour palette of 56 colours. In Spreadsheet::WriteExcel these colours are accessed via their palette index in the range 8..63. This index is used to set the colour of fonts, cell patterns and cell borders. For example: .PP .Vb 6 \& my $format = $workbook\->add_format( \& color => 12, # index for blue \& font => \*(AqArial\*(Aq, \& size => 12, \& bold => 1, \& ); .Ve .PP The most commonly used colours can also be accessed by name. The name acts as a simple alias for the colour index: .PP .Vb 10 \& black => 8 \& blue => 12 \& brown => 16 \& cyan => 15 \& gray => 23 \& green => 17 \& lime => 11 \& magenta => 14 \& navy => 18 \& orange => 53 \& pink => 33 \& purple => 20 \& red => 10 \& silver => 22 \& white => 9 \& yellow => 13 .Ve .PP For example: .PP .Vb 1 \& my $font = $workbook\->add_format(color => \*(Aqred\*(Aq); .Ve .PP Users of \s-1VBA\s0 in Excel should note that the equivalent colour indices are in the range 1..56 instead of 8..63. .PP If the default palette does not provide a required colour you can override one of the built-in values. This is achieved by using the \f(CW\*(C`set_custom_color()\*(C'\fR workbook method to adjust the \s-1RGB \s0(red green blue) components of the colour: .PP .Vb 1 \& my $ferrari = $workbook\->set_custom_color(40, 216, 12, 12); \& \& my $format = $workbook\->add_format( \& bg_color => $ferrari, \& pattern => 1, \& border => 1 \& ); \& \& $worksheet\->write_blank(\*(AqA1\*(Aq, $format); .Ve .PP The default Excel colour palette is shown in \f(CW\*(C`palette.html\*(C'\fR in the \f(CW\*(C`docs\*(C'\fR directory of the distro. You can generate an Excel version of the palette using \f(CW\*(C`colors.pl\*(C'\fR in the \f(CW\*(C`examples\*(C'\fR directory. .SH "DATES AND TIME IN EXCEL" .IX Header "DATES AND TIME IN EXCEL" There are two important things to understand about dates and times in Excel: .IP "1 A date/time in Excel is a real number plus an Excel number format." 4 .IX Item "1 A date/time in Excel is a real number plus an Excel number format." .PD 0 .ie n .IP "2 Spreadsheet::WriteExcel doesn't automatically convert date/time strings in ""write()"" to an Excel date/time." 4 .el .IP "2 Spreadsheet::WriteExcel doesn't automatically convert date/time strings in \f(CWwrite()\fR to an Excel date/time." 4 .IX Item "2 Spreadsheet::WriteExcel doesn't automatically convert date/time strings in write() to an Excel date/time." .PD .PP These two points are explained in more detail below along with some suggestions on how to convert times and dates to the required format. .SS "An Excel date/time is a number plus a format" .IX Subsection "An Excel date/time is a number plus a format" If you write a date string with \f(CW\*(C`write()\*(C'\fR then all you will get is a string: .PP .Vb 1 \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq02/03/04\*(Aq); # !! Writes a string not a date. !! .Ve .PP Dates and times in Excel are represented by real numbers, for example \*(L"Jan 1 2001 12:30 \s-1AM\*(R"\s0 is represented by the number 36892.521. .PP The integer part of the number stores the number of days since the epoch and the fractional part stores the percentage of the day. .PP A date or time in Excel is just like any other number. To have the number display as a date you must apply an Excel number format to it. Here are some examples. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqdate_examples.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& $worksheet\->set_column(\*(AqA:A\*(Aq, 30); # For extra visibility. \& \& my $number = 39506.5; \& \& $worksheet\->write(\*(AqA1\*(Aq, $number); # 39506.5 \& \& my $format2 = $workbook\->add_format(num_format => \*(Aqdd/mm/yy\*(Aq); \& $worksheet\->write(\*(AqA2\*(Aq, $number , $format2); # 28/02/08 \& \& my $format3 = $workbook\->add_format(num_format => \*(Aqmm/dd/yy\*(Aq); \& $worksheet\->write(\*(AqA3\*(Aq, $number , $format3); # 02/28/08 \& \& my $format4 = $workbook\->add_format(num_format => \*(Aqd\-m\-yyyy\*(Aq); \& $worksheet\->write(\*(AqA4\*(Aq, $number , $format4); # 28\-2\-2008 \& \& my $format5 = $workbook\->add_format(num_format => \*(Aqdd/mm/yy hh:mm\*(Aq); \& $worksheet\->write(\*(AqA5\*(Aq, $number , $format5); # 28/02/08 12:00 \& \& my $format6 = $workbook\->add_format(num_format => \*(Aqd mmm yyyy\*(Aq); \& $worksheet\->write(\*(AqA6\*(Aq, $number , $format6); # 28 Feb 2008 \& \& my $format7 = $workbook\->add_format(num_format => \*(Aqmmm d yyyy hh:mm AM/PM\*(Aq); \& $worksheet\->write(\*(AqA7\*(Aq, $number , $format7); # Feb 28 2008 12:00 PM .Ve .SS "Spreadsheet::WriteExcel doesn't automatically convert date/time strings" .IX Subsection "Spreadsheet::WriteExcel doesn't automatically convert date/time strings" Spreadsheet::WriteExcel doesn't automatically convert input date strings into Excel's formatted date numbers due to the large number of possible date formats and also due to the possibility of misinterpretation. .PP For example, does \f(CW\*(C`02/03/04\*(C'\fR mean March 2 2004, February 3 2004 or even March 4 2002. .PP Therefore, in order to handle dates you will have to convert them to numbers and apply an Excel format. Some methods for converting dates are listed in the next section. .PP The most direct way is to convert your dates to the \s-1ISO8601 \s0\f(CW\*(C`yyyy\-mm\-ddThh:mm:ss.sss\*(C'\fR date format and use the \f(CW\*(C`write_date_time()\*(C'\fR worksheet method: .PP .Vb 1 \& $worksheet\->write_date_time(\*(AqA2\*(Aq, \*(Aq2001\-01\-01T12:20\*(Aq, $format); .Ve .PP See the \f(CW\*(C`write_date_time()\*(C'\fR section of the documentation for more details. .PP A general methodology for handling date strings with \f(CW\*(C`write_date_time()\*(C'\fR is: .PP .Vb 4 \& 1. Identify incoming date/time strings with a regex. \& 2. Extract the component parts of the date/time using the same regex. \& 3. Convert the date/time to the ISO8601 format. \& 4. Write the date/time using write_date_time() and a number format. .Ve .PP Here is an example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqexample.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Set the default format for dates. \& my $date_format = $workbook\->add_format(num_format => \*(Aqmmm d yyyy\*(Aq); \& \& # Increase column width to improve visibility of data. \& $worksheet\->set_column(\*(AqA:C\*(Aq, 20); \& \& # Simulate reading from a data source. \& my $row = 0; \& \& while (<DATA>) { \& chomp; \& \& my $col = 0; \& my @data = split \*(Aq \*(Aq; \& \& for my $item (@data) { \& \& # Match dates in the following formats: d/m/yy, d/m/yyyy \& if ($item =~ qr[^(\ed{1,2})/(\ed{1,2})/(\ed{4})$]) { \& \& # Change to the date format required by write_date_time(). \& my $date = sprintf "%4d\-%02d\-%02dT", $3, $2, $1; \& \& $worksheet\->write_date_time($row, $col++, $date, $date_format); \& } \& else { \& # Just plain data \& $worksheet\->write($row, $col++, $item); \& } \& } \& $row++; \& } \& \& _\|_DATA_\|_ \& Item Cost Date \& Book 10 1/9/2007 \& Beer 4 12/9/2007 \& Bed 500 5/10/2007 .Ve .PP For a slightly more advanced solution you can modify the \f(CW\*(C`write()\*(C'\fR method to handle date formats of your choice via the \f(CW\*(C`add_write_handler()\*(C'\fR method. See the \f(CW\*(C`add_write_handler()\*(C'\fR section of the docs and the write_handler3.pl and write_handler4.pl programs in the examples directory of the distro. .SS "Converting dates and times to an Excel date or time" .IX Subsection "Converting dates and times to an Excel date or time" The \f(CW\*(C`write_date_time()\*(C'\fR method above is just one way of handling dates and times. .PP The Spreadsheet::WriteExcel::Utility module which is included in the distro has date/time handling functions: .PP .Vb 1 \& use Spreadsheet::WriteExcel::Utility; \& \& $date = xl_date_list(2002, 1, 1); # 37257 \& $date = xl_parse_date("11 July 1997"); # 35622 \& $time = xl_parse_time(\*(Aq3:21:36 PM\*(Aq); # 0.64 \& $date = xl_decode_date_EU("13 May 2002"); # 37389 .Ve .PP Note: some of these functions require additional \s-1CPAN\s0 modules. .PP For date conversions using the \s-1CPAN \s0\f(CW\*(C`DateTime\*(C'\fR framework see DateTime::Format::Excel <http://search.cpan.org/search?dist=DateTime\-Format\-Excel>. .SH "OUTLINES AND GROUPING IN EXCEL" .IX Header "OUTLINES AND GROUPING IN EXCEL" Excel allows you to group rows or columns so that they can be hidden or displayed with a single mouse click. This feature is referred to as outlines. .PP Outlines can reduce complex data down to a few salient sub-totals or summaries. .PP This feature is best viewed in Excel but the following is an \s-1ASCII\s0 representation of what a worksheet with three outlines might look like. Rows 3\-4 and rows 7\-8 are grouped at level 2. Rows 2\-9 are grouped at level 1. The lines at the left hand side are called outline level bars. .PP .Vb 10 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& _ | 1 | A | | | | ... \& | _ | 2 | B | | | | ... \& | | | 3 | (C) | | | | ... \& | | | 4 | (D) | | | | ... \& | \- | 5 | E | | | | ... \& | _ | 6 | F | | | | ... \& | | | 7 | (G) | | | | ... \& | | | 8 | (H) | | | | ... \& | \- | 9 | I | | | | ... \& \- | . | ... | ... | ... | ... | ... .Ve .PP Clicking the minus sign on each of the level 2 outlines will collapse and hide the data as shown in the next figure. The minus sign changes to a plus sign to indicate that the data in the outline is hidden. .PP .Vb 9 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& _ | 1 | A | | | | ... \& | | 2 | B | | | | ... \& | + | 5 | E | | | | ... \& | | 6 | F | | | | ... \& | + | 9 | I | | | | ... \& \- | . | ... | ... | ... | ... | ... .Ve .PP Clicking on the minus sign on the level 1 outline will collapse the remaining rows as follows: .PP .Vb 5 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& 1 2 3 | | A | B | C | D | ... \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& | 1 | A | | | | ... \& + | . | ... | ... | ... | ... | ... .Ve .PP Grouping in \f(CW\*(C`Spreadsheet::WriteExcel\*(C'\fR is achieved by setting the outline level via the \f(CW\*(C`set_row()\*(C'\fR and \f(CW\*(C`set_column()\*(C'\fR worksheet methods: .PP .Vb 2 \& set_row($row, $height, $format, $hidden, $level, $collapsed) \& set_column($first_col, $last_col, $width, $format, $hidden, $level, $collapsed) .Ve .PP The following example sets an outline level of 1 for rows 1 and 2 (zero-indexed) and columns B to G. The parameters \f(CW$height\fR and \f(CW$XF\fR are assigned default values since they are undefined: .PP .Vb 3 \& $worksheet\->set_row(1, undef, undef, 0, 1); \& $worksheet\->set_row(2, undef, undef, 0, 1); \& $worksheet\->set_column(\*(AqB:G\*(Aq, undef, undef, 0, 1); .Ve .PP Excel allows up to 7 outline levels. Therefore the \f(CW$level\fR parameter should be in the range \f(CW\*(C`0 <= $level <= 7\*(C'\fR. .PP Rows and columns can be collapsed by setting the \f(CW$hidden\fR flag for the hidden rows/columns and setting the \f(CW$collapsed\fR flag for the row/column that has the collapsed \f(CW\*(C`+\*(C'\fR symbol: .PP .Vb 3 \& $worksheet\->set_row(1, undef, undef, 1, 1); \& $worksheet\->set_row(2, undef, undef, 1, 1); \& $worksheet\->set_row(3, undef, undef, 0, 0, 1); # Collapsed flag. \& \& $worksheet\->set_column(\*(AqB:G\*(Aq, undef, undef, 1, 1); \& $worksheet\->set_column(\*(AqH:H\*(Aq, undef, undef, 0, 0, 1); # Collapsed flag. .Ve .PP Note: Setting the \f(CW$collapsed\fR flag is particularly important for compatibility with OpenOffice.org and Gnumeric. .PP For a more complete example see the \f(CW\*(C`outline.pl\*(C'\fR and \f(CW\*(C`outline_collapsed.pl\*(C'\fR programs in the examples directory of the distro. .PP Some additional outline properties can be set via the \f(CW\*(C`outline_settings()\*(C'\fR worksheet method, see above. .SH "DATA VALIDATION IN EXCEL" .IX Header "DATA VALIDATION IN EXCEL" Data validation is a feature of Excel which allows you to restrict the data that a users enters in a cell and to display help and warning messages. It also allows you to restrict input to values in a drop down list. .PP A typical use case might be to restrict data in a cell to integer values in a certain range, to provide a help message to indicate the required value and to issue a warning if the input data doesn't meet the stated criteria. In Spreadsheet::WriteExcel we could do that as follows: .PP .Vb 10 \& $worksheet\->data_validation(\*(AqB3\*(Aq, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqInput an integer:\*(Aq, \& input_message => \*(AqBetween 1 and 100\*(Aq, \& error_message => \*(AqSorry, try again.\*(Aq, \& }); .Ve .PP The above example would look like this in Excel: <http://homepage.eircom.net/~jmcnamara/perl/data_validation.jpg>. .PP For more information on data validation see the following Microsoft support article \*(L"Description and examples of data validation in Excel\*(R": <http://support.microsoft.com/kb/211485>. .PP The following sections describe how to use the \f(CW\*(C`data_validation()\*(C'\fR method and its various options. .ie n .SS "data_validation($row, $col, { parameter => 'value', ... })" .el .SS "data_validation($row, \f(CW$col\fP, { parameter => 'value', ... })" .IX Subsection "data_validation($row, $col, { parameter => 'value', ... })" The \f(CW\*(C`data_validation()\*(C'\fR method is used to construct an Excel data validation. .PP It can be applied to a single cell or a range of cells. You can pass 3 parameters such as \f(CW\*(C`($row, $col, {...})\*(C'\fR or 5 parameters such as \f(CW\*(C`($first_row, $first_col, $last_row, $last_col, {...})\*(C'\fR. You can also use \f(CW\*(C`A1\*(C'\fR style notation. For example: .PP .Vb 2 \& $worksheet\->data_validation(0, 0, {...}); \& $worksheet\->data_validation(0, 0, 4, 1, {...}); \& \& # Which are the same as: \& \& $worksheet\->data_validation(\*(AqA1\*(Aq, {...}); \& $worksheet\->data_validation(\*(AqA1:B5\*(Aq, {...}); .Ve .PP See also the note about \*(L"Cell notation\*(R" for more information. .PP The last parameter in \f(CW\*(C`data_validation()\*(C'\fR must be a hash ref containing the parameters that describe the type and style of the data validation. The allowable parameters are: .PP .Vb 6 \& validate \& criteria \& value | minimum | source \& maximum \& ignore_blank \& dropdown \& \& input_title \& input_message \& show_input \& \& error_title \& error_message \& error_type \& show_error .Ve .PP These parameters are explained in the following sections. Most of the parameters are optional, however, you will generally require the three main options \f(CW\*(C`validate\*(C'\fR, \f(CW\*(C`criteria\*(C'\fR and \f(CW\*(C`value\*(C'\fR. .PP .Vb 6 \& $worksheet\->data_validation(\*(AqB3\*(Aq, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 100, \& }); .Ve .PP The \f(CW\*(C`data_validation\*(C'\fR method returns: .PP .Vb 4 \& 0 for success. \& \-1 for insufficient number of arguments. \& \-2 for row or column out of bounds. \& \-3 for incorrect parameter or value. .Ve .SS "validate" .IX Subsection "validate" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`validate\*(C'\fR parameter is used to set the type of data that you wish to validate. It is always required and it has no default value. Allowable values are: .PP .Vb 8 \& any \& integer \& decimal \& list \& date \& time \& length \& custom .Ve .IP "\(bu" 4 \&\fBany\fR is used to specify that the type of data is unrestricted. This is the same as not applying a data validation. It is only provided for completeness and isn't used very often in the context of Spreadsheet::WriteExcel. .IP "\(bu" 4 \&\fBinteger\fR restricts the cell to integer values. Excel refers to this as 'whole number'. .Sp .Vb 3 \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 100, .Ve .IP "\(bu" 4 \&\fBdecimal\fR restricts the cell to decimal values. .Sp .Vb 3 \& validate => \*(Aqdecimal\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 38.6, .Ve .IP "\(bu" 4 \&\fBlist\fR restricts the cell to a set of user specified values. These can be passed in an array ref or as a cell range (named ranges aren't currently supported): .Sp .Vb 4 \& validate => \*(Aqlist\*(Aq, \& value => [\*(Aqopen\*(Aq, \*(Aqhigh\*(Aq, \*(Aqclose\*(Aq], \& # Or like this: \& value => \*(AqB1:B3\*(Aq, .Ve .Sp Excel requires that range references are only to cells on the same worksheet. .IP "\(bu" 4 \&\fBdate\fR restricts the cell to date values. Dates in Excel are expressed as integer values but you can also pass an \s-1ISO860\s0 style string as used in \f(CW\*(C`write_date_time()\*(C'\fR. See also \*(L"\s-1DATES AND TIME IN EXCEL\*(R"\s0 for more information about working with Excel's dates. .Sp .Vb 5 \& validate => \*(Aqdate\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 39653, # 24 July 2008 \& # Or like this: \& value => \*(Aq2008\-07\-24T\*(Aq, .Ve .IP "\(bu" 4 \&\fBtime\fR restricts the cell to time values. Times in Excel are expressed as decimal values but you can also pass an \s-1ISO860\s0 style string as used in \f(CW\*(C`write_date_time()\*(C'\fR. See also \*(L"\s-1DATES AND TIME IN EXCEL\*(R"\s0 for more information about working with Excel's times. .Sp .Vb 5 \& validate => \*(Aqtime\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 0.5, # Noon \& # Or like this: \& value => \*(AqT12:00:00\*(Aq, .Ve .IP "\(bu" 4 \&\fBlength\fR restricts the cell data based on an integer string length. Excel refers to this as 'Text length'. .Sp .Vb 3 \& validate => \*(Aqlength\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 10, .Ve .IP "\(bu" 4 \&\fBcustom\fR restricts the cell based on an external Excel formula that returns a \f(CW\*(C`TRUE/FALSE\*(C'\fR value. .Sp .Vb 2 \& validate => \*(Aqcustom\*(Aq, \& value => \*(Aq=IF(A10>B10,TRUE,FALSE)\*(Aq, .Ve .SS "criteria" .IX Subsection "criteria" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`criteria\*(C'\fR parameter is used to set the criteria by which the data in the cell is validated. It is almost always required except for the \f(CW\*(C`list\*(C'\fR and \f(CW\*(C`custom\*(C'\fR validate options. It has no default value. Allowable values are: .PP .Vb 8 \& \*(Aqbetween\*(Aq \& \*(Aqnot between\*(Aq \& \*(Aqequal to\*(Aq | \*(Aq==\*(Aq | \*(Aq=\*(Aq \& \*(Aqnot equal to\*(Aq | \*(Aq!=\*(Aq | \*(Aq<>\*(Aq \& \*(Aqgreater than\*(Aq | \*(Aq>\*(Aq \& \*(Aqless than\*(Aq | \*(Aq<\*(Aq \& \*(Aqgreater than or equal to\*(Aq | \*(Aq>=\*(Aq \& \*(Aqless than or equal to\*(Aq | \*(Aq<=\*(Aq .Ve .PP You can either use Excel's textual description strings, in the first column above, or the more common operator alternatives. The following are equivalent: .PP .Vb 3 \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqgreater than\*(Aq, \& value => 100, \& \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 100, .Ve .PP The \f(CW\*(C`list\*(C'\fR and \f(CW\*(C`custom\*(C'\fR validate options don't require a \f(CW\*(C`criteria\*(C'\fR. If you specify one it will be ignored. .PP .Vb 2 \& validate => \*(Aqlist\*(Aq, \& value => [\*(Aqopen\*(Aq, \*(Aqhigh\*(Aq, \*(Aqclose\*(Aq], \& \& validate => \*(Aqcustom\*(Aq, \& value => \*(Aq=IF(A10>B10,TRUE,FALSE)\*(Aq, .Ve .SS "value | minimum | source" .IX Subsection "value | minimum | source" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`value\*(C'\fR parameter is used to set the limiting value to which the \f(CW\*(C`criteria\*(C'\fR is applied. It is always required and it has no default value. You can also use the synonyms \f(CW\*(C`minimum\*(C'\fR or \f(CW\*(C`source\*(C'\fR to make the validation a little clearer and closer to Excel's description of the parameter: .PP .Vb 4 \& # Use \*(Aqvalue\*(Aq \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 100, \& \& # Use \*(Aqminimum\*(Aq \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& \& # Use \*(Aqsource\*(Aq \& validate => \*(Aqlist\*(Aq, \& source => \*(Aq$B$1:$B$3\*(Aq, .Ve .SS "maximum" .IX Subsection "maximum" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`maximum\*(C'\fR parameter is used to set the upper limiting value when the \f(CW\*(C`criteria\*(C'\fR is either \f(CW\*(Aqbetween\*(Aq\fR or \f(CW\*(Aqnot between\*(Aq\fR: .PP .Vb 4 \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, .Ve .SS "ignore_blank" .IX Subsection "ignore_blank" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`ignore_blank\*(C'\fR parameter is used to toggle on and off the 'Ignore blank' option in the Excel data validation dialog. When the option is on the data validation is not applied to blank data in the cell. It is on by default. .PP .Vb 1 \& ignore_blank => 0, # Turn the option off .Ve .SS "dropdown" .IX Subsection "dropdown" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`dropdown\*(C'\fR parameter is used to toggle on and off the 'In\-cell dropdown' option in the Excel data validation dialog. When the option is on a dropdown list will be shown for \f(CW\*(C`list\*(C'\fR validations. It is on by default. .PP .Vb 1 \& dropdown => 0, # Turn the option off .Ve .SS "input_title" .IX Subsection "input_title" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`input_title\*(C'\fR parameter is used to set the title of the input message that is displayed when a cell is entered. It has no default value and is only displayed if the input message is displayed. See the \f(CW\*(C`input_message\*(C'\fR parameter below. .PP .Vb 1 \& input_title => \*(AqThis is the input title\*(Aq, .Ve .PP The maximum title length is 32 characters. \s-1UTF8\s0 strings are handled automatically in perl 5.8 and later. .SS "input_message" .IX Subsection "input_message" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`input_message\*(C'\fR parameter is used to set the input message that is displayed when a cell is entered. It has no default value. .PP .Vb 6 \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqEnter the applied discount:\*(Aq, \& input_message => \*(Aqbetween 1 and 100\*(Aq, .Ve .PP The message can be split over several lines using newlines, \f(CW"\en"\fR in double quoted strings. .PP .Vb 1 \& input_message => "This is\ena test.", .Ve .PP The maximum message length is 255 characters. \s-1UTF8\s0 strings are handled automatically in perl 5.8 and later. .SS "show_input" .IX Subsection "show_input" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`show_input\*(C'\fR parameter is used to toggle on and off the 'Show input message when cell is selected' option in the Excel data validation dialog. When the option is off an input message is not displayed even if it has been set using \f(CW\*(C`input_message\*(C'\fR. It is on by default. .PP .Vb 1 \& show_input => 0, # Turn the option off .Ve .SS "error_title" .IX Subsection "error_title" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`error_title\*(C'\fR parameter is used to set the title of the error message that is displayed when the data validation criteria is not met. The default error title is 'Microsoft Excel'. .PP .Vb 1 \& error_title => \*(AqInput value is not valid\*(Aq, .Ve .PP The maximum title length is 32 characters. \s-1UTF8\s0 strings are handled automatically in perl 5.8 and later. .SS "error_message" .IX Subsection "error_message" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`error_message\*(C'\fR parameter is used to set the error message that is displayed when a cell is entered. The default error message is \*(L"The value you entered is not valid.\enA user has restricted values that can be entered into the cell.\*(R". .PP .Vb 6 \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& error_title => \*(AqInput value is not valid\*(Aq, \& error_message => \*(AqIt should be an integer between 1 and 100\*(Aq, .Ve .PP The message can be split over several lines using newlines, \f(CW"\en"\fR in double quoted strings. .PP .Vb 1 \& input_message => "This is\ena test.", .Ve .PP The maximum message length is 255 characters. \s-1UTF8\s0 strings are handled automatically in perl 5.8 and later. .SS "error_type" .IX Subsection "error_type" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`error_type\*(C'\fR parameter is used to specify the type of error dialog that is displayed. There are 3 options: .PP .Vb 3 \& \*(Aqstop\*(Aq \& \*(Aqwarning\*(Aq \& \*(Aqinformation\*(Aq .Ve .PP The default is \f(CW\*(Aqstop\*(Aq\fR. .SS "show_error" .IX Subsection "show_error" This parameter is passed in a hash ref to \f(CW\*(C`data_validation()\*(C'\fR. .PP The \f(CW\*(C`show_error\*(C'\fR parameter is used to toggle on and off the 'Show error alert after invalid data is entered' option in the Excel data validation dialog. When the option is off an error message is not displayed even if it has been set using \f(CW\*(C`error_message\*(C'\fR. It is on by default. .PP .Vb 1 \& show_error => 0, # Turn the option off .Ve .SS "Data Validation Examples" .IX Subsection "Data Validation Examples" Example 1. Limiting input to an integer greater than a fixed value. .PP .Vb 6 \& $worksheet\->data_validation(\*(AqA1\*(Aq, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => 0, \& }); .Ve .PP Example 2. Limiting input to an integer greater than a fixed value where the value is referenced from a cell. .PP .Vb 6 \& $worksheet\->data_validation(\*(AqA2\*(Aq, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aq>\*(Aq, \& value => \*(Aq=E3\*(Aq, \& }); .Ve .PP Example 3. Limiting input to a decimal in a fixed range. .PP .Vb 7 \& $worksheet\->data_validation(\*(AqA3\*(Aq, \& { \& validate => \*(Aqdecimal\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 0.1, \& maximum => 0.5, \& }); .Ve .PP Example 4. Limiting input to a value in a dropdown list. .PP .Vb 5 \& $worksheet\->data_validation(\*(AqA4\*(Aq, \& { \& validate => \*(Aqlist\*(Aq, \& source => [\*(Aqopen\*(Aq, \*(Aqhigh\*(Aq, \*(Aqclose\*(Aq], \& }); .Ve .PP Example 5. Limiting input to a value in a dropdown list where the list is specified as a cell range. .PP .Vb 5 \& $worksheet\->data_validation(\*(AqA5\*(Aq, \& { \& validate => \*(Aqlist\*(Aq, \& source => \*(Aq=E4:G4\*(Aq, \& }); .Ve .PP Example 6. Limiting input to a date in a fixed range. .PP .Vb 7 \& $worksheet\->data_validation(\*(AqA6\*(Aq, \& { \& validate => \*(Aqdate\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => \*(Aq2008\-01\-01T\*(Aq, \& maximum => \*(Aq2008\-12\-12T\*(Aq, \& }); .Ve .PP Example 7. Displaying a message when the cell is selected. .PP .Vb 9 \& $worksheet\->data_validation(\*(AqA7\*(Aq, \& { \& validate => \*(Aqinteger\*(Aq, \& criteria => \*(Aqbetween\*(Aq, \& minimum => 1, \& maximum => 100, \& input_title => \*(AqEnter an integer:\*(Aq, \& input_message => \*(Aqbetween 1 and 100\*(Aq, \& }); .Ve .PP See also the \f(CW\*(C`data_validate.pl\*(C'\fR program in the examples directory of the distro. .SH "ROW HEIGHTS AND WORKSHEET OBJECTS" .IX Header "ROW HEIGHTS AND WORKSHEET OBJECTS" The following relates to worksheet objects such as images, comments and charts. .PP If you specify the height of a row that contains a worksheet object then Spreadsheet::WriteExcel will adjust the height of the object to maintain its default or user specified dimensions. In this way the object won't appear stretched or compressed in Excel. .PP However, Excel can also adjust the height of a row automatically if it contains cells that have the text wrap property set or contain large fonts. In these cases the height of the row is unknown to Spreadsheet::WriteExcel at execution time and the scaling calculations it performs are incorrect. The effect of this is that the object is stretched with the row when it is displayed in Excel. .PP In order to avoid this issue you should use the \f(CW\*(C`set_row()\*(C'\fR method to explicitly specify the height of any row that may otherwise be changed automatically by Excel. .SH "FORMULAS AND FUNCTIONS IN EXCEL" .IX Header "FORMULAS AND FUNCTIONS IN EXCEL" .SS "Caveats" .IX Subsection "Caveats" The first thing to note is that there are still some outstanding issues with the implementation of formulas and functions: .PP .Vb 6 \& 1. Writing a formula is much slower than writing the equivalent string. \& 2. You cannot use array constants, i.e. {1;2;3}, in functions. \& 3. Unary minus isn\*(Aqt supported. \& 4. Whitespace is not preserved around operators. \& 5. Named ranges are not supported. \& 6. Array formulas are not supported. .Ve .PP However, these constraints will be removed in future versions. They are here because of a trade-off between features and time. Also, it is possible to work around issue 1 using the \f(CW\*(C`store_formula()\*(C'\fR and \f(CW\*(C`repeat_formula()\*(C'\fR methods as described later in this section. .SS "Introduction" .IX Subsection "Introduction" The following is a brief introduction to formulas and functions in Excel and Spreadsheet::WriteExcel. .PP A formula is a string that begins with an equals sign: .PP .Vb 2 \& \*(Aq=A1+B1\*(Aq \& \*(Aq=AVERAGE(1, 2, 3)\*(Aq .Ve .PP The formula can contain numbers, strings, boolean values, cell references, cell ranges and functions. Named ranges are not supported. Formulas should be written as they appear in Excel, that is cells and functions must be in uppercase. .PP Cells in Excel are referenced using the A1 notation system where the column is designated by a letter and the row by a number. Columns range from A to \s-1IV\s0 i.e. 0 to 255, rows range from 1 to 65536. The \f(CW\*(C`Spreadsheet::WriteExcel::Utility\*(C'\fR module that is included in the distro contains helper functions for dealing with A1 notation, for example: .PP .Vb 1 \& use Spreadsheet::WriteExcel::Utility; \& \& ($row, $col) = xl_cell_to_rowcol(\*(AqC2\*(Aq); # (1, 2) \& $str = xl_rowcol_to_cell(1, 2); # C2 .Ve .PP The Excel \f(CW\*(C`$\*(C'\fR notation in cell references is also supported. This allows you to specify whether a row or column is relative or absolute. This only has an effect if the cell is copied. The following examples show relative and absolute values. .PP .Vb 4 \& \*(Aq=A1\*(Aq # Column and row are relative \& \*(Aq=$A1\*(Aq # Column is absolute and row is relative \& \*(Aq=A$1\*(Aq # Column is relative and row is absolute \& \*(Aq=$A$1\*(Aq # Column and row are absolute .Ve .PP Formulas can also refer to cells in other worksheets of the current workbook. For example: .PP .Vb 6 \& \*(Aq=Sheet2!A1\*(Aq \& \*(Aq=Sheet2!A1:A5\*(Aq \& \*(Aq=Sheet2:Sheet3!A1\*(Aq \& \*(Aq=Sheet2:Sheet3!A1:A5\*(Aq \& q{=\*(AqTest Data\*(Aq!A1} \& q{=\*(AqTest Data1:Test Data2\*(Aq!A1} .Ve .PP The sheet reference and the cell reference are separated by \f(CW\*(C`!\*(C'\fR the exclamation mark symbol. If worksheet names contain spaces, commas or parentheses then Excel requires that the name is enclosed in single quotes as shown in the last two examples above. In order to avoid using a lot of escape characters you can use the quote operator \f(CW\*(C`q{}\*(C'\fR to protect the quotes. See \f(CW\*(C`perlop\*(C'\fR in the main Perl documentation. Only valid sheet names that have been added using the \f(CW\*(C`add_worksheet()\*(C'\fR method can be used in formulas. You cannot reference external workbooks. .PP The following table lists the operators that are available in Excel's formulas. The majority of the operators are the same as Perl's, differences are indicated: .PP .Vb 10 \& Arithmetic operators: \& ===================== \& Operator Meaning Example \& + Addition 1+2 \& \- Subtraction 2\-1 \& * Multiplication 2*3 \& / Division 1/4 \& ^ Exponentiation 2^3 # Equivalent to ** \& \- Unary minus \-(1+2) # Not yet supported \& % Percent (Not modulus) 13% # Not supported, [1] \& \& \& Comparison operators: \& ===================== \& Operator Meaning Example \& = Equal to A1 = B1 # Equivalent to == \& <> Not equal to A1 <> B1 # Equivalent to != \& > Greater than A1 > B1 \& < Less than A1 < B1 \& >= Greater than or equal to A1 >= B1 \& <= Less than or equal to A1 <= B1 \& \& \& String operator: \& ================ \& Operator Meaning Example \& & Concatenation "Hello " & "World!" # [2] \& \& \& Reference operators: \& ==================== \& Operator Meaning Example \& : Range operator A1:A4 # [3] \& , Union operator SUM(1, 2+2, B3) # [4] \& \& \& Notes: \& [1]: You can get a percentage with formatting and modulus with MOD(). \& [2]: Equivalent to ("Hello " . "World!") in Perl. \& [3]: This range is equivalent to cells A1, A2, A3 and A4. \& [4]: The comma behaves like the list separator in Perl. .Ve .PP The range and comma operators can have different symbols in non-English versions of Excel. These will be supported in a later version of Spreadsheet::WriteExcel. European users of Excel take note: .PP .Vb 2 \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq=SUM(1; 2; 3)\*(Aq); # Wrong!! \& $worksheet\->write(\*(AqA1\*(Aq, \*(Aq=SUM(1, 2, 3)\*(Aq); # Okay .Ve .PP The following table lists all of the core functions supported by Excel 5 and Spreadsheet::WriteExcel. Any additional functions that are available through the \*(L"Analysis ToolPak\*(R" or other add-ins are not supported. These functions have all been tested to verify that they work. .PP .Vb 10 \& ABS DB INDIRECT NORMINV SLN \& ACOS DCOUNT INFO NORMSDIST SLOPE \& ACOSH DCOUNTA INT NORMSINV SMALL \& ADDRESS DDB INTERCEPT NOT SQRT \& AND DEGREES IPMT NOW STANDARDIZE \& AREAS DEVSQ IRR NPER STDEV \& ASIN DGET ISBLANK NPV STDEVP \& ASINH DMAX ISERR ODD STEYX \& ATAN DMIN ISERROR OFFSET SUBSTITUTE \& ATAN2 DOLLAR ISLOGICAL OR SUBTOTAL \& ATANH DPRODUCT ISNA PEARSON SUM \& AVEDEV DSTDEV ISNONTEXT PERCENTILE SUMIF \& AVERAGE DSTDEVP ISNUMBER PERCENTRANK SUMPRODUCT \& BETADIST DSUM ISREF PERMUT SUMSQ \& BETAINV DVAR ISTEXT PI SUMX2MY2 \& BINOMDIST DVARP KURT PMT SUMX2PY2 \& CALL ERROR.TYPE LARGE POISSON SUMXMY2 \& CEILING EVEN LEFT POWER SYD \& CELL EXACT LEN PPMT T \& CHAR EXP LINEST PROB TAN \& CHIDIST EXPONDIST LN PRODUCT TANH \& CHIINV FACT LOG PROPER TDIST \& CHITEST FALSE LOG10 PV TEXT \& CHOOSE FDIST LOGEST QUARTILE TIME \& CLEAN FIND LOGINV RADIANS TIMEVALUE \& CODE FINV LOGNORMDIST RAND TINV \& COLUMN FISHER LOOKUP RANK TODAY \& COLUMNS FISHERINV LOWER RATE TRANSPOSE \& COMBIN FIXED MATCH REGISTER.ID TREND \& CONCATENATE FLOOR MAX REPLACE TRIM \& CONFIDENCE FORECAST MDETERM REPT TRIMMEAN \& CORREL FREQUENCY MEDIAN RIGHT TRUE \& COS FTEST MID ROMAN TRUNC \& COSH FV MIN ROUND TTEST \& COUNT GAMMADIST MINUTE ROUNDDOWN TYPE \& COUNTA GAMMAINV MINVERSE ROUNDUP UPPER \& COUNTBLANK GAMMALN MIRR ROW VALUE \& COUNTIF GEOMEAN MMULT ROWS VAR \& COVAR GROWTH MOD RSQ VARP \& CRITBINOM HARMEAN MODE SEARCH VDB \& DATE HLOOKUP MONTH SECOND VLOOKUP \& DATEVALUE HOUR N SIGN WEEKDAY \& DAVERAGE HYPGEOMDIST NA SIN WEIBULL \& DAY IF NEGBINOMDIST SINH YEAR \& DAYS360 INDEX NORMDIST SKEW ZTEST .Ve .PP You can also modify the module to support function names in the following languages: German, French, Spanish, Portuguese, Dutch, Finnish, Italian and Swedish. See the \f(CW\*(C`function_locale.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .PP For a general introduction to Excel's formulas and an explanation of the syntax of the function refer to the Excel help files or the following: <http://office.microsoft.com/en\-us/assistance/CH062528031033.aspx>. .PP If your formula doesn't work in Spreadsheet::WriteExcel try the following: .PP .Vb 6 \& 1. Verify that the formula works in Excel (or Gnumeric or OpenOffice.org). \& 2. Ensure that it isn\*(Aqt on the Caveats list shown above. \& 3. Ensure that cell references and formula names are in uppercase. \& 4. Ensure that you are using \*(Aq:\*(Aq as the range operator, A1:A4. \& 5. Ensure that you are using \*(Aq,\*(Aq as the union operator, SUM(1,2,3). \& 6. Ensure that the function is in the above table. .Ve .PP If you go through steps 1\-6 and you still have a problem, mail me. .SS "Improving performance when working with formulas" .IX Subsection "Improving performance when working with formulas" Writing a large number of formulas with Spreadsheet::WriteExcel can be slow. This is due to the fact that each formula has to be parsed and with the current implementation this is computationally expensive. .PP However, in a lot of cases the formulas that you write will be quite similar, for example: .PP .Vb 6 \& $worksheet\->write_formula(\*(AqB1\*(Aq, \*(Aq=A1 * 3 + 50\*(Aq, $format); \& $worksheet\->write_formula(\*(AqB2\*(Aq, \*(Aq=A2 * 3 + 50\*(Aq, $format); \& ... \& ... \& $worksheet\->write_formula(\*(AqB99\*(Aq, \*(Aq=A999 * 3 + 50\*(Aq, $format); \& $worksheet\->write_formula(\*(AqB1000\*(Aq, \*(Aq=A1000 * 3 + 50\*(Aq, $format); .Ve .PP In this example the cell reference changes in iterations from \f(CW\*(C`A1\*(C'\fR to \f(CW\*(C`A1000\*(C'\fR. The parser treats this variable as a \fItoken\fR and arranges it according to predefined rules. However, since the parser is oblivious to the value of the token, it is essentially performing the same calculation 1000 times. This is inefficient. .PP The way to avoid this inefficiency and thereby speed up the writing of formulas is to parse the formula once and then repeatedly substitute similar tokens. .PP A formula can be parsed and stored via the \f(CW\*(C`store_formula()\*(C'\fR worksheet method. You can then use the \f(CW\*(C`repeat_formula()\*(C'\fR method to substitute \f(CW$pattern\fR, \f(CW$replace\fR pairs in the stored formula: .PP .Vb 1 \& my $formula = $worksheet\->store_formula(\*(Aq=A1 * 3 + 50\*(Aq); \& \& for my $row (0..999) { \& $worksheet\->repeat_formula($row, 1, $formula, $format, \*(AqA1\*(Aq, \*(AqA\*(Aq.($row +1)); \& } .Ve .PP On an arbitrary test machine this method was 10 times faster than the brute force method shown above. .PP For more information about how Spreadsheet::WriteExcel parses and stores formulas see the \f(CW\*(C`Spreadsheet::WriteExcel::Formula\*(C'\fR man page. .PP It should be noted however that the overall speed of direct formula parsing will be improved in a future version. .SH "EXAMPLES" .IX Header "EXAMPLES" See Spreadsheet::WriteExcel::Examples for a full list of examples. .SS "Example 1" .IX Subsection "Example 1" The following example shows some of the basic features of Spreadsheet::WriteExcel. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook called simple.xls and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqsimple.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& # The general syntax is write($row, $column, $token). Note that row and \& # column are zero indexed \& \& # Write some text \& $worksheet\->write(0, 0, \*(AqHi Excel!\*(Aq); \& \& \& # Write some numbers \& $worksheet\->write(2, 0, 3); # Writes 3 \& $worksheet\->write(3, 0, 3.00000); # Writes 3 \& $worksheet\->write(4, 0, 3.00001); # Writes 3.00001 \& $worksheet\->write(5, 0, 3.14159); # TeX revision no.? \& \& \& # Write some formulas \& $worksheet\->write(7, 0, \*(Aq=A3 + A6\*(Aq); \& $worksheet\->write(8, 0, \*(Aq=IF(A5>3,"Yes", "No")\*(Aq); \& \& \& # Write a hyperlink \& $worksheet\->write(10, 0, \*(Aqhttp://www.perl.com/\*(Aq); .Ve .SS "Example 2" .IX Subsection "Example 2" The following is a general example which demonstrates some features of working with multiple worksheets. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new Excel workbook \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqregions.xls\*(Aq); \& \& # Add some worksheets \& my $north = $workbook\->add_worksheet(\*(AqNorth\*(Aq); \& my $south = $workbook\->add_worksheet(\*(AqSouth\*(Aq); \& my $east = $workbook\->add_worksheet(\*(AqEast\*(Aq); \& my $west = $workbook\->add_worksheet(\*(AqWest\*(Aq); \& \& # Add a Format \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& $format\->set_color(\*(Aqblue\*(Aq); \& \& # Add a caption to each worksheet \& foreach my $worksheet ($workbook\->sheets()) { \& $worksheet\->write(0, 0, \*(AqSales\*(Aq, $format); \& } \& \& # Write some data \& $north\->write(0, 1, 200000); \& $south\->write(0, 1, 100000); \& $east\->write (0, 1, 150000); \& $west\->write (0, 1, 100000); \& \& # Set the active worksheet \& $south\->activate(); \& \& # Set the width of the first column \& $south\->set_column(0, 0, 20); \& \& # Set the active cell \& $south\->set_selection(0, 1); .Ve .SS "Example 3" .IX Subsection "Example 3" This example shows how to use a conditional numerical format with colours to indicate if a share price has gone up or down. .PP .Vb 2 \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqstocks.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Set the column width for columns 1, 2, 3 and 4 \& $worksheet\->set_column(0, 3, 15); \& \& \& # Create a format for the column headings \& my $header = $workbook\->add_format(); \& $header\->set_bold(); \& $header\->set_size(12); \& $header\->set_color(\*(Aqblue\*(Aq); \& \& \& # Create a format for the stock price \& my $f_price = $workbook\->add_format(); \& $f_price\->set_align(\*(Aqleft\*(Aq); \& $f_price\->set_num_format(\*(Aq$0.00\*(Aq); \& \& \& # Create a format for the stock volume \& my $f_volume = $workbook\->add_format(); \& $f_volume\->set_align(\*(Aqleft\*(Aq); \& $f_volume\->set_num_format(\*(Aq#,##0\*(Aq); \& \& \& # Create a format for the price change. This is an example of a \& # conditional format. The number is formatted as a percentage. If it is \& # positive it is formatted in green, if it is negative it is formatted \& # in red and if it is zero it is formatted as the default font colour \& # (in this case black). Note: the [Green] format produces an unappealing \& # lime green. Try [Color 10] instead for a dark green. \& # \& my $f_change = $workbook\->add_format(); \& $f_change\->set_align(\*(Aqleft\*(Aq); \& $f_change\->set_num_format(\*(Aq[Green]0.0%;[Red]\-0.0%;0.0%\*(Aq); \& \& \& # Write out the data \& $worksheet\->write(0, 0, \*(AqCompany\*(Aq,$header); \& $worksheet\->write(0, 1, \*(AqPrice\*(Aq, $header); \& $worksheet\->write(0, 2, \*(AqVolume\*(Aq, $header); \& $worksheet\->write(0, 3, \*(AqChange\*(Aq, $header); \& \& $worksheet\->write(1, 0, \*(AqDamage Inc.\*(Aq ); \& $worksheet\->write(1, 1, 30.25, $f_price ); # $30.25 \& $worksheet\->write(1, 2, 1234567, $f_volume); # 1,234,567 \& $worksheet\->write(1, 3, 0.085, $f_change); # 8.5% in green \& \& $worksheet\->write(2, 0, \*(AqDump Corp.\*(Aq ); \& $worksheet\->write(2, 1, 1.56, $f_price ); # $1.56 \& $worksheet\->write(2, 2, 7564, $f_volume); # 7,564 \& $worksheet\->write(2, 3, \-0.015, $f_change); # \-1.5% in red \& \& $worksheet\->write(3, 0, \*(AqRev Ltd.\*(Aq ); \& $worksheet\->write(3, 1, 0.13, $f_price ); # $0.13 \& $worksheet\->write(3, 2, 321, $f_volume); # 321 \& $worksheet\->write(3, 3, 0, $f_change); # 0 in the font color (black) .Ve .SS "Example 4" .IX Subsection "Example 4" The following is a simple example of using functions. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& # Create a new workbook and add a worksheet \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqstats.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(\*(AqTest data\*(Aq); \& \& # Set the column width for columns 1 \& $worksheet\->set_column(0, 0, 20); \& \& \& # Create a format for the headings \& my $format = $workbook\->add_format(); \& $format\->set_bold(); \& \& \& # Write the sample data \& $worksheet\->write(0, 0, \*(AqSample\*(Aq, $format); \& $worksheet\->write(0, 1, 1); \& $worksheet\->write(0, 2, 2); \& $worksheet\->write(0, 3, 3); \& $worksheet\->write(0, 4, 4); \& $worksheet\->write(0, 5, 5); \& $worksheet\->write(0, 6, 6); \& $worksheet\->write(0, 7, 7); \& $worksheet\->write(0, 8, 8); \& \& $worksheet\->write(1, 0, \*(AqLength\*(Aq, $format); \& $worksheet\->write(1, 1, 25.4); \& $worksheet\->write(1, 2, 25.4); \& $worksheet\->write(1, 3, 24.8); \& $worksheet\->write(1, 4, 25.0); \& $worksheet\->write(1, 5, 25.3); \& $worksheet\->write(1, 6, 24.9); \& $worksheet\->write(1, 7, 25.2); \& $worksheet\->write(1, 8, 24.8); \& \& # Write some statistical functions \& $worksheet\->write(4, 0, \*(AqCount\*(Aq, $format); \& $worksheet\->write(4, 1, \*(Aq=COUNT(B1:I1)\*(Aq); \& \& $worksheet\->write(5, 0, \*(AqSum\*(Aq, $format); \& $worksheet\->write(5, 1, \*(Aq=SUM(B2:I2)\*(Aq); \& \& $worksheet\->write(6, 0, \*(AqAverage\*(Aq, $format); \& $worksheet\->write(6, 1, \*(Aq=AVERAGE(B2:I2)\*(Aq); \& \& $worksheet\->write(7, 0, \*(AqMin\*(Aq, $format); \& $worksheet\->write(7, 1, \*(Aq=MIN(B2:I2)\*(Aq); \& \& $worksheet\->write(8, 0, \*(AqMax\*(Aq, $format); \& $worksheet\->write(8, 1, \*(Aq=MAX(B2:I2)\*(Aq); \& \& $worksheet\->write(9, 0, \*(AqStandard Deviation\*(Aq, $format); \& $worksheet\->write(9, 1, \*(Aq=STDEV(B2:I2)\*(Aq); \& \& $worksheet\->write(10, 0, \*(AqKurtosis\*(Aq, $format); \& $worksheet\->write(10, 1, \*(Aq=KURT(B2:I2)\*(Aq); .Ve .SS "Example 5" .IX Subsection "Example 5" The following example converts a tab separated file called \f(CW\*(C`tab.txt\*(C'\fR into an Excel file called \f(CW\*(C`tab.xls\*(C'\fR. .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::WriteExcel; \& \& open (TABFILE, \*(Aqtab.txt\*(Aq) or die "tab.txt: $!"; \& \& my $workbook = Spreadsheet::WriteExcel\->new(\*(Aqtab.xls\*(Aq); \& my $worksheet = $workbook\->add_worksheet(); \& \& # Row and column are zero indexed \& my $row = 0; \& \& while (<TABFILE>) { \& chomp; \& # Split on single tab \& my @Fld = split(\*(Aq\et\*(Aq, $_); \& \& my $col = 0; \& foreach my $token (@Fld) { \& $worksheet\->write($row, $col, $token); \& $col++; \& } \& $row++; \& } .Ve .PP \&\s-1NOTE:\s0 This is a simple conversion program for illustrative purposes only. For converting a \s-1CSV\s0 or Tab separated or any other type of delimited text file to Excel I recommend the more rigorous csv2xls program that is part of H.Merijn Brand's Text::CSV_XS module distro. .PP See the examples/csv2xls link here: <http://search.cpan.org/~hmbrand/Text\-CSV_XS/MANIFEST>. .SS "Additional Examples" .IX Subsection "Additional Examples" The following is a description of the example files that are provided in the standard Spreadsheet::WriteExcel distribution. They demonstrate the different features and options of the module. See Spreadsheet::WriteExcel::Examples for more details. .PP .Vb 8 \& Getting started \& =============== \& a_simple.pl A get started example with some basic features. \& demo.pl A demo of some of the available features. \& regions.pl A simple example of multiple worksheets. \& stats.pl Basic formulas and functions. \& formats.pl All the available formatting on several worksheets. \& bug_report.pl A template for submitting bug reports. \& \& \& Advanced \& ======== \& autofilter.pl Examples of worksheet autofilters. \& autofit.pl Simulate Excel\*(Aqs autofit for column widths. \& bigfile.pl Write past the 7MB limit with OLE::Storage_Lite. \& cgi.pl A simple CGI program. \& chart_area.pl A demo of area style charts. \& chart_bar.pl A demo of bar (vertical histogram) style charts. \& chart_column.pl A demo of column (histogram) style charts. \& chart_line.pl A demo of line style charts. \& chart_pie.pl A demo of pie style charts. \& chart_scatter.pl A demo of scatter style charts. \& chart_stock.pl A demo of stock style charts. \& chess.pl An example of reusing formatting via properties. \& colors.pl A demo of the colour palette and named colours. \& comments1.pl Add comments to worksheet cells. \& comments2.pl Add comments with advanced options. \& copyformat.pl Example of copying a cell format. \& data_validate.pl An example of data validation and dropdown lists. \& date_time.pl Write dates and times with write_date_time(). \& defined_name.pl Example of how to create defined names. \& diag_border.pl A simple example of diagonal cell borders. \& easter_egg.pl Expose the Excel97 flight simulator. \& filehandle.pl Examples of working with filehandles. \& formula_result.pl Formulas with user specified results. \& headers.pl Examples of worksheet headers and footers. \& hide_sheet.pl Simple example of hiding a worksheet. \& hyperlink1.pl Shows how to create web hyperlinks. \& hyperlink2.pl Examples of internal and external hyperlinks. \& images.pl Adding images to worksheets. \& indent.pl An example of cell indentation. \& merge1.pl A simple example of cell merging. \& merge2.pl A simple example of cell merging with formatting. \& merge3.pl Add hyperlinks to merged cells. \& merge4.pl An advanced example of merging with formatting. \& merge5.pl An advanced example of merging with formatting. \& merge6.pl An example of merging with Unicode strings. \& mod_perl1.pl A simple mod_perl 1 program. \& mod_perl2.pl A simple mod_perl 2 program. \& outline.pl An example of outlines and grouping. \& outline_collapsed.pl An example of collapsed outlines. \& panes.pl An examples of how to create panes. \& properties.pl Add document properties to a workbook. \& protection.pl Example of cell locking and formula hiding. \& repeat.pl Example of writing repeated formulas. \& right_to_left.pl Change default sheet direction to right to left. \& row_wrap.pl How to wrap data from one worksheet onto another. \& sales.pl An example of a simple sales spreadsheet. \& sendmail.pl Send an Excel email attachment using Mail::Sender. \& stats_ext.pl Same as stats.pl with external references. \& stocks.pl Demonstrates conditional formatting. \& tab_colors.pl Example of how to set worksheet tab colours. \& textwrap.pl Demonstrates text wrapping options. \& win32ole.pl A sample Win32::OLE example for comparison. \& write_arrays.pl Example of writing 1D or 2D arrays of data. \& write_handler1.pl Example of extending the write() method. Step 1. \& write_handler2.pl Example of extending the write() method. Step 2. \& write_handler3.pl Example of extending the write() method. Step 3. \& write_handler4.pl Example of extending the write() method. Step 4. \& write_to_scalar.pl Example of writing an Excel file to a Perl scalar. \& \& \& Unicode \& ======= \& unicode_utf16.pl Simple example of using Unicode UTF16 strings. \& unicode_utf16_japan.pl Write Japanese Unicode strings using UTF\-16. \& unicode_cyrillic.pl Write Russian Cyrillic strings using UTF\-8. \& unicode_list.pl List the chars in a Unicode font. \& unicode_2022_jp.pl Japanese: ISO\-2022\-JP to utf8 in perl 5.8. \& unicode_8859_11.pl Thai: ISO\-8859_11 to utf8 in perl 5.8. \& unicode_8859_7.pl Greek: ISO\-8859_7 to utf8 in perl 5.8. \& unicode_big5.pl Chinese: BIG5 to utf8 in perl 5.8. \& unicode_cp1251.pl Russian: CP1251 to utf8 in perl 5.8. \& unicode_cp1256.pl Arabic: CP1256 to utf8 in perl 5.8. \& unicode_koi8r.pl Russian: KOI8\-R to utf8 in perl 5.8. \& unicode_polish_utf8.pl Polish : UTF8 to utf8 in perl 5.8. \& unicode_shift_jis.pl Japanese: Shift JIS to utf8 in perl 5.8. \& \& \& Utility \& ======= \& csv2xls.pl Program to convert a CSV file to an Excel file. \& tab2xls.pl Program to convert a tab separated file to xls. \& datecalc1.pl Convert Unix/Perl time to Excel time. \& datecalc2.pl Calculate an Excel date using Date::Calc. \& lecxe.pl Convert Excel to WriteExcel using Win32::OLE. \& \& \& Developer \& ========= \& convertA1.pl Helper functions for dealing with A1 notation. \& function_locale.pl Add non\-English function names to Formula.pm. \& writeA1.pl Example of how to extend the module. .Ve .SH "LIMITATIONS" .IX Header "LIMITATIONS" The following limits are imposed by Excel: .PP .Vb 7 \& Description Limit \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\- \& Maximum number of chars in a string 32767 \& Maximum number of columns 256 \& Maximum number of rows 65536 \& Maximum chars in a sheet name 31 \& Maximum chars in a header/footer 254 .Ve .PP For Excel 2007+ file limits see the Excel::Writer::XLSX module. .PP The minimum file size is 6K due to the \s-1OLE\s0 overhead. The maximum file size is approximately 7MB (7087104 bytes) of \s-1BIFF\s0 data. This can be extended by installing Takanori Kawai's OLE::Storage_Lite module <http://search.cpan.org/search?dist=OLE\-Storage_Lite> see the \f(CW\*(C`bigfile.pl\*(C'\fR example in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .SH "DOWNLOADING" .IX Header "DOWNLOADING" The latest version of this module is always available at: <http://search.cpan.org/search?dist=Spreadsheet\-WriteExcel/>. .SH "REQUIREMENTS" .IX Header "REQUIREMENTS" This module requires Perl >= 5.005, Parse::RecDescent, File::Temp and OLE::Storage_Lite: .PP .Vb 3 \& http://search.cpan.org/search?dist=Parse\-RecDescent/ # For formulas. \& http://search.cpan.org/search?dist=File\-Temp/ # For set_tempdir(). \& http://search.cpan.org/search?dist=OLE\-Storage_Lite/ # For files > 7MB. .Ve .PP Note, these aren't strict requirements. Spreadsheet::WriteExcel will work without these modules if you don't use \fIwrite_formula()\fR, \fIset_tempdir()\fR or create files greater than 7MB. However, it is best to install them if possible and they will be installed automatically if you use a tool such as \s-1CPAN\s0.pm or ppm. .SH "INSTALLATION" .IX Header "INSTALLATION" See the \s-1INSTALL\s0 or install.html docs that come with the distribution or: <http://search.cpan.org/src/JMCNAMARA/Spreadsheet\-WriteExcel\-2.31/INSTALL>. .SH "PORTABILITY" .IX Header "PORTABILITY" Spreadsheet::WriteExcel will work on the majority of Windows, \s-1UNIX\s0 and Macintosh platforms. Specifically, the module will work on any system where perl packs floats in the 64 bit \s-1IEEE\s0 format. The float must also be in little-endian format but it will be reversed if necessary. Thus: .PP .Vb 1 \& print join(\*(Aq \*(Aq, map { sprintf \*(Aq%#02x\*(Aq, $_ } unpack(\*(AqC*\*(Aq, pack \*(Aqd\*(Aq, 1.2345)), "\en"; .Ve .PP should give (or in reverse order): .PP .Vb 1 \& 0x8d 0x97 0x6e 0x12 0x83 0xc0 0xf3 0x3f .Ve .PP In general, if you don't know whether your system supports a 64 bit \s-1IEEE\s0 float or not, it probably does. If your system doesn't, WriteExcel will \f(CW\*(C`croak()\*(C'\fR with the message given in the \*(L"\s-1DIAGNOSTICS\*(R"\s0 section. You can check which platforms the module has been tested on at the \s-1CPAN\s0 testers site: <http://testers.cpan.org/search?request=dist&dist=Spreadsheet\-WriteExcel>. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" .IP "Filename required by Spreadsheet::WriteExcel\->\fInew()\fR" 4 .IX Item "Filename required by Spreadsheet::WriteExcel->new()" A filename must be given in the constructor. .IP "Can't open filename. It may be in use or protected." 4 .IX Item "Can't open filename. It may be in use or protected." The file cannot be opened for writing. The directory that you are writing to may be protected or the file may be in use by another program. .IP "Unable to create tmp files via \fIFile::Temp::tempfile()\fR..." 4 .IX Item "Unable to create tmp files via File::Temp::tempfile()..." This is a \f(CW\*(C`\-w\*(C'\fR warning. You will see it if you are using Spreadsheet::WriteExcel in an environment where temporary files cannot be created, in which case all data will be stored in memory. The warning is for information only: it does not affect creation but it will affect the speed of execution for large files. See the \f(CW\*(C`set_tempdir\*(C'\fR workbook method. .IP "Maximum file size, 7087104, exceeded." 4 .IX Item "Maximum file size, 7087104, exceeded." The current \s-1OLE\s0 implementation only supports a maximum \s-1BIFF\s0 file of this size. This limit can be extended, see the \*(L"\s-1LIMITATIONS\*(R"\s0 section. .ie n .IP "Can't locate Parse/RecDescent.pm in @INC ..." 4 .el .IP "Can't locate Parse/RecDescent.pm in \f(CW@INC\fR ..." 4 .IX Item "Can't locate Parse/RecDescent.pm in @INC ..." Spreadsheet::WriteExcel requires the Parse::RecDescent module. Download it from \s-1CPAN: \s0<http://search.cpan.org/search?dist=Parse\-RecDescent> .IP "Couldn't parse formula ..." 4 .IX Item "Couldn't parse formula ..." There are a large number of warnings which relate to badly formed formulas and functions. See the \*(L"\s-1FORMULAS AND FUNCTIONS IN EXCEL\*(R"\s0 section for suggestions on how to avoid these errors. You should also check the formula in Excel to ensure that it is valid. .IP "Required floating point format not supported on this platform." 4 .IX Item "Required floating point format not supported on this platform." Operating system doesn't support 64 bit \s-1IEEE\s0 float or it is byte-ordered in a way unknown to WriteExcel. .IP "'file.xls' cannot be accessed. The file may be read-only ..." 4 .IX Item "'file.xls' cannot be accessed. The file may be read-only ..." You may sometimes encounter the following error when trying to open a file in Excel: \*(L"file.xls cannot be accessed. The file may be read-only, or you may be trying to access a read-only location. Or, the server the document is stored on may not be responding.\*(R" .Sp This error generally means that the Excel file has been corrupted. There are two likely causes of this: the file was FTPed in \s-1ASCII\s0 mode instead of binary mode or else the file was created with \f(CW\*(C`UTF\-8\*(C'\fR data returned by an \s-1XML\s0 parser. See \*(L"Warning about XML::Parser and perl 5.6\*(R" for further details. .SH "THE EXCEL BINARY FORMAT" .IX Header "THE EXCEL BINARY FORMAT" The following is some general information about the Excel binary format for anyone who may be interested. .PP Excel data is stored in the \*(L"Binary Interchange File Format\*(R" (\s-1BIFF\s0) file format. Details of this format are given in \*(L"Excel 97\-2007 Binary File Format Specification\*(R" <http://www.microsoft.com/interop/docs/OfficeBinaryFormats.mspx>. .PP Daniel Rentz of OpenOffice.org has also written a detailed description of the Excel workbook records, see <http://sc.openoffice.org/excelfileformat.pdf>. .PP Charles Wybble has collected together additional information about the Excel file format. See \*(L"The Chicago Project\*(R" at <http://chicago.sourceforge.net/devel/>. .PP The \s-1BIFF\s0 data is stored along with other data in an \s-1OLE\s0 Compound File. This is a structured storage which acts like a file system within a file. A Compound File is comprised of storages and streams which, to follow the file system analogy, are like directories and files. .PP The \s-1OLE\s0 format is explained in the \*(L"Windows Compound Binary File Format Specification\*(R" <http://www.microsoft.com/interop/docs/supportingtechnologies.mspx> .PP The Digital Imaging Group have also detailed the \s-1OLE\s0 format in the \s-1JPEG2000\s0 specification: see Appendix A of <http://www.i3a.org/pdf/wg1n1017.pdf>. .PP Please note that the provision of this information does not constitute an invitation to start hacking at the \s-1BIFF\s0 or \s-1OLE\s0 file formats. There are more interesting ways to waste your time. ;\-) .SH "WRITING EXCEL FILES" .IX Header "WRITING EXCEL FILES" Depending on your requirements, background and general sensibilities you may prefer one of the following methods of getting data into Excel: .IP "\(bu" 4 Win32::OLE module and office automation .Sp This requires a Windows platform and an installed copy of Excel. This is the most powerful and complete method for interfacing with Excel. See <http://www.activestate.com/ASPN/Reference/Products/ActivePerl\-5.6/faq/Windows/ActivePerl\-Winfaq12.html> and <http://www.activestate.com/ASPN/Reference/Products/ActivePerl\-5.6/site/lib/Win32/OLE.html>. If your main platform is \s-1UNIX\s0 but you have the resources to set up a separate Win32/MSOffice server, you can convert office documents to text, postscript or \s-1PDF\s0 using Win32::OLE. For a demonstration of how to do this using Perl see Docserver: <http://search.cpan.org/search?mode=module&query=docserver>. .IP "\(bu" 4 \&\s-1CSV,\s0 comma separated variables or text .Sp If the file extension is \f(CW\*(C`csv\*(C'\fR, Excel will open and convert this format automatically. Generating a valid \s-1CSV\s0 file isn't as easy as it seems. Have a look at the \s-1DBD::RAM, DBD::CSV,\s0 Text::xSV and Text::CSV_XS modules. .IP "\(bu" 4 \&\s-1DBI\s0 with \s-1DBD::ADO\s0 or \s-1DBD::ODBC\s0 .Sp Excel files contain an internal index table that allows them to act like a database file. Using one of the standard Perl database modules you can connect to an Excel file as a database. .IP "\(bu" 4 DBD::Excel .Sp You can also access Spreadsheet::WriteExcel using the standard \s-1DBI\s0 interface via Takanori Kawai's DBD::Excel module <http://search.cpan.org/dist/DBD\-Excel> .IP "\(bu" 4 Spreadsheet::WriteExcelXML .Sp This module allows you to create an Excel \s-1XML\s0 file using the same interface as Spreadsheet::WriteExcel. See: <http://search.cpan.org/dist/Spreadsheet\-WriteExcelXML> .IP "\(bu" 4 Excel::Template .Sp This module allows you to create an Excel file from an \s-1XML\s0 template in a manner similar to HTML::Template. See <http://search.cpan.org/dist/Excel\-Template/>. .IP "\(bu" 4 Spreadsheet::WriteExcel::FromXML .Sp This module allows you to turn a simple \s-1XML\s0 file into an Excel file using Spreadsheet::WriteExcel as a back-end. The format of the \s-1XML\s0 file is defined by a supplied \s-1DTD: \s0<http://search.cpan.org/dist/Spreadsheet\-WriteExcel\-FromXML>. .IP "\(bu" 4 Spreadsheet::WriteExcel::Simple .Sp This provides an easier interface to Spreadsheet::WriteExcel: <http://search.cpan.org/dist/Spreadsheet\-WriteExcel\-Simple>. .IP "\(bu" 4 Spreadsheet::WriteExcel::FromDB .Sp This is a useful module for creating Excel files directly from a \s-1DB\s0 table: <http://search.cpan.org/dist/Spreadsheet\-WriteExcel\-FromDB>. .IP "\(bu" 4 \&\s-1HTML\s0 tables .Sp This is an easy way of adding formatting via a text based format. .IP "\(bu" 4 \&\s-1XML\s0 or \s-1HTML\s0 .Sp The Excel \s-1XML\s0 and \s-1HTML\s0 file specification are available from <http://msdn.microsoft.com/library/officedev/ofxml2k/ofxml2k.htm>. .PP For other Perl-Excel modules try the following search: <http://search.cpan.org/search?mode=module&query=excel>. .SH "READING EXCEL FILES" .IX Header "READING EXCEL FILES" To read data from Excel files try: .IP "\(bu" 4 Spreadsheet::ParseExcel .Sp This uses the OLE::Storage\-Lite module to extract data from an Excel file. <http://search.cpan.org/dist/Spreadsheet\-ParseExcel>. .IP "\(bu" 4 Spreadsheet::ParseExcel_XLHTML .Sp This module uses Spreadsheet::ParseExcel's interface but uses xlHtml (see below) to do the conversion: <http://search.cpan.org/dist/Spreadsheet\-ParseExcel_XLHTML> Spreadsheet::ParseExcel_XLHTML .IP "\(bu" 4 xlHtml .Sp This is an open source \*(L"Excel to \s-1HTML\s0 Converter\*(R" C/\*(C+ project at <http://chicago.sourceforge.net/xlhtml/>. .IP "\(bu" 4 DBD::Excel (reading) .Sp You can also access Spreadsheet::ParseExcel using the standard \s-1DBI\s0 interface via Takanori Kawai's DBD::Excel module <http://search.cpan.org/dist/DBD\-Excel>. .IP "\(bu" 4 Win32::OLE module and office automation (reading) .Sp See, the section \*(L"\s-1WRITING EXCEL FILES\*(R"\s0. .IP "\(bu" 4 \&\s-1HTML\s0 tables (reading) .Sp If the files are saved from Excel in a \s-1HTML\s0 format the data can be accessed using HTML::TableExtract <http://search.cpan.org/dist/HTML\-TableExtract>. .IP "\(bu" 4 \&\s-1DBI\s0 with \s-1DBD::ADO\s0 or \s-1DBD::ODBC.\s0 .Sp See, the section \*(L"\s-1WRITING EXCEL FILES\*(R"\s0. .IP "\(bu" 4 XML::Excel .Sp Converts Excel files to \s-1XML\s0 using Spreadsheet::ParseExcel <http://search.cpan.org/dist/XML\-Excel>. .IP "\(bu" 4 OLE::Storage, aka \s-1LAOLA\s0 .Sp This is a Perl interface to \s-1OLE\s0 file formats. In particular, the distro contains an Excel to \s-1HTML\s0 converter called Herbert, <http://user.cs.tu\-berlin.de/~schwartz/pmh/>. This has been superseded by the Spreadsheet::ParseExcel module. .PP For other Perl-Excel modules try the following search: <http://search.cpan.org/search?mode=module&query=excel>. .PP If you wish to view Excel files on a UNIX/Linux platform check out the excellent Gnumeric spreadsheet application at <http://www.gnome.org/projects/gnumeric/> or OpenOffice.org at <http://www.openoffice.org/>. .PP If you wish to view Excel files on a Windows platform which doesn't have Excel installed you can use the free Microsoft Excel Viewer <http://office.microsoft.com/downloads/2000/xlviewer.aspx>. .SH "MODIFYING AND REWRITING EXCEL FILES" .IX Header "MODIFYING AND REWRITING EXCEL FILES" An Excel file is a binary file within a binary file. It contains several interlinked checksums and changing even one byte can cause it to become corrupted. .PP As such you cannot simply append or update an Excel file. The only way to achieve this is to read the entire file into memory, make the required changes or additions and then write the file out again. .PP You can read and rewrite an Excel file using the Spreadsheet::ParseExcel::SaveParser module which is a wrapper around Spreadsheet::ParseExcel and Spreadsheet::WriteExcel. It is part of the Spreadsheet::ParseExcel package: <http://search.cpan.org/search?dist=Spreadsheet\-ParseExcel>. .PP However, you can only rewrite the features that Spreadsheet::WriteExcel supports so macros, graphs and some other features in the original Excel file will be lost. Also, formulas aren't rewritten, only the result of a formula is written. .PP Here is an example: .PP .Vb 1 \& #!/usr/bin/perl \-w \& \& use strict; \& use Spreadsheet::ParseExcel; \& use Spreadsheet::ParseExcel::SaveParser; \& \& # Open the template with SaveParser \& my $parser = new Spreadsheet::ParseExcel::SaveParser; \& my $template = $parser\->Parse(\*(Aqtemplate.xls\*(Aq); \& \& my $sheet = 0; \& my $row = 0; \& my $col = 0; \& \& # Get the format from the cell \& my $format = $template\->{Worksheet}[$sheet] \& \->{Cells}[$row][$col] \& \->{FormatNo}; \& \& # Write data to some cells \& $template\->AddCell(0, $row, $col, 1, $format); \& $template\->AddCell(0, $row+1, $col, "Hello", $format); \& \& # Add a new worksheet \& $template\->AddWorksheet(\*(AqNew Data\*(Aq); \& \& # The SaveParser SaveAs() method returns a reference to a \& # Spreadsheet::WriteExcel object. If you wish you can then \& # use this to access any of the methods that aren\*(Aqt \& # available from the SaveParser object. If you don\*(Aqt need \& # to do this just use SaveAs(). \& # \& my $workbook; \& \& { \& # SaveAs generates a lot of harmless warnings about unset \& # Worksheet properties. You can ignore them if you wish. \& local $^W = 0; \& \& # Rewrite the file or save as a new file \& $workbook = $template\->SaveAs(\*(Aqnew.xls\*(Aq); \& } \& \& # Use Spreadsheet::WriteExcel methods \& my $worksheet = $workbook\->sheets(0); \& \& $worksheet\->write($row+2, $col, "World2"); \& \& $workbook\->close(); .Ve .SH "Warning about XML::Parser and perl 5.6" .IX Header "Warning about XML::Parser and perl 5.6" You must be careful when using Spreadsheet::WriteExcel in conjunction with perl 5.6 and XML::Parser (and other \s-1XML\s0 parsers) due to the fact that the data returned by the parser is generally in \f(CW\*(C`UTF\-8\*(C'\fR format. .PP When \f(CW\*(C`UTF\-8\*(C'\fR strings are added to Spreadsheet::WriteExcel's internal data it causes the generated Excel file to become corrupt. .PP Note, this doesn't affect perl 5.005 (which doesn't try to handle \f(CW\*(C`UTF\-8\*(C'\fR) or 5.8 (which handles it correctly). .PP To avoid this problem you should upgrade to perl 5.8, if possible, or else you should convert the output data from XML::Parser to \s-1ASCII\s0 or \s-1ISO\-8859\-1\s0 using one of the following methods: .PP .Vb 1 \& $new_str = pack \*(AqC*\*(Aq, unpack \*(AqU*\*(Aq, $utf8_str; \& \& \& use Unicode::MapUTF8 \*(Aqfrom_utf8\*(Aq; \& $new_str = from_utf8({\-str => $utf8_str, \-charset => \*(AqISO\-8859\-1\*(Aq}); .Ve .SH "Warning about Office Service Pack 3" .IX Header "Warning about Office Service Pack 3" If you have Office Service Pack 3 (\s-1SP3\s0) installed you may see the following warning when you open a file created by Spreadsheet::WriteExcel: .PP .Vb 1 \& "File Error: data may have been lost". .Ve .PP This is usually caused by multiple instances of data in a cell. .PP \&\s-1SP3\s0 changed Excel's default behaviour when it encounters multiple data in a cell so that it issues a warning when the file is opened and it displays the first data that was written. Prior to \s-1SP3\s0 it didn't issue a warning and displayed the last data written. .PP For a longer discussion and some workarounds see the following: <http://groups.google.com/group/spreadsheet\-writeexcel/browse_thread/thread/3dcea40e6620af3a>. .SH "BUGS" .IX Header "BUGS" Formulas are formulae. .PP \&\s-1XML\s0 and \f(CW\*(C`UTF\-8\*(C'\fR data on perl 5.6 can cause Excel files created by Spreadsheet::WriteExcel to become corrupt. See \*(L"Warning about XML::Parser and perl 5.6\*(R" for further details. .PP The format object that is used with a \f(CW\*(C`merge_range()\*(C'\fR method call is marked internally as being associated with a merged range. It is a fatal error to use a merged format in a non-merged cell. The current workaround is to use separate formats for merged and non-merged cell. This restriction will be removed in a future release. .PP Nested formulas sometimes aren't parsed correctly and give a result of \*(L"#VALUE\*(R". If you come across a formula that parses like this, let me know. .PP Spreadsheet::ParseExcel: All formulas created by Spreadsheet::WriteExcel are read as having a value of zero. This is because Spreadsheet::WriteExcel only stores the formula and not the calculated result. .PP OpenOffice.org: No known issues in this release. .PP Gnumeric: No known issues in this release. .PP If you wish to submit a bug report run the \f(CW\*(C`bug_report.pl\*(C'\fR program in the \f(CW\*(C`examples\*(C'\fR directory of the distro. .SH "Migrating to Excel::Writer::XLSX" .IX Header "Migrating to Excel::Writer::XLSX" Spreadsheet::WriteExcel is in maintenance only mode and has effectively been superseded by Excel::Writer::XLSX. .PP Excel::Writer::XLSX is an \s-1API\s0 compatible, drop-in replacement for Spreadsheet::WriteExcel. It also has many more features such as conditional formats, better charts, better formula handling, Excel tables and even sparklines. .PP To convert your Spreadsheet::WriteExcel program to Excel::Writer::XLSX you only need do the following: .IP "\(bu" 4 Substitute Excel::Writer::XLSX for Spreadsheet::WriteExcel in your program. .IP "\(bu" 4 Change the file extension of the output file from \f(CW\*(C`.xls\*(C'\fR to \f(CW\*(C`.xlsx\*(C'\fR. .IP "\(bu" 4 Optionally replace \f(CW\*(C`store_formula()\*(C'\fR and \f(CW\*(C`repeat_formula()\*(C'\fR with \f(CW\*(C`write_formula()\*(C'\fR which is no longer an expensive operation in Excel::Writer::XLSX. However, you can leave them unchanged if required. .PP There are some differences between the formats and the modules that are worth noting: .IP "\(bu" 4 The default font in the \s-1XLSX\s0 format is Calibri 11 not Arial 10. .IP "\(bu" 4 Default column widths and row heights are different between \s-1XLS\s0 and \s-1XLSX.\s0 .IP "\(bu" 4 The Excel::Writer::XLSX module uses more memory by default but has a optimisation mode to reduce usage for large files. .IP "\(bu" 4 The \s-1XLSX\s0 format doesn't have reading support that is as complete as Spreadsheet::ParseExcel. .SH "REPOSITORY" .IX Header "REPOSITORY" The Spreadsheet::WriteExcel source code in host on github: <http://github.com/jmcnamara/spreadsheet\-writeexcel>. .SH "MAILING LIST" .IX Header "MAILING LIST" There is a Google group for discussing and asking questions about Spreadsheet::WriteExcel. This is a good place to search to see if your question has been asked before: <http://groups.google.com/group/spreadsheet\-writeexcel>. .PP Alternatively you can keep up to date with future releases by subscribing at: <http://freshmeat.net/projects/writeexcel/>. .SH "DONATIONS" .IX Header "DONATIONS" If you'd care to donate to the Spreadsheet::WriteExcel project, you can do so via PayPal: <http://tinyurl.com/7ayes>. .SH "SEE ALSO" .IX Header "SEE ALSO" Spreadsheet::ParseExcel: <http://search.cpan.org/dist/Spreadsheet\-ParseExcel>. .PP Spreadsheet-WriteExcel-FromXML: <http://search.cpan.org/dist/Spreadsheet\-WriteExcel\-FromXML>. .PP Spreadsheet::WriteExcel::FromDB: <http://search.cpan.org/dist/Spreadsheet\-WriteExcel\-FromDB>. .PP Excel::Template: <http://search.cpan.org/~rkinyon/Excel\-Template/>. .PP DateTime::Format::Excel: <http://search.cpan.org/dist/DateTime\-Format\-Excel>. .PP \&\*(L"Reading and writing Excel files with Perl\*(R" by Teodor Zlatanov, at \s-1IBM\s0 developerWorks: <http://www\-106.ibm.com/developerworks/library/l\-pexcel/>. .PP \&\*(L"Excel-Dateien mit Perl erstellen \- Controller im Gluck\*(R" by Peter Dintelmann and Christian Kirsch in the German Unix/web journal iX: <http://www.heise.de/ix/artikel/2001/06/175/>. .PP Spreadsheet::WriteExcel documentation in Japanese by Takanori Kawai. <http://member.nifty.ne.jp/hippo2000/perltips/Spreadsheet/WriteExcel.htm>. .PP Oesterly user brushes with fame: <http://oesterly.com/releases/12102000.html>. .PP The csv2xls program that is part of Text::CSV_XS: <http://search.cpan.org/~hmbrand/Text\-CSV_XS/MANIFEST>. .SH "ACKNOWLEDGMENTS" .IX Header "ACKNOWLEDGMENTS" The following people contributed to the debugging and testing of Spreadsheet::WriteExcel: .PP Alexander Farber, Andre de Bruin, Arthur@ais, Artur Silveira da Cunha, Bob Rose, Borgar Olsen, Brian Foley, Brian White, Bob Mackay, Cedric Bouvier, Chad Johnson, \s-1CPAN\s0 testers, Damyan Ivanov, Daniel Berger, Daniel Gardner, Dmitry Kochurov, Eric Frazier, Ernesto Baschny, Felipe Perez Galiana, Gordon Simpson, Hanc Pavel, Harold Bamford, James Holmes, James Wilkinson, Johan Ekenberg, Johann Hanne, Jonathan Scott Duff, J.C. Wren, Kenneth Stacey, Keith Miller, Kyle Krom, Marc Rosenthal, Markus Schmitz, Michael Braig, Michael Buschauer, Mike Blazer, Michael Erickson, Michael W J West, Ning Xie, Paul J. Falbe, Paul Medynski, Peter Dintelmann, Pierre Laplante, Praveen Kotha, Reto Badertscher, Rich Sorden, Shane Ashby, Sharron McKenzie, Shenyu Zheng, Stephan Loescher, Steve Sapovits, Sven Passig, Svetoslav Marinov, Tamas Gulacsi, Troy Daniels, Vahe Sarkissian. .PP The following people contributed patches, examples or Excel information: .PP Andrew Benham, Bill Young, Cedric Bouvier, Charles Wybble, Daniel Rentz, David Robins, Franco Venturi, Guy Albertelli, Ian Penman, John Heitmann, Jon Guy, Kyle R. Burton, Pierre-Jean Vouette, Rubio, Marco Geri, Mark Fowler, Matisse Enzer, Sam Kington, Takanori Kawai, Tom O'Sullivan. .PP Many thanks to Ron McKelvey, Ronzo Consulting for Siemens, who sponsored the development of the formula caching routines. .PP Many thanks to Cassens Transport who sponsored the development of the embedded charts and autofilters. .PP Additional thanks to Takanori Kawai for translating the documentation into Japanese. .PP Gunnar Wolf maintains the Debian distro. .PP Thanks to Damian Conway for the excellent Parse::RecDescent. .PP Thanks to Tim Jenness for File::Temp. .PP Thanks to Michael Meeks and Jody Goldberg for their work on Gnumeric. .SH "DISCLAIMER OF WARRANTY" .IX Header "DISCLAIMER OF WARRANTY" Because this software is licensed free of charge, there is no warranty for the software, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the software \*(L"as is\*(R" without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the software is with you. Should the software prove defective, you assume the cost of all necessary servicing, repair, or correction. .PP In no event unless required by applicable law or agreed to in writing will any copyright holder, or any other party who may modify and/or redistribute the software as permitted by the above licence, be liable to you for damages, including any general, special, incidental, or consequential damages arising out of the use or inability to use the software (including but not limited to loss of data or data being rendered inaccurate or losses sustained by you or third parties or a failure of the software to operate with any other software), even if such holder or other party has been advised of the possibility of such damages. .SH "LICENSE" .IX Header "LICENSE" Either the Perl Artistic Licence <http://dev.perl.org/licenses/artistic.html> or the \s-1GPL \s0<http://www.opensource.org/licenses/gpl\-license.php>. .SH "AUTHOR" .IX Header "AUTHOR" John McNamara jmcnamara@cpan.org .PP .Vb 8 \& The ashtray says \& You were up all night. \& When you went to bed \& With your darkest mind. \& Your pillow wept \& And covered your eyes. \& And you finally slept \& While the sun caught fire. \& \& You\*(Aqve changed. \& \-\- Jeff Tweedy .Ve .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright MM-MMXII, John McNamara. .PP All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.