merrymonk has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Excel - adding & writing to multiple worksheets query
by technojosh (Priest) on Oct 15, 2010 at 17:30 UTC | |
by merrymonk (Hermit) on Oct 15, 2010 at 17:52 UTC | |
by ww (Archbishop) on Oct 15, 2010 at 21:53 UTC |