Skip to content

Excel::Writer::XLSX

jmcnamara edited this page Oct 4, 2011 · 2 revisions

Excel::Writer::XLSX

Excel::Writer::XLSX - Create a new file in the Excel 2007+ XLSX format.

VERSION

This document refers to version 0.27 of Excel::Writer::XLSX, released October 2, 2011.

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)' );

DESCRIPTION

The Excel::Writer::XLSX 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.

Excel::Writer::XLSX and Spreadsheet::WriteExcel

Excel::Writer::XLSX uses the same interface as the Spreadsheet::WriteExcel 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 "Compatibility with Spreadsheet::WriteExcel".

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.

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 three easy steps:

1. Create a new Excel workbook (i.e. file) using new().

2. Add a worksheet to the new workbook using add_worksheet().

3. Write to the worksheet using write().

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

This will create an Excel file called perl.xlsx with a single worksheet and the text 'Hi Excel!' in the relevant cell. And that's it. Okay, so there is actually a zeroth step as well, but use module goes without saying. There are many examples that come with the distribution and which you can use to get you started. See EXAMPLES.

Those of you who read the instructions first and assemble the furniture afterwards will know how to proceed. ;-)