Difference between revisions of "Generating Excel Spreadsheets Programatically"

From PeformIQ Upgrade
Jump to navigation Jump to search
Line 5: Line 5:
* [http://search.cpan.org/ Write Excel Spreadsheets]
* [http://search.cpan.org/ Write Excel Spreadsheets]
* [http://aspn.activestate.com/ASPN/CodeDoc/Spreadsheet-WriteExcel/WriteExcel.html]
* [http://aspn.activestate.com/ASPN/CodeDoc/Spreadsheet-WriteExcel/WriteExcel.html]


= Python =
= 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++;
    }
}
#-------------------------------------------------------------------------------

Revision as of 16:43, 15 December 2007

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++;
   }
}

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