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

Reading Excel Files in Perl

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.

1. Installation:

First, you need to install the necessary module. Using CPAN:

cpan install Spreadsheet::ParseExcel

2. Basic Usage:

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";
        }
    }
}

Explanation:

  • 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.

  1. Excel file parsing in Perl example:

    • Description: Parsing Excel files involves extracting and processing data from spreadsheet files.
    • Code 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";
          }
      }
      
  2. Using Spreadsheet::Read module in Perl:

    • Description: The Spreadsheet::Read module in Perl provides a convenient way to read data from various spreadsheet formats, including Excel.
    • Code Example:
      use Spreadsheet::Read;
      
  3. Reading and extracting data from Excel in Perl:

    • Description: Reading and extracting data from an Excel file involves using the Spreadsheet::Read module to access and process spreadsheet content.
    • Code Example (combining with #1):
      # ... (same as Excel file parsing example)
      
  4. Perl Excel reader script:

    • Description: A Perl Excel reader script is designed to read and process data from Excel files.
    • Code Example (a simplified 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";
          }
      }
      
  5. Excel file processing with Perl:

    • Description: Excel file processing in Perl involves opening an Excel file, reading its content, and performing operations on the data.
    • Code Example (similar to #4):
      # ... (same as Perl Excel reader script)
      
  6. Handling different Excel file formats in Perl:

    • Description: The Spreadsheet::Read module supports various Excel file formats, such as XLSX, XLS, and others.
    • Code Example:
      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)
      
  7. Importing Excel data into Perl arrays or hashes:

    • Description: You can import Excel data into arrays or hashes for further manipulation.
    • Code Example (building on #3):
      # ... (same as Reading and extracting data from Excel in Perl)
      
  8. Excel file I/O with Perl:

    • Description: Excel file I/O involves reading from and writing to Excel files. The Spreadsheet::Read module handles both input and output operations.
    • Code Example (writing to an Excel file):
      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();