PerlからExcelを読む

Win32::OLEモジュールを使うとPerlからExcelやWordのファイルを読み書きができる。
Excelのファイルを読んで特定のカラムを表示するコードは以下の通り。

use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

# もしExcelが既に実行中であれば、既存のインスタンスを利用する
my $excel = undef;
eval {$excel = Win32::OLE->GetActiveObject('Excel.Application')};
die "Excel not installed" if $@;
unless (defined $excel) {
	$excel = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
	or die "Oops, cannot start Excel";
}

# 現在のアクティブなワークブックを取得
my $book = $excel->Workbooks->Open("test.xls");

#ワークシートを指定する
my $sheet = $book->Worksheets("hoge");

#カラムの内容を出力する
my $array_ref = $sheet->Columns("B:B")->{'Value'};

#中身を出力する
foreach my $ref_array(@$array_ref){
	foreach my $item(@$ref_array){
		if(defined($item)){
			print $item,"\n";
		} else{
			print "\n";
		}
	}
}

$book->Close();

とは言ってもExcelのデータがどうやって構築されているか知らないのでかなりチンプンカンプン。。。