-1

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");
serenesat
  • 4,611
  • 10
  • 37
  • 53
Sri
  • 33
  • 1
  • 8
  • please suggest me about code modifications ,and better perl modules for my requirement ...thank you in advance – Sri May 21 '15 at 08:29

1 Answers1

0

You closed the filehandle XML at line 23 but you have not open that anywhere.

First open a filehandle and then write.

This is the syntax to open a file for writing:

open my $fh, ">" $filename or die $!;
serenesat
  • 4,611
  • 10
  • 37
  • 53
  • hi , am getting below error Can't locate object method "print_xml" via package "XML::Writer" at exceltoxml1. pl line 25. ; so how to locate that object thank you for help – Sri May 21 '15 at 08:14
  • See the document of [XML::Writer](http://search.cpan.org/dist/XML-Writer/Writer.pm) – serenesat May 21 '15 at 09:01