我正在使用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');
答案 0 :(得分:3)
my($row, $coll) = 0;
$coll
未定义
使用
my($row, $coll)=(0,0);
merge_range
使用4行,coll,row,coll是完全有效的(前提是它们有值;)
答案 1 :(得分:2)
从我在Spreadsheet::WriteExcel
文档示例中可以看到,单元格范围应该作为单个字符串参数传递,尽管John指出,单独的参数也是有效的:
对于单个参数形式,它看起来像这样:
$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 );