Spreadsheet::WriteExcel 未使用 merge_range 在 Excel 中写入字符串

发布于 2024-12-18 00:05:13 字数 1005 浏览 0 评论 0原文

我正在使用 Spreadsheet::WriteExcel 创建一个 Excel 并向其中写入一些数据。 我正在使用 merge_range 来合并一些单元格。 但问题是我的下面的代码确实合并了单元格,但不显示其中的数据($str 内容)。 我无法弄清楚为什么????。

请帮帮我,我哪里出错了?

以下是我的代码的一部分:

#!/usr/bin/perl

use Spreadsheet::WriteExcel;
use strict;

my $wb = Spreadsheet::WriteExcel->new('excel_test.xls');
my $custom_ws = $wb->add_worksheet('Custom Parameters');

my $wb_format_merge = $wb->add_format();
$wb_format_merge->set_bold();
$wb_format_merge->set_text_wrap();
$wb_format_merge->set_border(1);
$wb_format_merge->set_align('left');
$wb_format_merge->set_valign('vcenter');

my($row, $coll) = 0;
my $merge_coll_len = 8;

###### write data ###
my $str = "Please select the follwong metrics to generate custom report in Details sheet";
$custom_ws->merge_range($row, $coll, $row, $coll + $merge_coll_len, $str,$merge_format);

$row += 2;
$custom_ws->write($row, $coll, 'select the data type for the reference');

I am using Spreadsheet::WriteExcel to create an excel and write some data into it.
I am using the merge_range to merger some cells.
But the problem is my below code does merge cells but dont show the data in it ($str contents).
I am not able to figure out why????.

Please help me, where did i going wrong?

Below is the part of my code:

#!/usr/bin/perl

use Spreadsheet::WriteExcel;
use strict;

my $wb = Spreadsheet::WriteExcel->new('excel_test.xls');
my $custom_ws = $wb->add_worksheet('Custom Parameters');

my $wb_format_merge = $wb->add_format();
$wb_format_merge->set_bold();
$wb_format_merge->set_text_wrap();
$wb_format_merge->set_border(1);
$wb_format_merge->set_align('left');
$wb_format_merge->set_valign('vcenter');

my($row, $coll) = 0;
my $merge_coll_len = 8;

###### write data ###
my $str = "Please select the follwong metrics to generate custom report in Details sheet";
$custom_ws->merge_range($row, $coll, $row, $coll + $merge_coll_len, $str,$merge_format);

$row += 2;
$custom_ws->write($row, $coll, 'select the data type for the reference');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柏林苍穹下 2024-12-25 00:05:13
my($row, $coll) = 0;

$coll 未定义

use

my($row, $coll)=(0,0);

使用 4 row,coll,row,coll 作为 merge_range< /code> 完全有效(只要它们有值;))

my($row, $coll) = 0;

$coll is undefined

use

my($row, $coll)=(0,0);

Using 4 row,coll,row,coll for the merge_range is perfectly valid (provided they have values ;) )

甜尕妞 2024-12-25 00:05:13

从我在 Spreadsheet::WriteExcel 文档中可以看到例如,单元格范围应该作为单个字符串参数传递,尽管正如约翰指出的那样,单独的参数也是有效的:

对于单个参数形式,它看起来像这样:

$custom_ws->merge_range('A1:H1', $str, $merge_format);

一个想法可能是滚动几个助手子程序允许使用这种形式:

sub range {

   my ( $from_row, $from_col ) = @{ $_[0] };
   my ( $to_row, $to_col ) = @{ $_[1] };

   die "Expecting to go from top-left to bottom-right"
       if $from_row > $to_row || $from_col > $to_col;

   return join '', alpha( $from_row ), $from_col, ':', alpha( $to_row ), $to_col;
}

sub alpha {
    my $row = 'A';
    $row++ for 1 .. +shift;
    return $row;
}

然后调用range

$custom_ws->merge_range( range( [ $row, $col ],
                                [ $row, $col + $length ] ),
                         $str,
                         $merge_format );

From what I can see in the Spreadsheet::WriteExcel documentation example, the cell ranges are supposed to be passed as a single string argument, although as John points out, separate arguments are valid also:

For the single argument form, it would look like this:

$custom_ws->merge_range('A1:H1', $str, $merge_format);

One idea could be to roll a couple of helper subroutines to allow for the use of this form:

sub range {

   my ( $from_row, $from_col ) = @{ $_[0] };
   my ( $to_row, $to_col ) = @{ $_[1] };

   die "Expecting to go from top-left to bottom-right"
       if $from_row > $to_row || $from_col > $to_col;

   return join '', alpha( $from_row ), $from_col, ':', alpha( $to_row ), $to_col;
}

sub alpha {
    my $row = 'A';
    $row++ for 1 .. +shift;
    return $row;
}

And then call range:

$custom_ws->merge_range( range( [ $row, $col ],
                                [ $row, $col + $length ] ),
                         $str,
                         $merge_format );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文