怎么读取excel里面的数据?
时间:2010-12-23
来源:互联网
我想要读取excel里面第一排里面所有的数据,应该怎么写?
作者: james_zhenghao 发布时间: 2010-12-23
右上角搜索,本版很多
作者: zhlong8 发布时间: 2010-12-23
用 Spreadsheet:
arseExcel 这个模块可以处理的

作者: scrit 发布时间: 2010-12-23
本帖最后由 yiten 于 2010-12-23 15:30 编辑
我这边有现成的,做下好人
复制代码
我这边有现成的,做下好人

- # parse excel 2003 file and return the data
- # examples:
- # 1. read the first sheet data: read_excel_2003('abc.xls'), return reference of sheet data
- # 2. read all the sheet data: read_excel_2003( 'abc.xls', 1 ), return array reference contains reference of every sheet data
- # 3. read specified sheet data: read_excel_2003( 'abc.xls', [ 0, 1, 4 ] ), return hash reference contains reference of the specified sheet data
- sub read_excel_2003 {
- my ( $file, $sheet_var ) = @_;
-
- die "File is not found: $file." unless -e $file;
-
- my $parser = Spreadsheet::ParseExcel->new();
- my $fmt = Spreadsheet::ParseExcel::FmtUnicode->new( Unicode_Map => 'CP936' );
-
- my $workbook = $parser->Parse( $file, $fmt );
-
- die $parser->error() unless $workbook;
-
- unless ( $sheet_var ) {
- return _parse_sheet( $workbook->worksheet(0) );
- }
- elsif ( !ref $sheet_var ) {
- my @result_array = ();
-
- for my $worksheet ( $workbook->worksheets() ) {
- push @result_array, _parse_sheet($worksheet);
- }
-
- return \@result_array;
- }
- elsif ( ref $sheet_var eq 'ARRAY' ) {
- my %result_hash = ();
-
- for my $sheet_num ( @$sheet_var ) {
- my $worksheet = $workbook->worksheet($sheet_num);
- $result_hash{$sheet_num} = _parse_sheet($worksheet) if $worksheet;
- }
-
- return \%result_hash;
- }
- }
-
- sub _parse_sheet {
- my $worksheet = shift;
-
- my @sheet_array = ();
-
- my ( $row_min, $row_max ) = $worksheet->row_range();
- my ( $col_min, $col_max ) = $worksheet->col_range();
-
- for my $row ( $row_min .. $row_max ) {
- my @row_array = ();
-
- for my $col ( $col_min .. $col_max ) {
- my $cell = $worksheet->get_cell( $row, $col );
-
- if ($cell) {
- if ( $cell->type() eq 'Date' ) {
- push @row_array, ExcelFmt( 'yyyy-mm-dd HH:MM:SS', $cell->unformatted() );
- }
- else {
- push @row_array, $cell->value();
- }
- }
- else {
- push @row_array, undef;
- }
- }
-
- push @sheet_array, \@row_array;
- }
-
- return \@sheet_array;
- }
作者: yiten 发布时间: 2010-12-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28