我正在将您的问题和我的答案重新发送给任何想知道的人 怎么做:
如何使用Perl从Excel(2007).xlsx文件中提取公式(包括单元格引用)?
问题:
假设'Sheet1'上的单元格[0] [0]包含123 假设'Sheet2'上的单元格[2] [2]包含'Sheet1'!A0对Sheet1上单元格的引用。
使用Perl和ths模块; Spreadsheet :: XLSX你得到以下内容:
#!/usr/local/bin/perl -w
use strict;
use warnings;
use Spreadsheet::XLSX;
my $excelFile = Spreadsheet::XLSX -> new ("MyExcelFile.xlsx");
foreach my $sheet (@{$excelFile -> {Worksheet}}) {
my $name = $sheet -> {Name};
if ( $name eq 'Sheet2' ) {
my $cell = $sheet -> {Cells} [2] [2]; # cell (C3)
my $curCellContent = $cell -> {Value};
print "$curCellContent\n"; # OUTPUT: 123
}
}
这通常很好,但我需要公式(参考):'Sheet1'!A0
答案 0 :(得分:1)
问题是Spreadsheet :: XLSX无法处理forumlas。 解决方案是使用Spreadsheet :: ParseXLSX。
# Tested with perl version 5.16 Spreadsheet::XLSX version 0.13
#!/usr/local/bin/perl -w
use strict;
use warnings;
use Spreadsheet::ParseXLSX;
my $parser = Spreadsheet::ParseXLSX->new;
my $excelWorkbook = $parser->parse("MyExcelFile.xlsx");
foreach my $sheet (@{$excelWorkbook -> {Worksheet}}) {
my $name = $sheet -> {Name};
print "\$name = $name\n";
if ( $name eq 'Sheet2' ) {
my $cell = $sheet -> {Cells} [2] [2]; # cell (C3)
my $curCellContentValue = $cell -> Value;
print "$curCellContentValue\n"; # OUTPUT: 123
my $#!/usr/local/bin/perl -w = $cell -> {Formula};
print "$curCellContentFormula\n"; # OUTPUT: 'Sheet1'!A0
}
}