emadmahou has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use DBI; use Spreadsheet::WriteExcel; my $listId; my $p_filename; $listId = shift; $p_filename = shift; use constant C_HEADING => 0; use constant C_WIDTH => 1; my @columns = ( ['AgentID', 10 ], ['Question1', 20 ], ['Question2', 20 ], ['Question3', 11 ], ['Question4', 11 ], ['Question5', 24 ], ['Question6', 11 ], ['Question7', 22 ], ['Question8', 22 ] ); my $workbook = Spreadsheet::WriteExcel->new($p_filename); my $sheet = $workbook->add_worksheet("Data"); my $default_format = $workbook->add_format(num_format => '@'); $defau +lt_format->set_font('Verdana'); $default_format->set_border(1); my $bold_format = $workbook->add_format(); $bold_format->set_font( +'Verdana'); $bold_format->set_bold(); $bold_format->set_border(1); $sheet->write(0,$_,$columns[$_]->[C_HEADING], $bold_format) for (0..$# +columns); $sheet->set_column($_, $_, $columns[$_]->[C_WIDTH]) for (0..$#columns) +; my $dbh = DBI->connect("DBI:ODBC:Driver={SQL Server};Server=server;Dat +abase=database;Trusted_Connection=Yes") or die("\n\nCONNECT ERROR:\n\ +n$DBI::errstr"); my $sth = $dbh->prepare(<<EOQ SELECT Custom1 AS AgentID, Custom13 AS Question1, Custom14 AS Question +2, Custom15 AS Question3, Custom16 AS Question4, Custom17 AS Question +5, Custom18 AS Question6, Custom19 AS Question7, Custom20 AS Question +8 FROM [purl].[dbo].[MailFiles] WHERE ListId='$listId' AND Completed ='1' EOQ ); $sth->execute(); my $i = 1; my $row; while ( $row = $sth->fetchrow_arrayref ) { $sheet->write_string($i,$_,$row->[$_], $default_format) for (0..$# +$row); $i++; } $sheet->activate(); exit;
I wrote this code to connect to a database and retrieve 9 columns my columns will have this values AgentID = 1234 Question1 = strongly agree Question2 = agree Question3 = strongly disagree Question4 = etc Question5 =etc Question6 =etc Question7 =etc Question8 = etc
I need to change the value of the questions from string to numeric
Strongly Agree – 5 Agree – 4 Neutral – 3 Disagree – 2 Strongly Disagree -1
then write it back to the excel file
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trying to manipulate data
by 1nickt (Canon) on Jan 13, 2016 at 17:52 UTC | |
by emadmahou (Acolyte) on Jan 13, 2016 at 18:58 UTC | |
by 1nickt (Canon) on Jan 13, 2016 at 19:02 UTC |