ParseExcel只支持xls 文件,无法支出xlsx.
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
#讀取Excel
my $parser = Spreadsheet::ParseExcel->new();
##对Excel内中文字符进行字符编码指定,防止乱码
my $formatter = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map=>"CP936");
my $workbook = $parser->parse('Demo_Excel.xls', $formatter);
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";
}
}
}
####写Excel
##创建一个新的xls文件,该模块只能先新建后写,不能对已有文件进行修改
my $workbook = Spreadsheet::WriteExcel->new('newexcel.xls');
##新建一个sheet,可以在括号内加入sheet名称,若不加,默认为sheet1
my $worksheet = $workbook->add_worksheet();
#设置表头字体格式
my $format_head = $workbook->add_format(
##为了避免字符串中数字使用原格式,使用@设置数字的默认格式同文字格式
num_format => '@',
##设置字体格式为黑体
font => decode("cp936", "黑体"),
bold => 1, ##加粗
align => 'center', ##居中
border => 1 ##边界框线
);
#设置字体格式
my $format_1 = $workbook->add_format(
num_format => '@',
size => 10,
font => decode("cp936", "宋体"),
border => 1,
align => 'center',
);
##设置数字格式
my $format_2 = $workbook->add_format(
##数字保留两位有效小数,0.00_下划线后必须加空格
num_format => '0.00_ ',
size => 10,
font => 'Arial',
border => 1,
align => 'center',
);
##此外还可以设置字体颜色,背景颜色等
###设置每一列宽度
$worksheet->set_column('A:A', 20);
$worksheet->set_column('B:B', 21);
##合并第一行AB列
$worksheet->merge_range('A1:B1', decode("cp936", "表格"), $format_head);
$worksheet->write(1, 0, decode("cp936", "第一行"), $format_1);
$worksheet->write('B2', 112, $format_2);
##关闭workbook
$workbook->close();
The module cannot read files in the Excel 2007 Open XML XLSX format. See the Spreadsheet::XLSX module instead.
example:
#!/usr/bin/perl
use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel::FmtUnicode;
####读Excel
my $parser = Spreadsheet::ParseExcel->new();
##对Excel内中文字符进行字符编码指定,防止乱码
my $formatter = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map=>"CP936");
my $workbook = $parser->parse('demo_excel.xls', $formatter);
if(!defined $workbook){
die $parser->error(), "\n";
}
for my $worksheet($workbook->worksheets()){ #foreach worksheet
my $sheet = $worksheet->get_name();
printf("Worksheet: %s\n", $sheet);
my ($row_min, $row_max) = $worksheet->row_range();
my ($col_min, $col_max) = $worksheet->col_range();
for my $row ($row_min .. $row_max){ #foreach line
my $cell = $worksheet->get_cell($row, 1); #對應的場景是每行第1列開始parse
next unless $cell;
my $value = $cell->value();
print "Value=", $cell->value(), "\n";
next if($value =~ /\#/);
next if($value !~ /0xA/);
my $point = $row;
#parsing 指定列(Column)
my $start_addr = $worksheet->get_cell($point, 1) ? $worksheet->get_cell($point, 1)->value() : "NULL";
my $end_addr = $worksheet->get_cell($point, 2) ? $worksheet->get_cell($point, 2)->value() : "NULL";
my $size = $worksheet->get_cell($point, 3) ? $worksheet->get_cell($point, 3)->value() : "NULL";
my $module = $worksheet->get_cell($point, 4) ? $worksheet->get_cell($point, 4)->value() : "NULL";
}
}
use Text::Iconv;
my $converter = Text::Iconv -> new ("utf-8", "windows-1251");
# Text::Iconv is not really required.
# This can be any object with the convert method. Or nothing.
use Spreadsheet::XLSX;
my $excel = Spreadsheet::XLSX -> new ('test.xlsx', $converter);
foreach my $sheet (@{$excel -> {Worksheet}}) {
printf("Sheet: %s\n", $sheet->{Name});
$sheet -> {MaxRow} ||= $sheet -> {MinRow};
foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {
$sheet -> {MaxCol} ||= $sheet -> {MinCol};
foreach my $col ($sheet -> {MinCol} .. $sheet -> {MaxCol}) {
my $cell = $sheet -> {Cells} [$row] [$col];
if ($cell) {
printf("( %s , %s ) => %s\n", $row, $col, $cell -> {Val});
}
}
}
}