I want to:
a. create a new Excel spreadsheet
b. add more than one table
c. give a name to each table
d. add data to each table
e. save the spreadsheet with all the data intact.

The Perl below is my attempt to do this. It asks you for the number of sheets.
For 1 all is ok and the spreadsheet is saved.
For 2, two tables, a spreadsheet is saved but there is only data in the 2nd table and only the second table has been renamed (I am reasonably sure data has been written
to the first table because with a ‘larger’ case I can see it being written).
For 3, it fails to add the third table
Can a wiser Monk explain how to cure these problems?
use strict ; use OLE; use Win32::OLE::Const "Microsoft Excel"; my ($excel, $workbook, $sheet, $j, $row, $range, $cell_str, $content, +$contentb, , $contentc, $width_found, $id, $test_cell_idg ); my ($worktable_name, $jwk, $jr, $sheets_total, $new_wk); # request the number of worktables print "Enter the number of worktables "; $sheets_total = <STDIN>; #___ DEFINE EXCEL $excel = CreateObject OLE "Excel.Application"; #___ MAKE EXCEL VISIBLE $excel -> {Visible} = 1; for($jwk = 1; $jwk <= $sheets_total; $jwk ++) { #___ ADD NEW WORKBOOK $workbook = $excel -> Workbooks -> Add; # $sheet = $workbook -> Worksheets("Sheet 1"); if($jwk == 1) { $sheet = $workbook -> Worksheets("Sheet1"); $sheet -> Activate; #___ ADD NEW WORKSHEET } else { $workbook -> Worksheets -> Add({After => $workbook -> Workshee +ts($workbook -> Worksheets -> {Count})}); $new_wk = "Sheet" . $jwk; print($new_wk); $sheet = $workbook -> Worksheets($new_wk); $sheet -> Activate; #___ CHANGE WORKSHEET NAME } $worktable_name = "worktable_name-" . $jwk; print "\njwk <$jwk> sheet name <$worktable_name> workbook <$workbo +ok> sheet <$sheet>\n"; $sheet -> {Name} = $worktable_name; for ($jr = 1; $jr <= 10; $jr++) { $range = 'A' . $jr; $sheet -> Range($range) -> {Value} = $jr . '-' . $jwk; } } $excel -> {DisplayAlerts} = 0; # This turns off the "This file already + exists" message. $workbook -> SaveAs ("c:\\n-sheets-test"); $excel -> Quit;

In reply to Excel - adding & writing to multiple worksheets query by merrymonk

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.