简体   繁体   中英

how to read .xlsm file line by line in perl

This code reads the data from .xlsm file .this read data each cell by cell like cell(0,0) and like so on from this code i am not able to read data of whole line .I want to read .xlsm file line by line and also only of specified worksheet not all.

#!/usr/bin/env perl
use strict;
use warnings;
use Spreadsheet::Reader::ExcelXML qw( :just_the_data);

my $workbook  = Spreadsheet::Reader::ExcelXML->new( file => 'filename.xlsm' 
);

if ( !$workbook->file_opened ) {
     die $workbook->error(), "\n";
 }

for my $worksheet ( $workbook->worksheets) {

    print "Reading worksheet named: " . $worksheet->get_name . "\n";

    while( 1 ){
            my $cell = $worksheet->get_next_value;

            print "Cell is: $cell\n";
            last if $cell eq 'EOF';
     }
 }

output:

    # SYNOPSIS Screen Output
    # 01: Row, Col    = (0, 0)
    # 02: Value       = Category
    # 03: Unformatted = Category
    # 04:
    # 05: Row, Col    = (0, 1)
    # 06: Value       = Total
    # 07: Unformatted = Total
    # 08:
    # 09: Row, Col    = (0, 2)
    # 10: Value       = Date
    # 11: Unformatted = Date
    # 12:
    # 13: Row, Col    = (1, 0)
    # 14: Value       = Red
    # 16: Unformatted = Red
    # 17:
    # 18: Row, Col    = (1, 1)
    # 19: Value       = 5
    # 20: Unformatted = 5
    # 21:
    # 22: Row, Col    = (1, 2)
    # 23: Value       = 2017-2-14 #(shows as 2/14/2017 in the sheet)
    # 24: Unformatted = 41318
    # 25:
    # More intermediate rows ...
    # 82:
    # 83: Row, Col    = (6, 2)
    # 84: Value       = 2016-2-6 #(shows as 2/6/2016 in the sheet)
    # 85: Unformatted = 40944

According to the documentation, you can use the fetchrow_arrayref and fetchrow_array methods on the workbook level to get a full row.

fetchrow_arrayref( $row )

Definition: In an homage to DBI I included this function to return an array ref of the cells or values in the requested $row. If no row is requested this returns the 'next' row. In the array ref any empty cell will show as 'undef'.

Accepts: undef = next|$row = a row integer indicating the desired row See the attribute "count_from_zero" in Spreadsheet::Reader::ExcelXML to understand which row is returned for $row.

Returns: an array ref of all possible column positions in that row with data filled in per the attribute "group_return_type" in Spreadsheet::Reader::ExcelXML.

Note that this module has its own definition of what a row is, and you can change that on import.

To load a specific worksheet, use the worksheet method on the workbook object. You need to know the name of the worksheet beforehand, or you can get all of the names with get_sheet_names .

worksheet( $name )

Definition: This method will return an object to read values in the identified worksheet. If no value is passed to $name then the 'next' worksheet in physical order is returned. 'next' will NOT wrap It also only iterates through the 'worksheets' in the workbook (not the 'chartsheets').

Accepts: the $name string representing the name of the worksheet object you want to open. This name is the word visible on the tab when opening the spreadsheet in Excel. (not the underlying zip member file name - which can be different. It will not accept chart tab names.)

Returns: a Worksheet object with the ability to read the worksheet of that name. It returns undef and sets the error attribute if a 'chartsheet' is requested. Or in 'next' mode it returns undef if past the last sheet.

Example: using the implied 'next' worksheet;

Here is a quick, untested example that will open a specific worksheet, get the data line by line and dump out the results.

use strict;
use warnings;
use Spreadsheet::Reader::ExcelXML qw( :just_the_data);
use Data::Dumper;

my $workbook  = Spreadsheet::Reader::ExcelXML->new( 
    file => 'filename.xlsm' );

my $worksheet = $workbook->worksheet('foo');

while (my $row = $worksheet->fetchrow_array_ref) {
    print Dumper $row;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM