perl脚本读取excel文件的特定列中的所有元素

时间:2016-08-28 00:33:04

标签: regex perl cpan

我编写了一个perl脚本来读取excel文件中列的特定单元格值。

use strict;
use warnings;
use feature 'say';
use Spreadsheet::Read;
use Spreadsheet::ParseExcel;

my $workbook = ReadData ("C::/Users/Tej/Work.xlsx");
print $workbook->[6]{D4} . "\n";

直到这里都好。我想编写逻辑来读取D列下的所有单元格值,直到该列中有值。任何人都可以帮助我。

由于

1 个答案:

答案 0 :(得分:3)

假设我的数据是:

enter image description here

一种方法是做你想要的(因为'D'对应第4列):

$ cat a.pl
use strict;
use warnings;
use feature 'say';
use Spreadsheet::Read;
use Spreadsheet::ParseExcel;
use Data::Dumper;

my $workbook = ReadData ("/tmp/file.xls", parser => "xls");
print Dumper($workbook->[1]{cell}[4]);
foreach my $cell (@{$workbook->[1]{cell}[4]}) {
   if ($cell) {
       print $cell . "\n";
   }
}


$ perl a.pl
$VAR1 = [
          undef,
          'grid',
          1115,
          1512,
          212
        ];
grid
1115
1512
212

另一种方法是使用Spreadsheet::BasicReadNamedCol

$ cat a.pl
use strict;
use warnings;
use feature 'say';
use Spreadsheet::BasicReadNamedCol;
use Data::Dumper;
my @columnHeadings = (
    'grid',
);
my $workbook = new Spreadsheet::BasicReadNamedCol("/tmp/file.xls");
$workbook->setColumns(@columnHeadings);
while (my $data = $workbook->getNextRow()) {
    print "@{$data}[0]\n";
}
$ perl a.pl
grid
1115
1512
212