I have the below script which is converting xlsx to csv but if the cell value has comma (,) in between, it is moving to next column in csv which is wrong. Colud you please correct it? Also, how can convert multiple xlsx files to mulptiple csv files in one go?
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::XLSX;
use Text::CSV qw(csv);
my $excel = Spreadsheet::XLSX -> new ('/path/file.xlsx');
my $csv = '/path/File.csv';
open(my $FH ,'>',"$csv") or die "failed to open";
my $line;
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 -> {MaxCol}) {
my $cell = $sheet -> {Cells} [$row] [$col];
#if ($cell) {
# $line .= "\"".$cell -> {Val}."\",";
# $line .= $cell -> {Val};
# if ($col != $sheet -> {MaxCol}) #appends the comma only if the column being processed is not the last
# {
# $line .= ",";
# }
#}
if (defined $cell && defined $cell->Value) {
if ($col != $sheet -> {MaxCol})
{
print $FH $cell->Value.",";
}
else
{
print $FH $cell->Value ;
}
} else {
if ($col != $sheet -> {MaxCol})
{ print $FH ",";
}
}
}
$FH =~ s/,$//; # replace comma at the end of the string with empt
print $FH "\n";
}