Home   >>   Perl   >>   Script To Convert Text File Into XLS File
Script To Convert Text File Into XLS File PDF Print E-mail
( 0 Votes )
How To - Perl
Written by Christian Foronda   
Monday, 06 September 2010 15:33

Create the script:

	$ vi txt2xls
	#!/usr/bin/perl -w

	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 @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 () {
	chomp;

	if ($ARGV[0] =~ /\|/)
	{
	$del="\\|";
	}
	else
	{
	$del = $ARGV[0];
	}

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

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

Make it executable;:

	$ chmod +x txt2xls

Usage example:

	$ ./txt2xls "|" OutputFile



blog comments powered by Disqus
Last Updated on Monday, 06 September 2010 15:37