#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use List::Compare; use Spreadsheet::ParseExcel; my @array = (); sub check { foreach my $arg (@_) { # Check each $ARGV input for xls and csv extensions my $suffix = ( split /\./, $arg )[-1]; if ($suffix =~ /xls$/ || $suffix =~ /csv$/) { # For demonstration purposes # print "I found it ".$_."!\n"; next; } elsif ($suffix !~ /xls$/ || $suffix !~ /csv$/) { print "\nPlease enter two type inputs '*.xls' to read from and '*csv' to write at.\n"; print "Any other file format (e.g. '*.txt') is not acceptable.\n\n"; exit(); } } # End of foreach(@ARGV) return(@_); } my @arguments = check(@ARGV); sub extra { foreach my $file (@_) { # Check each $ARGV input for the csv extension my $suffix = ( split /\./, $file )[-1]; if ($suffix =~ /csv$/) { # For demonstration purposes # print "I found it: ".$file."!\n"; my @result = sub_write($file); return @result; } my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse($file); if ( !defined $workbook ) { die $parser->error(), ".\n"; } foreach my $worksheet ( $workbook->worksheets() ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my ($final,$value); foreach my $row ( $row_min .. $row_max ) { foreach my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; $final .= "," . $cell->value(); =test # Based on the CPAN tutorial output: Spreadsheet::ParseExcel print "This is the \$final: ".$final."\n"; print "Row, Col = ($row, $col)\n"; print "Value = ", $cell->value(), "\n"; print "Unformatted = ", $cell->unformatted(), "\n"; print "\n"; =cut } # remove the first character of the string ($final) if (defined $final) { substr($final, 0, 1) = ""; # remove all the not necessary following characters ($final) chop($final); # print "This is the \$final before: ".$final."\n"; push(@array,$final); $final = (); } else { next; } } } # end of Worksheets } # End of foreach (@_) return 0; } # End of sub (extra) sub sub_write { my $csv = shift; open(my $write , ">" , $csv) or die "Can not open ".$csv.": $!\n"; # > write mode foreach my $line (@array) { print $write $line . "\n"; } close ($write) or die ("Could not close: ".$csv." - $!\n"); return (@array,$csv); } my @output = extra(@arguments); print Dumper(\@output); #### Item_1,Cost_1,Sold_1,Profit_1 Keyboard_1,$10.00,$16.00,$6.00 Monitor_1,$80.00,$120.00,$40.00 Mouse_1,$5.00,$7.00,$2.00 ,,Total_1,$48.00 #### Item_1,Cost_1,Sold_1,Profit_1 Keyboard_1,$10.00,$16.00,$6.00 Monitor_1,$80.00,$120.00,$40.00 Mouse_1,$5.00,$7.00,$2.00 ,,Total_1,$48.00 #### Item_2,Cost_2,Sold_2,Profit_2 Keyboard_2,$10.00,$16.00,$6.00 Monitor_2,$80.00,$120.00,$40.00 Mouse_2,$5.00,$7.00,$2.00 ,,Total_2,$48.00 Item_3,Cost_3,Sold_3,Profit_3 Keyboard_3,$10.00,$16.00,$6.00 Monitor_3,$80.00,$120.00,$40.00 Mouse_3,$5.00,$7.00,$2.00 ,,Total_3,$48.00 #### Item_1,Cost_1,Sold_1,Profit_1 Keyboard_1,$10.00,$16.00,$6.00 Monitor_1,$80.00,$120.00,$40.00 Mouse_1,$5.00,$7.00,$2.00 ,,Total_1,$48.00 Item_2,Cost_2,Sold_2,Profit_2 Keyboard_2,$10.00,$16.00,$6.00 Monitor_2,$80.00,$120.00,$40.00 Mouse_2,$5.00,$7.00,$2.00 ,,Total_2,$48.00 Item_3,Cost_3,Sold_3,Profit_3 Keyboard_3,$10.00,$16.00,$6.00 Monitor_3,$80.00,$120.00,$40.00 Mouse_3,$5.00,$7.00,$2.00 ,,Total_3,$48.00