I am trying to convert xlsx
to xml
using Perl modules. According to my requirement the Perl script should take .xlsx
file and convert to .xml
format exactly. While compiling my code I am getting error:
print() on unopened filehandle XML at xlsxtoxml.pl.
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use XML::Writer;
use Spreadsheet::XLSX;
my $excel = Spreadsheet::XLSX -> new ('Template.xlsx');
foreach my $sheet (@{$excel -> {Worksheet}}) {
$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) {
#print XML $cell -> {Val};
}
unless($col == $sheet -> {MaxCol}) {print XML ",";}
}
unless( $row == $sheet -> {MaxRow}){print XML "\n";}
}
}
close(XML);
my $xml_obj = XML::Writer->new();
$xml_obj->print_xml("outTemplate.xml");