Solution Found! Thank you everyone who helped, here is my final part of the script that formats XLS:
...
use strict;
use warnings;
use DBI;
use POSIX;
use File::Basename;
use Net::SFTP::Foreign;
use Text::CSV::Simple;
use Spreadsheet::WriteExcel;
...
# Generate XLS from array:
my $strExcelFilename = "filename.xls";
my $workbook = Spreadsheet::WriteExcel->new($strExcelFilename);
my $worksheet = $workbook->addworksheet("worksheetname");
my $intStartRow = 1;
my $i = $intStartRow;
my $intQueryArray = 0;
my $o = $intQueryArray;
my $intStartCol = 0;
my $u = $intStartCol;
$sth = execute_query("QUERY THAT RETURNS DATA FOR MULTIPLE COLUMNS IN
+ONE COLUMN");
my %header = (
-bold => 1,
-italic => 1,
-text_wrap => 1,
-border => 1,
);
my $format = $workbook->add_format(%header);
$worksheet->write('A1', "something", $format);
$worksheet->write('B1', "something", $format);
$worksheet->write('C1', "something", $format);
$worksheet->write('D1', "something", $format);
$worksheet->write('E1', "something", $format);
$worksheet->write('F1', "something", $format);
$worksheet->write('G1', "something", $format);
# Format and split strings from query into their corresponding columns
my @res;
while ($res[$o] = $sth->fetchrow_array()) {
my $set = $res[$o];
$set =~ s/[\(\)]//g;
my @values = split(',', $set);
foreach my $value (@values) {
$worksheet->write($i, $u, $value);
$u++
}
$u = 0;
$i++;
$o++;
}
Original Post
Hello all, I am pretty new to Perl but have coded in other languages and I have a problem. I have a data set coming back from a query that has all of the tables data in the first column. Each row in the first column has data like this:
(x,y,z,etc)
(x,y,z,etc)
(x,y,z,etc)
I need to split that data across to corresponding columns. Using "," as the delimiter between data, and removing the "()" wrapped around the results, producing something like this:
COL -> A B C D E F G
ROWS-> 1 x y z . . . .
2 x y z . . . .
3 x y z . . . .
4 x y z . . . .
My code first puts the returnset from the query into an object $sth, sets the column headers, and formats them. Then I use:
...
while (my @res = $sth->fetchrow_array()) {
$worksheet->write($i, 0, $res[0]);
$i++;
}
...
This populates my excel file's first column with the data. I need help on how to place each (x,y,z,etc) into their own columns. There are also times when there is no data and a null or blank needs to be inserted. Can I do this when I am originally populating through my array while loop? Or should I populate like have already done then add in new code to split the data across the rows? I have done some searching online and found something similar to what I need:
...
$/ = '';
my %list;
while ( my $block = <DATA> ) {
my ($tag, @list) = split /\n/, $block;
s/^-- // for @list;
$list{$tag} = \@list;
}
my $row = 0;
foreach my $tag ( @{ $list{'main'} } ) {
if ( exists $list{$tag} ) {
$worksheet->write($row, 0, $tag);
$worksheet->write_row($row++, 1, $list{$tag});
}
}
...
But even after reading the perl regular expression documentation, I am still very confused as to how to read through this code and apply it to my own problem. Any help would be greatly appreciated !
-squarez
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.