Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am new to perl programming: I am using the below code to convert excel file to CSV file and the format of date changes My source file.xls has dates as below: TDate VDate IDate 20/03/2014 00:08 26/03/2014 20/03/2014 08:07 Output of the .csv files looks as (changes the format when time exists in it). TDate VDate IDate 3-20-14 0:08 26/03/2014 3-20-14 8:07 Please help me with the code which does not change the datetime format of TDate and IDate Thanks. Below is the script I am using. Vijay perl script:

#!/usr/bin/perl -w # For each tab (worksheet) in a file (workbook), # spit out columns separated by ",", # and rows separated by c/r. use Spreadsheet::ParseExcel; use strict; my $file='output2.csv'; open STDOUT, ">", $file or die "$0: open: $!"; open STDERR, ">&STDOUT" or die "$0: dup: $!"; my $filename = shift || "/home/src/file.xls"; my $e = new Spreadsheet::ParseExcel; my $eBook = $e->Parse($filename); my $sheets = $eBook->{SheetCount}; my ($eSheet, $sheetName); foreach my $sheet (0 .. $sheets - 1) { $eSheet = $eBook->{Worksheet}[$sheet]; $sheetName = $eSheet->{Name}; #print "#Worksheet $sheet: $sheetName\n"; next unless (exists ($eSheet->{MaxRow}) and (exists ($eSheet->{Max +Col}))); foreach my $row ($eSheet->{MinRow} .. $eSheet->{MaxRow}) { foreach my $column ($eSheet->{MinCol} .. $eSheet->{MaxCol}) { if (defined $eSheet->{Cells}[$row][$column]) { print $eSheet->{Cells}[$row][$column]->Value . ","; } else { print ","; } } print "\n"; } }

Replies are listed 'Best First'.
Re: Excel to CSV datetime conversion- format changes
by vsmeruga (Acolyte) on Apr 24, 2014 at 15:10 UTC
    As it is my first time I have created a thread. I haven't logged in. that is why it is showing as anonymous monk. Actually the question is raised by me. I do not want to duplicate it creating the same again.
Re: Excel to CSV datetime conversion- format changes
by vsmeruga (Acolyte) on Apr 24, 2014 at 15:18 UTC

    I will be bit clear on Date formats

    source: file.xls has dates as below:

    TDate 20/03/2014 08:07

    VDate 26/03/2014

    IDate 20/03/2014 08:07

    Output of the .csv files looks as (changes the format when time exists in it).

    TDate; 3-20-14 0:08

    VDate 26/03/2014

    IDate 3-20-14 8:07

      Excel format handling is (to me) a twisty maze of passages. There's a map of format codes to default formats, which can change depending on locale (see the various Spreadsheet::ParseExcel::Fmt* libraries in the distribution). You may need to debug to see where things go off. As a workaround, you can use the ExcelFmt() function from Spreadsheet::ParseExcel::Utility in the distribution to format these cells (feeding it the value from calling the cell's unformatted() method rather than value()). You can also try posting the issue with the problem spreadsheet at the bug tracker (as indicated in the distribution). The maintainer may get around to looking at it.

        Thanks for the reply. I have seen ExcelFmt() function but I am not able to utilize in the current code. Can you please help me where to call that ExcelFmt in the line of code.