Dear Monks,
I am tinkering with a script for writing data fetched from MSSQL to Excel directly.
It works great so far, but my integer return values are represented as decimal strings -
like 57.00 and a char denoted by a square that I cannot depict, instead of the expected 57.
Excel warns me that the number is stored as text.
Any hints how to pass those integers correctly are greatly appreciated - and please bash my code, as I am still a Perl n00b!
#!/usr/bin/perl
use strict;
use warnings;
use Win32::ODBC;
use Spreadsheet::WriteExcel::Big;
# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel::Big->new("mssql.xls");
# Add a worksheet
my $worksheet = $workbook->addworksheet();
# Add and define a format
my $header = $workbook->addformat(); # Add a format
my $format = $workbook->addformat();
$header->set_bg_color('yellow');
$header->set_border();
$format->set_border();
# dsn needs to be configured - see Data Sources (ODBC) -> User dsn
my $dsn = "s-atlas";
my $db1;
if (!($db1 = new Win32::ODBC($dsn))){
print "Error connecting to $dsn\nError: " . Win32::ODBC::Error() .
+ "\n";
exit;
}
my $sql;
while (<>) {$sql .= $_}
if ($db1->Sql($sql)){
print "db1 SQL failed.\nError: " . $db1->Error() . "\n";
$db1->Close();
exit;
}
my @data;
my @fields;
my $col = my $row = 0;
while ($db1->FetchRow()){
$col = 0;
@data = $db1->Data();
if (!@fields){
@fields = $db1->FieldNames();
#print "$_ " for @fields, "\n";
$worksheet->write($row, $col++, $_, $header) for @fields;
}else{
#print "$_ " for @data, "\n";
$worksheet->write($row, $col++, $_, $format) for @data;
}
# catch >= 65535 rows in new worksheet
if ($row == 65535){
$worksheet = $workbook->addworksheet();
$row = $col = 0;
$worksheet->write($row, $col++, $_, $header) for @fields;
}
$row++;
}
Update: Solved - well kind of, a chop did the trick:
for (@data) {
chop;
if ($_ =~ m/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/){
print;
$worksheet->write_number($row, $col++, $_, $format);
}else{
$worksheet->write($row, $col++, $_, $format)
}
}
It prints now as expected, also in Excel.
How can I find out what the strange character was?
I only want to chop this one :)
Update 2: after
thors hint about the strange character beeing a NUL value, I was able to get rid of it with the statement:
s/\0//g for @data;
Update 3 - this should be even better:
y/\0// for @data;
Update 4: This does not work as expected though, as no interpolation is done during character translation.
I will have to stick to the substitution as mentioned with update 2.
Last Update: I finaly found the real cause for this:
When setting up your User DSN in the ODBC Data Source Administrator,
make sure you uncheck "Use regional settings when outputting currency, numbers, dates and times"!
Cheers,
jonix
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.