Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Parsing a .xlsx file with chinese characters

by Sithiris (Novice)
on Oct 01, 2011 at 21:45 UTC ( #929072=perlquestion: print w/replies, xml ) Need Help??
Sithiris has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am fairly new to writing perl scripts and as of yet I have not really wrote any scripts that require other modules, so my understanding of how to do this may be wrong.

Recently my work has requested that I produce a script capable of converting an xlsx file to a UTF-8 encoded flat file. Due to my lack of experience I first focused on using Spreadsheet::XLSX to actually parse a sample file and that appeared to work fine, however when I used my actual file which contained Chinese characters the characters were not displayed correctly; does anyone have any advice on how to get around this problem?

  • Comment on Parsing a .xlsx file with chinese characters

Replies are listed 'Best First'.
Re: Parsing a .xlsx file with chinese characters
by CountZero (Bishop) on Oct 02, 2011 at 07:13 UTC
    It would be most helpful if you could post a small script that actually shows the problem you describe. There are so many ways to break UTF-8 encoded files. It is even entirely possible that the problem is with the program you use to view the flat file.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      At the moment I am just using the synopsis code found on the Spreadsheet::XLSX cpan page

      use Spreadsheet::XLSX; my $excel = Spreadsheet::XLSX -> new ('sample.xlsx'); foreach my $sheet (@{$excel -> {Worksheet}}) { printf("Sheet: %s\n", $sheet->{Name}); $sheet -> {MaxRow} ||= $sheet -> {MinRow}; foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) { $sheet -> {MaxCol} ||= $sheet -> {MinCol}; foreach my $col ($sheet -> {MinCol} .. $sheet -> {Max +Col}) { my $cell = $sheet -> {Cells} [$row] [$col]; if ($cell) { printf("( %s , %s ) => %s\n", $row, $col, +$cell -> {Val}); } } } }

      This output everything fine including more standard Unicode characters however with the Chinese characters it output characters like 礼……’—œ‰™…司–天œ instead. I know it is a problem with how I am using the module as I managed to get a script working ok with Spreadsheet::Parse Excel and a .xls file however my work requires the files to be .xlsx and so I can not use this script

        I gave this a test with the exact code you gave on my machine, and it worked great!

        $ perl Sheet: Sheet1 ( 0 , 0 ) => this ( 1 , 1 ) => is ( 2 , 2 ) => a ( 3 , 3 ) => test ( 4 , 1 ) => 什麼 Sheet: Sheet2 Sheet: Sheet3 $

        (PM may convert the text (traditional Chinese "shenme" -- what) into an entity here, but it definitely worked in my xterm)

        It may be that whatever you're using to view the file isn't expecting UTF-8; or, perhaps the encoding in the XLSX itself isn't UTF-8 (but I'm not sure if that's an option in XLSX files or what!).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://929072]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2018-05-28 06:05 GMT
Find Nodes?
    Voting Booth?