简体   繁体   中英

Perl dump data from a hash to a spreadsheet

I have a hash with key and Values(array). I want to dump them to a spreadsheet

%hash
   key1 -> foo bar 
   key2-> john adam gill
   key3-> apple banana mango orange

Code:

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

my $workbook = Excel::Writer::XLSX->new( 'c:\TEMP\filename.xlsx' ); 
my $worksheet = $workbook->add_worksheet();

my $row = 1;
my $col = 1;

foreach my $k (keys %hash)
{
    $worksheet->write($row, $col)->{Value} =  $k;                # title
    $worksheet->write($row, $col++,)->{Value} =  $hash{$k};     # value
    $row++;
}

Current Output
Can't call method "add_worksheet" on an undefined value

Desired Output

在此处输入图片说明

It seems that Excel::Writer::XLSX->new failed. Add this code after calling new , to see what went wrong:

if (not defined $workbook)
{
    die "Failed to create spreadsheet: $!";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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