CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



giridhar276
Novice

Jul 4, 2013, 6:57 PM

Post #1 of 2 (1419 views)
Unable to create spreadsheet in cgi script Can't Post

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



Code
#!/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";

$report_name="Network_Availability_Report_".$report_month."_".$report_year.".xlsx";

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="http://25.209.200.143/$report_name"><img src="http://25.209.200.143/tcs/images/xlsx.png" width=40 height=40></a>~;
print '</body>';
print '</html>';

1;


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.

Error


Code
  
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.

Regards,
J


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


FishMonger
Veteran / Moderator

Jul 9, 2013, 6:58 AM

Post #2 of 2 (1383 views)
Re: [giridhar276] Unable to create spreadsheet in cgi script [In reply to] Can't Post

The error message is indicating that this line failed.

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

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

Code
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).

Code
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.

Code
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:

Code
Excel::Writer::XLSX->new("$report_name");

do this:

Code
Excel::Writer::XLSX->new($report_name);


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives