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

Hi can anyone please help on this.I am new to perl and this is my first script.in my xlsx file i have sheet1 and sheet2. From sheet 1 i want to create 2 CSV file from clumn 9,10,11,21 to file1.csv and coulmn 12,13,14,15,16,17,18,19,21 to file2.csv and from sheet2 another CSV file from 10,11,12,14 to file3.csv columns. But my current code writes all the rows and columns of 2 sheets. my code is
use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; use Spreadsheet::XLSX; my $excel = Spreadsheet::XLSX -> new ('sample.xlsx',); ##print "$excel \n"; my @sheets = qw(sheet1 sheet2); my $sheet1 = $sheet[0]; my $sheet2 = $sheet[1]; print "$sheet1\n"; print "$sheet2\n"; foreach my $sheet (@{$excel -> {Worksheet}}) { printf("Sheet: %s\n", $sheet->{Name}); $sheet -> {MaxRow} ||= $sheet -> {MinRow}; foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) { $sheet -> {MaxCol} ||= $sheet -> {MinCol}; foreach my $col ($sheet -> {MinCol} .. $sheet -> {Max +Col}) { my $cell = $sheet -> {Cells} [$row] [$col]; if ($cell) { printf("( %s , %s ) => %s\n", $row, $col, +$cell -> {Val}); } } } }
Can anyone please help me on this?

Replies are listed 'Best First'.
Re: Create CSV file from xlsx file
by Tux (Canon) on Oct 24, 2013 at 11:40 UTC
      Hi Tux after running the script as u mention its giving error as syntax error at test.pl line 11, near "$ xlscat -S " Execution of test.pl aborted due to compilation errors.

        xlscat is a perl script (read the links given), have you simple added the lines Tux gave you to your existing script? If so don't. The examples given to you run from the command line, your script is not required.

Re: Create CSV file from xlsx file
by kcott (Archbishop) on Oct 24, 2013 at 14:09 UTC

    G'day viji234,

    Welcome to the monastery.

    "But my current code writes all the rows and columns of 2 sheets. my code is"

    Except that's not your code, is it? Apart from a couple of apparently pointless print statements near the start of the code, you've just copied the SYNOPSIS of the Spreadsheet::XLSX documentation. That code does what the documentation is explaining: it's not going to magically morph into different code to suit your current requirements.

    You should be able to see from the code, that you can identify which sheet you're dealing with, using the value of $sheet->{Name}.

    Instead of looping through all columns, just get the ones you want from each row, e.g.

    my @wanted_cols = (9, 10, 11, 21); my @wanted_cells = @{$sheet->{Cells}[$row]}[@wanted_cols];

    The @array[@indices] construct is called an array slice. See perldata: Slices for details.

    To write your data to a CSV file, use Text::CSV.

    -- Ken

      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}); } } }

        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