惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - xiaoyixy

WeakHashMap相关 Java常见问题[转] MONO,原来你是水中月 Lucene 搜索引擎倒排索引原理 PXE Network Boot and Install Linux over NFS server 让进程在后台运行方法汇总 IBM terminology abstraction Perl Note(2) 亲爱的,我想念你 Perl命令行应用 深入PAM - xiaoyixy Perl Note(1) Shell技巧 GRUB awk学习 高手好习惯 Shell脚本 用户管理 - xiaoyixy re notes - xiaoyixy
用 Spreadsheet::ParseExcel处理中文excel文件
xiaoyixy · 2008-10-29 · via 博客园 - xiaoyixy

需要用的模块:
Spreadsheet::ParseExcel
Unicode::Map
IO-stringy
OLE-Storage_Lite

其中IO-stringy,OLE-Storage_Lite为运行的必要包
Spreadsheet::ParseExcel是解析Excel的必要程序
Unicode::Map为完全支持中文的字符集转换包

perldoc Spreadsheet::ParseExcel 介绍的很详细了
中文 excel 处理需要稍微多做一步
用 Spreadsheet::ParseExcel::FmtUnicode 指定 Unicode_Map 为 CP936

然后直接调用 Parse 方法就行了
返回值是一个关键数组 内容就是 excel 文件的内容

$oBook->{File} 是文件名
$oBook->{SheetCount} 是sheet个数
$oBook->{Worksheet}[0]->{Name} 是第一个 sheet 的名字
$oBook->{Worksheet}[1]->{MaxRow} 是第二个 sheet 的最大行
$oBook->{Worksheet}[2]->{Cells}[1][0]->{Val} 是第三个 sheet 的第二行第一列的值
$oBook->{Worksheet}[2]->{Cells}[1][0]->Value 是第三个 sheet 的第二行第一列的转化后的中文值

use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode;

my $oExcel = new Spreadsheet::ParseExcel;
my $oCode = "CP936";
my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map => $oCode);
my $oBook = $oExcel->Parse($excelFile, $oFmtJ);

然后根据情况 循环sheet 行/row 列/column 处理就行了.

#==================================================

Demo

#!/usr/bin/perl -w

use strict;

use Spreadsheet::ParseExcel;

use Spreadsheet::ParseExcel::FmtUnicode;

use Encode qw /from_to/;

my $oExcel = new Spreadsheet::ParseExcel;

die "You must provide a filename to $0 to be parsed as an Excel file"

  unless @ARGV;

#set for charactor

my $oFmtC = Spreadsheet::ParseExcel::FmtUnicode->new( Unicode_Map => "CP936" );

my $oBook = $oExcel->Parse( $ARGV[0], $oFmtC );

my ( $iR, $iC, $oWkS, $oWkC );

print "FILE :",  $oBook->{File},       "\n";

print "COUNT :", $oBook->{SheetCount}, "\n";

print "AUTHOR:", $oBook->{Author},     "\n"

  if defined $oBook->{Author};

for ( my $iSheet = 0 ; $iSheet < $oBook->{SheetCount} ; $iSheet++ )

{

$oWkS = $oBook->{Worksheet}[$iSheet];

print "--------- SHEET:", $oWkS->{Name}, "\n";

for ( my $iR = $oWkS->{MinRow} ;

 defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;

 $iR++ )

{

for ( my $iC = $oWkS->{MinCol} ;

 defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;

 $iC++ )

{

$oWkC = $oWkS->{Cells}[$iR][$iC];

my $val = $oWkC->Value;

$val =~ s/\s+//ig;

from_to( $val, "CP936", "utf8" );

#print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC);

print "( $iR , $iC ) =>", $val, "\n" if ($oWkC);

}

}

}