Perl 读写excel

1.读取excel:
use strict;
use Spreadsheet::ParseExcel;

my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse('Book1.xls');

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";
}
}
}

2.写excel:

use Spreadsheet::WriteExcel;
#************生成Excel文档****************
my $xl = Spreadsheet::WriteExcel->new("TEST.xls");
#生成Excel表
my $xlsheet = $xl->add_worksheet(“TestSheet”);
#添加格式(表头)
$rptheader = $xl->add_format(); # Add a format
$rptheader->set_bold();
$rptheader->set_size('12');
$rptheader->set_align('center');
#添加格式(表内容)
$normcell = $xl->add_format(); # Add a format
$normcell->set_size('9');
$normcell->set_align('center');
$normcell->set_bg_color('22');
#设置列的宽度
$xlsheet->set_column('A:A',10);
$xlsheet->set_column('B:B',12);
$xlsheet->set_column('C:C',17);
#写表头(格式是使用上面添加的表头格式)
$xlsheet->write("A2","Number", $rptheader);
$xlsheet->write("B2","Name",$rptheader);
$xlsheet->write("C2","Language",$rptheader);
#写内容(格式是使用上面添加的表内容格式)
$xlsheet->write("A3","1", $normcell);
$xlsheet->write("B3","Test",$normcell);
$xlsheet->write("C3","Perl",$normcell);
#关闭操作excel的对象.
$xl->close();
#!/usr/bin/perl
use Spreadsheet::WriteExcel;
#************生成Excel文档****************
my $xl = Spreadsheet::WriteExcel->new("TEST.xls");
#生成Excel表
my $xlsheet = $xl->add_worksheet(“TestSheet”);
#添加格式(表头)
$rptheader = $xl->add_format(); # Add a format
$rptheader->set_bold();
$rptheader->set_size('12');
$rptheader->set_align('center');
#添加格式(表内容)
$normcell = $xl->add_format(); # Add a format
$normcell->set_size('9');
$normcell->set_align('center');
$normcell->set_bg_color('22');
#设置列的宽度
$xlsheet->set_column('A:A',10);
$xlsheet->set_column('B:B',12);
$xlsheet->set_column('C:C',17);
#写表头(格式是使用上面添加的表头格式)
$xlsheet->write("A2","Number", $rptheader);
$xlsheet->write("B2","Name",$rptheader);
$xlsheet->write("C2","Language",$rptheader);
#写内容(格式是使用上面添加的表内容格式)
$xlsheet->write("A3","1", $normcell);
$xlsheet->write("B3","Test",$normcell);
$xlsheet->write("C3","Perl",$normcell);
#关闭操作excel的对象.
$xl->close();


解决中文乱码:

use Spreadsheet::WriteExcel;
use Encode;#(这里增加Encode)
#************生成Excel文档****************
my $xl = Spreadsheet::WriteExcel->new("TEST.xls");
#生成Excel表
my $xlsheet = $xl->add_worksheet(decode('utf8' ,“测试写入Excel”));#(中文名称)
#添加格式(表头)
$rptheader = $xl->add_format(); # Add a format
$rptheader->set_bold();
$rptheader->set_size('12');
$rptheader->set_align('center');
#添加格式(表内容)
$normcell = $xl->add_format(); # Add a format
$normcell->set_size('9');
$normcell->set_align('center');
$normcell->set_bg_color('22');
#设置列的宽度
$xlsheet->set_column('A:A',10);
$xlsheet->set_column('B:B',12);
$xlsheet->set_column('C:C',17);
#写表头(格式是使用上面添加的表头格式)(这里是输入中文)
$xlsheet->write("A2",decode(‘utf8’,"号码"), $rptheader);
$xlsheet->write("B2", decode(‘utf8’,"名称"),$rptheader);
$xlsheet->write("C2", decode(‘utf8’,"语言"),$rptheader);
#写内容(格式是使用上面添加的表内容格式)(这里是输入中文)
$xlsheet->write("A3", decode(‘utf8’,"1"), $normcell);
$xlsheet->write("B3", decode(‘utf8’,"测试"),$normcell);
$xlsheet->write("C3", decode(‘utf8’,"Perl"),$normcell);
#关闭操作excel的对象.
$xl->close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值