i need to open a template and write data into that. i dont need to open a new .xlsx file. With Spreadsheet::WriteExcelXML is it possible to open a existing file?

You said you need to open a template file. Usually, that implies making a new file based on the template. And writing back to the same file makes the template not a template any more.

But, it is possible to write the data back to the original file:

# open file in read/write mode open(my $xlshandle, '+<', $xlsfile) or die "Can't open '$xlsfile' for +read/write: $!\n"; binmode($xlshandle); # insert code to read spreadsheet # reset handle to beginning of file seek($xlshandle, 0, 0); my $newxls = Spreadsheet::WriteExcelXML->new($xlshandle); # handle, NO +T file # insert code to put updated data into $newxls $newxls->close(); close($xlshandle);

BUT, it's much safer to:

my $newxlsfile = $xlsfile . '.new'; my $newxls = Spreadsheet::WriteExcelXML->new($newxlsfile); # file, NOT + handle # insert code to put updated data into $newxls $newxls->close(); close($xlshandle); rename($xlsfile, $xlsfile . '.bak'); rename($newxlsfile, $xlsfile);

You will still get a file with the same name as the original file, AND will have a back up of the original file.

Update: added a call to binmode();


In reply to Re^3: Modules for xlsx files by RonW
in thread Modules for xlsx files by ravi45722

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.