首页 > 代码库 > Perl生成excel文件

Perl生成excel文件

#生成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,0,‘Province‘,$format);
   $worksheet->write(0,1,‘us_ta_1‘,$format);  
   $worksheet->write(0,2,‘us_ta_2‘,$format);  
   $worksheet->write(0,3,‘D-value‘,$format);  
   $worksheet->write(0,4,‘Divide us_ta_1‘,$format);  
   $worksheet->write(0,5,‘Divide us_ta_2‘,$format);  
   $worksheet->write(0,6,‘Result‘,$format);  
   $worksheet->write(0,7,‘us_ta_1‘,$format);  
   $worksheet->write(0,8,‘us_ta_2‘,$format);  
   $worksheet->write(0,9,‘D-value‘,$format);  
   $worksheet->write(0,10,‘Divide us_ta_1‘,$format);  
   $worksheet->write(0,11,‘Divide us_ta_2‘,$format);  
   $worksheet->write(0,12,‘Result‘,$format);                                                              
                                   
   
   $col =0;
   $row = 1;
 for my $key(sort keys %us)
 {
   my @k=split/;/,$us{$key},-1;
    $worksheet->write($row,0,$key,$format2);  
 	 for($i=1;$i<13;$i++){
 	 	
 	 	if($i<7){
 	  $worksheet->write($row,$i,$k[$i-1],$format3);  
 	  }
   
   else
   {
   	$worksheet->write($row,$i,$k[$i-1],$format4);
   	}
  
 }
  $row++;
} 


如果不能运行,请先安装Spreadsheet::WriteExcel