简体   繁体   English

Perl - Spreadsheet :: WriteExcel函数说明

[英]Perl - Spreadsheet::WriteExcel function explanation

I'd like to use the function "autofit_columns" as found here: CPAN 我想使用此处的函数“autofit_columns”: CPAN

Here's my program so far(I skipped the DB connect and query part) 到目前为止这是我的程序(我跳过了DB连接和查询部分)

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? 问题:我的列没有在xls文件中自动调试...任何想法为什么?

I don't get the peice of code: 我没有得到代码:

for my $width (@{$worksheet->{__col_widths}}) {

                $worksheet->set_column($col, $col, $width) if $width;
                $col++;
            } 

You need to look at that example again and implement add_write_handler part too before you write anything to your worksheet. 在向工作表写入任何内容之前,您需要再次查看该示例并实现add_write_handler部分。

Please take a look at 请看一下

$worksheet->add_write_handler(qr[\w], \&store_string_widths);

line and then at store_string_widths subroutine implementation. 行然后在store_string_widths子例程实现。

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. 祝你好运。

You are missing the part of the example code that adds the callback function: 您缺少添加回调函数的示例代码部分:

$worksheet->add_write_handler(qr[\w], \&store_string_widths);

You are also missing the store_string_widths() function. 您还缺少store_string_widths()函数。

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. 这在Spreadsheet :: WriteExcel中有点小巧。 It will be more integrated into the module in Excel::Writer::XLSX which is the replacement for WriteExcel. 它将更多地集成到Excel :: Writer :: XLSX中的模块中,它是WriteExcel的替代品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM