After doing some testing on this, it looks like MS uses latin1 (or something very close) internally.
I used a slightly modified version of the example script from Spreadsheet::ParseExcel, replacing the "print" with a database insert.
When processing some data from a spreadsheet into a PostgreSQL database, cells with symbols such as 0xae (ascii 92, the "registered" symbol, ®), I constantly came up against the database error:
DBD::Pg::db do failed: ERROR: invalid byte sequence for encoding "UTF
+8": 0xae
After setting the client encoding to latin1 (keeping the database at UTF8):
$dbh->do("set client_encoding to latin1");
the data went in OK.
If there is a different/better way to process this, I'd be interested to know.
Update: there is a better way...
#!/usr/bin/perl
use warnings;
use strict;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse('Book1.xls');
binmode(STDOUT, ":utf8");
foreach my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
foreach my $row ( 1 .. $row_max ) {
foreach my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
next unless defined($col_mapping{$col});
my $value = $cell->value();
utf8::upgrade($value);
... store_in_database($value); ...
}
}
}
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, 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, 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.
|
|