package Excel::Writer::XLSX; ############################################################################### # # Excel::Writer::XLSX - Create a new file in the Excel 2007+ XLSX format. # # Copyright 2000-2018, John McNamara, jmcnamara@cpan.org # # Documentation after __END__ # use 5.008002; use strict; use warnings; use Exporter; use strict; use Excel::Writer::XLSX::Workbook; our @ISA = qw(Excel::Writer::XLSX::Workbook Exporter); our $VERSION = '0.98'; ############################################################################### # # new() # sub new { my $class = shift; my $self = Excel::Writer::XLSX::Workbook->new( @_ ); # Check for file creation failures before re-blessing bless $self, $class if defined $self; return $self; } 1; __END__ =head1 NAME Excel::Writer::XLSX - Create a new file in the Excel 2007+ XLSX format. =head1 SYNOPSIS To write a string, a formatted string, a number and a formula to the first worksheet in an Excel workbook called perl.xlsx: use Excel::Writer::XLSX; # Create a new Excel workbook my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' ); # Add a worksheet $worksheet = $workbook->add_worksheet(); # Add and define a format $format = $workbook->add_format(); $format->set_bold(); $format->set_color( 'red' ); $format->set_align( 'center' ); # Write a formatted and unformatted string, row and column notation. $col = $row = 0; $worksheet->write( $row, $col, 'Hi Excel!', $format ); $worksheet->write( 1, $col, 'Hi Excel!' ); # Write a number and a formula using A1 notation $worksheet->write( 'A3', 1.2345 ); $worksheet->write( 'A4', '=SIN(PI()/4)' ); $workbook->close(); =head1 DESCRIPTION The C module can be used to create an Excel file in the 2007+ XLSX format. The XLSX format is the Office Open XML (OOXML) format used by Excel 2007 and later. Multiple worksheets can be added to a workbook and formatting can be applied to cells. Text, numbers, and formulas can be written to the cells. This module cannot, as yet, be used to write to an existing Excel XLSX file. =head1 Excel::Writer::XLSX and Spreadsheet::WriteExcel C uses the same interface as the L module which produces an Excel file in binary XLS format. Excel::Writer::XLSX supports all of the features of Spreadsheet::WriteExcel and in some cases has more functionality. For more details see L. The main advantage of the XLSX format over the XLS format is that it allows a larger number of rows and columns in a worksheet. The XLSX file format also produces much smaller files than the XLS file format. =head1 QUICK START Excel::Writer::XLSX 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 four easy steps: 1. Create a new Excel I (i.e. file) using C. 2. Add a worksheet to the new workbook using C. 3. Write to the worksheet using C. 4. C the file. Like this: use Excel::Writer::XLSX; # Step 0 my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' ); # Step 1 $worksheet = $workbook->add_worksheet(); # Step 2 $worksheet->write( 'A1', 'Hi Excel!' ); # Step 3 $workbook->close(); # Step 4 This will create an Excel file called C with a single worksheet and the text C<'Hi Excel!'> in the relevant cell. And that's it. Okay, so there is actually a zeroth step as well, but C goes without saying. There are many examples that come with the distribution and which you can use to get you started. See L. Those of you who read the instructions first and assemble the furniture afterwards will know how to proceed. ;-) =head1 WORKBOOK METHODS The Excel::Writer::XLSX module provides an object oriented interface to a new Excel workbook. The following methods are available through a new workbook. new() add_worksheet() add_format() add_chart() add_shape() add_vba_project() set_vba_name() close() set_properties() set_custom_property() define_name() set_tempdir() set_custom_color() sheets() get_worksheet_by_name() set_1904() set_optimization() set_calc_mode() get_default_url_format() If you are unfamiliar with object oriented interfaces or the way that they are implemented in Perl have a look at C and C in the main Perl documentation. =head2 new() A new Excel workbook is created using the C constructor which accepts either a filename or a filehandle as a parameter. The following example creates a new Excel file based on a filename: my $workbook = Excel::Writer::XLSX->new( 'filename.xlsx' ); my $worksheet = $workbook->add_worksheet(); $worksheet->write( 0, 0, 'Hi Excel!' ); $workbook->close(); Here are some other examples of using C with filenames: my $workbook1 = Excel::Writer::XLSX->new( $filename ); my $workbook2 = Excel::Writer::XLSX->new( '/tmp/filename.xlsx' ); my $workbook3 = Excel::Writer::XLSX->new( "c:\\tmp\\filename.xlsx" ); my $workbook4 = Excel::Writer::XLSX->new( 'c:\tmp\filename.xlsx' ); The last two examples demonstrates how to create a file on DOS or Windows where it is necessary to either escape the directory separator C<\> or to use single quotes to ensure that it isn't interpolated. For more information see C. It is recommended that the filename uses the extension C<.xlsx> rather than C<.xls> since the latter causes an Excel warning when used with the XLSX format. The C constructor returns a Excel::Writer::XLSX object that you can use to add worksheets and store data. It should be noted that although C 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 C method. If the file cannot be created, due to file permissions or some other reason, C will return C. Therefore, it is good practice to check the return value of C before proceeding. As usual the Perl variable C<$!> will be set if there is a file creation error. You will also see one of the warning messages detailed in L: my $workbook = Excel::Writer::XLSX->new( 'protected.xlsx' ); die "Problems creating new Excel file: $!" unless defined $workbook; You can also pass a valid filehandle to the C constructor. For example in a CGI program you could do something like this: binmode( STDOUT ); my $workbook = Excel::Writer::XLSX->new( \*STDOUT ); The requirement for C is explained below. See also, the C program in the C directory of the distro. In C programs where you will have to do something like the following: # mod_perl 1 ... tie *XLSX, 'Apache'; binmode( XLSX ); my $workbook = Excel::Writer::XLSX->new( \*XLSX ); ... # mod_perl 2 ... tie *XLSX => $r; # Tie to the Apache::RequestRec object binmode( *XLSX ); my $workbook = Excel::Writer::XLSX->new( \*XLSX ); ... See also, the C and C programs in the C directory of the distro. 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. For example here is a way to write an Excel file to a scalar: #!/usr/bin/perl -w use strict; use Excel::Writer::XLSX; open my $fh, '>', \my $str or die "Failed to open filehandle: $!"; my $workbook = Excel::Writer::XLSX->new( $fh ); my $worksheet = $workbook->add_worksheet(); $worksheet->write( 0, 0, 'Hi Excel!' ); $workbook->close(); # The Excel file in now in $str. Remember to binmode() the output # filehandle before printing it. binmode STDOUT; print $str; See also the C and C programs in the C directory of the distro. B C. An Excel file is comprised of binary data. Therefore, if you are using a filehandle you should ensure that you C it prior to passing it to C.You should do this regardless of whether you are on a Windows platform or not. You don't have to worry about C if you are using filenames instead of filehandles. Excel::Writer::XLSX performs the C internally when it converts the filename to a filehandle. For more information about C see C and C in the main Perl documentation. =head2 add_worksheet( $sheetname ) At least one worksheet should be added to a new workbook. A worksheet is used to write data into cells: $worksheet1 = $workbook->add_worksheet(); # Sheet1 $worksheet2 = $workbook->add_worksheet( 'Foglio2' ); # Foglio2 $worksheet3 = $workbook->add_worksheet( 'Data' ); # Data $worksheet4 = $workbook->add_worksheet(); # Sheet4 If C<$sheetname> is not specified the default Excel convention will be followed, i.e. Sheet1, Sheet2, etc. The worksheet name must be a valid Excel worksheet name, i.e. it cannot contain any of the following characters, C<[ ] : * ? / \> and it must be less than 32 characters. In addition, you cannot use the same, case insensitive, C<$sheetname> for more than one worksheet. =head2 add_format( %properties ) The C 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. $format1 = $workbook->add_format( %props ); # Set properties at creation $format2 = $workbook->add_format(); # Set properties later See the L section for more details about Format properties and how to set them. =head2 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 C Worksheet method. my $chart = $workbook->add_chart( type => 'column' ); The properties that can be set are: type (required) subtype (optional) name (optional) embedded (optional) =over =item * C This is a required parameter. It defines the type of chart that will be created. my $chart = $workbook->add_chart( type => 'line' ); The available types are: area bar column line pie doughnut scatter stock =item * C Used to define a chart subtype where available. my $chart = $workbook->add_chart( type => 'bar', subtype => 'stacked' ); See the L documentation for a list of available chart subtypes. =item * C Set the name for the chart sheet. The name property is optional and if it isn't supplied will default to C. The name must be a valid Excel worksheet name. See C for more details on valid sheet names. The C property can be omitted for embedded charts. my $chart = $workbook->add_chart( type => 'line', name => 'Results Chart' ); =item * C Specifies that the Chart object will be inserted in a worksheet via the C Worksheet method. It is an error to try insert a Chart that doesn't have this flag set. my $chart = $workbook->add_chart( type => 'line', embedded => 1 ); # Configure the chart. ... # Insert the chart into the a worksheet. $worksheet->insert_chart( 'E2', $chart ); =back See Excel::Writer::XLSX::Chart for details on how to configure the chart object once it is created. See also the C programs in the examples directory of the distro. =head2 add_shape( %properties ) The C method can be used to create new shapes that may be inserted into a worksheet. You can either define the properties at creation time via a hash of property values or later via method calls. # Set properties at creation. $plus = $workbook->add_shape( type => 'plus', id => 3, width => $pw, height => $ph ); # Default rectangle shape. Set properties later. $rect = $workbook->add_shape(); See L for details on how to configure the shape object once it is created. See also the C programs in the examples directory of the distro. =head2 add_vba_project( 'vbaProject.bin' ) The C method can be used to add macros or functions to an Excel::Writer::XLSX file using a binary VBA project file that has been extracted from an existing Excel C file. my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' ); $workbook->add_vba_project( './vbaProject.bin' ); The supplied C utility can be used to extract the required C file from an existing Excel file: $ extract_vba file.xlsm Extracted 'vbaProject.bin' successfully Macros can be tied to buttons using the worksheet C method (see the L section for details): $worksheet->insert_button( 'C2', { macro => 'my_macro' } ); Note, Excel uses the file extension C instead of C for files that contain macros. It is advisable to follow the same convention. See also the C example file and the L. =head2 set_vba_name() The C method can be used to set the VBA codename for the workbook. This is sometimes required when a C included via C refers to the workbook. The default Excel VBA name of C is used if a user defined name isn't specified. See also L. =head2 close() In general your Excel file will be closed automatically when your program ends or when the Workbook object goes out of scope. However it is recommended to explicitly call the C method close the Excel file and avoid the potential issues outlined below. The C method is called like this: $workbook->close(); The return value of C is the same as that returned by perl when it closes the file created by C. This allows you to handle error conditions in the usual way: $workbook->close() or die "Error closing file: $!"; An explicit C 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. In addition, C 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: =over 4 =item * If C was not used to declare the scope of a workbook variable created using C. =item * If the C, C or C methods are called in subroutines. =back The reason for this is that Excel::Writer::XLSX relies on Perl's C 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. To avoid these issues it is recommended that you always close the Excel::Writer::XLSX filehandle using C. =head2 set_size( $width, $height ) The C method can be used to set the size of a workbook window. $workbook->set_size(1200, 800); The Excel window size was used in Excel 2007 to define the width and height of a workbook window within the Multiple Document Interface (MDI). In later versions of Excel for Windows this interface was dropped. This method is currently only useful when setting the window size in Excel for Mac 2011. The units are pixels and the default size is 1073 x 644. Note, this doesn't equate exactly to the Excel for Mac pixel size since it is based on the original Excel 2007 for Windows sizing. =head2 set_properties() The C method can be used to set the document properties of the Excel file created by C. These properties are visible when you use the C<< Office Button -> Prepare -> Properties >> option in Excel and are also available to external applications that read or index Windows files. The properties should be passed in hash format as follows: $workbook->set_properties( title => 'This is an example spreadsheet', author => 'John McNamara', comments => 'Created with Perl and Excel::Writer::XLSX', ); The properties that can be set are: title subject author manager company category keywords comments status hyperlink_base created - File create date. Such be an aref of gmtime() values. See also the C program in the examples directory of the distro. =head2 set_custom_property( $name, $value, $type) The C method can be used to set one of more custom document properties not covered by the C method above. These properties are visible when you use the C<< Office Button -> Prepare -> Properties -> Advanced Properties -> Custom >> option in Excel and are also available to external applications that read or index Windows files. The C method takes 3 parameters: $workbook-> set_custom_property( $name, $value, $type); Where the available types are: text date number bool For example: $workbook->set_custom_property( 'Checked by', 'Eve', 'text' ); $workbook->set_custom_property( 'Date completed', '2016-12-12T23:00:00Z', 'date' ); $workbook->set_custom_property( 'Document number', '12345' , 'number' ); $workbook->set_custom_property( 'Reference', '1.2345', 'number' ); $workbook->set_custom_property( 'Has review', 1, 'bool' ); $workbook->set_custom_property( 'Signed off', 0, 'bool' ); $workbook->set_custom_property( 'Department', $some_string, 'text' ); $workbook->set_custom_property( 'Scale', '1.2345678901234', 'number' ); Dates should by in ISO8601 C date format in Zulu time, as shown above. The C and C types are optional since they can usually be inferred from the data: $workbook->set_custom_property( 'Checked by', 'Eve' ); $workbook->set_custom_property( 'Reference', '1.2345' ); The C<$name> and C<$value> parameters are limited to 255 characters by Excel. =head2 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. For example to set a global/workbook name: # Global/workbook names. $workbook->define_name( 'Exchange_rate', '=0.96' ); $workbook->define_name( 'Sales', '=Sheet1!$G$1:$H$10' ); It is also possible to define a local/worksheet name by prefixing the name with the sheet name using the syntax C: # Local/worksheet name. $workbook->define_name( 'Sheet2!Sales', '=Sheet2!$G$1:$G$10' ); If the sheet name contains spaces or special characters you must enclose it in single quotes like in Excel: $workbook->define_name( "'New Data'!Sales", '=Sheet2!$G$1:$G$10' ); See the defined_name.pl program in the examples dir of the distro. Refer to the following to see Excel's syntax rules for defined names: L =head2 set_tempdir() C stores worksheet data in temporary files prior to assembling the final workbook. The C module is used to create these temporary files. File::Temp uses C to determine an appropriate location for these files such as C or C. You can find out which directory is used on your system as follows: perl -MFile::Spec -le "print File::Spec->tmpdir()" If the default temporary file directory isn't accessible to your application, or doesn't contain enough space, you can specify an alternative location using the C method: $workbook->set_tempdir( '/tmp/writeexcel' ); $workbook->set_tempdir( 'c:\windows\temp\writeexcel' ); The directory for the temporary file must exist, C will not create a new directory. =head2 set_custom_color( $index, $red, $green, $blue ) The method is maintained for backward compatibility with Spreadsheet::WriteExcel. Excel::Writer::XLSX programs don't require this method and colours can be specified using a Html style C<#RRGGBB> value, see L. =head2 sheets( 0, 1, ... ) The C method returns a list, or a sliced list, of the worksheets in a workbook. 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: for $worksheet ( $workbook->sheets() ) { print $worksheet->get_name(); } You can also specify a slice list to return one or more worksheet objects: $worksheet = $workbook->sheets( 0 ); $worksheet->write( 'A1', 'Hello' ); Or since the return value from C is a reference to a worksheet object you can write the above example as: $workbook->sheets( 0 )->write( 'A1', 'Hello' ); The following example returns the first and last worksheet in a workbook: for $worksheet ( $workbook->sheets( 0, -1 ) ) { # Do something } Array slices are explained in the C manpage. =head2 get_worksheet_by_name() The C function return a worksheet or chartsheet object in the workbook using the sheetname: $worksheet = $workbook->get_worksheet_by_name('Sheet1'); =head2 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. Excel::Writer::XLSX stores dates in the 1900 format by default. If you wish to change this you can call the C workbook method. You can query the current value by calling the C workbook method. This returns 0 for 1900 and 1 for 1904. See also L for more information about working with Excel's date system. In general you probably won't need to use C. =head2 set_optimization() The C method is used to turn on optimizations in the Excel::Writer::XLSX module. Currently there is only one optimization available and that is to reduce memory usage. $workbook->set_optimization(); See L for more background information. Note, that with this optimization turned on a row of data is written and then discarded when a cell in a new row is added via one of the Worksheet C methods. As such data should be written in sequential row order once the optimization is turned on. This method must be called before any calls to C. =head2 set_calc_mode( $mode ) Set the calculation mode for formulas in the workbook. This is mainly of use for workbooks with slow formulas where you want to allow the user to calculate them manually. The mode parameter can be one of the following strings: =over =item C The default. Excel will re-calculate formulas when a formula or a value affecting the formula changes. =item C Only re-calculate formulas when the user requires it. Generally by pressing F9. =item C Excel will automatically re-calculate formulas except for tables. =back =head2 get_default_url_format() The C method gets a copy of the default url format used when a user defined format isn't specified with the worksheet C method. The format is the hyperlink style defined by Excel for the default theme: my $url_format = $workbook->get_default_url_format(); =head1 WORKSHEET METHODS A new worksheet is created by calling the C method from a workbook object: $worksheet1 = $workbook->add_worksheet(); $worksheet2 = $workbook->add_worksheet(); The following methods are available through a new worksheet: write() write_number() write_string() write_rich_string() keep_leading_zeros() write_blank() write_row() write_col() write_date_time() write_url() write_url_range() write_formula() write_boolean() write_comment() show_comments() set_comments_author() add_write_handler() insert_image() insert_chart() insert_shape() insert_button() data_validation() conditional_formatting() add_sparkline() add_table() get_name() activate() select() hide() set_first_sheet() protect() set_selection() set_row() set_default_row() set_column() outline_settings() freeze_panes() split_panes() merge_range() merge_range_type() set_zoom() right_to_left() hide_zero() set_tab_color() autofilter() filter_column() filter_column_list() set_vba_name() =head2 Cell notation Excel::Writer::XLSX supports two forms of notation to designate the position of cells: Row-column notation and A1 notation. 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: (0, 0) # The top left cell in row-column notation. ('A1') # The top left cell in A1 notation. (1999, 29) # Row-column notation. ('AD2000') # The same cell in A1 notation. Row-column notation is useful if you are referring to cells programmatically: for my $i ( 0 .. 9 ) { $worksheet->write( $i, 0, 'Hello' ); # Cells A1 to A10 } A1 notation is useful for setting up a worksheet manually and for working with formulas: $worksheet->write( 'H1', 200 ); $worksheet->write( 'H2', '=H1+1' ); In formulas and applicable methods you can also use the C column notation: $worksheet->write( 'A1', '=SUM(B:B)' ); The C module that is included in the distro contains helper functions for dealing with A1 notation, for example: use Excel::Writer::XLSX::Utility; ( $row, $col ) = xl_cell_to_rowcol( 'C2' ); # (1, 2) $str = xl_rowcol_to_cell( 1, 2 ); # C2 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. Note: in Excel it is also possible to use a R1C1 notation. This is not supported by Excel::Writer::XLSX. =head2 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 C method acts as a general alias for several more specific methods: write_string() write_number() write_blank() write_formula() write_url() write_row() write_col() The general rule is that if the data looks like a I then a I is written. Here are some examples in both row-column and A1 notation: # Same as: $worksheet->write( 0, 0, 'Hello' ); # write_string() $worksheet->write( 1, 0, 'One' ); # 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, '' ); # write_blank() $worksheet->write( 6, 0, undef ); # write_blank() $worksheet->write( 7, 0 ); # write_blank() $worksheet->write( 8, 0, 'http://www.perl.com/' ); # write_url() $worksheet->write( 'A9', 'ftp://ftp.cpan.org/' ); # write_url() $worksheet->write( 'A10', 'internal:Sheet1!A1' ); # write_url() $worksheet->write( 'A11', 'external:c:\foo.xlsx' ); # write_url() $worksheet->write( 'A12', '=A3 + 3*A4' ); # write_formula() $worksheet->write( 'A13', '=SIN(PI()/4)' ); # write_formula() $worksheet->write( 'A14', \@array ); # write_row() $worksheet->write( 'A15', [\@array] ); # write_col() # And if the keep_leading_zeros property is set: $worksheet->write( 'A16', '2' ); # write_number() $worksheet->write( 'A17', '02' ); # write_string() $worksheet->write( 'A18', '00002' ); # write_string() # Write an array formula. Not available in Spreadsheet::WriteExcel. $worksheet->write( 'A19', '{=SUM(A1:B1*A2:B2)}' ); # write_formula() The "looks like" rule is defined by regular expressions: C if C<$token> is a number based on the following regex: C<$token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/>. C if C is set and C<$token> is an integer with leading zeros based on the following regex: C<$token =~ /^0\d+$/>. C if C<$token> is undef or a blank string: C, C<""> or C<''>. C if C<$token> is a http, https, ftp or mailto URL based on the following regexes: C<$token =~ m|^[fh]tt?ps?://|> or C<$token =~ m|^mailto:|>. C if C<$token> is an internal or external sheet reference based on the following regex: C<$token =~ m[^(in|ex)ternal:]>. C if the first character of C<$token> is C<"=">. C if the C<$token> matches C. C if C<$token> is an array ref. C if C<$token> is an array ref of array refs. C if none of the previous conditions apply. The C<$format> parameter is optional. It should be a valid Format object, see L: my $format = $workbook->add_format(); $format->set_bold(); $format->set_color( 'red' ); $format->set_align( 'center' ); $worksheet->write( 4, 0, 'Hello', $format ); # Formatted string The write() method will ignore empty strings or C tokens unless a format is also supplied. As such you needn't worry about special handling for empty or C values in your data. See also the C method. One problem with the C method is that occasionally data looks like a number but you don't want it treated as a number. For example, zip codes or ID 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 C 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 C section for a full discussion of this issue. You can also add your own data handlers to the C method using C. The C method will also handle Unicode strings in C format. The C methods return: 0 for success. -1 for insufficient number of arguments. -2 for row or column out of bounds. -3 for string too long. =head2 write_number( $row, $column, $number, $format ) Write an integer or a float to the cell specified by C<$row> and C<$column>: $worksheet->write_number( 0, 0, 123456 ); $worksheet->write_number( 'A2', 2.3451 ); See the note about L. The C<$format> parameter is optional. In general it is sufficient to use the C method. B: some versions of Excel 2007 do not display the calculated values of formulas written by Excel::Writer::XLSX. Applying all available Service Packs to Excel should fix this. =head2 write_string( $row, $column, $string, $format ) Write a string to the cell specified by C<$row> and C<$column>: $worksheet->write_string( 0, 0, 'Your text here' ); $worksheet->write_string( 'A2', 'or here' ); 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. The C<$format> parameter is optional. The C method will also handle strings in C format. See also the C programs in the examples directory of the distro. In general it is sufficient to use the C method. However, you may sometimes wish to use the C 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: # Write as a plain string $worksheet->write_string( 'A1', '01209' ); 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 C<@>: # Format as a string. Doesn't change to a number when edited my $format1 = $workbook->add_format( num_format => '@' ); $worksheet->write_string( 'A2', '01209', $format1 ); See also the note about L. =head2 write_rich_string( $row, $column, $format, $string, ..., $cell_format ) The C method is used to write strings with multiple formats. For example to write the string "This is B and this is I" you would use the following: my $bold = $workbook->add_format( bold => 1 ); my $italic = $workbook->add_format( italic => 1 ); $worksheet->write_rich_string( 'A1', 'This is ', $bold, 'bold', ' and this is ', $italic, 'italic' ); The basic rule is to break the string into fragments and put a C<$format> object before the fragment that you want to format. For example: # Unformatted string. 'This is an example string' # Break it into fragments. 'This is an ', 'example', ' string' # Add formatting before the fragments you want formatted. 'This is an ', $format, 'example', ' string' # In Excel::Writer::XLSX. $worksheet->write_rich_string( 'A1', 'This is an ', $format, 'example', ' string' ); String fragments that don't have a format are given a default format. So for example when writing the string "Some B text" you would use the first example below but it would be equivalent to the second: # With default formatting: my $bold = $workbook->add_format( bold => 1 ); $worksheet->write_rich_string( 'A1', 'Some ', $bold, 'bold', ' text' ); # Or more explicitly: my $bold = $workbook->add_format( bold => 1 ); my $default = $workbook->add_format(); $worksheet->write_rich_string( 'A1', $default, 'Some ', $bold, 'bold', $default, ' text' ); As with Excel, only the font properties of the format such as font name, style, size, underline, color and effects are applied to the string fragments. Other features such as border, background, text wrap and alignment must be applied to the cell. The C method allows you to do this by using the last argument as a cell format (if it is a format object). The following example centers a rich string in the cell: my $bold = $workbook->add_format( bold => 1 ); my $center = $workbook->add_format( align => 'center' ); $worksheet->write_rich_string( 'A5', 'Some ', $bold, 'bold text', ' centered', $center ); See the C example in the distro for more examples. my $bold = $workbook->add_format( bold => 1 ); my $italic = $workbook->add_format( italic => 1 ); my $red = $workbook->add_format( color => 'red' ); my $blue = $workbook->add_format( color => 'blue' ); my $center = $workbook->add_format( align => 'center' ); my $super = $workbook->add_format( font_script => 1 ); # Write some strings with multiple formats. $worksheet->write_rich_string( 'A1', 'This is ', $bold, 'bold', ' and this is ', $italic, 'italic' ); $worksheet->write_rich_string( 'A3', 'This is ', $red, 'red', ' and this is ', $blue, 'blue' ); $worksheet->write_rich_string( 'A5', 'Some ', $bold, 'bold text', ' centered', $center ); $worksheet->write_rich_string( 'A7', $italic, 'j = k', $super, '(n-1)', $center ); =begin html

