3个回答
展开全部
下面是一个简单的例子,先要安装几个模块,按照提示安装吧。
#!/usr/bin/perl -w
## Program : xls.pl
## Version : 1.0
## Date : 2010.7.8
##------------------------------------------------------------------------------
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode;
use Spreadsheet::WriteExcel;
#---------------------------写EXCEL-------------------------------------------------------
my $output= File::Spec->catfile('Copy.xls');
my $Map=new Unicode::Map("GB2312");
my $workbook=Spreadsheet::WriteExcel->new("$output");
my $out_worksheet=$workbook->add_worksheet("table");
$format = $workbook->add_format(align=>'center',bg_color=>'cyan',border=> 1);
#---------------------------读取EXCEL-------------------------------------------------------
$filename="input.xls";
my $oExcel = new Spreadsheet::ParseExcel;
my $code = "gb2312";
my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map =>$code);
my $oBook = $oExcel->Parse( $filename, $oFmtJ );
my $sheet=$oBook->{Worksheet}[0];
#$sheet_name=$sheet->{Name};
my ( $minRow, $maxRow ) = $sheet->row_range();
my ( $minCol, $maxCol ) = $sheet->col_range();
my @rowdata;
my $r_num=0;
for $r(0..$maxRow){
@row_data=getSingleRow($r,$maxCol,$sheet);
#print "@row_data"."\n";
my $i=0;
foreach(@row_data){
print $_;
$out_worksheet->write_unicode($r,$i++,$Map->to_unicode($_),$format);
}
}
sub getSingleRow{
my($row,$maxCol,$sheet)=@_;
my @row_all;
foreach my $col ( 0 .. $maxCol ){
my $cell = $sheet->get_cell( $row, $col );
next unless $cell;
$data=$cell->value;
push(@row_all,$data);
}
return @row_all;
}
#!/usr/bin/perl -w
## Program : xls.pl
## Version : 1.0
## Date : 2010.7.8
##------------------------------------------------------------------------------
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode;
use Spreadsheet::WriteExcel;
#---------------------------写EXCEL-------------------------------------------------------
my $output= File::Spec->catfile('Copy.xls');
my $Map=new Unicode::Map("GB2312");
my $workbook=Spreadsheet::WriteExcel->new("$output");
my $out_worksheet=$workbook->add_worksheet("table");
$format = $workbook->add_format(align=>'center',bg_color=>'cyan',border=> 1);
#---------------------------读取EXCEL-------------------------------------------------------
$filename="input.xls";
my $oExcel = new Spreadsheet::ParseExcel;
my $code = "gb2312";
my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map =>$code);
my $oBook = $oExcel->Parse( $filename, $oFmtJ );
my $sheet=$oBook->{Worksheet}[0];
#$sheet_name=$sheet->{Name};
my ( $minRow, $maxRow ) = $sheet->row_range();
my ( $minCol, $maxCol ) = $sheet->col_range();
my @rowdata;
my $r_num=0;
for $r(0..$maxRow){
@row_data=getSingleRow($r,$maxCol,$sheet);
#print "@row_data"."\n";
my $i=0;
foreach(@row_data){
print $_;
$out_worksheet->write_unicode($r,$i++,$Map->to_unicode($_),$format);
}
}
sub getSingleRow{
my($row,$maxCol,$sheet)=@_;
my @row_all;
foreach my $col ( 0 .. $maxCol ){
my $cell = $sheet->get_cell( $row, $col );
next unless $cell;
$data=$cell->value;
push(@row_all,$data);
}
return @row_all;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
能用 Win32::OLE 模块来检索来自 Windows Excel 文件的数据.
找到的一个例子:
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
# open Excel file
my $Book = $Excel->Workbooks->Open("c:/komodo projects/test.xls");
# You can dynamically obtain the number of worksheets, rows, and columns
# through the Excel OLE interface. Excel's Visual Basic Editor has more
# information on the Excel OLE interface. Here we just use the first
# worksheet, rows 1 through 4 and columns 1 through 3.
# select worksheet number 1 (you can also select a worksheet by name)
my $Sheet = $Book->Worksheets(1);
foreach my $row (1..4)
{
foreach my $col (1..3)
{
# skip empty cells
next unless defined $Sheet->Cells($row,$col)->{'Value'};
# print out the contents of a cell
printf "At ($row, $col) the value is %s and the formula is %s\n",
$Sheet->Cells($row,$col)->{'Value'},
$Sheet->Cells($row,$col)->{'Formula'};
}
}
# clean up after ourselves
$Book->Close;
找到的一个例子:
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
# open Excel file
my $Book = $Excel->Workbooks->Open("c:/komodo projects/test.xls");
# You can dynamically obtain the number of worksheets, rows, and columns
# through the Excel OLE interface. Excel's Visual Basic Editor has more
# information on the Excel OLE interface. Here we just use the first
# worksheet, rows 1 through 4 and columns 1 through 3.
# select worksheet number 1 (you can also select a worksheet by name)
my $Sheet = $Book->Worksheets(1);
foreach my $row (1..4)
{
foreach my $col (1..3)
{
# skip empty cells
next unless defined $Sheet->Cells($row,$col)->{'Value'};
# print out the contents of a cell
printf "At ($row, $col) the value is %s and the formula is %s\n",
$Sheet->Cells($row,$col)->{'Value'},
$Sheet->Cells($row,$col)->{'Formula'};
}
}
# clean up after ourselves
$Book->Close;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
my $filename = <STDIN>;
chop $filename;
my $Book = $Excel->Workbooks->Open("$filename");
my $Sheet = $Book->Worksheets(2);
open(A,">test.txt");
my $val3 = 0;
my $val4 = 0;
my $val5 = 0;
my $val7 = 0;
my $val8 = 0;
my $val9 = 0;
my $val10 = 0;
my $val11 = 0;
my $print_flag = 1;
my $print_flag1 = 0;
my $spa = "[\\t ]+";
my $ent = "[\\n]";
my @data1 = "";
my @data2 = "";
my $dat1 = 0;
my $dat2 = 0;
foreach my $row (4..200)
{
foreach my $col (3..5,8..14)
{
...
};
};
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
my $filename = <STDIN>;
chop $filename;
my $Book = $Excel->Workbooks->Open("$filename");
my $Sheet = $Book->Worksheets(2);
open(A,">test.txt");
my $val3 = 0;
my $val4 = 0;
my $val5 = 0;
my $val7 = 0;
my $val8 = 0;
my $val9 = 0;
my $val10 = 0;
my $val11 = 0;
my $print_flag = 1;
my $print_flag1 = 0;
my $spa = "[\\t ]+";
my $ent = "[\\n]";
my @data1 = "";
my @data2 = "";
my $dat1 = 0;
my $dat2 = 0;
foreach my $row (4..200)
{
foreach my $col (3..5,8..14)
{
...
};
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询