]> git.donarmstrong.com Git - bin.git/blob - convert_to_xls
switch hosts for update_mobile_org
[bin.git] / convert_to_xls
1 #! /usr/bin/perl
2 # conver_to_xls converts tab separated files to xls and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1153 2008-04-08 00:04:20Z don $
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 convert_to_xls - convert tab separated files to xls
18
19 =head1 SYNOPSIS
20
21 convert_to_xls tsv_file1 [tsv_file2] [options] > foo.xls
22
23  Options:
24   --debug, -d debugging level (Default 0)
25   --help, -h display this help
26   --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--debug, -d>
33
34 Debug verbosity. (Default 0)
35
36 =item B<--help, -h>
37
38 Display brief useage information.
39
40 =item B<--man, -m>
41
42 Display this manual.
43
44 =back
45
46 =head1 EXAMPLES
47
48
49 =cut
50
51 use Spreadsheet::WriteExcel;
52 use IO::File;
53
54 use vars qw($DEBUG);
55
56 my %options = (debug           => 0,
57                help            => 0,
58                man             => 0,
59                );
60
61 GetOptions(\%options,
62            'debug|d+','help|h|?','man|m');
63
64 pod2usage() if $options{help};
65 pod2usage({verbose=>2}) if $options{man};
66
67 $DEBUG = $options{debug};
68
69 my @USAGE_ERRORS;
70 if (not @ARGV) {
71      push @USAGE_ERRORS,"You must give at least one file";
72 }
73
74 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
75
76 my @FILES=@ARGV;
77 @ARGV=();
78
79 my $wb = Spreadsheet::WriteExcel->new(\*STDOUT) or die "Unable to create new spreadsheet";
80 for my $file (@FILES) {
81      my $fh = IO::File->new($file,'r') or die "Unable to open $file for reading: $!";
82      my $ws = $wb->add_worksheet($file) or die "Unable to create new worksheet";
83      my $row = 1;
84      while (<$fh>) {
85           chomp;
86           my @row = map {s/^"//; s/"$//; $_} split /\t/,$_;
87           for my $col (0..$#row) {
88                $ws->write($row,$col+1,$row[$col]);
89           }
90           $row++;
91      }
92 }
93 $wb->close;
94
95
96 __END__