8ddc19fbf149d94f496a6f28f6aea6dbb9e682c5
[migration-tools.git] / xls2tab
1 #!/usr/bin/perl
2
3 #~ Converter Script for XLS to TSV.  Handles Multiple Tabs into separate files.
4 #~ (c)2004 Anima Legato <l3gatosan@gmail.com>
5 #~
6 #~ This code is redistributable and modifiable under the same terms as Perl
7 #~ itself.
8
9 use strict;
10 use warnings;
11
12 use Spreadsheet::ParseExcel::Simple;
13 use File::Spec;
14
15 for (@ARGV) {
16     for (glob $_) {
17         next unless m/\.xls$/i;
18         next unless -r $_;
19         dump_books($_);
20     }
21 }
22
23 sub dump_books {
24     my ($vol, $path, $file) = File::Spec->splitpath(shift);
25     my $eBook = Spreadsheet::ParseExcel::Simple->read(File::Spec->catpath($vol,$path,$file));
26     unless (defined $eBook) {
27         warn "Can't open Spreadsheet in file $file (@".File::Spec->catpath($vol,$path,$file)."\n";
28         return undef;
29     }
30     
31     my @sheet = $eBook->sheets;
32     for (0..@sheet-1) {
33         next unless $sheet[$_]->has_data();
34         my $sfn = $file;
35         $sfn =~ s?\.xls$??i;
36         $sfn.= ((@sheet > 1) ? sprintf(".%02i",$_) : "").'.tab';
37         open TAB, '>', $sfn or do {
38             warn "Unable to write to $sfn";
39             next;
40         };
41         
42         while ($sheet[$_]->has_data) { 
43             my @row = $sheet[$_]->next_row;
44             print TAB join("\t",@row)."\n";
45         }
46     }
47 } ##--dump_books--##