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();