Check out Creating Pivot tables from Perl, I provided a non Perl solution to this problem which may be of use to you. Populating the Excel file with your data from the database should not be a problem.
Update: If you do need help querying your database check out the DBI module along with Database Programming. Regards using Win32::OLE with Excel have a look at Using Win32::OLE and Excel - Tips and Tricks from the tutorials section of this site. If you have an issue with anything I have mentioned (or anything else related to this task) let us know.