We help IT Professionals succeed at work.
Get Started

How do I solve this Perl Spreadsheet Parseexcel problem

630 Views
Last Modified: 2013-12-26
Hi all,
 
For the attached excel sheet, I am going through all the columns and printing out the contents into an array and then to a file. But in my excel sheet, the data rows are arranged like this
 
File1.txt       /ms/user/s/sayantag/        1       Header1       Header2       Header3       Header4       Header5
File1.txt       /ms/user/s/sayantag/        1       Data1       Data2       Data3       Data4       Data5
File1.txt       /ms/user/s/sayantag/        1       Data6       Data7       Data8       Data9       Data10
File1.txt       /ms/user/s/sayantag/        1       Data11       Data12       Data13       Data14       Data15
 
In the output array and hence into the file, the data gets populated one after another like Data1Data2Data3Data4Data5Data6Data7Data8Data9Data10Data11Data12Data13Data14Data15.
 
Where am I going wrong? Should I put the contents of an entire row of data into another array and then print it out into the file?

Pls help asap with the code

The code can be run with--

perl Process_MF.pl 1 3 5 1 4 2 2
 
#!/usr/bin/perl5.10 -w
 

 
use warnings;
use Spreadsheet::ParseExcel;
use strict;
use IO::Scalar;
use Data::Dumper;
 
my @etl_val = @ARGV;
 
print "@etl_val" . "\n" ;
 
my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse("/ms/user/s/sayantag/Demo_MF2.xls");
my $worksheet;
my %order_of_flat_file;
my @data_arr;
my $data;
my $etl;
my $path;
my @fin_arr;
 
for  $worksheet ( $workbook->worksheets() ) {
   my ( $row_min, $row_max ) = $worksheet->row_range();
   my ( $col_min, $col_max ) = $worksheet->col_range();
 
   my $row = $row_min + 2;
   while( $row <=$row_max ) {
        print "Row before if statement is $row\n";   
     if ($worksheet->get_cell( $row, 2 )->value ne "ETL") {
        print "Row after if statement is $row\n";
               
     my $etl_cell  = $worksheet->get_cell( $row, 2 );
        if(defined($etl_cell)) {
        $etl = $etl_cell->value;}
 
     my $path_cell = $worksheet->get_cell( $row, 1 );
        if(defined($path_cell)) {
        $path = $path_cell->value;
        $path =~ s/\s$//;
        print "PATH: $path" . "\n";}
 
     my $path_cell0 = $worksheet->get_cell( $row, 0 );
        if(defined($path_cell0)) {
        $path.= $path_cell0->value;
        print $path_cell0->value . "\n";
        print "PATH: $path" . "\n";
        print "ETL: $etl" . "\n";
        print "PATH: $path" . "\n";
       
        }
 
     for my $col ( $col_min+3 .. $col_max ) {
        print "ROW : $row" ."\n";
        print "COLUMN: $col" . "\n";
               
        my $cell = $worksheet->get_cell( $row, $col );
        if (defined($cell) ) {
          my $cell_val = $cell->value;
          print "Cell value is $cell_val\n";
          push(@data_arr, split("\t", $cell_val));
#          @data_arr = join ("\n", @data_arr);
#          print "The first array element is $data_arr[0]\n";
          print "Content of '", $path, "' will be:\n", @data_arr, "\n";
#          foreach my $val(@data_arr) {$val=~ s/\t{1,}/\|/g;}
         } 
        
         }
        
        $row++;    
       }
        else { print "The array looks like @data_arr\n";
              open("FILEHANDLE", '>', $path) or die "Cannot open the file ", $path, ": $!";
              print FILEHANDLE @data_arr ;
              close ("FILEHANDLE");
              @data_arr = ();
              $row = $row+2;
              print "Value of row before calling next is $row\n";
              next;
             
            }
     }
   }
 
#print "The array is @data_arr" . "\n";

Open in new window

Demo-MF2.xls
Comment
Watch Question
Programmer
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE