如何在perl中加载关于Excel的模块?

求详细步骤!谢谢!... 求详细步骤!谢谢! 展开
 我来答
kkcrossover
2011-04-06
知道答主
回答量:7
采纳率:0%
帮助的人:0
展开全部
下面是一个简单的例子,先要安装几个模块,按照提示安装吧。
#!/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;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
stp111
2011-04-04 · TA获得超过341个赞
知道小有建树答主
回答量:283
采纳率:0%
帮助的人:225万
展开全部
能用 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;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
手机用户54224
2011-04-05
知道答主
回答量:2
采纳率:0%
帮助的人:3413
展开全部
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)
{
...
};
};
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式