Output from rich_strings.pl

=end html As with C the maximum string size is 32767 characters. See also the note about L. =head2 keep_leading_zeros() This method changes the default handling of integers with leading zeros when using the C method. The C 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 C. One problem with this approach is that occasionally data looks like a number but you don't want it treated as a number. Zip codes and ID 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. To get around this you can use one of three options. Write a formatted number, write the number as a string or use the C method to change the default behaviour of C: # Implicitly write a number, the leading zero is removed: 1209 $worksheet->write( 'A1', '01209' ); # Write a zero padded number using a format: 01209 my $format1 = $workbook->add_format( num_format => '00000' ); $worksheet->write( 'A2', '01209', $format1 ); # Write explicitly as a string: 01209 $worksheet->write_string( 'A3', '01209' ); # Write implicitly as a string: 01209 $worksheet->keep_leading_zeros(); $worksheet->write( 'A4', '01209' ); The above code would generate a worksheet that looked like the following: ----------------------------------------------------------- | | A | B | C | D | ... ----------------------------------------------------------- | 1 | 1209 | | | | ... | 2 | 01209 | | | | ... | 3 | 01209 | | | | ... | 4 | 01209 | | | | ... 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. It should be noted that if the user edits the data in examples C and C the strings will revert back to numbers. Again this is Excel's default behaviour. To avoid this you can use the text format C<@>: # Format as a string (01209) my $format2 = $workbook->add_format( num_format => '@' ); $worksheet->write_string( 'A5', '01209', $format2 ); The C property is off by default. The C method takes 0 or 1 as an argument. It defaults to 1 if an argument isn't specified: $worksheet->keep_leading_zeros(); # Set on $worksheet->keep_leading_zeros( 1 ); # Set on $worksheet->keep_leading_zeros( 0 ); # Set off See also the C method. =head2 write_blank( $row, $column, $format ) Write a blank cell specified by C<$row> and C<$column>: $worksheet->write_blank( 0, 0, $format ); This method is used to add formatting to a cell which doesn't contain a string or number value. Excel differentiates between an "Empty" cell and a "Blank" cell. An "Empty" cell is a cell which doesn't contain data whilst a "Blank" cell is a cell which doesn't contain data but does contain formatting. Excel stores "Blank" cells but ignores "Empty" cells. As such, if you write an empty cell without formatting it is ignored: $worksheet->write( 'A1', undef, $format ); # write_blank() $worksheet->write( 'A2', undef ); # Ignored This seemingly uninteresting fact means that you can write arrays of data without special treatment for C or empty string values. See the note about L. =head2 write_row( $row, $column, $array_ref, $format ) The C 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 C method is then called for each element of the data. For example: @array = ( 'awk', 'gawk', 'mawk' ); $array_ref = \@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] ); Note: For convenience the C method behaves in the same way as C if it is passed an array reference. Therefore the following two method calls are equivalent: $worksheet->write_row( 'A1', $array_ref ); # Write a row of data $worksheet->write( 'A1', $array_ref ); # Same thing As with all of the write methods the C<$format> parameter is optional. If a format is specified it is applied to all the elements of the data array. Array references within the data will be treated as columns. This allows you to write 2D arrays of data in one go. For example: @eec = ( ['maggie', 'milly', 'molly', 'may' ], [13, 14, 15, 16 ], ['shell', 'star', 'crab', 'stone'] ); $worksheet->write_row( 'A1', \@eec ); Would produce a worksheet as follows: ----------------------------------------------------------- | | A | B | C | D | E | ... ----------------------------------------------------------- | 1 | maggie | 13 | shell | ... | ... | ... | 2 | milly | 14 | star | ... | ... | ... | 3 | molly | 15 | crab | ... | ... | ... | 4 | may | 16 | stone | ... | ... | ... | 5 | ... | ... | ... | ... | ... | ... | 6 | ... | ... | ... | ... | ... | ... To write the data in a row-column order refer to the C method below. Any C 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. To find out more about array references refer to C and C in the main Perl documentation. To find out more about 2D arrays or "lists of lists" refer to C. The C 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 C method above. See also the C program in the C directory of the distro. The C method allows the following idiomatic conversion of a text file to an Excel file: #!/usr/bin/perl -w use strict; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new( 'file.xlsx' ); my $worksheet = $workbook->add_worksheet(); open INPUT, 'file.txt' or die "Couldn't open file: $!"; $worksheet->write( $. -1, 0, [split] ) while ; $workbook->close(); =head2 write_col( $row, $column, $array_ref, $format ) The C 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 C method is then called for each element of the data. For example: @array = ( 'awk', 'gawk', 'mawk' ); $array_ref = \@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] ); As with all of the write methods the C<$format> parameter is optional. If a format is specified it is applied to all the elements of the data array. Array references within the data will be treated as rows. This allows you to write 2D arrays of data in one go. For example: @eec = ( ['maggie', 'milly', 'molly', 'may' ], [13, 14, 15, 16 ], ['shell', 'star', 'crab', 'stone'] ); $worksheet->write_col( 'A1', \@eec ); Would produce a worksheet as follows: ----------------------------------------------------------- | | A | B | C | D | E | ... ----------------------------------------------------------- | 1 | maggie | milly | molly | may | ... | ... | 2 | 13 | 14 | 15 | 16 | ... | ... | 3 | shell | star | crab | stone | ... | ... | 4 | ... | ... | ... | ... | ... | ... | 5 | ... | ... | ... | ... | ... | ... | 6 | ... | ... | ... | ... | ... | ... To write the data in a column-row order refer to the C method above. Any C 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. As noted above the C method can be used as a synonym for C and C handles nested array refs as columns. Therefore, the following two method calls are equivalent although the more explicit call to C would be preferable for maintainability: $worksheet->write_col( 'A1', $array_ref ); # Write a column of data $worksheet->write( 'A1', [ $array_ref ] ); # Same thing To find out more about array references refer to C and C in the main Perl documentation. To find out more about 2D arrays or "lists of lists" refer to C. The C 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 C method above. See also the C program in the C directory of the distro. =head2 write_date_time( $row, $col, $date_string, $format ) The C method can be used to write a date or time to the cell specified by C<$row> and C<$column>: $worksheet->write_date_time( 'A1', '2004-05-13T23:20', $date_format ); The C<$date_string> should be in the following format: yyyy-mm-ddThh:mm:ss.sss This conforms to an ISO8601 date but it should be noted that the full range of ISO8601 formats are not supported. The following variations on the C<$date_string> parameter are permitted: 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 Note that the C is required in all cases. A date should always have a C<$format>, otherwise it will appear as a number, see L and L. Here is a typical example: my $date_format = $workbook->add_format( num_format => 'mm/dd/yy' ); $worksheet->write_date_time( 'A1', '2004-05-13T23:20', $date_format ); 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. See also the date_time.pl program in the C directory of the distro. =head2 write_url( $row, $col, $url, $format, $label ) Write a hyperlink to a URL in the cell specified by C<$row> and C<$column>. 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 C<$label> parameter is optional. The label is written using the C method. Therefore it is possible to write strings, numbers or formulas as labels. The C<$format> parameter is also optional and the default Excel hyperlink style will be used if it isn't specified. If required you can access the default url format using the Workbook C method: my $url_format = $workbook->get_default_url_format(); There are four web style URI's supported: C, C, C and C: $worksheet->write_url( 0, 0, 'ftp://www.perl.org/' ); $worksheet->write_url( 'A3', 'http://www.perl.com/' ); $worksheet->write_url( 'A4', 'mailto:jmcnamara@cpan.org' ); You can display an alternative string using the C<$label> parameter: $worksheet->write_url( 1, 0, 'http://www.perl.com/', undef, 'Perl' ); If you wish to have some other cell data such as a number or a formula you can overwrite the cell using another call to C: $worksheet->write_url( 'A1', 'http://www.perl.com/' ); # Overwrite the URL string with a formula. The cell is still a link. # Note the use of the default url format for consistency with other links. my $url_format = $workbook->get_default_url_format(); $worksheet->write_formula( 'A1', '=1+1', $url_format ); There are two local URIs supported: C and C. These are used for hyperlinks to internal worksheet references or external workbook and worksheet references: $worksheet->write_url( 'A6', 'internal:Sheet2!A1' ); $worksheet->write_url( 'A7', 'internal:Sheet2!A1' ); $worksheet->write_url( 'A8', 'internal:Sheet2!A1:B2' ); $worksheet->write_url( 'A9', q{internal:'Sales Data'!A1} ); $worksheet->write_url( 'A10', 'external:c:\temp\foo.xlsx' ); $worksheet->write_url( 'A11', 'external:c:\foo.xlsx#Sheet2!A1' ); $worksheet->write_url( 'A12', 'external:..\foo.xlsx' ); $worksheet->write_url( 'A13', 'external:..\foo.xlsx#Sheet2!A1' ); $worksheet->write_url( 'A13', 'external:\\\\NET\share\foo.xlsx' ); All of the these URI types are recognised by the C method, see above. Worksheet references are typically of the form C. You can also refer to a worksheet range using the standard Excel notation: C. In external links the workbook and worksheet name must be separated by the C<#> character: C. You can also link to a named range in the target worksheet. For example say you have a named range called C in the workbook C you could link to it as follows: $worksheet->write_url( 'A14', 'external:c:\temp\foo.xlsx#my_name' ); Excel requires that worksheet names containing spaces or non alphanumeric characters are single quoted as follows C<'Sales Data'!A1>. If you need to do this in a single quoted string then you can either escape the single quotes C<\'> or use the quote operator C as described in C in the main Perl documentation. Links to network files are also supported. MS/Novell Network files normally begin with two back slashes as follows C<\\NETWORK\etc>. In order to generate this in a single or double quoted string you will have to escape the backslashes, C<'\\\\NETWORK\etc'>. If you are using double quote strings then you should be careful to escape anything that looks like a metacharacter. For more information see C. Finally, you can avoid most of these quoting problems by using forward slashes. These are translated internally to backslashes: $worksheet->write_url( 'A14', "external:c:/temp/foo.xlsx" ); $worksheet->write_url( 'A15', 'external://NETWORK/share/foo.xlsx' ); Note: Excel::Writer::XLSX will escape the following characters in URLs as required by Excel: C<< \s " < > \ [ ] ` ^ { } >> unless the URL already contains C<%xx> style escapes. In which case it is assumed that the URL was escaped correctly by the user and will by passed directly to Excel. Excel limits hyperlink links and anchor/locations to 255 characters each. See also, the note about L. =head2 write_formula( $row, $column, $formula, $format, $value ) Write a formula or function to the cell specified by C<$row> and C<$column>: $worksheet->write_formula( 0, 0, '=$B$3 + B4' ); $worksheet->write_formula( 1, 0, '=SIN(PI()/4)' ); $worksheet->write_formula( 2, 0, '=SUM(B1:B5)' ); $worksheet->write_formula( 'A4', '=IF(A3>1,"Yes", "No")' ); $worksheet->write_formula( 'A5', '=AVERAGE(1, 2, 3, 4)' ); $worksheet->write_formula( 'A6', '=DATEVALUE("1-Jan-2001")' ); Array formulas are also supported: $worksheet->write_formula( 'A7', '{=SUM(A1:B1*A2:B2)}' ); See also the C method below. See the note about L. For more information about writing Excel formulas see L 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 C<$value> is added at the end of the argument list: $worksheet->write( 'A1', '=2+2', $format, 4 ); However, this probably isn't something that you will ever need to do. If you do use this feature then do so with care. =head2 write_array_formula($first_row, $first_col, $last_row, $last_col, $formula, $format, $value) Write an array formula to a cell range. In Excel an array formula is a formula that performs a calculation on a set of values. It can return a single value or a range of values. An array formula is indicated by a pair of braces around the formula: C<{=SUM(A1:B1*A2:B2)}>. If the array formula returns a single value then the C<$first_> and C<$last_> parameters should be the same: $worksheet->write_array_formula('A1:A1', '{=SUM(B1:C1*B2:C2)}'); It this case however it is easier to just use the C or C methods: # Same as above but more concise. $worksheet->write( 'A1', '{=SUM(B1:C1*B2:C2)}' ); $worksheet->write_formula( 'A1', '{=SUM(B1:C1*B2:C2)}' ); For array formulas that return a range of values you must specify the range that the return values will be written to: $worksheet->write_array_formula( 'A1:A3', '{=TREND(C1:C3,B1:B3)}' ); $worksheet->write_array_formula( 0, 0, 2, 0, '{=TREND(C1:C3,B1:B3)}' ); 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. However, using this parameter only writes a single value to the upper left cell in the result array. For a multi-cell array formula where the results are required, the other result values can be specified by using C to write to the appropriate cell: # Specify the result for a single cell range. $worksheet->write_array_formula( 'A1:A3', '{=SUM(B1:C1*B2:C2)}, $format, 2005 ); # Specify the results for a multi cell range. $worksheet->write_array_formula( 'A1:A3', '{=TREND(C1:C3,B1:B3)}', $format, 105 ); $worksheet->write_number( 'A2', 12, format ); $worksheet->write_number( 'A3', 14, format ); In addition, some early versions of Excel 2007 don't calculate the values of array formulas when they aren't supplied. Installing the latest Office Service Pack should fix this issue. See also the C program in the C directory of the distro. Note: Array formulas are not supported by Spreadsheet::WriteExcel. =head2 write_boolean( $row, $column, $value, $format ) Write an Excel boolean value to the cell specified by C<$row> and C<$column>: $worksheet->write_boolean( 'A1', 1 ); # TRUE $worksheet->write_boolean( 'A2', 0 ); # FALSE $worksheet->write_boolean( 'A3', undef ); # FALSE $worksheet->write_boolean( 'A3', 0, $format ); # FALSE, with format. A C<$value> that is true or false using Perl's rules will be written as an Excel boolean C or C value. See the note about L. =head2 store_formula( $formula ) Deprecated. This is a Spreadsheet::WriteExcel method that is no longer required by Excel::Writer::XLSX. See below. =head2 repeat_formula( $row, $col, $formula, $format ) Deprecated. This is a Spreadsheet::WriteExcel method that is no longer required by Excel::Writer::XLSX. In Spreadsheet::WriteExcel it was computationally expensive to write formulas since they were parsed by a recursive descent parser. The C and C methods were used as a way of avoiding the overhead of repeated formulas by reusing a pre-parsed formula. In Excel::Writer::XLSX this is no longer necessary since it is just as quick to write a formula as it is to write a string or a number. The methods remain for backward compatibility but new Excel::Writer::XLSX programs shouldn't use them. =head2 write_comment( $row, $column, $string, ... ) The C 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. The following example shows how to add a comment to a cell: $worksheet->write ( 2, 2, 'Hello' ); $worksheet->write_comment( 2, 2, 'This is a comment.' ); As usual you can replace the C<$row> and C<$column> parameters with an C cell reference. See the note about L. $worksheet->write ( 'C3', 'Hello'); $worksheet->write_comment( 'C3', 'This is a comment.' ); The C method will also handle strings in C format. $worksheet->write_comment( 'C3', "\x{263a}" ); # Smiley $worksheet->write_comment( 'C4', 'Comment ca va?' ); In addition to the basic 3 argument form of C you can pass in several optional key/value pairs to control the format of the comment. For example: $worksheet->write_comment( 'C3', 'Hello', visible => 1, author => 'Perl' ); Most of these options are quite specific and in general the default comment behaves will be all that you need. However, should you need greater control over the format of the cell comment the following options are available: author visible x_scale width y_scale height color start_cell start_row start_col x_offset y_offset font font_size =over 4 =item Option: author This option is used to indicate who is the author of the cell comment. 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. $worksheet->write_comment( 'C3', 'Atonement', author => 'Ian McEwan' ); The default author for all cell comments can be set using the C method (see below). $worksheet->set_comments_author( 'Perl' ); =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 Excel::Writer::XLSX individual comments can be made visible as follows: $worksheet->write_comment( 'C3', 'Hello', visible => 1 ); It is possible to make all comments in a worksheet visible using the C worksheet method (see below). Alternatively, if all of the cell comments have been made visible you can hide individual comments: $worksheet->write_comment( 'C3', 'Hello', visible => 0 ); =item Option: x_scale This option is used to set the width of the cell comment box as a factor of the default width. $worksheet->write_comment( 'C3', 'Hello', x_scale => 2 ); $worksheet->write_comment( 'C4', 'Hello', x_scale => 4.2 ); =item Option: width This option is used to set the width of the cell comment box explicitly in pixels. $worksheet->write_comment( 'C3', 'Hello', width => 200 ); =item Option: y_scale This option is used to set the height of the cell comment box as a factor of the default height. $worksheet->write_comment( 'C3', 'Hello', y_scale => 2 ); $worksheet->write_comment( 'C4', 'Hello', y_scale => 4.2 ); =item Option: height This option is used to set the height of the cell comment box explicitly in pixels. $worksheet->write_comment( 'C3', 'Hello', height => 200 ); =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 Excel::Writer::XLSX or a Html style C<#RRGGBB> colour. See L
. $worksheet->write_comment( 'C3', 'Hello', color => 'green' ); $worksheet->write_comment( 'C4', 'Hello', color => '#FF6600' ); # Orange =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 C is moved to C. $worksheet->write_comment( 'C3', 'Hello', start_cell => 'E2' ); =item Option: start_row This option is used to set the row in which the comment will appear. See the C option above. The row is zero indexed. $worksheet->write_comment( 'C3', 'Hello', start_row => 0 ); =item Option: start_col This option is used to set the column in which the comment will appear. See the C option above. The column is zero indexed. $worksheet->write_comment( 'C3', 'Hello', start_col => 4 ); =item Option: x_offset This option is used to change the x offset, in pixels, of a comment within a cell: $worksheet->write_comment( 'C3', $comment, x_offset => 30 ); =item Option: y_offset This option is used to change the y offset, in pixels, of a comment within a cell: $worksheet->write_comment('C3', $comment, x_offset => 30); =item Option: font This option is used to change the font used in the comment from 'Tahoma' which is the default. $worksheet->write_comment('C3', $comment, font => 'Calibri'); =item Option: font_size This option is used to change the font size used in the comment from 8 which is the default. $worksheet->write_comment('C3', $comment, font_size => 20); =back You can apply as many of these options as you require. B: Excel only displays offset cell comments when they are displayed as "visible". Excel does B display hidden cells as moved when you mouse over them. B. If you specify the height of a row that contains a comment then Excel::Writer::XLSX will adjust the height of the comment to maintain the default or user specified dimensions. However, the height of a row can also be adjusted automatically by Excel if the text wrap property is set or large fonts are used in the cell. This means that the height of the row is unknown to the module at run time and thus the comment box is stretched with the row. Use the C method to specify the row height explicitly and avoid this problem. =head2 show_comments() This method is used to make all cell comments visible when a worksheet is opened. $worksheet->show_comments(); Individual comments can be made visible using the C parameter of the C method (see above): $worksheet->write_comment( 'C3', 'Hello', visible => 1 ); If all of the cell comments have been made visible you can hide individual comments as follows: $worksheet->show_comments(); $worksheet->write_comment( 'C3', 'Hello', visible => 0 ); =head2 set_comments_author() This method is used to set the default author of all cell comments. $worksheet->set_comments_author( 'Perl' ); Individual comment authors can be set using the C parameter of the C method (see above). The default comment author is an empty string, C<''>, if no author is specified. =head2 add_write_handler( $re, $code_ref ) This method is used to extend the Excel::Writer::XLSX write() method to handle user defined data. If you refer to the section on C above you will see that it acts as an alias for several more specific C methods. However, it doesn't always act in exactly the way that you would like it to. One solution is to filter the input data yourself and call the appropriate C method. Another approach is to use the C method to add your own automated behaviour to C. The C method take two arguments, C<$re>, a regular expression to match incoming data and C<$code_ref> a callback function to handle the matched data: $worksheet->add_write_handler( qr/^\d\d\d\d$/, \&my_write ); (In the these examples the C operator is used to quote the regular expression strings, see L for more details). The method is used as follows. say you wished to write 7 digit ID numbers as a string so that any leading zeros were preserved*, you could do something like the following: $worksheet->add_write_handler( qr/^\d{7}$/, \&write_my_id ); sub write_my_id { my $worksheet = shift; return $worksheet->write_string( @_ ); } * You could also use the C method for this. Then if you call C with an appropriate string it will be handled automatically: # Writes 0000000. It would normally be written as a number; 0. $worksheet->write( 'A1', '0000000' ); The callback function will receive a reference to the calling worksheet and all of the other arguments that were passed to C. The callback will see an C<@_> argument list that looks like the following: $_[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(). Your callback should C the return value of the C method that was called or C to indicate that you rejected the match and want C to continue as normal. So for example if you wished to apply the previous filter only to ID values that occur in the first column you could modify your callback function as follows: 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; } } Now, you will get different behaviour for the first column and other columns: $worksheet->write( 'A1', '0000000' ); # Writes 0000000 $worksheet->write( 'B1', '0000000' ); # Writes 0 You may add more than one handler in which case they will be called in the order that they were added. Note, the C method is particularly suited for handling dates. See the C programs in the C directory for further examples. =head2 insert_image( $row, $col, $filename, $x, $y, $x_scale, $y_scale ) This method can be used to insert a image into a worksheet. The image can be in PNG, JPEG or BMP format. The C<$x>, C<$y>, C<$x_scale> and C<$y_scale> parameters are optional. $worksheet1->insert_image( 'A1', 'perl.bmp' ); $worksheet2->insert_image( 'A1', '../images/perl.bmp' ); $worksheet3->insert_image( 'A1', '.c:\images\perl.bmp' ); The parameters C<$x> and C<$y> can be used to specify an offset from the top left hand corner of the cell specified by C<$row> and C<$col>. The offset values are in pixels. $worksheet1->insert_image('A1', 'perl.bmp', 32, 10); 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. The parameters C<$x_scale> and C<$y_scale> can be used to scale the inserted image horizontally and vertically: # Scale the inserted image: width x 2.0, height x 0.8 $worksheet->insert_image( 'A1', 'perl.bmp', 0, 0, 2, 0.8 ); Note: you must call C or C before C if you wish to change the default dimensions of any of the rows or columns that the image occupies. The height of a row can also change if you use a font that is larger than the default. This in turn will affect the scaling of your image. To avoid this you should explicitly set the height of the row using C if it contains a font size that will change the row height. BMP images must be 24 bit, true colour, bitmaps. In general it is best to avoid BMP images since they aren't compressed. =head2 insert_chart( $row, $col, $chart, $x, $y, $x_scale, $y_scale ) This method can be used to insert a Chart object into a worksheet. The Chart must be created by the C Workbook method and it must have the C option set. my $chart = $workbook->add_chart( type => 'line', embedded => 1 ); # Configure the chart. ... # Insert the chart into the a worksheet. $worksheet->insert_chart( 'E2', $chart ); See C for details on how to create the Chart object and L for details on how to configure it. See also the C programs in the examples directory of the distro. The C<$x>, C<$y>, C<$x_scale> and C<$y_scale> parameters are optional. The parameters C<$x> and C<$y> can be used to specify an offset from the top left hand corner of the cell specified by C<$row> and C<$col>. The offset values are in pixels. $worksheet1->insert_chart( 'E2', $chart, 3, 3 ); The parameters C<$x_scale> and C<$y_scale> can be used to scale the inserted chart horizontally and vertically: # Scale the width by 120% and the height by 150% $worksheet->insert_chart( 'E2', $chart, 0, 0, 1.2, 1.5 ); =head2 insert_shape( $row, $col, $shape, $x, $y, $x_scale, $y_scale ) This method can be used to insert a Shape object into a worksheet. The Shape must be created by the C Workbook method. my $shape = $workbook->add_shape( name => 'My Shape', type => 'plus' ); # Configure the shape. $shape->set_text('foo'); ... # Insert the shape into the a worksheet. $worksheet->insert_shape( 'E2', $shape ); See C for details on how to create the Shape object and L for details on how to configure it. The C<$x>, C<$y>, C<$x_scale> and C<$y_scale> parameters are optional. The parameters C<$x> and C<$y> can be used to specify an offset from the top left hand corner of the cell specified by C<$row> and C<$col>. The offset values are in pixels. $worksheet1->insert_shape( 'E2', $chart, 3, 3 ); The parameters C<$x_scale> and C<$y_scale> can be used to scale the inserted shape horizontally and vertically: # Scale the width by 120% and the height by 150% $worksheet->insert_shape( 'E2', $shape, 0, 0, 1.2, 1.5 ); See also the C programs in the examples directory of the distro. =head2 insert_button( $row, $col, { %properties }) The C method can be used to insert an Excel form button into a worksheet. This method is generally only useful when used in conjunction with the Workbook C method to tie the button to a macro from an embedded VBA project: my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' ); ... $workbook->add_vba_project( './vbaProject.bin' ); $worksheet->insert_button( 'C2', { macro => 'my_macro' } ); The properties of the button that can be set are: macro caption width height x_scale y_scale x_offset y_offset =over =item Option: macro This option is used to set the macro that the button will invoke when the user clicks on it. The macro should be included using the Workbook C method shown above. $worksheet->insert_button( 'C2', { macro => 'my_macro' } ); The default macro is C where X is the button number. =item Option: caption This option is used to set the caption on the button. The default is C