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

Hi Monks

My requirement is to insert graphs in multiple sheets in excel. But i am getting Graph with datas from sheet1 in both sheet1 and sheet2.

Attaching my code below

use Win32::OLE; $xlApp = Win32::OLE->new('Excel.Application'); $xlApp->{Visible} = 1; $xlBook = $xlApp->Workbooks->Add; ############################# FIRST CHART [SHEET 1]################### +############## my $Sheet = $xlBook->Worksheets("Sheet1"); $Sheet->{Name} = "SERVER NAME I"; # Get data from a txt file open (STAT, "memorystatus.txt") or die "memorystatus.txt not available +"; @data = <STAT>; $y = 1; foreach $line (@data) { chomp($line); $data[$i] = $line; ($day, $status) = split (/,/, $line); my $aRange = 'a'.$y; my $bRange = 'b'.$y; $Sheet->Range($aRange)->{Value} = $day; $Sheet->Range($bRange)->{Value} = $status; $y++; } $chart = $xlBook->Charts->Add; $chart->SetSourceData($Sheet, 2); $chart->{HasTitle} = 1; $chart->ChartTitle->{Text} = "SERVER NAME I"; $chart->Location(3, "SERVER NAME I"); ############################# SECOND CHART [SHEET 2]################## +############### # Second chart my $Sheet = $xlBook->Worksheets("Sheet2"); $Sheet->{Name} = "SERVER NAME II"; # Get data from a txt file open (STAT, "hourly_status.txt") or die "hourly_status.txt not availab +le"; @data = <STAT>; $y = 1; foreach $line (@data) { chomp($line); $data[$i] = $line; ($day, $status) = split (/,/, $line); my $aRange = 'a'.$y; my $bRange = 'b'.$y; $Sheet->Range($aRange)->{Value} = $day; $Sheet->Range($bRange)->{Value} = $status; $y++; } $chart2 = $xlBook->Charts->Add; $chart2->SetSourceData($Sheet, 2); $chart2->{HasTitle} = 1; $chart2->ChartTitle->{Text} = "SERVER NAME II"; $chart2->{Name} = "SERVER NAME II"; $chart2->Location(3, "SERVER NAME II");

Both the input file will look something like tis

DAY, STATUS SUNDAY, 5.25 MONDAY, 23.00 TUESDAY, 16.25 WEDNESDAY, 5.00 THURSDAY, 31.00 FRIDAY, 2.25 SATURDAY, 2.25

Can anyone help me on this

Thanks in advance

Replies are listed 'Best First'.
Re: Adding multiple graphs in Excel
by bluescreen (Friar) on Jun 21, 2010 at 14:24 UTC

    I think Win32::OLE - Excel chart location might help you with your problem basically you have to specify a location for the charts

    Said that I'm going to take the opportunity to give you an advice, if I can stay away from Platform specific solutions like Win32::OLE I would because if you ever want to migrate to another platform you'll have a big headache ( don't think you'd never have to ) and probably niether API docs are easy to find for close source apps like Excel nor contact the developers in case something goes wrong.

    There is a fantastic module in CPAN Spreasheet::WriteExcel with a lot of features including charts. Bonus: You'll get much more support here (multi-platform) and you'll probably save hundred MBs as you are not instantiating an Excel.Application :D

    Finally I'd recommend you start including use strict; and use warnings; in your code to help you diagnosing problems

      Hey guys, I din activate the sheet, that was the problem.

      @bluescreen, I'll try with Spreadsheet::WriteExcel, and sure i'll use strict and warnings. Thanks for the advice.