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

Hi, How can I find no of total rows and columns in a Excel Sheet

Replies are listed 'Best First'.
Re: Win32::OLE excel
by Mr. Muskrat (Canon) on May 30, 2003 at 16:05 UTC

      Indeed good link Muskrat, the contents of this node give the exact answer.

      -Tats

      Update: Change link so it doesn't log people out! Thanks Enlil

      Update2: Made first update actually work. :)

Re: Win32::OLE excel
by jsprat (Curate) on May 30, 2003 at 17:48 UTC
    I'm not 100% sure what you're asking. It sounds like you want the size of the UsedRange? If so, have a look at the Count property of the Rows and Columns collections of the UsedRange object (How's that for an awkward sentence).

    It should look something like this:

    use strict; use Win32::OLE; my $ole = Win32::OLE->new('Excel.Application', 'Quit') or die "$!"; $ole->{Visible} = 1; my $xls = $ole->{Workbooks}->Open('mr.xls'); my $sheet = $xls->{Sheets}->{Template}; print $sheet->{UsedRange}->{Rows}->{Count}, " rows, ", $sheet->{UsedRange}->{Columns}->{Count}, " columns used."; __END__ Output: C:\>perl xls.pl 40 rows, 11 columns used.

Re: Win32::OLE excel
by vek (Prior) on May 30, 2003 at 21:37 UTC
    If you're up for an alternative to Win32::OLE, you could use something like Spreadsheet::ParseExcel::Simple:
    use Spreadsheet::ParseExcel::Simple; use strict; my $file = shift; my $rows = 0; my $cols = 0; my $xls = Spreadsheet::ParseExcel::Simple->read($file); my @sheets = $xls->sheets; while ($sheets[0]->has_data) { $rows++; my @data = $sheets[0]->next_row; if (@data > $cols) { $cols = @data; } } print "Rows: " . $rows, "\n"; print "Cols: " . $cols, "\n";
    (untested)

    -- vek --
Re: Win32::OLE excel
by Itatsumaki (Friar) on May 30, 2003 at 16:07 UTC

    I think you'll need to be more precise about the structure of the spreadsheet. Are all the rows/columns containing data contiguous? In general you can have some 64k and 256 rows in an excel spreadsheet, max.

    -Tats