花了两天时间写的,感觉一般般,如果有时间,要参考awk思路再写写。
-
#!/usr/bin/perl -w
-
use strict;
-
use warnings;
-
use Spreadsheet::XLSX;
-
use Excel::Writer::XLSX;
-
use Encode;
-
use File::Spec;
-
-
print "请输入匹配条件:\n";
-
chomp (my $a=<STDIN>);
-
my @matchstr=split /\s+/,$a;
-
#print "matchstr is @matchstr\n";
-
-
print "请同时输入排除的条件,如果有的话:\n";
-
chomp (my $b=<STDIN>);
-
my @exceptstr=split /\s+/,$b;
-
-
sub writexcel;
-
-
my $dirname='C:';
-
my $filename=File::Spec->catfile($dirname,'my2.xlsx');
-
print "filename is $filename\n";
-
-
my $workbook=Spreadsheet::XLSX->new($filename) or die "open xlsx error";
-
my @worksheets=@{$workbook->{Worksheet}} or die "open sheets error";
-
-
my $workbook2 = Excel::Writer::XLSX->new('b.xlsx') or die "open xlsx2 error.";
-
-
foreach my $worksheet(@worksheets){
-
print "sheet is $worksheet->{Name}\n" or die "cannot print name";
-
my $row_min=$worksheet->{MinRow};
-
my $row_max=$worksheet->{MaxRow};
-
my $col_min=$worksheet->{MinCol};
-
my $col_max=$worksheet->{MaxCol};;
-
-
my @row_tmp;
-
my $s=0;
-
foreach my $row($row_min .. $row_max){
-
my @col_tmp;
-
my @b;
-
my $flag=0;
-
-
foreach my $col($col_min .. $col_max){
-
my $cell=$worksheet->{Cells}[$row][$col];
-
next unless $cell;
-
-
if ($cell){
-
-
my $a=$cell->{Val};
-
$a=encode("gbk",decode("utf8",$a));
-
#printf("( %s , %s ) => %s\n", $row, $col, $a);
-
push @b,$a;
-
}
-
} #col
-
-
if(scalar @matchstr != 0){
-
foreach my $matchstr(@matchstr){
-
if (grep(/$matchstr/,@b)){
-
$flag=1;
-
last;
-
}}
-
-
}
-
-
-
if(scalar @exceptstr != 0){
-
foreach my $exceptstr(@exceptstr){
-
if (grep(/$exceptstr/,@b)){
-
$flag=0;
-
last;
-
}}}
-
-
if ($flag == 1){
-
@col_tmp=@b;
-
push @row_tmp,[@col_tmp];}
-
-
my $r_t=scalar @row_tmp;
-
#print "row size is $r_t\n";
-
} #row
-
-
my $worksheet2=$workbook2->add_worksheet($s) or die "open sheets2 error";
-
writexcel($worksheet2,$workbook2,@row_tmp);
-
$s++;
-
} #sheet
-
-
-
sub writexcel {
-
my $worksheet=shift;
-
my $workbook=shift;
-
my @value=@_;
-
-
my $format=$workbook->add_format();
-
$format->set_bold();
-
$format->set_color('blue');
-
$format->set_align('center');
-
-
my @row2=@value;
-
-
for my $i(0 .. $#row2){
-
for my $k(0 .. $#{$row2[$i]}){
-
print "i is $i, k is $k\n";
-
my $t=$row2[$i][$k];
-
-
$t=decode("gbk",$t);
-
$worksheet->write($i,$k,$t,$format);
-
-
}
-
-
}
-
}
阅读(1756) | 评论(0) | 转发(0) |