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

Thank you knot.. as you mentioned i changed the script to as below. I am getting the error as below. The script is not writing anything. And also i want to use the sheet separtely not in the loop and want to create the CSV file.
Error: syntax error at test.pl line 20,near "$sheet1{" syntax error at test.pl line 50,near "$sheet1}" aborted due to compilation error
use Spreadsheet::XLSX; use Spreadsheet::Read; my $excel = Spreadsheet::XLSX -> new ('Sample.xlsx',); #print "$excel \n"; my @sheets = qw(Sheet1 Sheet2); my $sheet1 = $sheet[0]; my $sheet2 = $sheet[1]; foreach my $sheet1{ my $sheet=$excel -> Worksheet(); printf("Sheet: %s\n", $sheet); my $maxrow = $sheet -> {MaxRow}; my $Minrow = "3"; $maxroww ||= $Minrow; foreach my $row ( $Minrow .. $maxrow) { $sheet -> {MaxCol} ||= $sheet -> {MinCol}; my @wanted_cols = (9, 10, 11, 21); my @wanted_cells = @{$sheet->{Cells}[$row]}[@wanted_co +ls]; if ($wanted_cells) { printf("( %s , %s ) => %s\n", $row, $col, +$cell -> {Val}); } } }

Replies are listed 'Best First'.
Re^3: Create CSV file from xlsx file
by kcott (Archbishop) on Oct 25, 2013 at 07:41 UTC

    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

      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