Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

A simple read of XLSX into Perl

by gadi99 (Initiate)
on Feb 26, 2013 at 21:03 UTC ( [id://1020756]=perlquestion: print w/replies, xml ) Need Help??

gadi99 has asked for the wisdom of the Perl Monks concerning the following question:

New to Perl (== please bear with me). All I need is a code to read a 1-sheet Excel-2010 file. I know how many columns there are (3 of them) but don't know how many rows. Perl 5.14 on Win-XP. My .PL and XLSX are on the same folder. So I tried :

use strict; use warnings; use OLE; use Win32::OLE::Const "Microsoft Excel"; my $MY_EXCEL="E:\\readexcel.xlsx"; my $excel = CreateObject OLE "Excel.Application"; my $workbook = $excel -> Workbooks -> Open("$MY_EXCEL"); my $Sheet -> Range ("A1") -> {Value}; print "S H E E T : $Sheet\n"; $excel-> Quit;

But Perl called and said:

Useless use of hash element in void context at E:\readexcel.pl line 8. Can't call method "Range" on an undefined value at E:\readexcel.pl lin +e 8.

I searched and searched, in the Monastery and in the Great G, but to no avail. Pls see if you can help here. Many thanks.

I have just replied to "daskar" the following: Oh my goodness! I T - W O R K S ! ! ! ! My Perl is reading the XLSX. I'm going to convert to Monkeism right now. Thank you all. I'm going to read all the docs per your advise. Hooray for the monks! My code now is :
use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open("E:\\readexcel.xlsx"); my $Sheet = $Book->Worksheets(1); my $array = $Sheet->Range("A1:C3")->{'Value'}; $Book->Close; foreach my $ref_array (@$array) { foreach my $scalar (@$ref_array) { print "$scalar\t"; } print "\n"; }

Replies are listed 'Best First'.
Re: A simple read of XLSX into Perl
by McA (Priest) on Feb 26, 2013 at 21:13 UTC

    Hi,

    the line which causes trouble is:

    my $Sheet -> Range ("A1") -> {Value};

    That is NOT an assignment. I'm sure you forget to assign to the newly declared variable $Sheet.

    EDIT: Have you looked at Read XLSX Files ?

    Best regards
    McA

      UPDATE:

      I tried something different :

      use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32: +:OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open("E:\\readexcel.xlsx"); my $Sheet = $Book->Worksheets(1); my $array = $Sheet->Range("A0:C2")->{'Value'}; $Book->Close; Foreach my $ref_array (@$array) { foreach my $scalar (@$ref_array) { print "$scalar\t;" } print "\n;" }

      This time I got :

      E:\>perl E:\readexcel.pl Win32::OLE(0.1709) error 0x800a03ec in METHOD/PROPERTYGET "Range" at E:\readexcel.pl line 9

      Is there something wrong with my OLE…?

        A couple of comments. First, the post from the Anonymous Monk is correct about the column numbering. In the OLE environment, Excel's columns and rows are 1 based and 0 based. In other words, the upper left most corner cell is row 1, column 1 or A1, depending on which cell reference scheme that you're using.

        Secondly, I personally would discourage the practice of writing your code such that it takes over an active instance of Excel. For more info, check out the posts from me and davies in "use Win32::OLE::Const" not having an effect and Perl r/w Excel with OLE. (If you believe that davies and I contradict each other in those threads, I'd recommend going with what davies says since he's far more knowledgeable with both Excel and OLE.)

        Hi,

        I think it may be the "A0" that is causing the problem this time.

        Try "A1" instead.

        J.C.

Re: A simple read of XLSX into Perl
by ig (Vicar) on Feb 27, 2013 at 09:08 UTC

    I might try Spreadsheet::XLSX. The synopsis has an example of how to open a workbook and read all the cells in all the worksheets.

        Also note that it is (very) likely that the XLSX format will get a new parser from the author of Spreadsheet::ParseExcel.

        Any interested parties can try out (and contribute to) the code for the new XLSX parsing library which is still in a sort of alpha state, but does 'work'.

Re: A simple read of XLSX into Perl
by Anonymous Monk on Jun 27, 2013 at 10:30 UTC

    Thank you very much dude, I have been searching for this past 5 days.

    Im very new to Perl,

    I'm requesting you to please share the script for reading .txt file into perl


    Thanks in advance Dude.....
    Regards,
    pavan.
    vanguripavan@gmail.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1020756]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found