Hello Monks I am working on developing a perl API where I have designed the API script to extract a data from DB depending on the i/p given at the front end.Now I want to generate an excel file depending on the i/p that I will give in browser window.Below is the program which works fine and generates excel sheet if I entered the i/p from console window but gives an error like "Can't call method "add_worksheet" on an undefined value at /var/www/cgi-bin/excel_report.cgi line 57." If I try to give i/p through web browser.The corresponding code is as follows:

#!/usr/bin/perl -w use CGI ':standard'; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use DateTime::Format::ISO8601 qw( ); use strict; use Spreadsheet::WriteExcel; use lib "/view/sawans1_insert_data/vobs/pp/pptai/api/NightlyDB"; use API; my @test_run_id; my @total_passed; my @date; my $option_type; my $user_input; my $test_run_id; my $test_run_list_ref; my $sdt = DateTime::Format::ISO8601->parse_datetime(param('start_date' +)); my $edt = DateTime::Format::ISO8601->parse_datetime(param('end_date')) +; for (my $dt = $sdt->clone(); $dt<=$edt; $dt->add( days => 1 )){ push @date,$dt->ymd(); } $user_input->{pp_branch} = param('pp_branch'); # Create a new Excel workbook my $file = "test_run.xls"; my $workbook = Spreadsheet::WriteExcel->new($file); # Add a worksheet my $worksheet = $workbook->add_worksheet(); # Add and define a format my $format = $workbook->add_format(); # Add a format $format->set_bold(); $format->set_color('red'); $format->set_align('center'); # Write a formatted and unformatted string, row and column notatio +n. my $col = my $row1 = 0; $worksheet->write(0, 0, 'Date', $format); $worksheet->write(0, 1, 'Nightly Web ID', $format); $worksheet->write(0, 2, 'Number Of Tests Passed', $format); my $row = 1; my $i = 0; foreach (@date){ #create hash reference variable for every date in array @date $user_input->{start_date} = $_; $user_input->{end_date} = $_; #Call get_test_run function to create test run list refer +ences $test_run_list_ref = &API::get_test_run($user_input); #For each such reference calculate # of objects by addition foreach my $tr_obj (@$test_run_list_ref){ push @total_passed, $tr_obj->number_of_passed(); $tr_obj->nightly_web_id(); $worksheet->write($row,0, $_); $worksheet->write($row,1,$test_run_id[$i]); $worksheet->write($row,2,$total_passed[$i]); $row++; $i++; } } print header( -attachment=>'test_run.xls', );

Kindly Let me know if anybody found out where I am mistaking !!!!


In reply to @ generating Excel Attachment using Perl by siddheshsawant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.