perl 如何将大量数据保存到Excel文件中?

wtlkbnrh  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(238)

我想根据文本文件中的数据将数据保存到Excel文件中。我尝试在文本文件中放置2行。当将数据保存到Excel文件中时,我只能保存文本文件中的1行。我无法在Excel中保存两行。
下面是我的文本文件数据:

1957041.0F1,9850-LOGOUT PROBE,B27A,Waiting,12
4511533.021,9050-OFFLOAD SEND,B18A,24,Waiting,12

输出excel文件输出应该打印出这两行以及。我的输出excel文件

1957041.0F1,9850-LOGOUT PROBE,B27A,Waiting,12

下面是我示例代码:

use strict;  
use Excel::Writer::XLSX;  

my $workbook= Excel::Writer::XLSX->new( 'C:/Users/pphyuphway/Downloads/myExcel.xlsx' );  
 
my $worksheet = $workbook->add_worksheet(); 

my $file =  "C:/Users/pphyuphway/Downloads/lot.txt";
my $file1 =  "C:/Users/pphyuphway/Downloads/test321.txt";

open( my $fh4, "<", $file) or die "Could not open file '$file' $!";
my @check;
my $count = 0;
foreach my $check (<$fh4>) {
   my ($lot,$step,$designID,$status,$duration)  = split(/\,/,$check);

                    $count = $.;
                    print($count);
                    for(my $i = 1; $i<= $count ; $i++)
                      {
                            $worksheet->write( "A$i", "$lot");
                            $worksheet->write( "B$i", "$designID");
                            $worksheet->write( "C$i", "$step");
                            $worksheet->write( "D$i", "$duration");
                            $worksheet->write( "E$i", "$status");
                            $worksheet->write( "F$i", "Error");
                            $workbook->close;  
                            
                      }
 
                        
                     }
wbgh16ku

wbgh16ku1#

您不需要嵌套循环。对于每一行输入,写入电子表格。在循环后关闭电子表格。

use warnings;
use strict;
use Excel::Writer::XLSX;

my $workbook= Excel::Writer::XLSX->new( 'C:/Users/pphyuphway/Downloads/myExcel.xlsx' );
my $worksheet = $workbook->add_worksheet();

my $file = "C:/Users/pphyuphway/Downloads/lot.txt";
open( my $fh4, "<", $file) or die "Could not open file '$file' $!";

my $i = 1;
foreach my $check (<$fh4>) {
    my ($lot,$step,$designID,$status,$duration) = split(/,/, $check);
    $worksheet->write( "A$i", $lot);
    $worksheet->write( "B$i", $designID);
    $worksheet->write( "C$i", $step);
    $worksheet->write( "D$i", $duration);
    $worksheet->write( "E$i", $status);
    $worksheet->write( "F$i", "Error");
    $i++;
}
$workbook->close;

相关问题