Perl读写Excel

前端之家收集整理的这篇文章主要介绍了Perl读写Excel前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#读取excel,支持2003 和2007 @H_502_6@ #LJL @H_502_6@ use Encode; @H_502_6@ use Spreadsheet::XLSX;  @H_502_6@ use Spreadsheet::ParseExcel; @H_502_6@@H_502_6@@H_502_6@ my $file;         #Excel path @H_502_6@ my $sheetName;    #WorkSheet name @H_502_6@@H_502_6@@H_502_6@ sub readXlsx2007{ @H_502_6@my $excel = Spreadsheet::XLSX -> new ($file,$converter);  @H_502_6@my %field; @H_502_6@my @worksheet = @{$excel -> {Worksheet}}; @H_502_6@foreach my $sheet (@{$excel -> {Worksheet}}){ @H_502_6@#print encode('gbk',decode('utf8',$sheet->{Name}))."--------".$sheetName; @H_502_6@next if(uc(encode('gbk',$sheet->{Name})))ne $sheetName); @H_502_6@print "Process the Sheet: ".$sheetName."\n"; @H_502_6@my $poiId; @H_502_6@ my $brandName; @H_502_6@ my $address; @H_502_6@     my $lat; @H_502_6@     my $lon;  @H_502_6@     my $province; @H_502_6@     my $city; @H_502_6@     my $county; @H_502_6@     my $phone;  @H_502_6@     my $businessArea; @H_502_6@     my $businessId;     @H_502_6@     my @poiArray; @H_502_6@     my $poiStr; @H_502_6@$sheet -> {MaxRow} ||= $sheet -> {MinRow};         @H_502_6@foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}){ @H_502_6@print "Processed [".($row)."/".$sheet -> {MaxRow}."]\n"; @H_502_6@$sheet -> {MaxCol} ||= $sheet -> {MinCol};                 @H_502_6@foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {                 @H_502_6@my $cell = $sheet -> {Cells} [$row] [$col];  @H_502_6@my $value = encode('gbk',$cell->{Val})); @H_502_6@if($row==$sheet -> {MinRow}){ @H_502_6@$field{$value}=$col; @H_502_6@next; @H_502_6@}   @H_502_6@$poiId = $value if $col==$field{'POI_ID'}; @H_502_6@$brandName = $value if $col==$field{'BRAND_NAME'}; @H_502_6@$address = $value if $col==$field{'ADDRESS'}; @H_502_6@$lat = $value if $col==$field{'LATITUDE'}; @H_502_6@$lon = $value if $col==$field{'LONGTITUDE'}; @H_502_6@$province = $value if $col==$field{'PROVINCE'}; @H_502_6@$city = $value if $col==$field{'CITY'}; @H_502_6@$county = $value if $col==$field{'COUNTY'}; @H_502_6@$phone = $value if $col==$field{'PHONE'}; @H_502_6@         $businessArea= $value if $col==$field{'BUSINESS_AREA'}; @H_502_6@         $businessId= $value if $col==$field{'BUSINESS_ID'}; @H_502_6@         @H_502_6@         push @poiArray,$value; @H_502_6@}  @H_502_6@if($row==$row_min){ @H_502_6@next; @H_502_6@ } @H_502_6@     print "[Source Data]:".$poiId."|".$brandName."|".$address."|".$lat."|".$lon."|".$province."|".$city."|".$county."|".$phone."|".$businessArea."|".$businessId."\n"; @H_502_6@$poiStr=$poiId."|".$brandName."|".$address."|".$lat."|".$lon."|".$province."|".$city."|".$county."|".$phone."|".$businessArea."|".$businessId; @H_502_6@my $poi=GEOcoder($poiStr); @H_502_6@print "[Dealer Data]:".$poi."\n"; @H_502_6@} @H_502_6@  } @H_502_6@ } @H_502_6@@H_502_6@@H_502_6@@H_502_6@@H_502_6@ sub readXls2003{ @H_502_6@my %field; @H_502_6@my $parser   = Spreadsheet::ParseExcel->new(); @H_502_6@my $workbook = $parser->parse($file); @H_502_6@if ( !defined $workbook ) { @H_502_6@   die $parser->error(),".\n"; @H_502_6@} @H_502_6@my @worksheet = $workbook->worksheets(); @H_502_6@foreach my $currentSheet(@worksheet){ @H_502_6@next if(uc(encode('gbk',$currentSheet->get_name())))ne $sheetName); @H_502_6@print "Process the Sheet: ".$sheetName."\n"; @H_502_6@ @H_502_6@my ( $row_min,$row_max ) = $currentSheet->row_range(); @H_502_6@my ( $col_min,$col_max ) = $currentSheet->col_range(); @H_502_6@for my $row ( $row_min .. $row_max ) { @H_502_6@print "Processed [".($row)."/".$currentSheet->row_range()."]\n"; @H_502_6@my $poiId; @H_502_6@ my $brandName; @H_502_6@ my $address; @H_502_6@   my $lat; @H_502_6@   my $lon;  @H_502_6@   my $province; @H_502_6@   my $city; @H_502_6@   my $county; @H_502_6@   my $phone; @H_502_6@   my $businessArea; @H_502_6@     my $businessId; @H_502_6@     my @poiArray; @H_502_6@     my $poiStr; @H_502_6@  for my $col ( $col_min .. $col_max ) {     @H_502_6@   my $cell = $currentSheet->get_cell( $row,$col ); @H_502_6@   next unless $cell; @H_502_6@   my $value = encode('gbk',$cell->value()); @H_502_6@   if($row==$row_min){ @H_502_6@$field{$value} = $col; @H_502_6@next; @H_502_6@} @H_502_6@$poiId = $value if $col==$field{'POI_ID'}; @H_502_6@$brandName = $value if $col==$field{'BRAND_NAME'}; @H_502_6@$address = $value if $col==$field{'ADDRESS'}; @H_502_6@$lat = $value if $col==$field{'LATITUDE'}; @H_502_6@$lon = $value if $col==$field{'LONGTITUDE'}; @H_502_6@$province = $value if $col==$field{'PROVINCE'}; @H_502_6@$city = $value if $col==$field{'CITY'}; @H_502_6@$county = $value if $col==$field{'COUNTY'}; @H_502_6@$phone = $value if $col==$field{'PHONE'}; @H_502_6@$businessArea= $value if $col==$field{'BUSINESS_AREA'}; @H_502_6@         $businessId= $value if $col==$field{'BUSINESS_ID'}; @H_502_6@       @H_502_6@         push @poiArray,$value; @H_502_6@} @H_502_6@if($row==$row_min){ @H_502_6@next; @H_502_6@} @H_502_6@print "[Source Data]:".$poiId."|".$brandName."|".$address."|".$lat."|".$lon."|".$province."|".$city."|".$county."|".$phone."|".$businessArea."|".$businessId."\n"; @H_502_6@$poiStr=$poiId."|".$brandName."|".$address."|".$lat."|".$lon."|".$province."|".$city."|".$county."|".$phone."|".$businessArea."|".$businessId; @H_502_6@my $poi=GEOcoder($poiStr); @H_502_6@print "[Dealer Data]:".$poi."\n"; @H_502_6@ @H_502_6@} @H_502_6@  } @H_502_6@

}

@H_502_6@

@H_502_6@

========================================================================================================================

@H_502_6@

@H_502_6@

@H_502_6@

#生成excel #ljl use Spreadsheet::WriteExcel;  my %us; while(($key,$value) = each %us){ print "$key|$value\n"; }  # 创建一个新的EXCEL文件     my $workbook = Spreadsheet::WriteExcel->new('poi_count_top15.xls');         # 添加一个工作表     $worksheet = $workbook->add_worksheet();        #  新建一个样式     $format = $workbook->add_format(); # Add a format    $format->set_bg_color('green');   $format->set_bold();#设置字体为粗体     $format->set_color('red');#设置单元格前景色为红色     $format->set_align('center');#设置单元格居中            $format2=$workbook->add_format();   $format2->set_bg_color('gray');   $format2->set_bold();#设置字体为粗体    $format2->set_align('center');#设置单元格居中          $format3=$workbook->add_format();   $format3->set_bg_color('orange');   $format3->set_bold();#设置字体为粗体    $format3->set_align('center');#设置单元格居中          $format4=$workbook->add_format();   $format4->set_bg_color('brown');   $format4->set_bold();#设置字体为粗体    $format4->set_align('center');#设置单元格居中             $worksheet->write(0,'Province',$format);    $worksheet->write(0,1,'us_ta_1',$format);      $worksheet->write(0,2,'us_ta_2',3,'D-value',4,'Divide us_ta_1',5,'Divide us_ta_2',6,'Result',7,8,9,10,11,12,$format);                                                                                                          $col =0;    $row = 1;  for my $key(sort keys %us)  {    my @k=split/;/,$us{$key},-1;     $worksheet->write($row,$key,$format2);    for($i=1;$i<13;$i++){    if($i<7){   $worksheet->write($row,$i,$k[$i-1],$format3);     }        else    {    $worksheet->write($row,$format4);    }     }   $row++; } 

猜你在找的Perl相关文章