#!/usr/bin/perl # # Create a spreadsheet from an SQL query. # use strict; use warnings; use DBI; use Spreadsheet::WriteExcel; my $DSN = shift; my $usr = shift; my $pwd = shift or die "Missing argument(s)!"; # Attach to database and run query my $DB=DBI->connect($DSN,$usr,$pwd); my $ST=$DB->prepare(< 100 EOSQL $ST->execute; # Create a new workbook and worksheet my $xWB = Spreadsheet::WriteExcel->new('MySpreadsheet.xls'); my $xWS = $xWB->add_worksheet("Results"); # Add column headers (use column headers from query results) my $R=0; my $C=0; $xWS->write($R, $C++, $_) for @{$ST->{NAME}}; # Read the query results and write them into the spreadsheet while (my $ar=$ST->fetchrow_arrayref) { ++$R; $C=0; $xWS->write($R, $C++, $_) for @$ar; }