无法使用perl中的xlsx模块在excel文件中添加新行

时间:2017-05-15 04:59:53

标签: excel perl xlsx

我正在尝试在excel文件中追加新行,只有最后一个数据存在,因为它会覆盖以前写入的任何数据,在此代码中我有一个for循环再次循环以在第二行写入相同的数据但是,它会重写现有数据

use strict;
use warnings;
use Excel::Writer::XLSX;

my $workbook    = Excel::Writer::XLSX->new( 'ke.xlsx' );#create the excel

my $worksheet = $workbook->add_worksheet(); 

my $server= "se";
my $domain  = "de";
my $backup = "b";



# Some sample data for the table.
my $data = [[ $server, $domain ,$backup],
   ];
$worksheet->set_column( 'A:G', 20 );

for(my $i = 0 ;$i<2;$i++){
     $worksheet->add_table(   # Add a table to the worksheet.
    'A1:G8',
    {
       data      => $data,
        total_row => 1,
        columns   => [
            { 

                header => 'server name', 
            },

            {
                header         => 'Domain Name',


            },
            {
                header         => 'Back Up address',


            }
        ]
    }
);

}

3 个答案:

答案 0 :(得分:0)

您的代码每次都会在A1:G8处写入表格。如果您希望表位于不同的位置,则需要更改它。

答案 1 :(得分:0)

似乎使用样本数据为我工作:

use strict;
use warnings;
use Excel::Writer::XLSX;

my $workbook = Excel::Writer::XLSX->new('ke.xlsx' );#create the excel

my $worksheet = $workbook->add_worksheet(); 

#my $server= "se";
#my $domain  = "de";
#my $backup = "b";

# Some sample data for the table.
#my $data = [[ $server, $domain ,$backup],
#   ];

my $data = [ ["se", "de","b"],
             ["ll", "pp","t"],
             ["yu", "ar","e"],
             ["gt", "po","w"],
             ["br", "tp","g"] ];
$worksheet->set_column( 'A:G', 20 );

for(my $i = 0 ;$i<2;$i++){
     $worksheet->add_table(   # Add a table to the worksheet.
    'A1:G8',
    {
       data      => $data,
        total_row => 1,
        columns   => [
            { 
                header => 'server name', 
            },

            {
                header => 'Domain Name',
            },
            {
                header => 'Back Up address',
            }
        ]
    }
);

}

enter image description here

答案 2 :(得分:0)

我已经使用过这个模块,但是我们没有使用add_table而是使用了$ worksheet-&gt; write_row。 我认为你应该尝试使用write_row编写数据 https://metacpan.org/pod/Excel::Writer::XLSX

如果您不想使用write_row,那么有关add_table的更多示例,请访问:https://metacpan.org/pod/Excel::Writer::XLSX#TABLES-IN-EXCEL

希望有所帮助:)