in reply to Create CSV file from xlsx file

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

Replies are listed 'Best First'.
Re^2: Create CSV file from xlsx file
by viji234 (Initiate) on Oct 25, 2013 at 06:45 UTC
    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

        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: $!"; }