什么';用Perl解析Excel文件的最佳方法是什么?
用Perl解析Excel文件最简单的方法是什么?将其转换为文本文件也会起作用。我很幸运地使用了。最好的方法是使用我们心爱的人提供的模块 无论何时遇到问题,请立即询问:为什么我还没有检查CPAN? 哪一个模块我不能确定,这里有一个列表,让你开始,试试,看看什么是有效的什么';用Perl解析Excel文件的最佳方法是什么?,perl,excel,parsing,Perl,Excel,Parsing,用Perl解析Excel文件最简单的方法是什么?将其转换为文本文件也会起作用。我很幸运地使用了。最好的方法是使用我们心爱的人提供的模块 无论何时遇到问题,请立即询问:为什么我还没有检查CPAN? 哪一个模块我不能确定,这里有一个列表,让你开始,试试,看看什么是有效的 最好的方法是使用 以下是一个例子: #!/usr/bin/perl -w use strict; use warnings; use Spreadsheet::ParseExcel; my $parser = S
- 最好的方法是使用
以下是一个例子:
#!/usr/bin/perl -w
use strict;
use warnings;
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";
}
}
}
要使用Perl将Excel文件转换为文本,我建议使用电子表格::ParseExcel。@echo off
@echo off
ECHO CHECK THE VERSION FROM THE PROJECT
echo see also this [link][1]
for /f "tokens=*" %%i in ('Type Version.txt') do set _Version=%%i
ECHO The _Version is %_Version%
ECHO remove the output html files
del *.html /q
ECHO remove the output log files
del *.log /q
::pause
ECHO %0 > %0.log
ECHO %0.error.log >%0.error.log
set BaseDir=D:\perl\sfw\ExcelToHtml.%_Version%
echo BaseDir is %BaseDir% 1>>%0.log 2>>%0.error.log
ECHO.
set LogLevel=3
echo LogLevel is %LogLevel% 1>>%0.log 2>>%0.error.log
ECHO.
::set ExcelFileToParse="%BaseDir%\CoDA_ETL_Integration.xls"
SET ExcelFileToParse="%BaseDir%\TODO.xls"
echo ExcelFileToParse is %ExcelFileToParse% 1>>%0.log 2>>%0.error.log
echo.
set OutputDir=%BaseDir%
echo OutputDir is %Outputdir% 1>>%0.log 2>>%0.error.log
echo.
ECHO SET THE UNICODE FOR PERL FOR UTF-8
SET PERL_UNICODE=S
ECHO %%PERL_UNICODE%% IS %PERL_UNICODE%
::set PerlScript=parseExcelToCsv.pl
set PerlScript=ExcelToHtml.pl
echo PerlScript is %PerlScript% 1>>%0.log 2>>%0.error.log
ECHO.
echo Action !!! 1>>%0.log 2>>%0.error.log
echo perl %BaseDir%\%PerlScript% %LogLevel% %ExcelFileToParse% %OutputDir% 1>>%0.log 2>>%0.error.log
perl %BaseDir%\%PerlScript% %LogLevel% %ExcelFileToParse% %OutputDir% 1>>%0.log 2>>%0.error.log
%0.error.log
%0.log
::debug pause
use strict;
use Spreadsheet::ParseExcel;
use utf8 ;
use Encode ;
package ExcelToHtml ;
my $DebugLevel = 3 ;
print defined($ARGV[0]) ? $DebugLevel = $ARGV[0] : "Using max DebugLevel = $DebugLevel", " \n";
my $minimumAmountOfCommandLineArgs = 3 ;
my ( $ExcelFileToParse , $OutputDir , $BaseFileName ) = ();
sub main {
logMsg("$0 SCRIPT START " , 1 );
#Action !!! call here methods
checkCommandLineArgs();
buildNames ();
ParseExcelAndlogMsgFiles ( $ExcelFileToParse , $BaseFileName , $OutputDir) ;
logMsg("$0 SCRIPT STOP " , 1 );
} #eof MAIN
sub buildNames
{
$ExcelFileToParse = "$ARGV[1]" ;
$OutputDir="$ARGV[2]" ;
$BaseFileName = $ExcelFileToParse ;
logMsg( "\$ExcelFileToParse is $ExcelFileToParse" ) ;
logMsg ( "\$OutputDir is $OutputDir" ) ;
logMsg ("\$BaseFileName is $BaseFileName \n" );
$BaseFileName =~ s/^(.*)(\\|\/)(.*)/$3/; #strip the directory part
logMsg ("\$BaseFileName is $BaseFileName ");
$BaseFileName =~ s/^(.*)(\.)(.*)/$1/ ; #strip the file extension
logMsg ( "\$BaseFileName is $BaseFileName ");
} #eof sub buildNames
sub ParseExcelAndlogMsgFiles {
my $ExcelFileToParse = shift ;
my $BaseFileName = shift ;
my $OutputDir = shift ;
my $strToReturn = "";
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse("$ExcelFileToParse");
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 ) {
my $rowStr = "" ;
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
logMsg ( " Row, Col = ($row, $col)") ;
logMsg ( "\$cell->encoding() is " . $cell->encoding()) ;
logMsg (" Value = ", $cell->value() );
$rowStr .= makeCell($cell->value() ) ; #The Value
logMsg ( "Unformatted = ", $cell->unformatted()) ;
} #eof col
$rowStr = makeRow( $rowStr );
$strToReturn .= $rowStr ;
} #eof for my row
my $worksheetName = $worksheet->{'Name'} ;
my $FileSheet = "$OutputDir/$BaseFileName" . '.' . $worksheetName . '.' . 'html' ;
$strToReturn = makeTable ( $strToReturn );
$strToReturn = makeFile ( $strToReturn );
logMsg ( "\$strToReturn is $strToReturn " ) ;
PrintToFile ( $FileSheet, $strToReturn ) ;
$strToReturn = "" ;
$FileSheet = "" ;
} #eof for my worksheet
} #eof sub
sub trim
{
$_[0]=~s/^\s+//;
$_[0]=~s/\s+$//;
return $_[0];
}
# =========================================== eof sub trim
# Action !!!
sub makeRow
{
my $row = shift ;
return "<tr>" . $row . "</tr> \n" ;
}
sub makeCell
{
my $cell = shift ;
return "<td>" . "$cell" . " </td>" ;
}
sub makeTable
{
my $table = shift ;
return "\n <table> " . $table . "</table> \n" ;
}
sub makeFile
{
my $file = shift ;
$file = "<html> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head><body> $file </body></html>\n" ;
return $file ;
}
sub checkCommandLineArgs
{
if ( @ARGV != $minimumAmountOfCommandLineArgs )
{
logErrorMsg ( "Not enougn command line args supplied exit 1" , 1 );
die "usage: $0 1 args \n" ;
exit 1 ;
} #eof if
else
{
foreach (@ARGV) { logMsg ( "$_ " ) ; }
} #eof else if ( @ARGV != 4 )
} #eof sub checkCommandLineArgs
#log a message according to the DebugLevel
sub logMsg
{
my $msg = shift ;
my $importance = shift ;
my $CurrentPerlScriptName = "$0" ;
my $niceMonth = GetANiceMonth ();
my $logFile = "$CurrentPerlScriptName" . '_' . "$niceMonth" . '.log' ;
$msg = GetANiceTime () .' --- ' . $msg . " \n" ;
if ( $importance == 1 )
{
$msg = "\n============================================================ \n" . $msg ;
$msg = $msg . "============================================================ \n" ;
}
#debug nothing
if ( $DebugLevel == 0 ) { return ; }
#just logMsg the message
if ( $DebugLevel == 1 )
{
logMsg ( $msg );
} #eof if ( $DebugLevel == 1 )
#logMsg the message in
if ( $DebugLevel == 2 )
{
#READ ALL ROWS OF A FILE TO ALIST
open (LOG, ">> $logFile") || print "could not open the \$logFile $logFile !!!\n";
print LOG $msg ;
close LOG;
}
#logMsg the message in
if ( $DebugLevel == 3 )
{
#READ ALL ROWS OF A FILE TO ALIST
open (LOG, ">> $logFile") || print "could not open the \$logFile $logFile !!!\n";
print LOG $msg ;
close LOG;
print $msg ;
} #eof if ( $DebugLevel == 3 )
} #eof sub logMsg
#log a message according to the DebugLevel
sub logErrorMsg
{
my $errorMsg = shift ;
my $importance = shift ;
my $CurrentPerlScriptName = "$0" ;
my $niceMonth = GetANiceMonth ();
my $errorLogFile = "$CurrentPerlScriptName" . "_" . "$niceMonth" . '.error.log' ;
$errorMsg = GetANiceTime () .' --- ' . $errorMsg . " \n" ;
if ( $importance == 1 )
{
$errorMsg = "\n============================================================ \n" . $errorMsg ;
$errorMsg= $errorMsg. "============================================================ \n" ;
}
#debug nothing
if ( $DebugLevel == 0 ) { return ; }
#just logMsg the message
if ( $DebugLevel == 1 ) { print $errorMsg ; }
#logMsg the message in a error log file
if ( $DebugLevel == 2 )
{
#READ ALL ROWS OF A FILE TO ALIST
open (ERRLOG, ">> $errorLogFile") || print "could not open the \$errorLogFile $errorLogFile !!!\n";
print ERRLOG $errorMsg ;
close ERRLOG;
}
#logMsg the message in
if ( $DebugLevel == 3 )
{
#READ ALL ROWS OF A FILE TO ALIST
open (ERRLOG, ">> $errorLogFile") || print "could not open the \$errorLogFile $errorLogFile !!!\n";
print ERRLOG $errorMsg ;
close ERRLOG;
print $errorMsg ;
} #eof if ( $DebugLevel == 3 )
} #eof sub logErrorMsg
#GET A NICE TIME
sub GetANiceTime {
# Purpose: returns the time in yyyymmdd-format
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
#---- change 'month'- and 'year'-values to correct format ----
$min = "0$min" if ($min < 10);
$hour = "0$hour" if ($hour < 10);
$mon = $mon + 1;
$mon = "0$mon" if ($mon < 10);
$year = $year + 1900;
$mday = "0$mday" if ($mday < 10);
return "$year\.$mon\.$mday" . "-" . "$hour\:$min\:$sec";
} #eof sub GetANiceTime
sub GetANiceMonth {
# Purpose: returns the time in yyyymmdd-format
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
#---- change 'month'- and 'year'-values to correct format ----
$mon = $mon + 1;
$mon = "0$mon" if ($mon < 10);
$year = $year + 1900;
return "$year\.$mon" ;
} #eof sub GetANiceTime
sub PrintToFile {
my $FileOutput = shift ;
my $StringToPrint = shift ;
#READ ALL ROWS OF A FILE TO ALIST
open (FILEOUTPUT, ">$FileOutput") ||
print "could not open the \$FileOutput $FileOutput!\n";
print FILEOUTPUT $StringToPrint ;
close FILEOUTPUT ;
#debug $strToReturn .= $StringToPrint;
}
# =========================================== eof sub PrintToFile
#Action !!!
main();
1 ;
__END__
#VersionHistory:
#1.0. --- Yordan Georgiev --- Initial creation
#1.1 --- Yordan Georgiev --- Added conditional logging
ECHO检查项目中的版本
echo另见此[链接][1]
对于('Type Version.txt')中的/f“tokens=*”%%i,请设置_Version=%%i
回显_版本为%_版本%
ECHO删除输出的html文件
del*.html/q
ECHO删除输出日志文件
del*.log/q
::暂停
回显%0>%0.log
回显%0.error.log>%0.error.log
set BaseDir=D:\perl\sfw\ExcelToHtml.%\u版本%
echo BaseDir是%BaseDir%1>>%0.log 2>>%0.error.log
回声。
设置LogLevel=3
回显日志级别为%LogLevel%1>>%0.log 2>>%0.error.log
回声。
::set ExcelFileToParse=“%BaseDir%\CoDA\u ETL\u Integration.xls”
设置ExcelFileToParse=“%BaseDir%\TODO.xls”
echo ExcelFileToParse是%ExcelFileToParse%1>>%0.log 2>>%0.error.log
回声。
设置OutputDir=%BaseDir%
echo OutputDir是%OutputDir%1>>%0.log 2>>%0.error.log
回声。
ECHO为UTF-8的PERL设置UNICODE
设置PERL_UNICODE=S
ECHO%%PERL\u UNICODE%%是%PERL\u UNICODE%
::set PerlScript=parseExcelToCsv.pl
设置PerlScript=ExcelToHtml.pl
echo PerlScript是%PerlScript%1>>%0.log 2>>%0.error.log
回声。
回声行动!!!1> >%0.log 2>>%0.error.log
回显perl%BaseDir%\%PerlScript%%LogLevel%%ExcelFileToParse%%OutputDir%1>>%0.log 2>>%0.error.log
perl%BaseDir%%\%PerlScript%%LogLevel%%ExcelFileToParse%%OutputDir%1>>%0.log 2>>%0.error.log
%0.error.log
%0.log
::调试暂停
严格使用;
使用电子表格::ParseExcel;
使用utf8;
使用编码;
ExcelToHtml包;
我的$DebugLevel=3;
打印定义($ARGV[0])$DebugLevel=$ARGV[0]:“使用最大DebugLevel=$DebugLevel”,“\n”;
我的$minimumAmountOfCommandLineArgs=3;
我的($ExcelFileToParse,$OutputDir,$BaseFileName)=();
副总管{
logMsg(“$0脚本开始”,1);
#动作!!!在这里调用方法
checkCommandLineArgs();
buildNames();
ParseExcelandLogMsgFile($ExcelFileToParse、$BaseFileName、$OutputDir);
logMsg(“$0脚本停止”,1);
}#eof总管
子建筑物名称
{
$ExcelFileToParse=“$ARGV[1]”;
$OutputDir=“$ARGV[2]”;
$BaseFileName=$ExcelFileToParse;
logMsg(\$ExcelFileToParse是$ExcelFileToParse”);
logMsg(\$OutputDir是$OutputDir”);
logMsg(“\$BaseFileName是$BaseFileName\n”);
$BaseFileName=~s/^(.*)(\\\\/)(.*)/$3/;#剥离目录部分
logMsg(“\$BaseFileName是$BaseFileName”);
$BaseFileName=~s/^(.*)(\)(.*)/$1/;#去掉文件扩展名
logMsg(“\$BaseFileName是$BaseFileName”);
}#子建筑物名称的eof
子ParseExcelandLogMsgFile{
my$ExcelFileToParse=班次;
my$BaseFileName=shift;
my$OutputDir=shift;
我的$strotreturn=“”;
my$parser=电子表格::ParseExcel->new();
my$workbook=$parser->Parse($ExcelFileToParse);
对于我的$工作表($工作簿->工作表()){
my($row_min,$row_max)=$worksheet->row_range();
my($col_min,$col_max)=$worksheet->col_range();
对于我的$row($row\最小值..$row\最大值){
我的$rowStr=“”;
对于我的$col($col_min..$col_max){
my$cell=$worksheet->get_cell($row,$col);
下一步,除非$cell;
logMsg(“行,列=($Row,$Col)”);
logMsg(\$cell->encoding()是“$cell->encoding()”;
logMsg(“Value=,$cell->Value());
$rowStr.=makeCell($cell->value());#值
logMsg(“unformated=,$cell->unformated());
}#eof col
$rowStr=makeRow($rowStr);
$strToReturn.=$rowStr;
}#我这一排的eof
my$worksheetName=$worksheet->{'Name'};
my$FileSheet=“$OutputDir/$BaseFileName”。$worksheetName.'html';
$strotreturn=makeTable($strotreturn);
$strToReturn=makeFile($strToReturn);
logMsg(\$strotreturn是$strotreturn”);
PrintToFile($FileSheet,$strotreturn);
$strotreturn=“”;
$FileSheet=“”;
}#我的工作表的eof
}#eof接头
次修剪
{
$\[0]=~s/^\s+/;
$\[0]=~s/\s+$/;
返回美元[0];
}
#================================================================================================eof子修剪
#行动!!!
子makeRow
{
我的$row=班次;
返回“%$row”。\n”;
}
子生成单元
{
我的$cell=shift;
返回“$cell”;
}
子生成表
{
我的$table=shift;
返回“\n”。$table.\n”;
}
子生成文件
{
我的$file=shift;
$file=“$file\n”;
返回$file;
}
子检查命令行args
{
如果(@ARGV!=$minimumAmountOfCommandLineArgs)
{
logErrorMsg(“非enougn命令行参数提供的出口1”,1);
die“用法:$0 1参数\n”;
出口1;
}#eof如果
其他的
{
foreach(@ARGV){logMsg($);}
}#如果(@ARGV!=4),则eof else
}#eof子检查命令行args
#根据调试级别记录消息
子日志消息
{
我的$msg=shift;
我的$重要性=转变;
我的$CurrentPerlScriptName=“$0”;
我的$niceMonth=GetANiceMonth();
my$logFile=“$CurrentPerlScriptName”。“$niceMonth”。.log”;
$msg=GetANiceTime()。$msg.\n;
如果($重要性==1)