~ubuntu-branches/ubuntu/trusty/horae/trusty

« back to all changes in this revision

Viewing changes to 0CPAN/Spreadsheet-WriteExcel-2.15/examples/csv2xls.pl

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2008-02-23 23:13:02 UTC
  • mfrom: (2.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080223231302-mnyyxs3icvrus4ke
Tags: 066-3
Apply patch to athena_parts/misc.pl for compatibility with 
perl-tk 804.28.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl -w
2
 
 
3
 
###############################################################################
4
 
#
5
 
# Example of how to use the WriteExcel module
6
 
#
7
 
# Program to convert a CSV comma-separated value file into an Excel file.
8
 
# This is more or less an non-op since Excel can read CSV files.
9
 
# The program uses Text::CSV_XS to parse the CSV.
10
 
#
11
 
# Usage: csv2xls.pl file.csv newfile.xls
12
 
#
13
 
# reverse('�'), March 2001, John McNamara, jmcnamara@cpan.org
14
 
#
15
 
 
16
 
use strict;
17
 
use Spreadsheet::WriteExcel;
18
 
use Text::CSV_XS;
19
 
 
20
 
# Check for valid number of arguments
21
 
if (($#ARGV < 1) || ($#ARGV > 2)) {
22
 
   die("Usage: csv2xls csvfile.txt newfile.xls\n");
23
 
};
24
 
 
25
 
# Open the Comma Separated Variable file
26
 
open (CSVFILE, $ARGV[0]) or die "$ARGV[0]: $!";
27
 
 
28
 
# Create a new Excel workbook
29
 
my $workbook  = Spreadsheet::WriteExcel->new($ARGV[1]);
30
 
my $worksheet = $workbook->add_worksheet();
31
 
 
32
 
# Create a new CSV parsing object
33
 
my $csv = Text::CSV_XS->new;
34
 
 
35
 
# Row and column are zero indexed
36
 
my $row = 0;
37
 
 
38
 
 
39
 
while (<CSVFILE>) {
40
 
    if ($csv->parse($_)) {
41
 
        my @Fld = $csv->fields;
42
 
 
43
 
        my $col = 0;
44
 
        foreach my $token (@Fld) {
45
 
            $worksheet->write($row, $col, $token);
46
 
            $col++;
47
 
        }
48
 
        $row++;
49
 
    }
50
 
    else {
51
 
        my $err = $csv->error_input;
52
 
        print "Text::CSV_XS parse() failed on argument: ", $err, "\n";
53
 
    }
54
 
}