cocl04 has asked for the wisdom of the Perl Monks concerning the following question:
Background:
I need to generate a report with pivoted data. My current process selects data from an Oracle query via the DBI module and loads that data into excel. I normally use the Spreadsheet::WriteExcel module however, it does not have pivot capabilities. So, John M. suggested that I use Win32-OLE.
i.e.
my $connection = DBI->connect('dbi:Oracle:xxxxx','xxxxx','xxxxx',{ AutoCommit => 0, RaiseError => 1, PrintError => 1, }) || die "Database connection not made: $DBI::errstr";
#select statement
my $stmt1 = "select * from final_data_set_blu_ray order by weekend_date";
#prepare statement
$query1 = $connection->prepare($stmt1);
#execute
$query1->execute() or die $connection->errstr;
#load data into an array reference.
my $a_row = $query1->fetchall_arrayref();
Once I have the data in an array reference, I can assign the data to an array or whatever to load it to excel. At this point, I can’t find any Win32::OLE logic / syntax that provides a clear example of taking queried data with an unknown range and creating a pivot table in excel. All of the examples I find have a pre-determined range for the spreadsheet like the example below:
i.e.
# Write all the data at once...
$rng = $xlBook->ActiveSheet->Range("A1:C7"); $rng->{Value} = $mydata;
# Create a PivotTable for the data...
$tbl = $xlBook->ActiveSheet->PivotTableWizard(1, $rng, "", "MyPivotTab +le");
Using the above logic, is there a way to take the data from my query / DBI and assign it to a value like so “$rng->{Value} = $mydata;” to create a pivot table? With my queried data, my range will change each time. Can you give any tips to get around this?
I have worked on this for several days and I cannot find a solution. I wished that Spreadsheet::Write Excel had a pivot solution. Any help or direction will be greatly appreciated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pivot data in Excel via Win32Ole
by stefbv (Priest) on Nov 21, 2009 at 15:52 UTC |