perl_n00b has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, I'm afraid I'm stuck and in need of assistance!

I currently have my code writing into 2 .csv files but I would like to change that. Is there a way I can have it write into a separate worksheet on the same workbook as the input file ($excel)?

Thanks in advance!

Here's my code...
use warnings; use Spreadsheet::XLSX; my $outfile = "C:\\Users\\Owner\\Documents\\Herping\\Data\\Moon_Illum. +csv"; open my $ofh, ">", $outfile or die "cannot open $outfile: $!\n"; print $ofh ""; my $outfile2 = "C:\\Users\\Owner\\Documents\\Herping\\Data\\Moon_Phase +.csv"; open my $ofh2, ">", $outfile2 or die "cannot open $outfile2: $!\n"; print $ofh2 ""; my $excel = Spreadsheet::XLSX->new('C:\\Users\\Owner\\Documents\\Herpi +ng\\Snake_Data_2010.xlsm'); my $col = 41,; # Read col L , $col has to be a number foreach my $sheet (@{$excel->{Worksheet}}) { $sheet->{MaxRow} ||= $sheet->{MinRow}; foreach my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) { my $cell = $sheet->{Cells}[$row][$col]; if ($cell) { my @value = $cell->{Val}; use Astro::MoonPhase; foreach my $i (@value) { my $outfile = "C:\\Users\\Owner\\Documents\\Herping\\Data\ +\Moon_Illum.csv"; open my $ofh, ">>", $outfile or die "cannot open $outfile: + $!\n"; if ($i =~ /^\d/) { (my $MoonPhase, my $MoonIllum) = Astro::MoonPhase: +:phase(($i-24107)*60*60*24+25200); push(my @Illum, $MoonIllum); print $ofh "@Illum\n"; + } } } } } foreach my $sheet (@{$excel->{Worksheet}}) { $sheet->{MaxRow} ||= $sheet->{MinRow}; foreach my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) { my $cell = $sheet->{Cells}[$row][$col]; if ($cell) { my @value = $cell->{Val}; use Astro::MoonPhase; foreach my $i (@value) { my $outfile = "C:\\Users\\Owner\\Documents\\Herping\\Data\ +\Moon_Phase.csv"; open my $ofh, ">>", $outfile or die "cannot open $outfile: + $!\n"; if ($i =~ /^\d/) { + (my $MoonPhase, my $MoonIllum) = Astro::MoonPhase: +:phase(($i-24107)*60*60*24+25200); push(my @Phase, $MoonPhase); print $ofh "@Phase\n"; + } } } } }

Replies are listed 'Best First'.
Re: Writing into Excel Worksheet
by desemondo (Hermit) on Mar 01, 2010 at 03:25 UTC
    Is there a way I can have it write into a separate worksheet on the same workbook as the input file ($excel)
    yes. Spreadsheet::WriteExcel See method: add_worksheet()

    Ps. If you haven't already, bookmark search.cpan.org
Re: Writing into Excel Worksheet
by jrsimmon (Hermit) on Mar 01, 2010 at 20:14 UTC
    You might also consider Win32::OLE for a finer level of control over excel, if required.