Read an excel file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dillipkumar
    New Member
    • Mar 2008
    • 41

    #1

    Read an excel file

    Hi,

    Can anybody tell me how to read an excel file.

    I have written a script, it is not working with multi sheets. And also i want to print all workshet present in the excel file.


    use Spreadsheet::Pa rseExcel::Simpl e;
    use strict;

    my $xls = Spreadsheet::Pa rseExcel::Simpl e->read('file.xls ');
    foreach my $sheet($xls->sheets)
    {
    while ($sheet->has_data)
    {
    my @data = $sheet->next_row;
    print "@data\n";
    }
    }

    ~Thanks.
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    What error do you get?
    The same script worked for me in reading and printing data from multiple sheets in an excel file.

    Comment

    • dillipkumar
      New Member
      • Mar 2008
      • 41

      #3
      Hi Nithin,

      It is showing some error:

      Can't call method "sheets" on an undefined value at read_excel.pl line 10.

      ~Thanks

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by dillipkumar
        Hi Nithin,

        It is showing some error:

        Can't call method "sheets" on an undefined value at read_excel.pl line 10.

        ~Thanks
        That error means that it could not fetch the excel sheet. Are you sure it is in the same directory in which you are running the script?
        If not, give the absolute path in filename.

        Comment

        • dillipkumar
          New Member
          • Mar 2008
          • 41

          #5
          Hi,

          The script can't open that particular xls file.

          i have also given the absolute path for that file & same error is showing.


          ~Thanks

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            Then, it is not the problem with the script. Make sure the user running the script has read permissions for the file.

            Comment

            • dillipkumar
              New Member
              • Mar 2008
              • 41

              #7
              Hi,

              Now it is working fine...

              It was not working because of my OS, after restarted it is working fine.

              Can u please tell me how i knoe how many workseets is present in my excel file.


              ~Thanks

              Comment

              • nithinpes
                Recognized Expert Contributor
                • Dec 2007
                • 410

                #8
                That is quite straight-forward. As you can see in your script, $xls->sheets returns an array of sheets in the excel file. The length of this array would be the number of sheets present.
                Code:
                my $sheets = $xls->sheets;
                print "There are $sheets sheets";

                Comment

                Working...