使用Spreadsheet :: SimpleExcel

我正在学习在我的Perl脚本中使用模块。

我需要根据两列对Excel文件进​​行sorting,并将结果写入新文件,我可以使用Spreadsheet::SimpleExcel

 $excel->sort_data($file,12,'ASC'); $excel->sort_data($file,11,'DESC'); $excel->output_to_file("my_excel.xls") or die $excel->errstr(); 

但我怎么给我的input文件$excel或从input文件加载我的数据?

Spreadsheet :: SimpleExcel将不会为您读取/parsing现有的Excel文件。 首先需要使用Spreadsheet :: ParseExcel加载文件,然后在SimpleExcel中创build一个新的工作表,然后插入数据,然后对其进行sorting。 代码没有经过testing,但它应该给你一个如何去做的想法。

 #!/usr/bin/perl use strict; use warnings; use Spreadsheet::ParseExcel; use Spreadsheet::SimpleExcel; my $parser = Spreadsheet::ParseExcel->new(); my $input = $parser->parse('MyInputFile.xls'); my $output = Spreadsheet::SimpleExcel->new(); my $worksheet = $input->worksheets(0); $output->add_worksheet($worksheet->get_name); 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_data; for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; push @row_data, $cell->value(); } $output->add_row($worksheet->get_name, \@row_data); } $output->sort_data($worksheet->get_name, 12, 'ASC'); $output->sort_data($worksheet->get_name, 11, 'ASC'); $output->output_to_file('MyOutputFile.xls');