[Omaha.pm] Excel -> text file

Jay Hannah jhannah at omnihotels.com
Wed Aug 17 08:38:18 PDT 2005


 
I got sick of dealing with an archive full of Excel spreadsheets. Takes forever to find anything. So I spent 10m writing an .xls to text file dumping program.

Now I can grep my archive! Yay!

j



#!/usr/bin/perl

use strict;
use Spreadsheet::ParseExcel;

my $file = $ARGV[0];
die "Can't read file $file" unless (-r $file);
my $newfile = $file;
$newfile =~ s/\.xls$/\.unl/;
open (OUT, ">$newfile") or die "Can't write $newfile";

my $oExcel = Spreadsheet::ParseExcel->new();
my $oBook = $oExcel->Parse($file) or die "Can't parse Excel file '$file'";
my $oWks = $oBook->{Worksheet}[0];
my $row;
foreach $row ($oWks->{MinRow} .. $oWks->{MaxRow}) {
   next if ($row == 0);   # Header row
   last unless ($oWks->{Cells}[$row][0]);   # Blank line means stop
   foreach my $col (0..20) {
      my $value;
      if ($oWks->{Cells}[$row][$col]) {
         $value = $oWks->{Cells}[$row][$col]->Value;
      }
      print OUT "$value|";
   }
   print OUT "\n";
}

close OUT;





More information about the Omaha-pm mailing list