Sunday, November 12, 2017

Read Excel files in Perl

1. install the module for Excel xls files.
$ cpan install Spreadsheet::ParseExcel


2. install the module for Excel xlsx/xlsm files.
cpan install Archive::Zip
cpan install Spreadsheet::ParseXLSX
* on OpenSUSE, because of the bug in libzip, it is needed to do make install in the build folder.

3. try to parse the xls, xlsx, xlsm files.

# TestXLS.pl

#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;

my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('TestPerl.xls');

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

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

    my ( $row_min, $row_max ) = $worksheet->row_range();
    my ( $col_min, $col_max ) = $worksheet->col_range();
    my ( $worksheet_name) = $worksheet->get_name();
    print "Worksheet is $worksheet_name which has MinRow($row_min), MaxRow($row_max), MinCol($col_min), MaxCol($col_max)\n";

    for my $row ( $row_min .. $row_max ) { 
        for my $col ( $col_min .. $col_max ) { 

            my $cell = $worksheet->get_cell( $row, $col );
            next unless $cell;

            print "Row, Col    = ($row, $col)\n";
            print "Value       = ", $cell->value(),       "\n";
            print "Unformatted = ", $cell->unformatted(), "\n";
            print "\n";
        }   
    }   
}

# TestXLSX.pl

#!/usr/bin/perl -w

use Text::Iconv;
use Spreadsheet::XLSX;

my $converter = Text::Iconv -> new ("utf-8", "windows-1251");

# Text::Iconv is not really required.
# This can be any object with the convert method. Or nothing.

my $excel = Spreadsheet::XLSX -> new ('TestPerl.xlsx', $converter);

foreach my $sheet (@{$excel -> {Worksheet}}) {
    printf("Sheet: %s\n", $sheet->{Name});
    $sheet -> {MaxRow} ||= $sheet -> {MinRow};
    foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {
        $sheet -> {MaxCol} ||= $sheet -> {MinCol};
        foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {
            my $cell = $sheet -> {Cells} [$row] [$col];
            if ($cell) {
                printf("( %s , %s ) => %s\n", $row, $col, $cell -> {Val});
            }   
        }   
    }   
}

# TestXLSM.pl

#!/usr/bin/perl -w

use Text::Iconv;
use Spreadsheet::XLSX;

my $converter = Text::Iconv -> new ("utf-8", "windows-1251");

# Text::Iconv is not really required.
# This can be any object with the convert method. Or nothing.

my $excel = Spreadsheet::XLSX -> new ('TestPerl.xlsm', $converter);

foreach my $sheet (@{$excel -> {Worksheet}}) {
    printf("Sheet: %s\n", $sheet->{Name});
    $sheet -> {MaxRow} ||= $sheet -> {MinRow};
    foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {
        $sheet -> {MaxCol} ||= $sheet -> {MinCol};
        foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {
            my $cell = $sheet -> {Cells} [$row] [$col];
            if ($cell) {
                printf("( %s , %s ) => %s\n", $row, $col, $cell -> {Val});
            }   
        }   
    }   
}





No comments:

Post a Comment