perl - 解析excel

用win32的方式 不推荐

use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');

my $Book = $Excel->Workbooks->Open("c:/test.xls");

my $Sheet = $Book->Worksheets(1);
foreach my $row (1..4)
{
foreach my $col (1..3)
{
# skip empty cells
next unless defined $Sheet->Cells($row,$col)->{'Value'};
printf "At ($row, $col) the value is %s \n",
$Sheet->Cells($row,$col)->{'Value'},
$Sheet->Cells($row,$col)->{'Formula'};
}
}
$Book->Close;


[b]用 Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel 模块[/b]
http://blog.csdn.net/fl49809045/archive/2010/06/01/5638396.aspx
新建excel

use strict;
use warnings;

use Encode;
use Spreadsheet::WriteExcel;

# 设置new一个对象出来,并写上需要存成什么名字的xls
my $xls = Spreadsheet::WriteExcel->new( "c:/fukaiss.xls" );

# xml的内容名字
my $xlsContent = $xls->add_worksheet( 'report' );

# 这是对格式的设置,我们可以设置一个标题的,一个内容的,
# 我现在只设置一个内容
my $contentStyle = $xls->add_format();
$contentStyle->set_size( 8 );
$contentStyle->set_bold(); #设置字体为粗体
$contentStyle->set_align( 'center' );#设置单元格居中
$contentStyle->set_text_wrap(); #是否回车换行
$contentStyle->set_color('red'); #设置单元格前景色为红色

# 写表内容(格式是使用上面添加的表格式)
# 这个中的A,B,C是设置的excel中上面行的字母
# 这个地方中的文字我用了decode这样中文才能正常显示
# 最后面的contentStyle是我上面设置的行风格

$xlsContent->write( "A1", decode( 'utf8', "name" ), $contentStyle );
$xlsContent->write( "B1", decode( 'utf8', "time" ), $contentStyle );
$xlsContent->write( "C2", decode( 'utf8', "language" ), $contentStyle );
#$xlsContent->write($row, $col, 'Hi Excel!', $format); #行,列,内容,格式

#这是关闭,上面的内容设置成循环就能生成很多行了
$xls->close();



读取excel
[code]
#!/usr/bin/perl
use Spreadsheet::Read;
use Data::Dumper;
use Smart::Comments;

my $file = 'c:/fukaiss.xls';
my $spreadsheet = ReadData( $file) or die "Cannot read file ";#指定读的文件名
my $sheet_count = $spreadsheet->[0]{sheets} or die "No sheets in $file\n"; #这个是查有几个sheet
for my $sheet_index (1 .. $sheet_count){
my $sheet = $spreadsheet->[$sheet_index] or next;
printf("%s - %2d: [%-s] %3d Cols, %5d Rows\n",
$file,$sheet_index,$sheet->{label},$sheet->{maxcol},$sheet->{maxrow});#label是sheet名
for my $row (1 .. $sheet->{maxrow}) {
print join "\t" => map {
my $data = $sheet->{cell}[$_][$row] ;
defined $data ? $data : "-";
}1 .. $sheet->{maxcol};
print "\n";
};
}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值