星期日, 5月 28, 2006

perl 讀取 excel 檔的方法

#! /usr/env perl

use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

$Win32::OLE::Warn = 3;

my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');

# 打開 excel 檔
my $Book = $Excel->Workbooks->Open("c:/test/test.xls");

# 選擇 sheet , 1 代表第一個 sheet , 也可以直接給 sheet 的名稱
my $Sheet = $Book->Worksheets(1);

# 顯示 A1 的值
print $Sheet->Range("A1")->{'Value'}."\n";

$Book->Close;

沒有留言: