`
热开水
  • 浏览: 70173 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类

找了好久的perl在window上读excel的脚本

阅读更多
清单 1:安装 Excel 模块的 PPM 命令

ppm> install OLE::Storage_Lite
ppm> install Spreadsheet::ParseExcel
ppm> install Spreadsheet::WriteExcel

总是报一下错误!!
Can't locate Spreadsheet/Read.pm in @INC (you may need to install the Spreadsheet::Read module) (@INC contains: C:/strawberry/perl/site/lib C:/strawberry/perl/vendor/lib C:/strawberry/perl/lib .) at D:\201505\20150514\testr.pl line 5.
BEGIN failed--compilation aborted at D:\201505\20150514\testr.pl line 5.

后用以下程序测试通过:
#!/usr/bin/perl -w
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;                                # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit'); 
# open Excel file
my $Book = $Excel->Workbooks->Open("D:/201505/20150514/Book1.xlsx");
# You can dynamically obtain the number of worksheets, rows, and columns
# through the Excel OLE interface.  Excel's Visual Basic Editor has more
# information on the Excel OLE interface.  Here we just use the first
# worksheet, rows 1 through 4 and columns 1 through 3.
# select worksheet number 1 (you can also select a worksheet by name)
my $Sheet = $Book->Worksheets(1);
foreach my $row (1..4)
{
foreach my $col (1..3)
{
  # skip empty cells
  next unless defined $Sheet->Cells($row,$col)->{'Value'};
# print out the contents of a cell 
  printf "At ($row, $col) the value is %s and the formula is %s\n",
   $Sheet->Cells($row,$col)->{'Value'},
   $Sheet->Cells($row,$col)->{'Formula'};       
}
}
# clean up after ourselves
$Book->Close;


别人引用的:如果您正在使用 Windows 机器,请坚持使用 Win32::OLE 模块,除非您的机器上根本没有 Excel。虽然 Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel 模块的功能正不断完善,但 Win32::OLE 是目前获得 Excel 数据的最简便方式。

在 UNIX,特别是 Linux 上,请使用 Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel 模块对 Excel 数据进行编程访问。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics