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:
Perl CGI : unable to download the excel sheet from perl cgi page

 



giridhar276
Novice

Jun 7, 2013, 1:24 AM

Post #1 of 2 (913 views)
Perl CGI : unable to download the excel sheet from perl cgi page Can't Post

Hi All,

I have written an cgi perl script that displays an image(Excel image) and when clicked on that Image I need to download a excel sheet.

I made sure that excel sheet exists in the folder with the given name but still I am not able to download the sheet.


Code:print "<center><table border=0>";print "<tr><td aligh=left>";print qq~<a href="$report_name"><img src="http://mywebsite.com/images/xlsx.png" width=40 height=40></a>~;print qq~<a href="$report_name><img src="http://mywebsite.com/images/csv.jpg" width=40 height=40></a>~;print "</td></tr>";print "</table></center>";
I learned that we need to add the below lines for download the files using cgi perl.


Code:print ("Content-Type:application/x-download\n");print "Content-Disposition: attachment; filename=$report_name\n\n";
After adding ...


Code:print ("Content-Type:application/x-download\n");print "Content-Disposition: attachment; filename=$report_name\n\n";print "<center><table border=0>";print "<tr><td aligh=left>";print qq~<a href="$report_name"><img src="http://mywebsite.com/images/xlsx.png" width=40 height=40></a>~;print qq~<a href="$report_name><img src="http://mywebsite.com/images/csv.jpg" width=40 height=40></a>~;print "</td></tr>";print "</table></center>";
But only the above 2 lines on the browser and no luck...

Could anyone please let me know what went wrong ??

I am attaching the complete script with the thread.
(download statements are written in 311 and 312 lines)



Tons of thanks .....
Attachments: downloadtest.txt (10.3 KB)


hwnd
User

Jun 7, 2013, 10:53 AM

Post #2 of 2 (878 views)
Re: [giridhar276] Perl CGI : unable to download the excel sheet from perl cgi page [In reply to] Can't Post

I see alot you should change in your script to compact this down and make it easier to read and for us to understand. But from what I see could be the issue:

Try removing the second \n from from your second line, this confuses the HEADER and BODY block and starts the BODY block before the HEADER.

So replace:

Code
print "Content-Disposition: attachment; filename=$report_name\n\n";



With:

Code
print "Content-Disposition: attachment; filename=$report_name\n";



A few comments:


Code
1. Add use strict and use warnings to the top of your script and always implement this. 
And always use error handling in your scripts.

2. $q=new CGI; I'd use $q = CGI->new; (but you're using CGI standard, so you really don't need this)

3. Use lexical variables in the entire script, instead of using them sometimes
throughout your script. Example ( $report_month, should be my $report_month) and then
you can just call $report_month without using 'my' again.

4. You're chomping your variable data when you dont need to.

5. You're printing multiple header types which are confusing each other and
is most likely your issue here.

6. You're printing plain html in your script, when you can just use
the methods of CGI. See perldoc CGI..



Example of how I would change these things:


Code
Example of how I would change a few things.  

#!C:\Dwimperl\perl\bin\perl.exe

use strict;
use warnings;

use DBI;
use File::Copy;
use Excel::Writer::XLSX;
use CGI qw( :standard );
use CGI::Carp qw( fatalsToBrowser );

#__CONFIGURATION__

my $report_month = param('Field_month');
my $report_year = param('Field_year');

my @heading = join(' ', $report_month, $report_year,
'Network Availability Report');

my $report_name = join('', 'Telcom_Report_', $report_month,
$report_year, '.xlsx');

#__ROW/COL FORMAT__
# Stored in a hash..

my %hyperlink = ( underline => 1, color => 'blue' );

my %heading = ( bold => 1, color => 'blue', size => 15, merge => 1, align => 'center' );

my %text = ( border => 1, border_color => 'black', size => 12, align => 'left' );

my %heading1 = ( bold => 1, border => 1, border_color => 'black',
bgcolor => 'silver', size => 12, align => 'left' );


#__START WRITING__

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

$worksheet->set_row( 0, 40 );
$worksheet->set_column('A:A', 30);
$worksheet->set_column('B:B', 20);
$worksheet->set_column('C:C', 18);
$worksheet->set_column('E:F', 15);
$worksheet->set_column('G:G', 20);
$worksheet->set_column('H:H', 15);
$worksheet->set_column('I:J', 20);
$worksheet->set_column('K:L', 15);
$worksheet->set_column('M:M', 8);
$worksheet->set_column('N:N' ,15);
$worksheet->set_column('O:O', 4);

$worksheet->write('D1', \@headings, %heading);
$worksheet->write('A5', 'Client Name', %heading1);
$worksheet->write('B5', 'Bus.Hours', %heading1);
$worksheet->write('C5', 'Voice/Data', %heading1);
$worksheet->write('D5', 'Date', %heading1);
$worksheet->write('E5', 'Circuit Type', %heading1);
$worksheet->write('F5', 'Carrier', %heading1);
$worksheet->write('G5', 'Circuit #', %heading1);
$worksheet->write('H5', 'CA Ticket #', %heading1);
$worksheet->write('I5', 'Length of Ckt Outage Start Date & Time', %heading1);
$worksheet->write('J5', 'Site affected Outage Hrs./Min', %heading1);
$worksheet->write('K5', 'Length of outage in min', %heading1);
$worksheet->write('L5', 'Length of Ckt Outage End date & time', %heading1);
$worksheet->write('M5', 'Bus. Impact Mins', %heading1);
$worksheet->write('N5', 'Remarks', %heading1);
$worksheet->write('O5', 'HA', %heading1);


#__CONNECT TO DB__

my $dbh = DBI->connect('DBI:mysql:xyz.tcs.com', 'root', 'root123',
{ RaiseError => 1 }) or die $DBI::errstr;




..Continue...and print data...


 
 


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

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