比如 日志,和shell运行
require("/home/xj_liukaiyi/src/perl/excel/excelUtil.pl"); &parse_excel("2w.xls","歌曲列表","&read_wlh);
#
!/bin/perl -w
use Time :: Local ;
my $log_input_file = " ~/log " ;
sub set_log_input_file{
$log_input_file = $_ [ 0 ];
}
# &perl_log("tt","/home/xj_liukaiyi/log");
sub perl_log{
my $log_msg = shift @_ ;
print $log_msg , " \n " ;
system ' ttmp1=$(date +%Y-%k-%d);ttmp2=$(date +%M:%S);echo "$ttmp1 $ttmp2: ' . " $log_msg " . ' " >> ' . $log_input_file ;
}
# &system_util("xls -l","~/log");
sub system_util{
my $comm = shift @_ ;
my @return = system $comm ;
if ( $return [( @return - 1 )] == 0 ) {
& perl_log( " [success] " . $comm , $log_input_file );
} else {
& perl_log( " [error] " . $comm , $log_input_file );
}
return $return [( @return - 1 )];
}
1 ;
excel 读取util
use Time :: Local ;
my $log_input_file = " ~/log " ;
sub set_log_input_file{
$log_input_file = $_ [ 0 ];
}
# &perl_log("tt","/home/xj_liukaiyi/log");
sub perl_log{
my $log_msg = shift @_ ;
print $log_msg , " \n " ;
system ' ttmp1=$(date +%Y-%k-%d);ttmp2=$(date +%M:%S);echo "$ttmp1 $ttmp2: ' . " $log_msg " . ' " >> ' . $log_input_file ;
}
# &system_util("xls -l","~/log");
sub system_util{
my $comm = shift @_ ;
my @return = system $comm ;
if ( $return [( @return - 1 )] == 0 ) {
& perl_log( " [success] " . $comm , $log_input_file );
} else {
& perl_log( " [error] " . $comm , $log_input_file );
}
return $return [( @return - 1 )];
}
1 ;
require("/home/xj_liukaiyi/src/perl/excel/excelUtil.pl"); &parse_excel("2w.xls","歌曲列表","&read_wlh);
#
!/bin/perl -w
use Spreadsheet :: ParseExcel;
use Spreadsheet :: ParseExcel :: FmtUnicode;
use Encode;
use Unicode :: Map ();
my $Map = new Unicode :: Map ( " GB2312 " );
my $excelCODE = ' gb2312 ' ;
my $coslCODE = ' utf8 ' ;
my %parseCache ;
my %writeCache ;
my $oFmtJ = Spreadsheet :: ParseExcel :: FmtUnicode -> new(Unicode_Map => $excelCODE );
my $oExcel = new Spreadsheet :: ParseExcel;
#
#&parse_excel("all4.xls","乐扑","&read_lp);
# sub read_lp{ $_[1 .. 2.3 ] }
sub parse_excel{
my $sExcelName = shift ;
my $sSheetName = shift ;
my $action_fun = shift ;
my ( $iR , $iC , $oWkS , $oWkC );
# 开启 excel
my $oBook = $oExcel -> Parse( $sExcelName , $oFmtJ );
# 根据 sheet 名称匹配
for ( my $iSheet = 0 ; $iSheet < $oBook -> {SheetCount} ; $iSheet ++ ){
my $t_oWkS = $oBook -> {Worksheet}[ $iSheet ];
my $sWkSName = encode( $coslCODE , decode( $excelCODE , $t_oWkS -> {Name} ));
if ( $sWkSName =~/ $sSheetName / ){
$oWkS = $t_oWkS ;
last ;
}
}
for ( my $iR = $oWkS -> {MinRow} ;
defined $oWkS -> {MaxRow} && $iR <= $oWkS -> {MaxRow} ;
$iR ++ ){
my @arr_row ;
for ( my $iC = $oWkS -> {MinCol} ;
defined $oWkS -> {MaxCol} && $iC <= $oWkS -> {MaxCol} ;
$iC ++ ){
$oWkC = $oWkS -> {Cells}[ $iR ][ $iC ];
my $col_value = "" ;
$col_value = encode( $coslCODE , decode( $excelCODE , $oWkC -> Value) ) if ( $oWkC ) ;
push ( @arr_row , $col_value );
}
push ( @arr_row , $iR );
$action_fun -> ( @arr_row );
}
}
use Spreadsheet :: ParseExcel;
use Spreadsheet :: ParseExcel :: FmtUnicode;
use Encode;
use Unicode :: Map ();
my $Map = new Unicode :: Map ( " GB2312 " );
my $excelCODE = ' gb2312 ' ;
my $coslCODE = ' utf8 ' ;
my %parseCache ;
my %writeCache ;
my $oFmtJ = Spreadsheet :: ParseExcel :: FmtUnicode -> new(Unicode_Map => $excelCODE );
my $oExcel = new Spreadsheet :: ParseExcel;
#
#&parse_excel("all4.xls","乐扑","&read_lp);
# sub read_lp{ $_[1 .. 2.3 ] }
sub parse_excel{
my $sExcelName = shift ;
my $sSheetName = shift ;
my $action_fun = shift ;
my ( $iR , $iC , $oWkS , $oWkC );
# 开启 excel
my $oBook = $oExcel -> Parse( $sExcelName , $oFmtJ );
# 根据 sheet 名称匹配
for ( my $iSheet = 0 ; $iSheet < $oBook -> {SheetCount} ; $iSheet ++ ){
my $t_oWkS = $oBook -> {Worksheet}[ $iSheet ];
my $sWkSName = encode( $coslCODE , decode( $excelCODE , $t_oWkS -> {Name} ));
if ( $sWkSName =~/ $sSheetName / ){
$oWkS = $t_oWkS ;
last ;
}
}
for ( my $iR = $oWkS -> {MinRow} ;
defined $oWkS -> {MaxRow} && $iR <= $oWkS -> {MaxRow} ;
$iR ++ ){
my @arr_row ;
for ( my $iC = $oWkS -> {MinCol} ;
defined $oWkS -> {MaxCol} && $iC <= $oWkS -> {MaxCol} ;
$iC ++ ){
$oWkC = $oWkS -> {Cells}[ $iR ][ $iC ];
my $col_value = "" ;
$col_value = encode( $coslCODE , decode( $excelCODE , $oWkC -> Value) ) if ( $oWkC ) ;
push ( @arr_row , $col_value );
}
push ( @arr_row , $iR );
$action_fun -> ( @arr_row );
}
}
#
使用 demo
find . - maxdepth 1 - name " *xls " - exec perl - e '
require("/home/xj_liukaiyi/src/perl/excel/excelUtil.pl");
my $file=$ARGV[0];
sub myRead{
my $str="";
foreach$tmp(@_){
$str+"$tmp\t";
}
system "echo $str >> $file.data";
}
&parse_excel("$file",0,\&myRead);
print "$file\n";
' {} \ ;
本文转自博客园刘凯毅的博客,原文链接:perl 一些有用的 util,如需转载请自行联系原博主。
find . - maxdepth 1 - name " *xls " - exec perl - e '
require("/home/xj_liukaiyi/src/perl/excel/excelUtil.pl");
my $file=$ARGV[0];
sub myRead{
my $str="";
foreach$tmp(@_){
$str+"$tmp\t";
}
system "echo $str >> $file.data";
}
&parse_excel("$file",0,\&myRead);
print "$file\n";
' {} \ ;