기본적으로 link에있는 코드의 수정 된 버전 인 perl 스크립트를 사용하여 여러 CSV 파일을 Excel 스프레드 시트로 변환했지만 출력 파일을 열 수 없습니다. 팝업 메시지 "파일이 손상되었습니다."CSV로 Excel 변환하기 파일이 손상됨
#!/usr/intel/bin/perl -W
use strict;
use Spreadsheet::WriteExcel::Big;
use Text::CSV_XS;
# Check for valid number of arguments
if (($#ARGV < 1) || ($#ARGV > 2)) {
die("Usage: csv2xls csvfile_dir xlsfile\n");
};
my $csvdir = $ARGV[0];
my $outfile = $ARGV[1];
# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel::Big->new($outfile);
my $csvfile = "";
my $tab_title = "";
foreach $csvfile (glob("$csvdir/*.csv")) {
print "$csvfile\n";
# Open the Comma Separated Variable file
open (CSVFILE, $csvfile) or die "$csvfile: $!";
$csvfile =~ s/.*\///;
$tab_title = (split(/\./,$csvfile))[0];
print "-D- $tab_title\n";
# Create a new Excel worksheet
my $worksheet = $workbook->add_worksheet($tab_title);
# Create a new CSV parsing object
my $csv = Text::CSV_XS->new;
# Row and column are zero indexed
my $row = 0;
while (<CSVFILE>) {
if ($csv->parse($_)) {
my @Fld = $csv->fields;
print "-D- @Fld\n";
my $col = 0;
foreach my $token (@Fld) {
$worksheet->write($row, $col, $token);
$col++;
}
$row++;
} else {
my $err = $csv->error_input;
print "Text::CSV_XS parse() failed on argument: ", $err, "\n";
}
}
}
어떻게 수정할 수 있습니까?