2008年11月25日星期二

Perl:用Spreadsheet::Read读Excel的.xls表格文件

见:Spreadsheet::Read

#!/usr/bin/perl

use 5.010;
use utf8;
use Spreadsheet::Read;

# 指定系统locale
my $locale = $^O ne 'MSWin32' ? 'utf8' : 'cp936';
binmode( STDOUT, ":encoding($locale)" );

# 读入xls文件进行解析
my $file = $ARGV[0];
my $ref  = ReadData($file);

for my $i ( 1 .. $#$ref ) {

   # 第$i张表,可以取出表名,最大行/列数
   my $sheet = $ref->[$i];
   say $sheet->{label}, ",", $sheet->{maxrow}, ",", $sheet->{maxcol};
   next unless ( $sheet->{maxrow} and $sheet->{maxcol} );

   # 行优先读取表中所有单元
   for my $row ( 1 .. $sheet->{maxrow} ) {

       # 按列读取第$row行的所有单元格
       for my $col ( 1 .. $sheet->{maxcol} ) {

           # 读取($row, $col)处单元格的内容
           # 注意cr2cel的参数顺序是$col在前
           my $cell = cr2cell( $col, $row );
           say $sheet->{$cell};
       }    # $col
   }    # $row
}    # $i 
与技术无关的废话:WZ同学真是给我加工作量啊,整个侵权名单那个长啊,让我们忍无可忍的跑去研究xls转html了。。。

没有评论:

发表评论