#!/usr/bin/perl -w
use strict;

use Spreadsheet::WriteExcel;

convert_tab_to_excel();

sub convert_tab_to_excel {
    my $txtFile = $ARGV[0] || return usage("missing file parameter");

    my $excelFile = $txtFile;
    $excelFile =~ s/\.(\w+)$/\.xls/;

    my $workbook  = new Spreadsheet::WriteExcel($excelFile);
    my $worksheet = $workbook->add_worksheet();
    $worksheet->keep_leading_zeros();

    open INPUT, "$txtFile" or die "Couldn't open input file $txtFile: $!";

    my $count = 0;
    while ( my $line = <INPUT> ) {
        chomp($line);
        my @values = split( /\t/, $line );

        $worksheet->write( $count++, 0, \@values );
    }

    print "Done!\nFile created: $excelFile\n";
}

sub usage {
    my $err = shift;

    print "ERROR: $err\n";
    print "Usage: $0 FILE \n";

    return 1;
}

