I’m new to Perl and want to plot a chart using Excel. I found some code, but when I tried to execute it, it only shows “Press any key to continue…” on the command prompt. The code is executable by others. I also tried to download the Excel writer pack and placed them in the lib folder, but it isn’t executing at all. The code is found definitely executable.
#!/usr/bin/perl
use strict;
use warnings;
use Excel::Writer::XLSX;
my $workbook = Excel::Writer::XLSX->new('chart.xlsx');
my $worksheet = $workbook->add_worksheet();
# Add the worksheet data the chart refers to.
my $data = [
['Category', 2, 3, 4, 5, 6, 7],
['Value', 1, 4, 5, 2, 1, 5],
];
$worksheet->write('A1', $data);
# Add a worksheet chart.
my $chart = $workbook->add_chart(type => 'column');
# Configure the chart.
$chart->add_series(
categories => '=Sheet1!$A$2:$A$7',
values => '=Sheet1!$B$2:$B$7',
);
__END__
Does anyone have any idea?
Here’s what Oleg means by his answer:
Depending on the operation system you’re on, your way to call the perl program might look a little different. This it what it looks like for me.
simbabque@box:~> perl xsltest.pl
simbabque@box:~>
Now if you’re not sure where your working directory is, edit the code like this:
Now when you run your program, it should print something like this:
In my case, the working directory is the one I’m calling perl from. It could be another one, though.
Although it does not print anything to the screen, it should have created the file called
chart.xlsxin my working directory. I can look for it withlson the command line, or sayfile chart.xlsxto see if it is inded an Excel file.I could now open it in Excel or Libre Office.
If you are on Windows, open an Explorer and point it to where the
cwdfunction has said. There you can double-click your file to open it in Excel. If it is the same folder as the program you could also use the open dialogue of your text editor, right-click onchart.xlsxand click open instead of select.