如何在perl中从xlsx读取数据 [英] How to read data from xlsx in perl

查看:139
本文介绍了如何在perl中从xlsx读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些好人帮助我阅读扩展名为xlsx"的excel文件,我的脚本适用于xls"但不适用于xlsx",这是我收到错误的代码:无法调用方法工作表"" 在未定义的值上 如果文件是 "xlsx" 这里是我有的代码:

Need a few good people help me with reading excel file with extension "xlsx" my script works for "xls" but not "xlsx", here is the code I get error: Can't call method "worksheet" on an undefined value if the file is "xlsx" here is the code I do have:

#!/usr/bin/perl -w

use warnings;
use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::XLSX;
use Date::Format;

my $filename = "../test.xlsx";
#Parse excel file
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse("$filename");

#Get cell value from excel sheet1 row 1 column 2
my $worksheet = $workbook->worksheet('Sheet1');
my $cell = $worksheet->get_cell(0,1);

# Print the cell value when not blank
if ( defined $cell and $cell->value() ne "") {
    my $value = $cell->value();
    print "cell value is $value \n";
}

推荐答案

Spreadsheet::XLSX 与 Spreadsheet::ParseExcel 非常接近,适用于 .xlsx 文件;你需要说:

Spreadsheet::XLSX is an close equivalent of Spreadsheet::ParseExcel for .xlsx files; you need to say:

my $parser = Spreadsheet::XLSX->new();

而不是使用 ParseExcel.

instead of using ParseExcel.

这篇关于如何在perl中从xlsx读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