如何通过Excel :: Writer :: XLSX添加硬回车?

时间:2014-08-19 19:25:50

标签: perl xlsx

应该简单但很难谷歌:如何在插入.xlsx电子表格的文本中获得硬回报(如在Excel< Alt + Enter 中)通过perl模块Excel::Writer::XLSX

2 个答案:

答案 0 :(得分:3)

将“\ n”添加到字符串中,然后调整格式。

(参见文档:https://metacpan.org/pod/Excel::Writer::XLSX#set_text_wrap

use 5.014;
use warnings;
use Excel::Writer::XLSX;
my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );
my $worksheet = $workbook->add_worksheet();

my $format = $workbook->add_format();
$format->set_text_wrap(); # essential for correct "look" in Excel.
$worksheet->write( "B2", "Hi\nExcel!", $format );
                            ^^

答案 1 :(得分:3)

假设您正在使用Excel::Writer::XLSX,您只需输入换行符:

#!/usr/bin/perl

use warnings;
use strict;

use Excel::Writer::XLSX;

# Create a new Excel workbook
my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );

# Add a worksheet
my $worksheet = $workbook->add_worksheet();

my ($row, $col) = (0, 0);
$worksheet->write($row, $col, "Hi Excel!\nHi, again!");

$workbook->close;

大部分内容都是直接基于文档。