简体   繁体   English

读取 XLSM 文件的 Perl 模块是什么?

[英]What is the Perl module to read XLSM file?

My code works great for the XLSX file but it breaks at the XLSM input file.我的代码适用于 XLSX 文件,但它在 XLSM 输入文件处中断。

Requirements要求

  1. Read Excel's whole row rather than cell value.读取 Excel 的整行而不是单元格值。
  2. I don't want to use my $parser = Spreadsheet::ParseXLSX->new();我不想使用我的 $parser = Spreadsheet::ParseXLSX->new(); my $workbook = $parser->parse($prvs_excel_file);我的 $workbook = $parser->parse($prvs_excel_file); because it only parses cell value.因为它只解析单元格值。
#!/home/utils/perl
use strict;
use warnings;

sub usage { die "usage: $0 file.xlsx name [out.csv]\n"; }

my $xls = shift or usage;
my $sht = shift or usage;
my $out = shift // $xls;
   $out =~ s/\.xlsm$/.csv/i;

use Spreadsheet:: Read;
use Text::CSV_XS;

my $csv = Text::CSV_XS->new ({ binary => 1, eol => "\r\n", auto_diag => 1 });
open my $fh, ">", $out or die "$out: $!\n";

my $book  = ReadData ($xls) or die "$xls: $!\n";

my $sheet = $book->[$book->[0]{sheet}{$sht}];

foreach my $row (1 .. $sheet->{maxrow}) {
    $csv->print ($fh, [ Spreadsheet::Read::row ($sheet, $row) ]);
    }
close $fh;

It's been a while since I reviewed the available stuff out there but one of the things that Spreadsheet::Reader::ExcelXML solves is access to xlsm files.自从我查看可用的东西以来已经有一段时间了,但 Spreadsheet::Reader::ExcelXML 解决的问题之一是访问 xlsm 文件。 This is partly because potentially dangerous macros can be stored in spreadsheets with the xlsm extension.这部分是因为具有潜在危险的宏可以存储在具有 xlsm 扩展名的电子表格中。 Most other packages avoid this risk by intentionally not allowing that extension.大多数其他软件包通过故意不允许该扩展来避免这种风险。 Spreadsheet::Reader::ExcelXML avoids the risk of malicious macros by simply not providing access to the macro binary subfile through the interface. Spreadsheet::Reader::ExcelXML 通过不提供对宏二进制子文件的访问接口来避免恶意宏的风险。 The package is old though and somewhat brittle at this point. package 虽然很旧,但在这一点上有些脆弱。 However, It sounds like Håkon Hægland has expressed an interest in helping with the brittleness so good news!然而,听起来 Håkon Hægland 表示有兴趣帮助解决脆性问题,真是个好消息!

To potentially solve the OP question see the code below: From Spreadsheet::Reader::ExcelXML::Worksheet要潜在地解决 OP 问题,请参见以下代码:来自Spreadsheet::Reader::ExcelXML::Worksheet
Also review the methods 'fetchrow_array' and 'fetchrow_hashref'还要查看方法 'fetchrow_array' 和 'fetchrow_hashref'

use strict;
use warnings;
use Data::Dumper;
 
use Spreadsheet::Reader::ExcelXML;
my $workbook =  Spreadsheet::Reader::ExcelXML->new( #similar style to Spreadsheet::XLSX
                                        file => 't/test_files/TestBook.xlsx',# in the test folder of this package
                                        group_return_type => 'value',
                                );
 
if ( !$workbook->file_opened ) {
        die $workbook->error(), ".\n";
}
 
my      $worksheet = $workbook->worksheet( 'Sheet5' );
        $worksheet->set_custom_formats( {
                2 =>'yyyy-mm-dd',
        } );
my $value;
while( !$value or $value ne 'EOF' ){
        $value = $worksheet->fetchrow_arrayref;
        print Dumper( $value );
}
 
###########################
# SYNOPSIS Output
# $VAR1 = [ 'Superbowl Audibles', 'Column Labels' ];
# $VAR1 = [         'Row Labels',     2016-02-06', '2017-02-14', '2018-02-03', 'Grand Total' ];
# $VAR1 = [               'Blue',            '10',          '7',           '',          '17' ];
# $VAR1 = [              'Omaha',              '',           '',          '2',           '2' ];
# $VAR1 = [                'Red',            '30',          '5',          '3',          '38' ];
# $VAR1 = [        'Grand Total',            '40',         '12',          '5',          '57' ];
# $VAR1 = 'EOF';
###########################

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM