【问题标题】:perl script to read all the elements in particular column of an excel fileperl 脚本读取 excel 文件特定列中的所有元素
【发布时间】:2016-08-28 13:36:20
【问题描述】:

我编写了一个 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 列下的所有单元格值,直到该列中有值。谁能帮帮我。

谢谢

【问题讨论】:

    标签: regex perl cpan


    【解决方案1】:

    假设我的数据是:

    一种方法是做你想做的事(因为“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
    

    【讨论】:

    • 非常感谢您的详细回答。它有帮助。
    猜你喜欢
    • 2023-03-12
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    相关资源
    最近更新 更多