in reply to Re^7: Excel to CSV datetime conversion- format changes
in thread Excel to CSV datetime conversion- format changes

Spreadsheet::ParseExcel::Utility is not the same thing as Spreadsheet::ParseExcel, it's not a drop in replacement, it imports some extra functions, it's not a replacement for Spreadsheet::ParseExcel. Please read and understand the documentation of the modules you use within your programs.

Update: For example:

#!/usr/bin/perl -w # For each tab (worksheet) in a file (workbook), # spit out columns separated by ",", # and rows separated by c/r. use strict; use Spreadsheet::ParseExcel; use Spreadsheet::ParseExcel::Utility qw(ExcelFmt); # In your code: # my $e = Spreadsheet::ParseExcel::Utility qw(ExcelFmt); my $excel = Spreadsheet::ParseExcel->new(); # The rest of your script.....

Replies are listed 'Best First'.
Re^9: Excel to CSV datetime conversion- format changes
by vsmeruga (Acolyte) on Apr 25, 2014 at 10:55 UTC

    Hi I am sorry to come again. I am not a perl programmer, I am an architect. I have no friend who knows perl. I changed my script as below and still got error on the screen as

    Global symbol "$e" requires explicit package name at ./perl_excel_xls2n.pl line 18. Global symbol "$e" requires explicit package name at ./perl_excel_xls2n.pl line 22. Execution of ./perl_excel_xls2n.pl aborted due to compilation errors.

    Can't locate object method "new" via package "Spreadsheet::ParseExcel" at ./perl_excel_xls2n.pl line 15.

    #!/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 Spreadsheet::ParseExcel::Utility qw(ExcelFmt); use strict; my $file='output2.csv'; my $datefmt = 'dd/mm/yyyy'; # ISO 8601 open STDOUT, ">", $file or die "$0: open: $!"; open STDERR, ">&STDOUT" or die "$0: dup: $!"; my $filename = shift || "/home/etc/srcfile.xls"; #my $e = new Spreadsheet::ParseExcel; my $excel = Spreadsheet::ParseExcel->new(); my $eBook = $e->Parse($filename); my $sheets = $eBook->{SheetCount}; my ($eSheet, $sheetName); my $cell = $e->get_cell( $eSheet->{row}, $eSheet->{col} ); my $value = $cell->value(); 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 $cell->{Type} && $cell->{Type} eq 'Date') { $value = ExcelFmt($datefmt, $cell->{Val}); } if (defined $eSheet->{Cells}[$row][$column]) { print $eSheet->{Cells}[$row][$column]->Value . ","; } else { print ","; } } print "\n"; } }

      Don't worry. This problem is because in the example I gave you I use the variable name $excel which I found more descriptive than $e. Using easier to understand variable names helps when someone else looks at your code, or you haven't looked at it in a long time. By copying and pasting this one line then you'd also have to replace the other instances of $e with $excel.

      The quick and dirty way to fix this is to replace $excel with $e. From the command prompt can you run the following and post the output?

      cpan -D Spreadsheet::ParseExcel

      Able to get rid of the previous error.

      Can't locate object method "get_cell" via package "Spreadsheet::ParseExcel" at ./perl_excel_xls2n.pl line 22.

      #!/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 Spreadsheet::ParseExcel::Utility qw(ExcelFmt); use strict; my $file='output2.csv'; my $datefmt = 'dd/mm/yyyy'; # ISO 8601 open STDOUT, ">", $file or die "$0: open: $!"; open STDERR, ">&STDOUT" or die "$0: dup: $!"; my $filename = shift || "/home/etc/srcfile.xls"; #my $e = new Spreadsheet::ParseExcel; my $excel = Spreadsheet::ParseExcel->new(); my $eBook = $excel->Parse($filename); my $sheets = $eBook->{SheetCount}; my ($eSheet, $sheetName); my $cell = $excel->get_cell( $eSheet->{row}, $eSheet->{col} ); my $value = $cell->value(); 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 $cell->{Type} && $cell->{Type} eq 'Date') { $value = ExcelFmt($datefmt, $cell->{Val}); } if (defined $eSheet->{Cells}[$row][$column]) { print $eSheet->{Cells}[$row][$column]->Value . ","; } else { print ","; } } print "\n"; } }

        If in doubt, read the documentation. get_cell is applicable to worksheets, rather than parser instances. $excel is the name of your parser (Spreadsheet::ParseExcel) instance.

        Update: I think you need to sit down and step through your code, ensure it's doing what you think it should be doing.