免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2248 | 回复: 0
打印 上一主题 下一主题

Win32::OLE 读取excel数据???? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-05 10:33 |只看该作者 |倒序浏览
以下是CPAN中关于Win32::OLE的例子,其中“ # write a 2 rows by 3 columns range”部分为写入了一个2行3列的内容,而在“# print "XyzzyPerl"”部分为读取了Worksheets(1)中的内容,并放到多维数组$array中。
问题1:现在我想从硬盘上读取一xls格式的文件,放入数组$array中,该如何修改呢?
问题2:以上代码(仅仅2行3列)运行时间怎么需要近3s时间,效率怎么能提高呢?

#!/usr/bin/perl -W
use Win32::OLE;

        # use existing instance if Excel is already running
        eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
        die "Excel not installed" if $@;
        unless (defined $ex) {
            $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
                    or die "Oops, cannot start Excel";
        }

        # get a new workbook
        $book = $ex->Workbooks->Add;

        # write to a particular cell
        $sheet = $book->Worksheets(1);   #sheet为工作表的号码
        $sheet->Cells(1,2)->{Value} = "joson";  #元素写入的位置

        # write a 2 rows by 3 columns range
        $sheet->Range("C2:E3")->{Value} = [[ 22, 'Xyzzy', 'Plugh' ], #Range为表中的位置
                                           [ 42,    'Perl',  3.1415  ]];

        # print "XyzzyPerl"
        $array = $sheet->Range("A8:C9")->{Value};
        for (@$array) {
            for (@$_) {
                print defined($_) ? "$_|" : "<undef>|";
            }
            print "\n";
        }

        # save and exit
        $book->SaveAs( 'test.xls' );
          undef $book;
        undef $ex;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP