Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Using Perl to generate Excel files can be quite useful for automating reporting tasks, data extraction, or even simple data manipulations. The most popular Perl module for this purpose is Excel::Writer::XLSX
. This module allows you to create a new Excel file and write data to it.
Below is a step-by-step tutorial on how to create Excel files using Perl:
Firstly, you need to install the module. You can do this using CPAN:
cpan Excel::Writer::XLSX
Here's a simple script that demonstrates creating an Excel file:
use strict; use warnings; use Excel::Writer::XLSX; # Create a new Excel workbook my $workbook = Excel::Writer::XLSX->new('perl_excel_tutorial.xlsx'); # Add a worksheet to the workbook my $worksheet = $workbook->add_worksheet(); # Write data to the worksheet $worksheet->write(0, 0, 'Hello'); $worksheet->write(0, 1, 'World'); # Close the workbook (This is important!) $workbook->close();
Run the script, and you'll get a new file perl_excel_tutorial.xlsx
with the words "Hello" and "World" written in the first row.
You can add various formats to your data:
use strict; use warnings; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('perl_excel_formatting.xlsx'); my $worksheet = $workbook->add_worksheet(); # Define formats my $bold = $workbook->add_format(); $bold->set_bold(); my $red_bg = $workbook->add_format(); $red_bg->set_bg_color('red'); # Write data with formats $worksheet->write(0, 0, 'Hello', $bold); $worksheet->write(0, 1, 'World', $red_bg); $workbook->close();
You can easily add multiple worksheets to your workbook:
use strict; use warnings; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('perl_excel_sheets.xlsx'); my $sheet1 = $workbook->add_worksheet('First Sheet'); my $sheet2 = $workbook->add_worksheet('Second Sheet'); $sheet1->write(0, 0, 'This is the first sheet.'); $sheet2->write(0, 0, 'This is the second sheet.'); $workbook->close();
Formulas can also be added to the cells:
use strict; use warnings; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('perl_excel_formulas.xlsx'); my $worksheet = $workbook->add_worksheet(); $worksheet->write(0, 0, 10); $worksheet->write(1, 0, 20); $worksheet->write(2, 0, '=A1+A2'); $workbook->close();
When you open the generated Excel file, you'll see the cells A1 and A2 populated with numbers and A3 with the sum of A1 and A2.
There's a lot more you can do with Excel::Writer::XLSX
, like adding charts, images, or even handling larger data sets. The above examples are just a starting point, and you can find extensive documentation and more advanced examples on CPAN or in the module's documentation.
Writing data to Excel in Perl:
use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('output.xlsx'); my $worksheet = $workbook->add_worksheet(); # Write data to Excel $worksheet->write('A1', 'Hello'); $worksheet->write('B1', 'World'); $workbook->close();
Perl Excel module for spreadsheet generation:
Excel::Writer::XLSX
module for creating Excel files.use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('output.xlsx'); my $worksheet = $workbook->add_worksheet(); # Write data to Excel $worksheet->write('A1', 'Hello'); $worksheet->write('B1', 'World'); $workbook->close();
Creating Excel charts with Perl:
Excel::Writer::XLSX
.use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('chart_example.xlsx'); my $worksheet = $workbook->add_worksheet(); # Add data to Excel my @data = (1, 2, 3, 4, 5); $worksheet->write('A1', \@data); # Add chart my $chart = $workbook->add_chart(type => 'line'); $chart->add_series(values => '=Sheet1!$A$1:$A$5'); $worksheet->insert_chart('C1', $chart); $workbook->close();
Formatting cells in Perl Excel files:
use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('formatted_cells.xlsx'); my $worksheet = $workbook->add_worksheet(); # Apply formatting my $format = $workbook->add_format(color => 'red', bold => 1); $worksheet->write('A1', 'Formatted Text', $format); $workbook->close();
Adding formulas to Excel using Perl:
use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('formulas.xlsx'); my $worksheet = $workbook->add_worksheet(); # Add data $worksheet->write('A1', 10); $worksheet->write('A2', 20); # Add formula $worksheet->write_formula('A3', '=SUM(A1:A2)'); $workbook->close();
Handling multiple sheets in Perl Excel files:
use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('multiple_sheets.xlsx'); # Sheet 1 my $worksheet1 = $workbook->add_worksheet('Sheet1'); $worksheet1->write('A1', 'Sheet 1 Data'); # Sheet 2 my $worksheet2 = $workbook->add_worksheet('Sheet2'); $worksheet2->write('A1', 'Sheet 2 Data'); $workbook->close();
Perl module for Excel file manipulation:
Excel::Writer::XLSX
module for Excel manipulation.use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('excel_manipulation.xlsx'); my $worksheet = $workbook->add_worksheet(); # Write data $worksheet->write('A1', 'Hello'); $worksheet->write('B1', 'World'); $workbook->close();
Working with images in Perl Excel:
Excel::Writer::XLSX
.use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('image_example.xlsx'); my $worksheet = $workbook->add_worksheet(); # Insert image $worksheet->insert_image('A1', 'image.jpg'); $workbook->close();
Exporting data to Excel from Perl script:
use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('export_data.xlsx'); my $worksheet = $workbook->add_worksheet(); # Data to export my @data = ('Name', 'Age', 'Location'); $worksheet->write('A1', \@data); $workbook->close();