Perl reads Excel files and solves Chinese garbled problems

Source: Internet
Author: User



Use spreadsheet in CPAN::P arseexcel module to read the contents of the Excel file, when encountering Chinese garbled problem, use spreadsheet::P arseexcel::fmtunicode module recoding, When assigning Chinese to a variable, the Encode module is decoded by GB2312.


use strict;  
use Spreadsheet::ParseExcel;  
use Spreadsheet::ParseExcel::FmtUnicode;
use Encode;  
   
my $oFmtC=Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map=>"CP936");  
my $parser=Spreadsheet::ParseExcel->new();  
my $workbook=$parser->parse(‘test.xls‘,$oFmtC);  
   
if (!defined $workbook){  
    die $parser->error(),".\n";  
}  
   
for my $worksheet ($workbook->worksheets()){  
    my ($row_min,$row_max)=$worksheet->row_range();  
    my ($col_min,$col_max)=$worksheet->col_range();  
    for (my $row=$row_min;$row<=$row_max;$row++){ 
        for (my $col=$col_min;$col<=$col_max;$col++){  
            my $cell = $worksheet->get_cell($row,$col); 
            $a=$cell->value();  
            $a=decode("gb2312",$a);
            #do something 
        }  
    }  





This article is from "Ruyi Spirit Pro" blog, please be sure to keep this source http://417722381.blog.51cto.com/7856838/1880722



Perl reads Excel files and solves Chinese garbled problems


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.