Perl - Spreadsheet::WriteExcel 函数解释

发布于 2024-12-10 04:26:48 字数 1322 浏览 1 评论 0原文

我想使用此处找到的函数“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 技术交流群。

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

发布评论

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

评论(2

神经暖 2024-12-17 04:26:48

在向工作表写入任何内容之前,您需要再次查看该示例并实现 add_write_handler 部分。

请查看该

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

行,然后查看 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

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

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.

懒的傷心 2024-12-17 04:26:48

您缺少示例代码中添加回调函数的部分:

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

您还缺少 store_string_widths() 函数。

关于第二个问题,回调存储每列使用的最大字符串长度。该代码片段使用这些长度来设置从第一列到最后一列存储长度的每一列的列宽。如果列没有存储 autfit 宽度,则不会调整其宽度。

这在 Spreadsheet::WriteExcel 中有点棘手。它将更加集成到 Excel::Writer::XLSX 中的模块中,这是 WriteExcel 的替代品。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文