Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1343155
  • 博文数量: 243
  • 博客积分: 888
  • 博客等级: 准尉
  • 技术积分: 2955
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-05 14:33
个人简介

漫漫长路,其修远兮!

文章分类

全部博文(243)

文章存档

2017年(2)

2016年(22)

2015年(32)

2014年(57)

2013年(107)

2012年(23)

分类: LINUX

2012-12-14 11:14:05

主要是使用如下两个模块
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
 
一,解析excel

点击(此处)折叠或打开

  1. use strict;
  2. use Spreadsheet::ParseExcel;

  3. my $parser = Spreadsheet::ParseExcel->new();
  4. my $workbook = $parser->parse('Book.xls');
  5. if ( !defined $workbook ) {
  6.     die $parser->error(), ".\n";
  7. }
  8. for my $worksheet ( $workbook->worksheets() )
  9. {
  10.     my ( $row_min, $row_max ) = $worksheet->row_range();
  11.     my ( $col_min, $col_max ) = $worksheet->col_range();
  12.     for my $row ( $row_min .. $row_max ) {
  13.         for my $col ( $col_min .. $col_max ) {
  14.             my $cell = $worksheet->get_cell( $row, $col );
  15.             next unless $cell;
  16.             print "Row, Col = ($row, $col)\n";
  17.             print "Value = ", $cell->value(), "\n";
  18.             print "\n";
  19.         }
  20.     }
  21. }

二,写入excel文件

点击(此处)折叠或打开

  1. use strict;
  2. use Spreadsheet::WriteExcel;

  3. my $result = new Spreadsheet::WriteExcel("Book.xls");
  4. my $fmt = $result->add_format();
  5. $fmt->set_align('center');
  6. $fmt->set_color('black');

  7. my $sheet = "1";
  8. my $dest_sheet1 = $result->addworksheet($sheet);
  9. $dest_sheet1->write( 0, 0, T("book_id"), $fmt->{HEADER} );
  10. $dest_sheet1->write( 0, 1, T("book_name"), $fmt->{HEADER} );
  11. $dest_sheet1->write( 0, 2, T("book_type"), $fmt->{HEADER} );
  12. $dest_sheet1->write( 0, 3, T("book_price"), $fmt->{HEADER} );

  13. my $Num = 1;
  14. foreach ( sort { $a <=> $b } keys %{$res_get_result} ) {
  15.     $dest_sheet1->write( $Num, 0, T( $res_get_result->{$_}->{book_id} ), $fmt );
  16.     $dest_sheet1->write( $Num, 1, T( $res_get_result->{$_}->{book_name} ), $fmt );
  17.     $dest_sheet1->write( $Num, 2, T( $res_get_result->{$_}->{book_type} ), $fmt );
  18.     $dest_sheet1->write( $Num, 3, $res_get_result->{$_}->{book_price}, $fmt );
  19.     $Num++;
  20. }

 
阅读(1570) | 评论(0) | 转发(1) |
0

上一篇:mysql主主配置

下一篇:perl发邮件

给主人留下些什么吧!~~