- 论坛徽章:
- 0
|
名称
Spreadsheet::ParseExcel - 从Excel文件获取信息
接要
新的接口
use strict;
use Spreadsheet::ParseExcel;
my $excel = Spreadsheet::ParseExcel::Workbook->Parse($file);
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->{MaxCol}) {
my $cell = $sheet->{Cells}[$row][$col];
if ($cell) {
printf ("( %s , %s ) => %s\n", $row, $col, $cell->{Val});
# (译者注:$cell->{Val}取得的是单元的原始值。
# 如果要取得真正的UTF8编码的内容,应用$cell->Value方法。)
}
}
}
}
旧的接口
use strict;
use Spreadsheet::ParseExcel;
my $oExcel = Spreadsheet::ParseExcel->new;
#1.1 Normal Excel97
my $oBook = $oExcel->Parse('Excel/Test97.xls');
my($iR, $iC, $oWkS, $oWkC);
print "FILE :", $oBook->{File} , "\n";
print "COUNT :", $oBook->{SheetCount} , "\n";
print "AUTHOR:", $oBook->{Author} , "\n";
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];
print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC); # Formatted Value
print "( $iR , $iC ) =>", $oWkC->{Val}, "\n" if($oWkC); # Original Value
}
}
}
描述
Spreadsheet::ParseExcel模块使你可以从Excel95、Excel97、Excel2000文件里获取信息。
函数
new
$oExcel = Spreadsheet::ParseExcel->new( [ CellHandler => \&subCellHandler, NotSetCell => undef | 1, ]);
构造。
CellHandler (可选)
指定当一个单元被探测时的回调函数。
subCellHandler有如下参数:
sub subCellHandler ($oBook, $iSheet, $iRow, $iCol, $oCell);
注意:工作表的属性可能不完整。这个函数基本上是以行和列的顺序被调用。注意是基本上,不是完全。
NotSetCell ((可选)
指定是否设置工作簿对象的单元的值。
Parse
$oWorkbook = $oParse->Parse($sFileName [, $oFmt]);
返回工作簿对象。如果发生了错误,返回undef。
$sFileName
要解析的文件的名字。
从0.12(带OLE::Storage_Lite v.0.06),文件内容的标量引用(如\$sBuff)或者IO::Handle对象(包括IO::File等)也可以。
$oFmt
用来格式单元的值的格式化类。
ColorIdxToRGB
$sRGB = $oParse->ColorIdxToRGB($iColorIdx);
ColorIdxToRGB返回表示色彩索引的RGB字符串。RGB字符串有6个字符,描述了RGB的16进制值。(比如,红色是'FF000000')
Workbook
Spreadsheet::ParseExcel::Workbook
工作簿类有这些方法:
Parse
(类方法):同于Spreadsheet::ParseExcel
Worksheet
$oWorksheet = $oBook->Worksheet($sName);
Worksheet返回一个有名字的工作表对象或者undef。如果没有名为$sName并且$sName只包括数字,它将返回一个那个位置的工作表对象。
工作簿类有这些方法:
File
文件的名字
Author
文件的作者
Flg1904
如果有这个标志,表示文件自从1904年的日期。
Version
文件的版本
SheetCount
工作簿中的工作表数目
Worksheet[SheetNo]
Worksheet类的列表
PrintArea[SheetNo]
PrintArea列表引用的列表。
一个PrintArea是:[StartRow, StartColumn, EndRow, EndColumn]
PrintTitle[SheetNo]
PrintTitle哈希引用的列表。
每一个PrintTitle是:{ Row => [ StartRow, EndRow ], Column => [ StartColumn, EndColumn ] }
Worksheet
Spreadsheet::ParseExcel::Worksheet
工作表类有这些方法:
Cell ( ROW, COL )
返回在ROW行COL列的单元对象。如果不存在,返回undef。
RowRange ()
如果没有最大行数小于最小行数,返回包含工作表中的行数的最小值与最大值这两个元素的列表。
ColRange ()
如果没有最大列数小于最小列数,返回包含工作表中的列数的最小值与最大值这两个元素的列表。
工作表类有这些属性:
Name
工作表的名称
DefRowHeight
行的默认高度
DefColWidth
列的默认宽度
RowHeight[Row]
行高的列表
ColWidth[Col]
列宽的列表(undef表示DefColWidth)
Cells[Row][Col]
工作表中的单元信息的列表
Landscape
横向(0)打印还是纵向。
Scale
打印比率。
FitWidth
满行的页的数目。
FitHeight
满列的页的数目。
PageFit
Print with fit (or not).
PaperSize
纸张大小。值如下列:
Letter 1, LetterSmall 2, Tabloid 3 ,
Ledger 4, Legal 5, Statement 6 ,
Executive 7, A3 8, A4 9 ,
A4Small 10, A5 11, B4 12 ,
B5 13, Folio 14, Quarto 15 ,
10x14 16, 11x17 17, Note 18 ,
Envelope9 19, Envelope10 20, Envelope11 21 ,
Envelope12 22, Envelope14 23, Csheet 24 ,
Dsheet 25, Esheet 26, EnvelopeDL 27 ,
EnvelopeC5 28, EnvelopeC3 29, EnvelopeC4 30 ,
EnvelopeC6 31, EnvelopeC65 32, EnvelopeB4 33 ,
EnvelopeB5 34, EnvelopeB6 35, EnvelopeItaly 36 ,
EnvelopeMonarch 37, EnvelopePersonal 38, FanfoldUS 39 ,
FanfoldStdGerman 40, FanfoldLegalGerman 41, User 256
PageStart
开始页数。
UsePage
是否使用自定义的开始页。
LeftMergin, RightMergin, TopMergin, BottomMergin, HeaderMergin, FooterMergin
左、右、上下、头、尾的留白。
HCenter
是否横向中间打印。
VCenter
是否纵向中间打印。
Header
打印标题的内容。请看Excel的帮助。
Footer
打印页尾的内容。请看Excel的帮助。
PrintGrid
是否打印网格线
PrintHeaders
是否打印标题
NoColor
是否打印黑白模式。
Draft
是否打印草稿。
Notes
是否打印备注。
LeftToRight
从左到右(0)或者从上到下(1)打印。
HPageBreak
横向断页的列表的引用。
VPageBreak
纵向断页的列表的引用。
MergedArea
合并域的列表的引用。每一个合并域为:[StartRow, StartColumn, EndRow, EndColumn]
Cell
Spreadsheet::ParseExcel::Cell
单元类有这些属性:
Value
单元的格式化的值(注:此非属性,乃方法也)
Val
单元的原始值。
Type
单元的类型(文本、数字、日期)
Code
单元的字符编码(undef、'ucs2'、'_native_')undef表示单元看上去是ascii字符的。'_native_'表示单元看上去是'sjis'编码或者其它类似的。
#format
格式这个单元。
Merged
这个单元是否是合并的。
Rich
每一个字符的字体信息的列表引用。
每一个入口为:[ Start Position, Font Object]
请于sample/dmpExR.pl获取更多信息
#format
Spreadsheet::ParseExcel::Format
格式类有这些属性:
Font
本字体的字体对象。
AlignH
横向排列。
0:(标准),1:左,2:中,3:右,
4:填充,5:justify,7:等于空白
注意:6可能是合并的但是好像不工作。
AlignV
纵向排列。
0:上,1:纵向中间,2:下,3:纵向平铺,4:纵下等于空白
Indent
数值对齐
Wrap
是否卷绕。
Shrink
是否缩放显示
Rotate
在Excel97、2000:字符串的翻转角度。在Excel95或者以前:0:不翻转,1:上下翻转,2:匿时针90度,3:顺时针90度
JustLast
是否只有最后。我从来没有见过这个属性。
ReadDir
读入的方向。
BdrStyle
边框类型的列表引用:[左, 右, 上, 下]
BdrColor
边框色彩索引的列表引用:[左, 右, 上, 下]
BdrDiag
对话框种类、类型和色彩索引的列表引用:[Kind, Style, Color] Kind : 0: None, 1: Right-Down, 2:right-Up, 3:Both
Fill
填充模式和色彩索引的列表引用:[Pattern, Front Color, Back Color]
Lock
是否锁定。
Hidden
是否隐藏。
Style
类型格式(或者单元格式)
Font
Spreadsheet::ParseExcel::Font
格式类有这些属性:
Name
字体的名字。
Bold
是否粗体。
Italic
是否斜体。
Height
字体的高度。
Underline
是否有下划线。
UnderlineStyle
0:没有,1:单线,2:双线,0x21:加重单线,0x22:加重双线
Color
字体的色彩索引。
Strikeout
是否有删除线。
Super
0:无,1:大写,2:小写
格式化类
Spreadsheet::ParseExcel::Fmt*
格式化类将转换单元数据。
Spreadsheet::ParseExcel 包括2个具体类:FmtDefault和FmtJapanese。你可以根据需要创建自己的格式化类。
格式化类(Spreadsheet::ParseExcel::Fmt*)应该提供这些函数:
ChkType($oSelf, $iNumeric, $iFmtIdx)
告诉有特殊值的单元的类型。
$oSelf
格式化类实例Formatter itself
$iNumeric
如果有,这个值为数字
$iFmtIdx
Format index number of that cell
TextFmt($oSelf, $sText, $sCode)
转换原来的文本为接受的类型。
$oSelf
格式化类实例Formatter itself
$sText
原文本
$sCode
原文本的字符编码
ValFmt($oSelf, $oCell, $oBook)
转换原来的值为接受的类型。
$oSelf
格式化类实例Formatter itself
$oCell
单元对象
$oBook
工作簿对象
FmtString($oSelf, $oCell, $oBook)
得到$oCell的格式化字符串。
$oSelf
格式化类实例Formatter itself
$oCell
单元对象
$oBook
包含本单元的工作簿
已知问题
这个模块不能得到由Spreadsheet::WriteExcel模块创建的Excel文件里的公式的值。通常(比如,通过Excel应用程序),公式有结果。但是Spreadsheet::WriteExcel写入的公式没有结果。如果你设置了Excel应用程序“自动计算”为否。可能[工具]-{计算器]或者其它)你将看到同样的结果。
作者
Current maintainer: Gabor Szabo szabgab@cpan.org
http://www.szabgab.com/
Original author: Kawai Takanori (Hippo2000) kwitknr@cpan.org
http://member.nifty.ne.jp/hippo2000/ (Japanese)
http://member.nifty.ne.jp/hippo2000/index_e.htm (English)
译者
Alf <naihe2010@gmail.com>
其它
XLHTML, OLE::Storage, Spreadsheet::WriteExcel, OLE::Storage_Lite
This module is based on herbert within OLE::Storage and XLHTML.
XLSTools: http://perl.jonallen.info/projects/xlstools
要做的
增加测试,而且更多的测试
Spreadsheet::ParseExcel:密码保护的数据,公式的支持,超级链接的支持,命名范围的支持
Spreadsheet::ParseExcel::SaveParser:赶上Spreadsheet::WriteExcel的功能,创建Excel文件
其它
L<http://www.cpanforum.com/dist/Spreadsheet-ParseExcel>;
and
http://www.perlmonks.org/index.pl?node_id=490656
http://www.perlmonks.org/index.pl?node_id=379743
http://www.perlmonks.org/index.pl?node_id=433192
http://www.perlmonks.org/index.pl?node_id=422147
版权
Copyright (c) 2006-2007 Gabor Szabo Copyright (c) 2000-2006 Kawai Takanori All rights reserved.
You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
致谢
首先,我感谢这些程序与模块:XHTML,OLE::Storage和Spreadsheet::WriteExcel。
没有特别排序:Yamaji Haruna, Simamoto Takesi, Noguchi Harumi, Ikezawa Kazuhiro, Suwazono Shugo, Hirofumi Morisada, Michael Edwards, Kim Namusk, Slaven Rezic, Grant Stevens, and many many people + Kawai Mikako.
[[i] 本帖最后由 naihe2010 于 2009-12-17 17:26 编辑 [/i]] |
评分
-
查看全部评分
|