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
Perl, a versatile scripting language, can be used to read Excel files using various CPAN modules. One of the popular modules to achieve this is Spreadsheet::ParseExcel
.
Here's a basic tutorial to read Excel files in Perl using Spreadsheet::ParseExcel
.
First, you need to install the necessary module. Using CPAN:
cpan install Spreadsheet::ParseExcel
The following script reads an Excel file (example.xls
) and prints the content of each cell:
#!/usr/bin/perl use strict; use warnings; use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('example.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; } for my $worksheet ( $workbook->worksheets() ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; print "Row, Col = ($row, $col)\n"; print "Value = ", $cell->value(), "\n"; print "Unformatted = ", $cell->unformatted(), "\n"; print "\n"; } } }
Spreadsheet::ParseExcel->new()
: Creates a new parser instance.$parser->parse('example.xls')
: Parses the Excel file. Replace 'example.xls'
with your filename.$workbook->worksheets()
: Iterates through all the worksheets in the Excel file.$worksheet->row_range()
and $worksheet->col_range()
: Determine the range of rows and columns that have data in the worksheet.$worksheet->get_cell($row, $col)
: Retrieves the cell object for the specified row and column, if it exists.$cell->value()
: Returns the formatted value of the cell.$cell->unformatted()
: Returns the raw value of the cell.This script can be expanded upon based on the specifics of what you wish to extract or manipulate from the Excel file.
Note: Spreadsheet::ParseExcel
only supports Excel files up to Excel 2003 (.xls
). For .xlsx
(Excel 2007 and newer) files, you'd want to look into the Spreadsheet::XLSX
or Spreadsheet::ParseXLSX
modules.
Excel file parsing in Perl example:
use Spreadsheet::Read; my $workbook = ReadData('example.xlsx'); for my $sheet (1 .. $workbook->[0]{sheets}) { for my $row (1 .. $workbook->[$sheet]{maxrow}) { my @data = Spreadsheet::Read::cellrow($workbook->[$sheet], $row); # Process @data for each row print join(', ', @data), "\n"; } }
Using Spreadsheet::Read module in Perl:
use Spreadsheet::Read;
Reading and extracting data from Excel in Perl:
# ... (same as Excel file parsing example)
Perl Excel reader script:
use Spreadsheet::Read; my $workbook = ReadData('example.xlsx'); for my $sheet (1 .. $workbook->[0]{sheets}) { for my $row (1 .. $workbook->[$sheet]{maxrow}) { my @data = Spreadsheet::Read::cellrow($workbook->[$sheet], $row); # Process @data for each row print join(', ', @data), "\n"; } }
Excel file processing with Perl:
# ... (same as Perl Excel reader script)
Handling different Excel file formats in Perl:
use Spreadsheet::Read; my $workbook = ReadData('example.xlsx'); # Works with XLSX # or for XLS files # my $workbook = ReadData('example.xls'); # ... (rest of the code remains the same)
Importing Excel data into Perl arrays or hashes:
# ... (same as Reading and extracting data from Excel in Perl)
Excel file I/O with Perl:
use Spreadsheet::Write; my $workbook = Spreadsheet::Write->new('output.xlsx'); my $worksheet = $workbook->add_worksheet(); $worksheet->write('A1', 'Name'); $worksheet->write('B1', 'Age'); $worksheet->write('A2', 'Alice'); $worksheet->write('B2', 25); $workbook->close();