使用 Spreadsheet::WriteExcel 读取文件并将段写入 Excel
Read a file and write segments into Excel using Spreadsheet::WriteExcel
作为我上一个问题的扩展,我需要从第一行提取输入文件直到空行,然后将输出按顺序粘贴到 xls 的单个单元格中。例如,“E:sda E:qwe E:ass”将打印在A1单元格中,NA将打印在A3单元格中,“E:sda E:qwe E:ass”将打印在A5单元格中。输入文件的模式会改变,但每一段都由一个空行分隔。下面的代码片段只能将整个文件内容打印到一个单元格中。
输入文件:
E: sda
E: qwe
E: sss
NA
E: sda
E: qwe
E: sss
NA
NA
E: xx
E: xxxs
NA
代码:
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $filename = 'all_in_one.txt';
my $workbook = Spreadsheet::WriteExcel->new("sad.xls");
my $wrksheet = $workbook->add_worksheet("summary");
for (my $i = 9) {
my $result1 = read_out_your_file_misc($filename,"\n");
$wrksheet->write($i, 1, [$result1]);
$i = $i + 2;
}
sub read_out_your_file_misc {
my $filename = shift or die "ERROR: read_out_your_file_misc needs a filename";
my $delim = shift || ','; # make the default delimiter a comma
open my $fhh, '<', $filename or die "ERROR: $filename: $!";
my @content_of_file = <$fhh>; # read the whole file at once
close $fhh;
chomp @content_of_file; # chomp the complete array
if(wantarray) { # return an array if that's wanted
@content_of_file;
} else { # else return it as a scalar with $delim between elements
join($delim, @content_of_file);
}
}
您正在将输入文件的每一行读取到数组中的一个单独元素中。但是,你不想那样做。
如果您将 input record separator 变量 ($/
) 更改为使用 段落模式 ,您可以将每个 组 行到每隔一行的单独单元格中:
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $filename = 'all_in_one.txt';
local $/ = ""; # paragraph mode
open my $fhh, '<', $filename or die "ERROR: $filename: $!";
my @content_of_file = <$fhh>; # read the whole file at once
close $fhh;
chomp @content_of_file; # chomp the complete array
my $workbook = Spreadsheet::WriteExcel->new("sad.xls");
my $wrksheet = $workbook->add_worksheet("summary");
for my $i (0 .. $#content_of_file) {
$wrksheet->write(2*$i, 1, $content_of_file[$i]);
}
您将整个文件放在一个单元格中的原因是您 join
将所有行重新组合成一个标量变量 ($result1
),然后将该单个值写入一个单元格。另外,for
循环有点奇怪,它只循环一次。
作为我上一个问题的扩展,我需要从第一行提取输入文件直到空行,然后将输出按顺序粘贴到 xls 的单个单元格中。例如,“E:sda E:qwe E:ass”将打印在A1单元格中,NA将打印在A3单元格中,“E:sda E:qwe E:ass”将打印在A5单元格中。输入文件的模式会改变,但每一段都由一个空行分隔。下面的代码片段只能将整个文件内容打印到一个单元格中。
输入文件:
E: sda
E: qwe
E: sss
NA
E: sda
E: qwe
E: sss
NA
NA
E: xx
E: xxxs
NA
代码:
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $filename = 'all_in_one.txt';
my $workbook = Spreadsheet::WriteExcel->new("sad.xls");
my $wrksheet = $workbook->add_worksheet("summary");
for (my $i = 9) {
my $result1 = read_out_your_file_misc($filename,"\n");
$wrksheet->write($i, 1, [$result1]);
$i = $i + 2;
}
sub read_out_your_file_misc {
my $filename = shift or die "ERROR: read_out_your_file_misc needs a filename";
my $delim = shift || ','; # make the default delimiter a comma
open my $fhh, '<', $filename or die "ERROR: $filename: $!";
my @content_of_file = <$fhh>; # read the whole file at once
close $fhh;
chomp @content_of_file; # chomp the complete array
if(wantarray) { # return an array if that's wanted
@content_of_file;
} else { # else return it as a scalar with $delim between elements
join($delim, @content_of_file);
}
}
您正在将输入文件的每一行读取到数组中的一个单独元素中。但是,你不想那样做。
如果您将 input record separator 变量 ($/
) 更改为使用 段落模式 ,您可以将每个 组 行到每隔一行的单独单元格中:
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $filename = 'all_in_one.txt';
local $/ = ""; # paragraph mode
open my $fhh, '<', $filename or die "ERROR: $filename: $!";
my @content_of_file = <$fhh>; # read the whole file at once
close $fhh;
chomp @content_of_file; # chomp the complete array
my $workbook = Spreadsheet::WriteExcel->new("sad.xls");
my $wrksheet = $workbook->add_worksheet("summary");
for my $i (0 .. $#content_of_file) {
$wrksheet->write(2*$i, 1, $content_of_file[$i]);
}
您将整个文件放在一个单元格中的原因是您 join
将所有行重新组合成一个标量变量 ($result1
),然后将该单个值写入一个单元格。另外,for
循环有点奇怪,它只循环一次。