frei

旧「anney's room」からブログ「frei」だけ引っ越し&残しました

perl でExcelファイル読込。

訳あって必要になったのでメモ。

#!/usr/bin/perl -w

use strict;

use Spreadsheet::ParseExcel;

my $targetDir = '/home/anigon/excel';

my $objExcel = new Spreadsheet::ParseExcel;

my $objBook = '';

my @result = ();

foreach my $fileName (`ls -1 ${targetDir}`) { # ほんとはこんな横着しちゃダメだけどね

chomp $fileName;

next if $fileName !~ /xls$/;

$objBook = $objExcel->Parse("${targetDir}/${fileName}");

&_setData($objBook, \@result);

}

foreach my $sheet (@result) {

print '"', (join '","', @{$sheet}), '"', "\n";

}

exit;

sub _setData ($\@) {

my $objBook = shift;

my $refResultArray = shift;

my @tmpResult = ();

my $objCell = $objBook->{Worksheet}[0]; # マジックナンバーも本当はあんま使わない方がいい

$tmpResult[0] = $objCell->{Cells}[1][5]->Value; # それぞれ抜き出したいセルの場所を

$tmpResult[1] = $objCell->{Cells}[5][4]->Value; # {Cells}[何セル目}{何行目} で指定

$tmpResult[2] = $objCell->{Cells}[11][4]->Value;

push @{$refResultArray}, [@tmpResult];

}

# end of this script