如何使用Perl中的Excel模块在单元格中而不是下一个单元格中添加下一行

发布于 2025-02-13 20:09:39 字数 640 浏览 0 评论 0原文

我正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2025-02-20 20:09:40

如果要在一个单元格中整个文件,则无需按行读取它。您可以一口阅读文件,然后将其插入工作表中。我添加了按照 documentation 的格式。可能不是必需的(我没有Excel来测试它,Libreoffice在没有格式的情况下使其正常)。

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

my $WorkBook=Excel::Writer::XLSX->new('test.xls');
my $Worksheet=$WorkBook->add_worksheet();

open (my $in,'<',"file.txt")or die "could not open file.txt: $!";
my $file_content = do { local $/; <$in> };

my $format = $WorkBook->add_format();
$format->set_text_wrap();

$Worksheet-> write("A1",$file_content,$format);

如果您只想添加按行号选择的行,则可以将行读取到数组中。 &lt;&gt;运算符在列表上下文中使用时返回行列表。我在此处将一个元素备份到@lines,因此您的行号以1开头。然后使用数组切片并加入它:

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

my @line_numbers =  (4,5);

my $WorkBook=Excel::Writer::XLSX->new('test.xls');
my $Worksheet=$WorkBook->add_worksheet();

open (my $in,'<',"file.txt")or die "could not open file.txt: $!";

my @lines = ('', <$in>);
my $content = join ('',@lines[@line_numbers]);

my $format = $WorkBook->add_format();
$format->set_text_wrap();
$Worksheet-> write("A1",$content,$format);

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).

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

my $WorkBook=Excel::Writer::XLSX->new('test.xls');
my $Worksheet=$WorkBook->add_worksheet();

open (my $in,'<',"file.txt")or die "could not open file.txt: $!";
my $file_content = do { local $/; <$in> };

my $format = $WorkBook->add_format();
$format->set_text_wrap();

$Worksheet-> write("A1",$file_content,$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:

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

my @line_numbers =  (4,5);

my $WorkBook=Excel::Writer::XLSX->new('test.xls');
my $Worksheet=$WorkBook->add_worksheet();

open (my $in,'<',"file.txt")or die "could not open file.txt: $!";

my @lines = ('', <$in>);
my $content = join ('',@lines[@line_numbers]);

my $format = $WorkBook->add_format();
$format->set_text_wrap();
$Worksheet-> write("A1",$content,$format);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文