Rev | Author | # | Line |
---|---|---|---|
7 | AristotlePagaltzis | 1 | This [Perl] script converts all the worksheets in a MicrosoftExcel SpreadSheet to individual [CSV] files. |
2 | |||
3 | The following modules are required: | ||
4 | |||
5 | * [Spreadsheet::ParseExcel | http://search.cpan.org/dist/Spreadsheet-ParseExcel-Simple/] | ||
6 | * [Getopt::Long | http://search.cpan.org/dist/Getopt-Long/] (already included with any standard [Perl] installation) | ||
8 | JohnMcPherson | 7 | |
7 | AristotlePagaltzis | 8 | |
9 | ---- | ||
10 | |||
11 | <verbatim> | ||
12 | #!/usr/bin/perl | ||
13 | |||
14 | =head1 NAME | ||
15 | |||
16 | excel2csv -- dump all worksheets from an Excel file to CSV files | ||
17 | |||
18 | =head1 SYNOPSIS | ||
19 | |||
20 | F<excel2csv> | ||
21 | S<B<-h>> | ||
22 | |||
23 | F<excel2csv> | ||
24 | S<B<--man>> | ||
25 | |||
26 | F<rename> | ||
27 | S<B<[ -s ]>> | ||
28 | S<B<[ -S ]>> | ||
29 | S<B<[ -v ]>> | ||
30 | S<B<file.xls [ F<file2.xls> F<file3.xls> ... ]>> | ||
31 | |||
32 | =head1 DESCRIPTION | ||
33 | |||
34 | C<excel2csv> takes any number of Excel files on the command line, reads them, and dumps each worksheet therein to a separate CSV file, named after the worksheet. | ||
35 | |||
36 | =head1 ARGUMENTS | ||
37 | |||
38 | =over 4 | ||
39 | |||
40 | =item B<-h>, B<--help> | ||
41 | |||
42 | See a synopsis. | ||
43 | |||
44 | =item B<--man> | ||
45 | |||
46 | Browse the manpage. | ||
47 | |||
48 | =back | ||
49 | |||
50 | =head1 OPTIONS | ||
51 | |||
52 | =over 4 | ||
53 | |||
54 | =item B<-s>, B<--suffix> | ||
55 | |||
56 | The suffix for the CSV files generated. Default is C<.csv>. | ||
57 | |||
58 | =item B<-S>, B<--separator> | ||
59 | |||
60 | The field separator used in the generated CSV files. Default is C<;>. | ||
61 | |||
62 | =item B<-v>, B<--verbose> | ||
63 | |||
64 | Print additional information about the operations (not) executed. | ||
65 | |||
66 | =back | ||
67 | |||
68 | =head1 BUGS | ||
69 | |||
70 | CSV generation is fugded -- no quoting issues are taken into consideration. CSV generation should rely on one of the modules available for the purpose instead. | ||
71 | |||
72 | =head1 AUTHORS | ||
73 | |||
74 | Drew Broadley, with contributions from Aristotle Pagaltzis | ||
75 | |||
76 | =head1 COPYRIGHT | ||
77 | |||
78 | FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME | ||
79 | |||
80 | Drew Broadley has not clarified the copyright on this code. | ||
81 | |||
82 | FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME | ||
83 | |||
84 | =cut | ||
85 | |||
86 | use strict; | ||
87 | use warnings; | ||
88 | |||
89 | use Spreadsheet::ParseExcel; | ||
90 | use Getopt::Long 2.24, qw(:config bundling no_ignore_case no_auto_abbrev); | ||
91 | use Pod::Usage; | ||
92 | |||
93 | Getopt::Long::GetOptions( | ||
94 | 'h|help' => sub { pod2usage( -verbose => 1 ) }, | ||
95 | 'man' => sub { pod2usage( -verbose => 2 ) }, | ||
96 | 's|suffix=s' => \(my $opt_suffix = '.csv'), | ||
97 | 'S|sep=s' => \(my $opt_separator = ';'), | ||
98 | 'v|verbose' => \(my $opt_verbose), | ||
99 | ) or pod2usage(); | ||
100 | |||
101 | my $excel = Spreadsheet::ParseExcel->new(); | ||
102 | for (@ARGV) { | ||
103 | my $book = $excel->Parse($_); | ||
104 | |||
105 | my $last_sheet = $book->{SheetCount} - 1; | ||
106 | |||
107 | for my $worksheet ( @{ $book->{Worksheet} }[ 0 .. $last_sheet ] ) { | ||
108 | |||
109 | next | ||
110 | if not defined $worksheet->{MaxRow} | ||
111 | or not defined $worksheet->{MaxCol}; | ||
112 | |||
113 | print $worksheet->{Name} . "\n" if $opt_verbose; | ||
114 | |||
115 | my $filename = $worksheet->{Name} . $opt_suffix; | ||
116 | open my $fh, ">", $filename | ||
117 | or die "Can open $filename to write: $!\n"; | ||
118 | |||
119 | my @row = $worksheet->{MinRow} .. $worksheet->{MaxRow}; | ||
120 | my @col = $worksheet->{MinCol} .. $worksheet->{MaxCol}; | ||
121 | |||
122 | for my $row ( @{ $worksheet->{Cells} }[ @row ] ) { | ||
123 | my @cellvalue = map { | ||
124 | $_ = $_->Value() if ref $_; | ||
125 | $_ = '' if not defined $_; | ||
126 | $_; | ||
127 | } @$row[ @col ]; | ||
128 | print $fh join($opt_separator, @cellvalue), "\n"; | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | </verbatim> |
lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 3 times)