The task here is reading an excell file, process some fields and export with Write::Excel. I have two problems with this:
1) Dates These are read as a number and there is no $oWkC attribute that would signal it should be read as a date
Indeed, although it appears correctly as say 20/11/2007 in the excel file, the 'Type' attribute is not a Date, it is
2)Non-english characters. How can these be read and written to in a consistent manner?
The code:
1) Dates These are read as a number and there is no $oWkC attribute that would signal it should be read as a date
Indeed, although it appears correctly as say 20/11/2007 in the excel file, the 'Type' attribute is not a Date, it is
2)Non-english characters. How can these be read and written to in a consistent manner?
The code:
Code:
use strict;
use Storable;
use Spreadsheet::WriteExcel;
require('dumpvar.pl');
$main::vers=Spreadsheet::WriteExcel->VERSION;
my $fh;my $str;
my $excfile=shift;
if(-e $excfile){
# Requires perl 5.8 or later
my $outfile='NEW'.$excfile;
$main::workbook = Spreadsheet::WriteExcel->new("$outfile");
#$main::workbook = Spreadsheet::WriteExcel->new(\*STDOUT);
$main::format = $main::workbook->addformat();
$main::format0 = $main::workbook->addformat();
$main::format0->set_bold();
$main::format1 = $main::workbook->addformat(num_format => '@');
&excparse($excfile);
}else{die "nonexistent file $excfile\n";}
sub excparse{
use strict;
use Spreadsheet::ParseExcel;
my $file=shift ;
my $iR;my $oWkS; my $oWkC;my $debugflag=1;#set to 0 for production runs
my $oExcel = new Spreadsheet::ParseExcel;
my $el;
my $oBook = $oExcel->Parse($file ) ;
if($debugflag){ print "=========================================\n";
print "FILE :", $oBook->{File} , "\n";
print "COUNT :", $oBook->{SheetCount} , "\n";
print "AUTHOR:", $oBook->{Author} , "\n";
print "filename-= $file obook=$oBook\n"; } ;
my @format_check=();
my %headers;
my $lastsheet=-1;
#binmode STDOUT, ":utf8";
for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++){
if($iSheet >$lastsheet){
if($main::vers>0.40){
$main::worksheet{$iSheet} = $main::workbook->add_worksheet($iSheet);}
else{$main::worksheet{$iSheet}=$main::workbook->addworksheet($iSheet);}
$main::worksheet{$iSheet}->activate();}
$oWkS = $oBook->{Worksheet}[$iSheet];
for(my $iR = $oWkS->{MinRow} ;
defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {
print "ir=$iR\n";
for(my $iC = $oWkS->{MinCol} ;
defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
$iC++) {
my $oWkC = $oWkS->{Cells}[$iR][$iC];
if($oWkC){ $el=$oWkC->{'Val'};
$el=$oWkC->{'_Value'} if ( $oWkC->{'Type'} eq 'Date'
or $el=~/[\r\n]/ );#### check here!!!
if($el=~/[\r\n]/){print "el=$el\n";
}
$el=~s/^\s+//;
$el=~s/[\r\n]/ /g;#get rid of new lines ;
}else{$el='';}#if oWkC
#print "ic=$iC owkc= $oWkC ir=$iR el=$el\n" if $debugflag;
if($iR){
#if($iC>13){
#$main::worksheet{$iSheet}->write_utf16be_string($iR,$iC,$el,$main::format0);
#$main::worksheet{$iSheet}->write_utf1be($iR,$iC,$el,$main::format0);
unless($el=~/(.*)\/(.*)\/(.*)/){
$main::worksheet{$iSheet}->write_string($iR,$iC,$el,$main::format1);}
else{ my $format=$main::format1; $format->set_num_format('mm/dd/yy');
$main::worksheet{$iSheet}->write_string($iR,$iC,$el,$format); }
}
#}
else{
$main::worksheet{$iSheet}->write($iR,$iC,$el,$main::format0);}
goto I10 if $iR==15;
}#foreach iC
}#for iR
$lastsheet=$iSheet;
}#foreach sheet
I10:$main::workbook->close() or print "Error closing file: $!";