使用perl动态计算excel行

时间:2012-11-14 19:56:16

标签: perl excel

我正在编写一个解析Excel文件的Perl脚本。此脚本的目的是计算第1列中每个单元格值,即第2列中的值。

每个示例看起来像这样的Excel文件:

12    abc
12    abc
12    efg
12    efg
13    hij
13    hij
13    klm

我的脚本会返回:

对于单元格值12,我有:

2 values "abc", 2 values "efg" and for cell value 13 i have : 2 values "hij" and 1 value "klm". 

我的脚本看起来像这样(我从perl doc中拿了这个例子):

 use Spreadsheet::XLSX;

 my $excel = Spreadsheet::XLSX -> new ('Book1.xlsx');

 foreach my $sheet (@{$excel -> {Worksheet}}) {

    printf("Sheet: %s\n", $sheet->{Name});

    $sheet -> {MaxRow} ||= $sheet -> {MinRow}; 

     foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {

            $sheet -> {MaxCol} ||= $sheet -> {MinCol};

            foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {

                    my $cell = $sheet -> {Cells} [$row] [$col];

                    if ($cell) {
                        #here I should count the cell values 
                    }
                print $cell;
            }

    }


 }

我不知道怎么做,因为我以前从未使用过perl,而且我找不到符合我想要的在线示例。任何帮助将非常感激。 感谢

2 个答案:

答案 0 :(得分:0)

使用哈希。用$hash{$column1}{$column2}++计算。迭代键并打印计数值。是的,我给你做了一些工作来填充column1,column2的值,并迭代哈希。

答案 1 :(得分:0)

以下评论的脚本可能会有所帮助:

use strict;
use warnings;
use Spreadsheet::XLSX;
use Data::Dumper;

# No need to iterate through columns, so set val for col 1
my $col1 = 0;
my %hash;

my $excel = Spreadsheet::XLSX->new('Book1.xlsx');

# Just get the first sheet
my $sheet = ${ $excel->{Worksheet} }[0];

# Calculate the range of rows
$sheet->{MaxRow} ||= $sheet->{MinRow};

# Iterate through each row
foreach my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) {

    # The cell in column 1
    my $cell = $sheet->{Cells}[$row][$col1];

    if ($cell) {

        # The adjacent cell in column 2
        my $adjacentCell = $sheet->{Cells}[$row][ $col1 + 1 ];

        # Use a hash of hashes
        $hash{ $cell->{Val} }{ $adjacentCell->{Val} }++;
    }
}

# Numerically sort the keys; the value is a hash reference
for my $key1 ( sort { $a <=> $b } keys %hash ) {
    print "For cell value $key1: ";

    # Dereference the hash reference and get the keys/values
    while ( my ( $key2, $val2 ) = each %{ $hash{$key1} } ) {
        print qq{$val2 value(s) "$key2" };
    }
    print "\n";
}

# Show the hash structure
print "\n", Dumper \%hash;

输出:

For cell value 12: 2 value(s) "abc" 2 value(s) "efg" 
For cell value 13: 1 value(s) "klm" 2 value(s) "hij" 

$VAR1 = {
          '13' => {
                    'klm' => 1,
                    'hij' => 2
                  },
          '12' => {
                    'abc' => 2,
                    'efg' => 2
                  }
        };

您可以执行以下操作来显示与键“13”关联的值:

# Show only the value(s) for key '13'
print "For cell value 13: ";

# Dereference the hash reference for key '13' and get the keys/values
while ( my ( $key2, $val2 ) = each %{ $hash{13} } ) {
    print qq{$val2 value(s) "$key2" };
}

输出:

For cell value 13: 1 value(s) "klm" 2 value(s) "hij"