Perl Excel example - A Perl program to parse Microsoft Excel XLS files

A co-worker, Chris Smith, created a very elegant Perl program to parse a Microsoft Excel XLS file for me some time ago. I recently modified that program to convert what was essentially a "glossary" in an Access database.

I first saved the Access database table as an Excel file. Then I used the following Perl program to extract the contents of the second and third columns from that file, and write them out to another file. The output file was in a Wiki (technically TWiki) format.

Perl Excel example

I hope the Perl program shown (named Perl_XLS_Reader.pl) below makes sense.

#!/usr/local/bin/perl

#######################################################################
##
#######################################################################

##  Define Variables
#######################################################################

##  Path Preferences
$path_to_excel_file  = "C:/Work2/Glossary_AccessToWiki/Glossary.xls";
$path_to_output_file = "C:/Work2/Glossary_AccessToWiki/Glossary.wiki";

##  Begin Program
#######################################################################

use Spreadsheet::ParseExcel;

do_main();

##  Subroutines
#######################################################################

sub do_main
{
  my ($oBook, $iR, $iC, $oWkS, $oWkC);
  $oBook = Spreadsheet::ParseExcel::Workbook->Parse($path_to_excel_file);
  open(FILE_OUT, "> $path_to_output_file");
  foreach my $oWkS (@{$oBook->{Worksheet}})
  {
    for (my $iR = $oWkS->{MinRow}; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++)
    {
      print FILE_OUT "   [[" . $oWkS->{Cells}[$iR][1]->Value . "]]: " . $oWkS->{Cells}[$iR][2]->Value . "\n";
    }
  }
  close(FILE_OUT);
}

If anyone would like to have an explanation of how this Perl XLS reader program works, just leave me a message below, and I'll be glad to write more. In the meantime, feel free to use this Perl program for your own Excel XLS file reading needs.