在Perl中将.XLSX转换为.CSV

时间:2018-08-29 08:46:03

标签: excel perl xlsx

我有一个包含在下面的perl脚本

use strict;
use Spreadsheet::ParseExcel;

my $inputFile    = shift;
my $outputFile    = shift;

if(!-e $inputFile || !$outputFile) {
    print qq~
Usage: excelToTabDelim inputFile outputFile
    Parses an excel input file into a tab delimited out file
~;
    exit 1;
}

if(open(OUT, "> $outputFile")) {
    my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($inputFile);
    foreach my $worksheet (@{$workbook->{Worksheet}}) { # looping through worksheets
        for(my $row = $worksheet->{MinRow}; defined $worksheet->{MaxRow} && $row <= $worksheet->{MaxRow}; $row++) { # loop through rows
            my @line;
            for(my $column = $worksheet->{MinCol}; defined $worksheet->{MaxCol} && $column <= $worksheet->{MaxCol}; $column++) { # loop through columns
                my $value = $worksheet->{Cells}[$row][$column] ? $worksheet->{Cells}[$row][$column]->Value : "";
                $value =~ s/^\s+//;
                $value =~ s/\s+$//;
                $value =~ s/[\r\n]+//g;
                $value = " " if $value ne "0" && !$value;
                push(@line, $value);
            }
            $" = "\t";
            print OUT "@line\n";
        }
    }
    close(OUT);
}
else {
    print "Error: Could not write to output file '$outputFile'\n";
}

该脚本会将.xlsx文件转换为.csv文件。我面临的问题是,当我运行脚本时,正在形成的output.csv为零字节。谁能告诉我代码有什么问题吗?

以下是excel文件的示例 enter image description here

0 个答案:

没有答案