如何将许多数据保存到Excel文件中?
我想根据文本文件的数据将数据保存到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;
}
}
I would like to save data into Excel file based on data from text file. I tried to put 2 lines in text file. When saving the data in Excel file, I can only save 1 line from text file. I cannot save both lines in excel.
Below is my text file data :
1957041.0F1,9850-LOGOUT PROBE,B27A,Waiting,12
4511533.021,9050-OFFLOAD SEND,B18A,24,Waiting,12
output excel file out should be printed out that two lines as well.
my output excel file
1957041.0F1,9850-LOGOUT PROBE,B27A,Waiting,12
below is my sample code:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要嵌套循环。对于每行输入,请写入电子表格。循环后关闭电子表格。
You don't need nested loops. For each line of input, write to the spreadsheet. Close the spreadsheet after the loop.