简体   繁体   English

使用 Spreadsheet::WriteExcel 读取文件并将段写入 Excel

[英]Read a file and write segments into Excel using Spreadsheet::WriteExcel

As an extension to my previous question, I need to extract the input file from first line until an empty line and paste the output into a single cell of xls in an order.作为我上一个问题的扩展,我需要从第一行提取输入文件直到一个空行,然后按顺序将 output 粘贴到单个 xls 单元格中。 For example, "E: sda E: qwe E: ass" will be printed in cell A1, NA will be printed on A3, "E: sda E: qwe E: ass" will be printed in cell A5.例如,“E:sda E:qwe E:ass”将打印在单元格 A1 中,NA 将打印在 A3 中,“E:sda E:qwe E:ass”将打印在单元格 A5 中。 The pattern of the input file will change, but each segment is separated by an empty line.输入文件的模式会改变,但每个段由空行分隔。 The below code snippet is only able to print the whole file content into a cell.下面的代码片段只能将整个文件内容打印到一个单元格中。

Input file:输入文件:

E: sda
E: qwe
E: sss

NA

E: sda
E: qwe
E: sss

NA

NA

E: xx
E: xxxs

NA

Code:代码:

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);
    }
}

You are reading each line of your input file into a separate element in an array.您正在将输入文件的每一行读入数组中的单独元素。 But, you don't want to do that.但是,你不想那样做。

If you change the input record separator variable ( $/ ) to use paragraph mode , you can place each group of lines into a separate cell in every other row:如果您将输入记录分隔符变量 ( $/ ) 更改为使用段落模式,则可以将每组行放置在每隔一行的单独单元格中:

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]);
}

The reason you get the whole file in one cell is that you join all the lines back together into a scalar variable ( $result1 ), then write that single value to a cell.将整个文件放在一个单元格中的原因是您join所有行重新连接到一个标量变量( $result1 )中,然后将该单个值写入一个单元格。 Also, the for loop is a little strange, and it only loops once.另外, for循环有点奇怪,它只循环一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM