Perl - Spreadsheet::WriteExcel 函数解释
我想使用此处找到的函数“autofit_columns”:CPAN
这是到目前为止我的程序(我跳过了数据库连接和查询部分)
my $workbook = Spreadsheet::WriteExcel->new("TEST.xls");
my $bold = $workbook->add_format();
$bold->set_bold();
my $number = $workbook->add_format();
$number->set_num_format(0x01);
$worksheet = $workbook->add_worksheet('Sheet1');
my @headings = ('Blabla...');
foreach $i (@headings){
$worksheet->write(0, $col++, $i, $bold);
};
$col=0;
$lrow=1;
while (@row = $sth->fetchrow_array()) {
$worksheet->write($lrow,$col,\@row);
$lrow++;
};
$sth->finish;
$dbh->disconnect;
autofit_columns($worksheet);
$workbook->close();
sub autofit_columns {
my $worksheet = shift;
my $col = 0;
for my $width (@{$worksheet->{__col_widths}}) {
$worksheet->set_column($col, $col, $width) if $width;
$col++;
}
}
问题:我的列不是自动调整到 xls 文件中...知道为什么吗?
我不明白这段代码:
for my $width (@{$worksheet->{__col_widths}}) {
$worksheet->set_column($col, $col, $width) if $width;
$col++;
}
I'd like to use the function "autofit_columns" as found here:CPAN
Here's my program so far(I skipped the DB connect and query part)
my $workbook = Spreadsheet::WriteExcel->new("TEST.xls");
my $bold = $workbook->add_format();
$bold->set_bold();
my $number = $workbook->add_format();
$number->set_num_format(0x01);
$worksheet = $workbook->add_worksheet('Sheet1');
my @headings = ('Blabla...');
foreach $i (@headings){
$worksheet->write(0, $col++, $i, $bold);
};
$col=0;
$lrow=1;
while (@row = $sth->fetchrow_array()) {
$worksheet->write($lrow,$col,\@row);
$lrow++;
};
$sth->finish;
$dbh->disconnect;
autofit_columns($worksheet);
$workbook->close();
sub autofit_columns {
my $worksheet = shift;
my $col = 0;
for my $width (@{$worksheet->{__col_widths}}) {
$worksheet->set_column($col, $col, $width) if $width;
$col++;
}
}
PROBLEM: My columns are not autofitted in the xls file... Any idea why?
I don't get the peice of code:
for my $width (@{$worksheet->{__col_widths}}) {
$worksheet->set_column($col, $col, $width) if $width;
$col++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在向工作表写入任何内容之前,您需要再次查看该示例并实现
add_write_handler
部分。请查看该
行,然后查看
store_string_widths
子例程实现。答案是您需要在每次写入时存储字符串的绝对宽度。然后,将所有数据写入工作表后,您需要遍历行并找到每列的最大字符串“长度” - 这将是所需的列宽度。
祝你好运。
You need to look at that example again and implement
add_write_handler
part too before you write anything to your worksheet.Please take a look at
line and then at
store_string_widths
subroutine implementation.Answer is that you need to store absolute width of the string at each write. Then, after you wrote all data to your worksheet, you need to walk through rows and find the biggest string's 'length' for each column - that would be desired column width.
Wish you luck.
您缺少示例代码中添加回调函数的部分:
您还缺少
store_string_widths()
函数。关于第二个问题,回调存储每列使用的最大字符串长度。该代码片段使用这些长度来设置从第一列到最后一列存储长度的每一列的列宽。如果列没有存储 autfit 宽度,则不会调整其宽度。
这在 Spreadsheet::WriteExcel 中有点棘手。它将更加集成到 Excel::Writer::XLSX 中的模块中,这是 WriteExcel 的替代品。
You are missing the part of the example code that adds the callback function:
You are also missing the
store_string_widths()
function.In relation to your second question, the callback stores the maximum string length used for each column. The code snippet is using these lengths to set the column width for each column from the first to the last column that has a length stored. If a column hasn't an autfit width stored then its width isn't adjusted.
This is all a little hacky in Spreadsheet::WriteExcel. It will be more integrated into the module in Excel::Writer::XLSX which is the replacement for WriteExcel.