perl
用win32的方式 不推荐use strict;use Win32::OLE qw(in with);use Win32::OLE::Const 'Microsoft Excel';$Win32::OLE::Warn = 3; # die on errors...my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');my $Book = $Excel->Workbooks->Open("c:/test.xls"); my $Sheet = $Book->Worksheets(1);foreach my $row (1..4){ foreach my $col (1..3) {# skip empty cellsnext unless defined $Sheet->Cells($row,$col)->{'Value'};printf "At ($row, $col) the value is %s \n", $Sheet->Cells($row,$col)->{'Value'}, $Sheet->Cells($row,$col)->{'Formula'}; }}$Book->Close;
用 Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel 模块
http://blog.csdn.net/fl49809045/archive/2010/06/01/5638396.aspx
新建excel
use strict;use warnings; use Encode;use Spreadsheet::WriteExcel;# 设置new一个对象出来,并写上需要存成什么名字的xlsmy $xls = Spreadsheet::WriteExcel->new( "c:/fukaiss.xls" ); # xml的内容名字my $xlsContent = $xls->add_worksheet( 'report' ); # 这是对格式的设置,我们可以设置一个标题的,一个内容的,# 我现在只设置一个内容my $contentStyle = $xls->add_format(); $contentStyle->set_size( 8 ); $contentStyle->set_bold(); #设置字体为粗体 $contentStyle->set_align( 'center' );#设置单元格居中 $contentStyle->set_text_wrap(); #是否回车换行 $contentStyle->set_color('red'); #设置单元格前景色为红色 # 写表内容(格式是使用上面添加的表格式)# 这个中的A,B,C是设置的excel中上面行的字母# 这个地方中的文字我用了decode这样中文才能正常显示# 最后面的contentStyle是我上面设置的行风格 $xlsContent->write( "A1", decode( 'utf8', "name" ), $contentStyle );$xlsContent->write( "B1", decode( 'utf8', "time" ), $contentStyle );$xlsContent->write( "C2", decode( 'utf8', "language" ), $contentStyle );#$xlsContent->write($row, $col, 'Hi Excel!', $format); #行,列,内容,格式 #这是关闭,上面的内容设置成循环就能生成很多行了$xls->close();
读取excel
#!/usr/bin/perluse Spreadsheet::Read;use Data::Dumper;use Smart::Comments; my $file = 'c:/fukaiss.xls';my $spreadsheet = ReadData( $file) or die "Cannot read file ";#指定读的文件名my $sheet_count = $spreadsheet->{sheets} or die "No sheets in $file\n"; #这个是查有几个sheetfor my $sheet_index (1 .. $sheet_count){ my $sheet = $spreadsheet->[$sheet_index] or next; printf("%s - %2d: [%-s] %3d Cols, %5d Rows\n", $file,$sheet_index,$sheet->{label},$sheet->{maxcol},$sheet->{maxrow});#label是sheet名 for my $row (1 .. $sheet->{maxrow}) { print join "\t" => map { my $data = $sheet->{cell}[$_][$row] ; defined $data ? $data : "-"; }1 .. $sheet->{maxcol}; print "\n"; };}
页:
[1]