in reply to Re^2: Create CSV file from xlsx file
in thread Create CSV file from xlsx file

Please add these lines to the top of your code (after the shebang line, e.g. #!/path/to/perl ..., if one exists):

use strict; use warnings; use diagnostics;

Next, please read strict, warnings and diagnostics to understand why I've asked you to do this.

Next, as your knowledge of Perl seems extremely limited, please read "perlintro -- a brief introduction and overview of Perl".

Now, rerun your script and see if you can work out what that first error is. It'll now be a few lines after line 20. If you can't see the problem, go back to perlintro and compare the code there with what you've written. Keep repeating this until you've found the problem and fixed it.

You haven't shown line 50. The error message is telling you this is also a syntax problem. Attempt to fix it in the same manner.

When you've done all that, you still won't get any output because of another error. The code you added to the top of your script will tell you about this. It should be easy to fix.

-- Ken

Replies are listed 'Best First'.
Re^4: Create CSV file from xlsx file
by viji234 (Initiate) on Oct 28, 2013 at 08:25 UTC
    thank you ken.. those 2 lines really helped me a lot and now i am able to print those values. While creating it in the CSV file I am getting the 4 columns values in the single column.But i want each values in separate column?
    use strict; use warnings; use diagnostics; use Spreadsheet::XLSX; use Spreadsheet::Read; use Text::CSV; my $excel = Spreadsheet::XLSX -> new ('Sample.xlsx',); my $csv = Text::CSV->new (); foreach my $sheet (@{$excel -> {Worksheet}}) { printf("Sheet: %s\n", $sheet->{Name}); my @worksheet = qw(sheet1 sheet2); my $maxrow = $sheet -> {MaxRow}; my $Minrow = 1; my @wanted_cols = (8, 9, 10, 20); $maxrow ||= $Minrow; open FH, ">new.csv" or die "new.csv: $!"; foreach my $row ($Minrow .. $maxrow) { foreach my $wanted_cols (@wanted_cols) { my $wanted_cells = $sheet->{Cells}[$row][$wan +ted_cols]; print FH ( $wanted_cells -> {Val} ); } print FH ("\n"); } close FH or die "new.csv: $!"; }

      You are requiring use Spreadsheet::Read;, but you don't use it. Why?

      You are requiring use Text::CSV, but you only use it to instantiate a $csv which you do not use at all!

      Did you read any of the manuals of the modules you obviously try to use?

      Hint: your print FH ( $wanted_cells -> {Val} ); should be using $csv and the initialisation of $csv should have the eol attribute.


      Enjoy, Have FUN! H.Merijn
        Thank you :) I changed the code to as you mentioned but the CSV file is empty and I am getting an error as below.
        #error Sheet: sheet1 Uncaught exception from user code: Expected fields to be an array ref at test_csv_1.pl line 38. Text::CSV_PP::print('Text::CSV=HASH(0x348e558)', 'S.no') calle +d at test_csv_1.pl line 38
        use strict; use warnings; use diagnostics; use Spreadsheet::XLSX; use Text::CSV; my $excel = Spreadsheet::XLSX -> new ('Sample.xlsx',); my $csv = Text::CSV->new ({eol => "\n"}); foreach my $sheet (@{$excel -> {Worksheet}}) { printf("Sheet: %s\n", $sheet->{Name}); #my @worksheet = qw(sheet1 sheet2); my $maxrow = $sheet -> {MaxRow}; my $Minrow = 1; my @wanted_cols = (8, 9, 10, 20); $maxrow ||= $Minrow; open FH, ">new.csv" or die "new.csv: $!"; foreach my $row ($Minrow .. $maxrow) { foreach my $wanted_cols (@wanted_cols) { my $wanted_cells =$sheet->{Cells}[$row][$want +ed_cols]; $csv -> print ( $wanted_cells -> {Val} ); } $csv -> print ("\n"); } close FH or die "new.csv: $!"; }