Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Pivot data in Excel via Win32Ole

by cocl04 (Sexton)
on Nov 20, 2009 at 22:36 UTC ( [id://808540]=perlquestion: print w/replies, xml ) Need Help??

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 (Curate) on Nov 21, 2009 at 15:52 UTC
    With my queried data, my range will change each time
    I see no problem with that, you can calculate the range. You have the number of rows and cols returned by the DB query.

    If I didn't understand correctly, than you have to provide a working example of what you do and the expected results.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://808540]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found