Hi, I have a question regarding the use of writing directly to a spreadsheet (using Spreadsheet::WriteExcel). At the moment I have DBI/DBD function which extracts data to an intermediate file. The file is opened and some parsing is done before applying to a spreadhseet (writing to an xls file). I'd like to try and get rid of the intermediate step of writing to the file by writing directly to the spreadhseet (.xls) but am not sure how to do this ?
Here's a snippet of what I do currently .

# Open the output file for the cmdtxt audit trail selected open TXT,">$txt" or die "Can't create $txt : $!\n"; # execute the sql statement $sth=$dbh->prepare("@sql"); $sth->execute; while ($data = $sth->fetchrow_arrayref()) { chomp $data->[13]; $data->[13] =~ tr/\t//; # get rid of the tabs if ( $data->[4] > 1) { print TXT "$data->[13]"; } else { print TXT "\nBOR $data->[3]|$data->[2]|$data->[9]|$data->[10 +]|$data->[4]|$data->[13]"; } } $sth->finish; $dbh->disconnect; close TXT; # Create the spreadsheet and set up formats $workbook = Spreadsheet::WriteExcel->new("my.xls"); # Set up various workbook formats here.... # Create some worksheets $sheet1 = $workbook->add_worksheet('Tools'); $sheet2 = $workbook->add_worksheet('Client'); $sheet3 = $workbook->add_worksheet('Fares'); # create some column headings for each worksheet here... # Set up some lookup lists using Regexp::List here... # Read the intermediate file and populate the worksheets $/="BOR "; open TXT,"<$txt" or die "Can't open $txt : $!\n"; while( <TXT> ) { chomp; if( m[/\-] ) { $_ .= <TXT> until m[\-/]; s[ \s? / \- .+? \- / \s? ][]smgx;# cut out some unwanted txt chomp; } my @rec = split /\|/; $aa = $rec[0]; $bb = $rec[1]; $cc = $rec[2]; $dd = $rec[3]; # $rec[4] isn't used here $ee = $rec[5]; if ($ee =~ /$regexp1/) { popxls($sheet1,$normal,$darow); # populate worksheet 1 $darow++; } elsif ($ee =~ /$regexp2/) { popxls($sheet2,$normal,$arow); # populate worksheet 2 $arow++; } elsif ($ee =~ /$regexp3/) { popxls($sheet3,$normal,$srow); # populate worksheet3 $srow++; } } close TXT; sub popxls { $col = 0; my $type = $_[0]; my $fmt = $_[1]; my $row = $_[2]; $type->write($row, $col, "$ee", $fmt); $col++; $cmdtype->write($row, $col, $aa, $fmt); $col++; $cmdtype->write($row, $col, "$bb", $fmt); $col++; $cmdtype->write($row, $col, "$cc", $fmt); $col++; $cmdtype->write($row, $col, "$dd", $fmt); }
It seems inefficient to me to write to an intermediate file then reopen this file and do some more processing on it before writing to the spreadsheet file. Can this be done in memory instead ? if so how ?

Thanks in advance


In reply to Direct to spreadsheet by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.