Tuesday, December 14, 2010

(Perl Programming) How to read data from excel spreadsheet using Spreadsheet::ParseExcel module?

The Spreadsheet::ParseExcel Perl module can be used to read information from Excel 95-2003 binary files.

You can download Spreadsheet::ParseExcel module from CPAN repository (http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel-0.58/lib/Spreadsheet/ParseExcel.pm) or if you are using Active Perl just run “ppm install Spreadsheet-ParseExcel” command.

In previous program we have created Sales.xls file.  Below mentioned program is fetching (reading) the data from Sales.xls file and then storing it into sales table.

 #!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Spreadsheet::ParseExcel;

#create connection with data base, Connect function requires database name(test), user name(root) and #password(xyz).
# if connection is not established die option display an error message (standard Error Message) and your script #will die

my $DBH = DBI->connect('DBI:mysql:test', 'root', 'xyz') or die "Connection Error: $DBI::errstr\n";

#The new() method is used to create a new Spreadsheet::ParseExcel parser object.
my $parser   = Spreadsheet::ParseExcel->new();
#The Parser parse() method returns a "Workbook" object.
my $workbook = $parser->Parse("Sales.xls");

#The Parser error() method returns an error string if a parse() fails
#    error()                              error_code()
#   =======                         ============
#   ''                                          0
#  'File not found'                       1
#   'No Excel data found in file'    2
#   'File is encrypted'                  3

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

#The worksheet() method returns a single Worksheet object using either its name or index
my $worksheet = $workbook->worksheet(0); # or sheet1

#Returns a two-element list ($min, $max) containing the minimum and maximum defined rows in the worksheet.
my ( $row_min, $row_max ) = $worksheet->row_range();

#Returns a two-element list ($min, $max) containing the minimum and maximum of defined columns in the #worksheet.
my ( $col_min, $col_max ) = $worksheet->col_range();

for my $row ( $row_min .. $row_max ) {
   
     my $i=0;
     my @Temp_Array=();
          
     for my $col ( $col_min .. $col_max ) {
  
    #Return the "Cell" object at row $row and column $col if it is defined.
    #Otherwise returns undef.
   
     my $cell = $worksheet->get_cell( $row, $col );
      
        #if not defined next unless $cell (defined cell);  
        if (! defined ($cell) ) {   
            $Temp_Array[$i++]=" ";
            next;
        }
  
        #The value() method returns the formatted value of the cell.
         $Temp_Array[$i++]= $cell->value();
     
     } # column for loop ends here
       
     #avoid first row of excel sheet (avoid Title row)
     if ($Temp_Array[0] ne "Year" ) {
    
     #Insert fetched data from cell into Sales table
     my $Query="insert into sales (sale_year, sale_amount ) values  ($Temp_Array[0], $Temp_Array[1])";
     $DBH->do($Query);
          
     }
     
 } # row for loop ends here
 
  $DBH->disconnect; #disconnect Database connection

#===========Output Sales Table===============#

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.