regex - perl 脚本读取 Excel 文件特定列中的所有元素

标签 regex perl cpan

我编写了一个 perl 脚本来读取 Excel 文件中列的特定单元格值。

use strict;
use warnings;
use feature 'say';
use Spreadsheet::Read;
use Spreadsheet::ParseExcel;

my $workbook = ReadData ("C::/Users/Tej/Work.xlsx");
print $workbook->[6]{D4} . "\n";

到这里一切都很好。我想编写逻辑来读取 D 列下的所有单元格值,直到该列中有值为止。谁能帮我解决一下。

谢谢

最佳答案

假设我的数据是:

enter image description here

一种方法是做你想做的事(因为“D”对应于第四列):

$ cat a.pl
use strict;
use warnings;
use feature 'say';
use Spreadsheet::Read;
use Spreadsheet::ParseExcel;
use Data::Dumper;

my $workbook = ReadData ("/tmp/file.xls", parser => "xls");
print Dumper($workbook->[1]{cell}[4]);
foreach my $cell (@{$workbook->[1]{cell}[4]}) {
   if ($cell) {
       print $cell . "\n";
   }
}


$ perl a.pl
$VAR1 = [
          undef,
          'grid',
          1115,
          1512,
          212
        ];
grid
1115
1512
212

另一种方法是使用 Spreadsheet::BasicReadNamedCol :

$ cat a.pl
use strict;
use warnings;
use feature 'say';
use Spreadsheet::BasicReadNamedCol;
use Data::Dumper;
my @columnHeadings = (
    'grid',
);
my $workbook = new Spreadsheet::BasicReadNamedCol("/tmp/file.xls");
$workbook->setColumns(@columnHeadings);
while (my $data = $workbook->getNextRow()) {
    print "@{$data}[0]\n";
}
$ perl a.pl
grid
1115
1512
212

关于regex - perl 脚本读取 Excel 文件特定列中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39187003/

相关文章:

regex - 替换事件电子邮件草稿正文中的字符串

regex - sed 从字符串中提取版本号(只有版本,没有其他数字)

perl - Cygwin Perl 安装缺少 CPAN

mysql - 连接到数据库 Perl

perl - 如何自动化 CPAN 配置?

javascript - 正则表达式 : substring not followed by character

Python Regex - 检查后面是否有小写字母的大写字母

perl - 将字符串转换为数字 “Argument isn’ t 数字错误”

perl carton cpanfile,可选安装到主 perl 环境

用于编写excel2007工作簿的perl模块