A Perl program to parse Microsoft Excel XLS files

By Alvin J. Alexander, devdaily.com

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

I hope the Perl program shown below makes sense. If not, send me an email, and I'll see if I can help.

#!/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);
}

devdaily logo