用perl加载带中文的excel表格(xlsx)
Posted 生信开发者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用perl加载带中文的excel表格(xlsx)相关的知识,希望对你有一定的参考价值。
use Spreadsheet::XLSX;
use Unicode::UTF8simple;
my %hash=();
loadCHPO($infileCHPO,\%hash);
sub loadCHPO{
my ($infile,$hashRef)=@_;
my $uref =new Unicode::UTF8simple;
$infile=$uref->fromUTF8('gb2312',$infile);
my $excel = Spreadsheet::XLSX -> new ($infile);
my @title = ("ClassCN","ID","nameEN","nameCN","defEN","defCN");
foreach my $sheet (@{$excel->{Worksheet}}) {
# printf("Sheet: %s\n", $sheet->{Name});
my ( $row_min, $row_max ) = $sheet->row_range();
my ( $col_min, $col_max ) = $sheet->col_range();
# print $sheet->{MaxRow},"\t",$sheet->{MaxCol},"\t",$row_max ,"\t",$col_max,"\n";
foreach my $row ($row_min .. $row_max) {
next if ($row==$row_min);
my $cell = $sheet->{Cells}[$row][1];
if ($cell ) {
# $hashRef->{$sheet->{Cells}[$row][1]->{Val}} =
# {map {$title[$_] => $sheet->{Cells}[$row][$_]->{Val}} ($col_min .. $col_max)};
$hashRef->{$cell->{Val}} = {map {$title[$_] =>
$sheet->{Cells}[$row][$_] && $sheet->{Cells}[$row][$_]->{Val}=~/([\x80-\xFF][\x80-\xFF])/ ?
decode("utf8",$sheet->{Cells}[$row][$_]->{Val}) : $sheet->{Cells}[$row][$_]->{Val}
} ($col_min .. $col_max)};
}
}
}
print STDERR "Done load $infile\n";
}
以上是关于用perl加载带中文的excel表格(xlsx)的主要内容,如果未能解决你的问题,请参考以下文章