Advertisement
Guest User

XLS->CSV sketch (Evgenii Lepikhin)

a guest
Mar 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.69 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use Spreadsheet::XLSX;
  4. use Text::CSV::Slurp;
  5.  
  6. my $excel = Spreadsheet::XLSX -> new ($ARGV[0]);
  7.  
  8. my @rows;
  9. foreach my $sheet (@{$excel -> {Worksheet}}) {
  10.    $sheet -> {MaxRow} ||= $sheet -> {MinRow};
  11.    foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {
  12.      $sheet -> {MaxCol} ||= $sheet -> {MinCol};
  13.      my %cells = ();
  14.      foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {
  15.          my $cell = $sheet -> {Cells} [$row] [$col];
  16.          if ($cell) {
  17.             $cells{"column $col"} = $cell->{Val};
  18.          }
  19.       }
  20.      push @rows, \%cells;
  21.   }
  22. }
  23.  
  24. my $csv = Text::CSV::Slurp->new();
  25. my $csv = $csv->create(input => \@rows);
  26. print $csv;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement