Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Perlscript in ASP to create excel file

Status
Not open for further replies.

chooper123

Programmer
Sep 25, 2012
2
GB


Hi All, I am struggling with opening an excel application and writing data to it from perlscript in a .asp webpage. I have test example .pl file which does the job nicely...opening up excel and the file. See below code:

Code:
#!/usr/bin/perl -w ###################################################################### +######### # # Example of how to use the Spreadsheet::WriteExcel module to send an +Excel # file to a browser in a CGI program. # # On Windows the hash-bang line should be something like: # # #!C:\Perl\bin\perl.exe # # The "Content-Disposition" line will cause a prompt to be generated t +o save # the file. If you want to stream the file to the browser instead, com +ment out # that line as shown below. # # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org # use strict; use Spreadsheet::WriteExcel; # Set the filename and send the content type my $filename ="cgitest.xls"; print "Content-type: application/vnd.ms-excel\n"; # The Content-Disposition will generate a prompt to save the file. If +you want # to stream the file to the browser, comment out the following line. print "Content-Disposition: attachment; filename=$filename\n"; print "\n"; # Create a new workbook and add a worksheet. The special Perl filehand +le - will # redirect the output to STDOUT # my $workbook = Spreadsheet::WriteExcel->new("-"); my $worksheet = $workbook->add_worksheet(); # Set the column width for column 1 $worksheet->set_column(0, 0, 20); # Create a format my $format = $workbook->add_format(); $format->set_bold(); $format->set_size(15); $format->set_color('blue'); # Write to the workbook $worksheet->write(0, 0, "Hi Excel!", $format); __END__

This next bit is where I come unstuck. When I put this code in a .asp file with <%@ Language=PerlScript %> tags in it then it doesn't work. So the code in .pl file opens Excel fine but with the code in the .asp file nothing happens. This is what I tried to do:
Code:
<%@ Language=PerlScript %> <% use strict; use Spreadsheet::WriteExcel; # Set the filename and send the content type my $filename ="cgitest.xls"; print "Content-type: application/vnd.ms-excel\n"; # The Content-Disposition will generate a prompt to save the file. If +you want # to stream the file to the browser, comment out the following line. print "Content-Disposition: attachment; filename=$filename\n"; print "\n"; # Create a new workbook and add a worksheet. The special Perl filehand +le - will # redirect the output to STDOUT # my $workbook = Spreadsheet::WriteExcel->new("-"); my $worksheet = $workbook->add_worksheet(); # Set the column width for column 1 $worksheet->set_column(0, 0, 20); # Create a format my $format = $workbook->add_format(); $format->set_bold(); $format->set_size(15); $format->set_color('blue'); # Write to the workbook $worksheet->write(0, 0, "Hi Excel!", $format); __END__ %>

Probably, needless to say I am a novice. I am stumped on this and would value some guidance. Kind regards, Catherine
 
Is it really all on one line like that, or did your post get munged. If it's the former... I'd fix that first!

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Sorry, no it isn't all on one line. I'll put the codes again.

This is the working code in .pl file

Code:
#!/usr/bin/perl -w

###############################################################################
#
# Example of how to use the Spreadsheet::WriteExcel module to send an Excel
# file to a browser in a CGI program.
#
# On Windows the hash-bang line should be something like:
#
#     #!C:\Perl\bin\perl.exe
#
# The "Content-Disposition" line will cause a prompt to be generated to save
# the file. If you want to stream the file to the browser instead, comment out
# that line as shown below.
#
# reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
#

use strict;
use Spreadsheet::WriteExcel;

# Set the filename and send the content type
my $filename ="cgitest.xls";

print "Content-type: application/vnd.ms-excel\n";
# The Content-Disposition will generate a prompt to save the file. If you want
# to stream the file to the browser, comment out the following line.
print "Content-Disposition: attachment; filename=$filename\n";
print "\n";

# Create a new workbook and add a worksheet. The special Perl filehandle - will
# redirect the output to STDOUT
#
my $workbook  = Spreadsheet::WriteExcel->new("-");
my $worksheet = $workbook->add_worksheet();


# Set the column width for column 1
$worksheet->set_column(0, 0, 20);


# Create a format
my $format = $workbook->add_format();
$format->set_bold();
$format->set_size(15);
$format->set_color('blue');


# Write to the workbook
$worksheet->write(0, 0, "Hi Excel!", $format);

__END__

and this is the code when I put it in a .asp webpage with with <%@ Language=PerlScript %> tags. And it doesn't work.

Code:
<%@ Language=PerlScript %>

<%
###############################################################################
#
# Example of how to use the Spreadsheet::WriteExcel module to send an Excel
# file to a browser in a CGI program.
#
# On Windows the hash-bang line should be something like:
#
#     #!C:\Perl\bin\perl.exe
#
# The "Content-Disposition" line will cause a prompt to be generated to save
# the file. If you want to stream the file to the browser instead, comment out
# that line as shown below.
#
# reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
#

use strict;
use Spreadsheet::WriteExcel;

# Set the filename and send the content type
my $filename ="cgitest.xls";

print "Content-type: application/vnd.ms-excel\n";
# The Content-Disposition will generate a prompt to save the file. If you want
# to stream the file to the browser, comment out the following line.
print "Content-Disposition: attachment; filename=$filename\n";
print "\n";

# Create a new workbook and add a worksheet. The special Perl filehandle - will
# redirect the output to STDOUT
#
my $workbook  = Spreadsheet::WriteExcel->new("-");
my $worksheet = $workbook->add_worksheet();


# Set the column width for column 1
$worksheet->set_column(0, 0, 20);


# Create a format
my $format = $workbook->add_format();
$format->set_bold();
$format->set_size(15);
$format->set_color('blue');


# Write to the workbook
$worksheet->write(0, 0, "Hi Excel!", $format);

%>

Thanks for any help. It's not working in the .asp page. Some ideas of where I am going wrong would be great...
Cath
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top