我想从一张 Excel 表格中提取一些信息并将其重新格式化为另一张表格。数据结构类似于:
col1 col2
row1 School 1
row2 Dean John
row3 No.stu. 55
row4 some irrelevant stuff
row5 School2 2
row6 Dean Tony
row7 No. stu. 60
row8 some irrelevant stuff
row9 School 3
row10 Dean James
row11 No.stu. 56
row12 No. teacher 20
我想要实现的输出是:
col1 col2 col3
row1 School Dean No.stu. No. teacher
row2 1 John 55
row3 2 Tony 60
row4 3 James 56 20
我被建议用来从 Excel 中提取信息的代码如下(感谢来自PerlMonks的 hdb )。
use strict; use warnings;
use Spreadsheet::ParseExcel;
my ($infile) = @ARGV;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse($infile);
die $parser->error unless defined $workbook;
my ($worksheet) = $workbook->worksheets();
my %data; # Accumulate data here
my $row = 0;
my $school = 0;
while (1) {
my $cell = $worksheet->get_cell($row, 0);
last unless defined($cell);
my $key = $cell->value();
my $data = $worksheet->get_cell($row++, 1)->value();
if( $key eq "School" ) {
$school = $data;
}
else {
$data{$school}{$key} = $data;
}
}
现在下一步是将所有内容重新写入新的 Excel 工作表。我怎么能那样做?