#!perl
use strict;
use Getopt::Long;
use warnings;
use Cwd;
require Spreadsheet::WriteExcel;
require IO::Scalar;
my @file;
my $xls_sheet;
my $help;
my $recordsep;
my $ARGC=@ARGV;
my $cur_dir = cwd; # while $0 is the $cur_dir/cover.pl
my @excel_file_list=();
my @excel_file="";
$xls_sheet = "$ARGV[1]";
opendir DIR, "$ARGV[0]" || die "can not open dir $ARGV[0].\n";
while(my $file=readdir(DIR)){
print "$file\n";
if(lc($file) =~ /\.txt/){
@excel_file_list = ("$ARGV[0]/$file", @excel_file_list);
print "@excel_file_list\n";
}
}
closedir(DIR);
@file = @excel_file_list;
my $number = 1;
my $file = undef;
my $xls_str;
tie *XLS, 'IO::Scalar', \$xls_str;
my $workbook = Spreadsheet::WriteExcel->new(\*XLS);
my $name;
foreach $file ( @file ){
$name = "Node $number";
#print "Lagi buka file: $file\n";
open F, "<", $file or croak("Can't open $file: $!"); #$/ = $recordsep || "\n" || "\t" || "\|"; my @data =;
close F;
$/ = "\n";
my $last;
#print "Lagi Generate Sheet: $name\n";
my $worksheet = $workbook->addworksheet( $name );
$worksheet->add_write_handler(qr[\w], \&store_string_widths);
for ( my $row = 0; $row < @data; $row++ ) { chomp( $data[$row] ); # Split on single tab #$data[$row] =~ s/^\s+//; my @Fld = split('\t', $data[$row]); my $col = 0; foreach my $token (@Fld) { $worksheet->write($row, $col, $token);
$col++;
$last = $row + 5;
autofit_columns($worksheet);
}
#$worksheet->write ($row, $col, 'AZ' );
}
$last++;
my $myname = "A$last";
#print "$myname\n";
$number++;
#$worksheet->write ($myname, 'Teman AZ' );
}
$workbook->close();
#print "Sheet done..$xls_sheet\n";
open( XL_SHEET,">$xls_sheet" ) || die( "ga ada filenya neh : $xls_sheet\n" );
print XL_SHEET $xls_str;
close( XL_SHEET );
print "Done...!!!!!!!!!!!!!!!!!!!!!!!!!\n";
sub autofit_columns {
my $worksheet = shift;
my $col = 0;
for my $width (@{$worksheet->{__col_widths}}) {
$worksheet->set_column($col, $col, $width) if $width;
$col++;
}
}
sub store_string_widths {
my $worksheet = shift;
my $col = $_[1];
my $token = $_[2];
# Ignore some tokens that we aren't interested in.
return if not defined $token; # Ignore undefs.
return if $token eq ''; # Ignore blank cells.
return if ref $token eq 'ARRAY'; # Ignore array refs.
return if $token =~ /^=/; # Ignore formula
# Ignore numbers
return if $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
# Ignore various internal and external hyperlinks. In a real scenario
# you may wish to track the length of the optional strings used with
# urls.
return if $token =~ m{^[fh]tt?ps?://};
return if $token =~ m{^mailto:};
return if $token =~ m{^(?:in|ex)ternal:};
# We store the string width as data in the Worksheet object. We use
# a double underscore key name to avoid conflicts with future names.
#
my $old_width = $worksheet->{__col_widths}->[$col];
my $string_width = string_width($token);
if (not defined $old_width or $string_width > $old_width) {
# You may wish to set a minimum column width as follows.
#return undef if $string_width <>{__col_widths}->[$col] = $string_width;
}
# Return control to write();
return undef;
}
###############################################################################
#
# Very simple conversion between string length and string width for Arial 10.
# See below for a more sophisticated method.
#
sub string_width {
return 0.9 * length $_[0];
}
__END__
###############################################################################
#
# This function uses an external module to get a more accurate width for a
# string. Note that in a real program you could "use" the module instead of
# "require"-ing it and you could make the Font object global to avoid repeated
# initialisation.
#
# Note also that the $pixel_width to $cell_width is specific to arial. For
# other fonts you should calculate appropriate relationships. A future verison
# of S::WE will provide a way of specifying column widths in pixels instead of
# cell units in order to simplify this conversion.
#
sub string_width {
require Font::TTFMetrics;
my $arial = Font::TTFMetrics->new('c:\windows\fonts\arial.ttf');
my $font_size = 10;
my $dpi = 96;
my $units_per_em = $arial->get_units_per_em();
my $font_width = $arial->string_width($_[0]);
# Convert to pixels as per TTFMetrics docs.
my $pixel_width = 6 + $font_width *$font_size *$dpi /(72 *$units_per_em);
# Add extra pixels for border around text.
$pixel_width += 6;
# Convert to cell width (for Arial) and for cell widths > 1.
my $cell_width = ($pixel_width -5) /7;
return $cell_width;
}
__END__
Wednesday, July 4, 2007
convert all text file in 1 dir to a 1 xls file
Related Posts (ajax)
Labels
3g
aaa
Ajax
all leagues
belajar
billing
books
cdma
cdr
cisco
evdo
friends
google
gsm
hspda
hsupa
ims
indonesia
IP
islam
jquery
kpi
leadership
linux
lte
map
mediasi
mml
monitor
moshell
mpls
network
nokia
omc
oracle
oss
paging
parma
performance
perl
players
politics
pstn
radio
script
shell scipt
siemens
signalling
songs
story
switching
telecom
tips
tools
umts
voip
wimax
windows
0 Comment :
Post a Comment