Home: Perl Programming Help: Intermediate:
Unable to create spreadsheet in cgi script


Jul 4, 2013, 6:57 PM

Views: 4371
Unable to create spreadsheet in cgi script

hi folks,

I am trying to download xlsx from cgi page in browser but not sure where I made a mistake.

the cgi script contains the code for creating xlsx and just by clicking on the image I should be able to

#!/usr/bin/perl -w 
use Excel::Writer::XLSX;
use DBI;
use DBD::mysql;
use CGI;
use CGI qw(:standard);
use CGI::Carp qw ( fatalsToBrowser );

$q=new CGI;

$report_year = $q->param('Field_year');
$report_month = $q->param('Field_month');
$report_year = $q->param('Field_year');
$string="$report_month"." "."$report_year"." "."Network Availability Report";


my $workbook = Excel::Writer::XLSX->new("$report_name");
my $worksheet = $workbook->add_worksheet($report_month);

print "Content-type: text/html\n\n";
print '<html>';
print '<head>';
print '<title>Hello Word - First CGI Program</title>';
print '</head>';
print '<body>';
print "<h2>$report_month</h2>";
print "<h2>$report_year</h2>";
#print qq~<a href="$report_name"><img src="" width=40 height=40></a>~;
print '</body>';
print '</html>';


If I click on the image then I need to open the spreadsheet.

I have managed to execute the same in windows and able to display it on windows.

Now I am deploying the code in unix but failed to display this cgi script.


Can't call method "add_worksheet" on an undefined value at /srv/www/cgi-bin/gnsncc/temp_final_report.cgi line 20.

I made sure that all the scripts has 755 permissions.

I am executing the scripts from cgi-bin folder.

Could anyone please let me know what went wrong.


(This post was edited by giridhar276 on Jul 4, 2013, 7:02 PM)

Veteran / Moderator

Jul 9, 2013, 6:58 AM

Views: 4335
Re: [giridhar276] Unable to create spreadsheet in cgi script

The error message is indicating that this line failed.

my $workbook   = Excel::Writer::XLSX->new("$report_name");

i.e., $workbook is undefined. You should test for that before creating the worksheet.

defined $workbook or die "Problems creating new Excel file: $!";

1) You're missing 2 very important pragmas, which should be in every Perl script you write. Add these pragmas and declare all of your vars (like you did with $workbook).

use strict; 
use warnings;

2) Why are you loading CGI twice?

3) Why are you assigning $report_year twice?

4) No need to use concatenation when assigning $report_name, it just adds unnecessary clutter.

my $report_name = "Network_Availability_Report_$report_month_$report_year.xlsx";

5) Since you're loading the CGI module, why not use its methods to output the html? Doing so would cleanup that portion.

6) Don't quote single vars unnecessarily.
See perldoc -q quoting http://perldoc.perl.org/perlfaq4.html#What%27s-wrong-with-always-quoting-%22$vars%22?

e.g., instead of:


do this: