Advertisement
kijato

xls2csv.pl

Nov 10th, 2015
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.23 KB | None | 0 0
  1. #!C:\programs\perl\perl.exe
  2. #!C:\perl\perl.exe
  3. #!/usr/bin/perl -w
  4.  
  5. use strict;
  6. use warnings;
  7. use Spreadsheet::ParseExcel;
  8. #use encoding "LATIN2";
  9. #use encoding 'utf8';
  10. #$|=0;
  11.  
  12. my $fajl=$ARGV[0] || die "$!\n";
  13. my $formated=$ARGV[1] || 1;
  14. my $separator=$ARGV[2] || ';';
  15.  
  16. my $parser   = Spreadsheet::ParseExcel->new();
  17. my $workbook = $parser->parse($fajl);
  18. if ( !defined $workbook ) { die $parser->error(), ".\n"; }
  19.  
  20. for my $worksheet ( $workbook->worksheets() ) {
  21.   my ( $row_min, $row_max ) = $worksheet->row_range();
  22.   my ( $col_min, $col_max ) = $worksheet->col_range();
  23.   #print "Sheet name: ".$worksheet->{Name}."\n";
  24.   for my $row ( $row_min .. $row_max ) {
  25.     print $worksheet->{Name}." [",$row+1,"];";
  26.     for my $col ( $col_min .. $col_max ) {
  27.       my $cell = $worksheet->get_cell( $row, $col );
  28.       #next unless $cell;
  29.       unless ($cell) { print $separator; next; }
  30.       #print "Row, Col    = ($row, $col)\t";
  31.       if ($formated) {
  32.         print $cell->value(); # The value() method returns the formatted value of the cell.
  33.       } else {
  34.         print $cell->unformatted(); # The unformatted() method returns the unformatted value of the cell.
  35.       }
  36.       print $separator;
  37.     }
  38.     print "\n";
  39.   }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement