【问题标题】:column header in excel file with Perl带有Perl的excel文件中的列标题
【发布时间】:2012-11-22 12:03:32
【问题描述】:

我有一个包含许多列的 Excel 文件,但我只需要对其中的 3 列进行一些计算。

我正在使用SpreadSheet::ParseExcel 来遍历我的 Excel 文件。如何使用 perl 指定我想要的列?

【问题讨论】:

    标签: perl excel


    【解决方案1】:
    #!/usr/bin/perl -w
    
    use strict;
    use Spreadsheet::ParseExcel;
    
    my $parser   = Spreadsheet::ParseExcel->new();
    my $workbook = $parser->parse('Book1.xls');
    
    if ( !defined $workbook ) {
        die $parser->error(), ".\n";
    }
    
    for my $worksheet ( $workbook->worksheets() ) {
    
        my ( $row_min, $row_max ) = $worksheet->row_range();
        my @columns = (1,5,6) # enter your 3 columns here
    
        for my $row ( $row_min .. $row_max ) {
            for my $col (@columns) {
    
                my $cell = $worksheet->get_cell( $row, $col );
                next unless $cell;
    
                # do the calculations here
                print "Row, Col    = ($row, $col)\n";
                print "Value       = ", $cell->value(),       "\n";
                print "Unformatted = ", $cell->unformatted(), "\n";
                print "\n";
                # operations done
            }
        }
    }
    

    来源:Spreadsheet::ParseExcel 模块的文档。

    【讨论】:

    • 但是有没有办法得到他们的名字而不是他们的号码?
    • 获取该单元格的值。
    • 读取标题行,将名称散列到列号,然后在数据行中,get_cell($row, $col_for_header{'SomeHeader'})
    • 这里的代码几乎相同:Spreadsheet::ParseExcel。如果代码被改编,为什么不引用它的来源?
    • @Kenosis:是的,我从官方文档中获取了它,以便 OP 易于理解。我忘了在那里添加源,我现在已经编辑了包含源的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多