Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: Create CSV file from xlsx file

by viji234 (Initiate)
on Oct 28, 2013 at 08:25 UTC ( #1059949=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^4: Create CSV file from xlsx file
Download Code
Re^5: Create CSV file from xlsx file
by Tux (Monsignor) on Oct 28, 2013 at 09:17 UTC

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

        You either did not read the docs, or they are still unclear to you. To me even the error message is clear. Text::CSV's print () needs completely different arguments then what you pass.

        PLEASE, do us all a favor and read the manuals before you post questions here that appear rather silly.

        use warnings; use diagnostics; use Spreadsheet::XLSX; use Text::CSV; my $excel = Spreadsheet::XLSX->new ("Sample.xlsx"); my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, eol => "\n" + }); foreach my $sheet (@{$excel->{Worksheet}}) { printf ("Sheet: %s\n", $sheet->{Name}); #my @worksheet = qw(sheet1 sheet2); my $Minrow = 1; my $maxrow = $sheet->{MaxRow} || $Minrow; my @wanted_cols = (8, 9, 10, 20); open my $fh, ">", "new.csv" or die "new.csv: $!"; foreach my $row ($Minrow .. $maxrow) { $csv->print ($fh, [ map { $sheet->{Cells}[$row][$_]{Val} } @wanted_cols ]}; } close my $fh or die "new.csv: $!"; }

        Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1059949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-08-01 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (10 votes), past polls