excel的读操作

来源:互联网 发布:中文域名收录 编辑:程序博客网 时间:2024/06/16 11:03

zz : http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel-0.59/lib/Spreadsheet/ParseExcel.pm#NAME

#!/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 ( $col_min, $col_max ) = $worksheet->col_range();        for my $row ( $row_min .. $row_max ) {            for my $col ( $col_min .. $col_max ) {                my $cell = $worksheet->get_cell( $row, $col );                next unless $cell;                print "Row, Col    = ($row, $col)\n";                print "Value       = ", $cell->value(),       "\n";                print "Unformatted = ", $cell->unformatted(), "\n";                print "\n";            }        }    }

这里注意几个细节:

1,formy$worksheet($workbook->worksheets())

如果知道是那一个worksheet,则可以直接写名字,如:

my $worksheet = $workbook->worksheet('eb');


2,

my $cell = $worksheet->get_cell( $row, $col );next unless $cell;

如果没有这句话的话,遇到某一个表格是空时,会出错,直接退出了。


0 0
原创粉丝点击