Generating Excel Spreadsheets Programatically

From PeformIQ Upgrade
Revision as of 17:43, 15 December 2007 by MatthewBretherton (talk | contribs)
Jump to navigation Jump to search

Back to Capacity Forecasting Tool

Perl


Python

Examples

PERL

Ex 1

#!/usr/bin/env perl
#
#-------------------------------------------------------------------------------

use strict;
use Spreadsheet::WriteExcel;

#-------------------------------------------------------------------------------

if ($#ARGV ne 1) {
   print "\n Usage: txt2xls \n Example: txt2xls \"|\" *.psv\n\n";
}

my $token;
my $file;
my $del;
my $wb;

my $separator = quotemeta($ARGV[0]);
my @files     = @ARGV[1..$#ARGV];

foreach $file (@files){
   open (TXTFILE, "$file") or die;

   my $wb      = Spreadsheet::WriteExcel->new("$file.xls");
   my $excel   = $wb->addworksheet();
   my $row     = 0;
   my $col;

   while (<TXTFILE>) {
      chomp;

      my @Fld = split(/$separator/, $_);

      $col = 0;

      foreach $token (@Fld) {
         $excel->write($row, $col, $token);
         $col++;
      }

      $row++;
   }
}

#-------------------------------------------------------------------------------