-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert_excel.pl
executable file
·55 lines (38 loc) · 1.22 KB
/
convert_excel.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use IO::File;
my $filename = shift @ARGV;
chomp $filename;
my $dir = shift @ARGV;
$dir = "." unless defined $dir;
chomp $dir;
die "File $filename not found" unless -e $filename;
die "File $filename not readable" unless -r $filename;
die "Dir not found" unless -d $dir;
die "Dir not writeable" unless -w $dir;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse($filename);
for my $worksheet ( $workbook->worksheets() ) {
my $name = $worksheet->{Name};
$name =~s/ /_/g;
my $outfile = "$dir/$name.csv";
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
next unless ($row_max>0 && $col_max>0);
my $fh = new IO::File;
$fh->open("> $outfile") or die "Can't open $outfile for writing: $!";
for my $row ( $row_min .. $row_max ) {
my @row;
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
my $value = $cell ? $cell->unformatted() : '';
$value = '"'.$value.'"' if ($cell && ($cell->{Type} eq 'Text'));
push @row, $value;
}
print $fh join ",", @row;
print $fh "\n";
}
$fh->close;
}