Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > printing to xls with Unicode encoding character

Reply
Thread Tools

printing to xls with Unicode encoding character

 
 
sam
Guest
Posts: n/a
 
      03-12-2005
Hi,

I want to convert the result from the perl script to excel file. The
result contains chinese characters. What can I use to do handle this
function in Perl?

Thanks
Sam
 
Reply With Quote
 
 
 
 
peter pilsl
Guest
Posts: n/a
 
      03-12-2005
sam wrote:
> Hi,
>
> I want to convert the result from the perl script to excel file. The
> result contains chinese characters. What can I use to do handle this
> function in Perl?
>


What is the exact problem?

I use a script here that reads unicode from a database and prints out
CSV-data on a utf8-terminal. Then I import this CSV in OpenOffice
(cause I run linux) and specifiy UTF8 as encoding and I've all
characters just fine.
Then I saved as xls and mailed to the translators and everything was
fine. (I didnt have chinese, but I had a lot of german umlauts, spanish,
slovensky, hungarian, french and some eastern languages I dont reckognize

The code of my script (using a private module and accessing parts of its
internal datastructure, but you get the idea !!) is:

I think the locate-part is not important, cause no collating takes place
in this script, but its my standard-header when dealing with unicode.
Maybe the utf8::encode is what you are missing in your script?

----------------------------
#!/usr/bin/perl -w

use strict;
use goldfisch::tt2;
use POSIX qw(locale_h);
use locale;
setlocale(LC_COLLATE, "de_AT.UTF-8");
$|=1;

my $db=$ARGV[0];
my $lang=$ARGV[1];

my $var=goldfisch::tt2->new();

foreach (keys %{$var->{g_ptr}->{db}->{$db}->{pdata}->{de}}) {
my $x=$var->{g_ptr}->{db}->{$db}->{pdata}->{de}->{$_};
utf8::encode($x);
print $_,"\t",$x,"\n";
}
----------------------------

best,
peter


--
http://www2.goldfisch.at/know_list
 
Reply With Quote
 
 
 
 
sam
Guest
Posts: n/a
 
      03-13-2005
peter pilsl wrote:
> sam wrote:
>
>> Hi,
>>
>> I want to convert the result from the perl script to excel file. The
>> result contains chinese characters. What can I use to do handle this
>> function in Perl?
>>

>
> What is the exact problem?
>

I use Spreadsheet::WriteExcel save the result from print statement to an
external file, then opened by MS Excel. But the characters are not
readable by chinese MS Excel. They are all printed as garbage in Excel.
But if I use mouse highlight the chinese from the webpage and paste it
to Excel, it is printed as chinese.


> I use a script here that reads unicode from a database and prints out
> CSV-data on a utf8-terminal. Then I import this CSV in OpenOffice
> (cause I run linux) and specifiy UTF8 as encoding and I've all
> characters just fine.
> Then I saved as xls and mailed to the translators and everything was
> fine. (I didnt have chinese, but I had a lot of german umlauts, spanish,
> slovensky, hungarian, french and some eastern languages I dont
> reckognize
>
> The code of my script (using a private module and accessing parts of its
> internal datastructure, but you get the idea !!) is:
>
> I think the locate-part is not important, cause no collating takes place
> in this script, but its my standard-header when dealing with unicode.
> Maybe the utf8::encode is what you are missing in your script?
>

Thanks for the code. May I ask what is goldfisch::tt2?

Thanks
Sam

> ----------------------------
> #!/usr/bin/perl -w
>
> use strict;
> use goldfisch::tt2;
> use POSIX qw(locale_h);
> use locale;
> setlocale(LC_COLLATE, "de_AT.UTF-8");
> $|=1;
>
> my $db=$ARGV[0];
> my $lang=$ARGV[1];
>
> my $var=goldfisch::tt2->new();
>
> foreach (keys %{$var->{g_ptr}->{db}->{$db}->{pdata}->{de}}) {
> my $x=$var->{g_ptr}->{db}->{$db}->{pdata}->{de}->{$_};
> utf8::encode($x);
> print $_,"\t",$x,"\n";
> }
> ----------------------------
>
> best,
> peter
>
>

 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      03-13-2005
On Sun, 13 Mar 2005, sam wrote:

> I use Spreadsheet::WriteExcel save the result from print statement
> to an external file, then opened by MS Excel.


You're doing this from a Windows implementation of Perl?
Are you using the -C option on the command line (or the equivalent
environment setting) to get wide system calls?

> But the characters are not readable by chinese MS Excel. They are
> all printed as garbage in Excel.


I understand what you're saying, although some details (hex dump of
the bytes, with some mention of what they were supposed to be) would
be more useful for a diagnosis than "garbage".

> But if I use mouse highlight the chinese from the webpage


"the webpage"? What is this "webpage", and where can we see it,
please?

 
Reply With Quote
 
jmcnamara@cpan.org
Guest
Posts: n/a
 
      03-13-2005
The more recent versions of Spreadsheet::WriteExcel support utf8
transparently with Perl 5.8. For older versions of perl you can use
UTF-16.

See this example of Chinese in Big5 format from the standard
Spreadsheet::WriteExcel distro:

http://search.cpan.org/src/JMCNAMARA...nicode_big5.pl

http://search.cpan.org/src/JMCNAMARA...icode_big5.txt

There are also several other encoding examples in the same distro.

John.
--

 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      03-13-2005
On Sun, 13 Mar 2005 http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> The more recent versions of Spreadsheet::WriteExcel support utf8
> transparently with Perl 5.8. For older versions of perl you can use
> UTF-16.
>
> See this example of Chinese in Big5 format from the standard
> Spreadsheet::WriteExcel distro:
>
> http://search.cpan.org/src/JMCNAMARA...nicode_big5.pl


Thanks for posting this. Just for my own edification - if this is run
on Windows, *does* it need the -C wide system calls option, or was
that a red-herring?
 
Reply With Quote
 
sam
Guest
Posts: n/a
 
      03-14-2005
(E-Mail Removed) wrote:
> The more recent versions of Spreadsheet::WriteExcel support utf8
> transparently with Perl 5.8. For older versions of perl you can use
> UTF-16.
>
> See this example of Chinese in Big5 format from the standard
> Spreadsheet::WriteExcel distro:
>
> http://search.cpan.org/src/JMCNAMARA...nicode_big5.pl
>
> http://search.cpan.org/src/JMCNAMARA...icode_big5.txt
>
> There are also several other encoding examples in the same distro.
>
> John.
> --
>

Hi John, thank you very much for this info, this bit of coding is
aparently missing from my perl code.

Thanks
Sam
 
Reply With Quote
 
jmcnamara@cpan.org
Guest
Posts: n/a
 
      03-14-2005
> if this is run on Windows, *does* it need the -C wide system calls
> option, or was that a red-herring?


The program will work on Windows without -C. So I guess that it is a
red-herring (at least on more recent perls):

>From perlrun:

In Perls earlier than 5.8.1 the -C switch was a Win32-only
switch that enabled the use of Unicode-aware "wide system
call" Win32 APIs. This feature was practically unused,
however, and the command line switch was therefore "recycled".

http://perldoc.perldrunks.org/perlru...mmand-Switches

John.
--

 
Reply With Quote
 
peter pilsl
Guest
Posts: n/a
 
      03-14-2005
sam wrote:
>>

> Thanks for the code. May I ask what is goldfisch::tt2?
>


its a template-engine based on TemplateToolkit2 that runs under mod_perl
and provides many project-specific data (that is stored in a
sql-database and read then the module is loaded by apache on startup and
then shared between all the threads). Some of this data needs to be
exported to excel for translation and so I use goldfisch::tt2 for
reading from the database, cause its very convinient.

The module is very project-specific and strictly private
you can take a sneak-preview to the whole project at

http://tt2.adulteducation.at/6m/
or the almost ready english version at
http://tt2.adulteducation.at/6m/en

comments welcome and completely OT here



best,
peter



--
http://www2.goldfisch.at/know_list
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      03-14-2005
On Mon, 14 Mar 2005 (E-Mail Removed) wrote:

> The program will work on Windows without -C. So I guess that it is a
> red-herring (at least on more recent perls):


Thanks - then I offer my apologies to the questioner for giving
a misleading answer.

> From perlrun:
> In Perls earlier than 5.8.1 the -C switch was a Win32-only
> switch that enabled the use of Unicode-aware "wide system
> call" Win32 APIs. This feature was practically unused,
> however, and the command line switch was therefore "recycled".


Oh yes, so it does. But that prompts the inevitable question as to
how Win32 now controls the choice of Win32 APIs ...?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resolving unicode escapes to unicode character Tyler Ruby 1 07-29-2011 01:47 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
Getting unicode escape sequence from unicode character? Kenneth McDonald Python 1 12-27-2006 10:27 PM
character encoding +missing character sequence raavi Java 2 03-02-2006 05:01 AM
Unicode character in Japanese printing, but only square =?Utf-8?B?U3BlbmNlciBILiBQcnVl?= ASP .Net 4 04-01-2005 07:57 PM



Advertisments