如何使用Perl中的Excel模块在单元格中而不是下一个单元格中添加下一行
我正在学习Perl中的Excel模块。我正在阅读一个有4行的文件。当我使用Excel模块时,它将打印到下一个单元格。有什么方法可以在同一单元格中打印。 我还尝试在行末尾添加“^@”符号,并在同一单元格中添加带有额外字符的单元格,
请预先提前
文件1:
line 1
line 2
line 3
line 4
use strict;
use warnings;
use Excel::Writer::XLSX;
my $WorkBook=Excel::Writer::XLSX->new('test.xls');
my $Worksheet=$WorkBook->add_worksheet();
my %data;
open (INPUT,"file");
my $line_no=1;
while (<INPUT>) {
$data{$line_no}=$_;
$line_no=$line_no+1;
}
foreach my $i (keys %data) {
$Worksheet->write("A$i",$data{$i});
}
所有语法均已照顾和粘贴的代码段。
I am learning EXCEL module in perl. I am reading a file which has 4 lines. When i use Excel module it is print to next cell. Is there any way to print in same cell.
I have also tried adding "^@" symbol at the end of line and it is adding in same cell with extra character
Thanks in advance
file 1:
line 1
line 2
line 3
line 4
use strict;
use warnings;
use Excel::Writer::XLSX;
my $WorkBook=Excel::Writer::XLSX->new('test.xls');
my $Worksheet=$WorkBook->add_worksheet();
my %data;
open (INPUT,"file");
my $line_no=1;
while (<INPUT>) {
$data{$line_no}=$_;
$line_no=$line_no+1;
}
foreach my $i (keys %data) {
$Worksheet->write("A$i",$data{$i});
}
All syntax is taken care and snippet of code pasted here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在一个单元格中整个文件,则无需按行读取它。您可以一口阅读文件,然后将其插入工作表中。我添加了按照 documentation 的格式。可能不是必需的(我没有Excel来测试它,Libreoffice在没有格式的情况下使其正常)。
如果您只想添加按行号选择的行,则可以将行读取到数组中。
&lt;&gt;
运算符在列表上下文中使用时返回行列表。我在此处将一个元素备份到@lines
,因此您的行号以1开头。然后使用数组切片并加入它:If you want the whole file in one cell, you don't need to read it line by line. You can read the file in one go, then insert it into your worksheet. I added the format as suggested by the documentation. It might not be necessary ( I don't have EXCEL to test it, LibreOffice renders it fine without the format).
In case you want to add only lines selected by line numbers, you can read the lines into an array. The
<>
operator returns a list of lines when used in list context. I prepended an element to@lines
here, so your line numbers start with 1. Then use an array slice and join it: