免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2524 | 回复: 6
打印 上一主题 下一主题

怎么读取excel里面的数据? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-23 13:49 |只看该作者 |倒序浏览
我想要读取excel里面第一排里面所有的数据,应该怎么写?

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2010-12-23 14:22 |只看该作者
右上角搜索,本版很多

论坛徽章:
0
3 [报告]
发表于 2010-12-23 14:23 |只看该作者
用  Spreadsheet:arseExcel 这个模块可以处理的

论坛徽章:
0
4 [报告]
发表于 2010-12-23 15:26 |只看该作者
本帖最后由 yiten 于 2010-12-23 15:30 编辑

我这边有现成的,做下好人

  1. # parse excel 2003 file and return the data
  2. # examples:
  3. # 1. read the first sheet data: read_excel_2003('abc.xls'), return reference of sheet data
  4. # 2. read all the sheet data: read_excel_2003( 'abc.xls', 1 ), return array reference contains reference of every sheet data
  5. # 3. read specified sheet data: read_excel_2003( 'abc.xls', [ 0, 1, 4 ] ), return hash reference contains reference of the specified sheet data
  6. sub read_excel_2003 {
  7.     my ( $file, $sheet_var ) = @_;

  8.     die "File is not found: $file." unless -e $file;

  9.     my $parser = Spreadsheet::ParseExcel->new();
  10.     my $fmt = Spreadsheet::ParseExcel::FmtUnicode->new( Unicode_Map => 'CP936' );

  11.     my $workbook = $parser->Parse( $file, $fmt );

  12.     die $parser->error() unless $workbook;

  13.     unless ( $sheet_var ) {
  14.         return _parse_sheet( $workbook->worksheet(0) );
  15.     }
  16.     elsif ( !ref $sheet_var ) {
  17.         my @result_array = ();

  18.         for my $worksheet ( $workbook->worksheets() ) {
  19.             push @result_array, _parse_sheet($worksheet);
  20.         }

  21.         return \@result_array;
  22.     }
  23.     elsif ( ref $sheet_var eq 'ARRAY' ) {
  24.         my %result_hash = ();
  25.         
  26.         for my $sheet_num ( @$sheet_var ) {
  27.             my $worksheet = $workbook->worksheet($sheet_num);
  28.             $result_hash{$sheet_num} = _parse_sheet($worksheet) if $worksheet;
  29.         }

  30.         return \%result_hash;
  31.     }
  32. }

  33. sub _parse_sheet {
  34.     my $worksheet = shift;

  35.     my @sheet_array = ();

  36.     my ( $row_min, $row_max ) = $worksheet->row_range();
  37.     my ( $col_min, $col_max ) = $worksheet->col_range();

  38.     for my $row ( $row_min .. $row_max ) {
  39.         my @row_array = ();

  40.         for my $col ( $col_min .. $col_max ) {
  41.             my $cell = $worksheet->get_cell( $row, $col );

  42.             if ($cell) {
  43.                 if ( $cell->type() eq 'Date' ) {
  44.                     push @row_array, ExcelFmt( 'yyyy-mm-dd HH:MM:SS', $cell->unformatted() );
  45.                 }
  46.                 else {
  47.                     push @row_array, $cell->value();
  48.                 }
  49.             }
  50.             else {
  51.                 push @row_array, undef;
  52.             }
  53.         }

  54.         push @sheet_array, \@row_array;
  55.     }

  56.     return \@sheet_array;
  57. }

复制代码

论坛徽章:
0
5 [报告]
发表于 2010-12-24 10:32 |只看该作者
用这个模块读取Spreadsheet:arseExcel;

论坛徽章:
0
6 [报告]
发表于 2010-12-24 10:33 |只看该作者
  1. Spreadsheet::ParseExcel;     用这个模块
复制代码

论坛徽章:
0
7 [报告]
发表于 2010-12-27 10:22 |只看该作者
多谢各位的指点,尤其是yiten,有啥不明白的会再麻烦你~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP