failure ( 171): for host ***.***.**.*** trying to POST /cgi-bin/upload.cgi, cgi_scan_headers reports: HTTP4044: the CGI program /cgi-bin/upload.cgi did not produce a valid header (program termindated without a vaild CGI header.
####
my $IN = $upload_path . param('excel') . ".txt";
exec "chmod 775 $IN";
my $OUT = "master.xml";
exec "chmod 775 master.xml";
##
##
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
$CGI::POST_MAX = 2097152; ## (2mb upload max)
$CGI::DISABLE_UPLOADS = 0; ## allow uploads
my $upload_path = "/export/";
my $filename = $upload_path . param('excel');
sub print_header {
my $title = shift;
print header, start_html($title);
}
sub init {
my $upload = upload('excel');
unless (param('excel')) {
print_header ('Excel to XML Converter');
print_upload_form();
## make sure there's actually something being uploaded
} elsif (!$upload && cgi_error) {
print_header ('Excel to XML Converter: Error!');
print h1("No file uploaded!");
## all is well, show conversion confirmation
} else {
show_convert();
}
}
sub print_upload_form {
print h1('Upload Excel File.'),
start_multipart_form(),
"Excel file:", filefield( -name=>'excel' ),
br,
submit( -name=>"Convert" ),
end_form;
}
## show the conversion process
sub show_convert {
print header, start_html ("Converting..."),
"Converting...
",
$filename,
br,
convert(),
"
Done!";
}
sub convert {
## convert to the xSV file...
my $fh = upload('excel');
my $bytes_read = -s $fh;
open OUTFILE, '>', $filename or die "Couldn't open output file:
$!\n";
while ( my $bytes = read($fh, my $buffer, 2097152)) {
print OUTFILE $buffer;
}
close OUTFILE;
## convert to xSV...
txt_to_xSV();
## convert xSV to XML...
xSV_to_XML();
## copy/back up file, not added yet
"Received $bytes_read bytes
";
}
sub txt_to_xSV {
require Spreadsheet::ParseExcel;
my $oExcel = new Spreadsheet::ParseExcel;
my $dir = $ARGV[1];
chomp $dir;
unless (-d $dir) {
print "Can't find directory $dir";
exit;
}
my $filename = "$dir/$ARGV[0]";
chomp $filename;
unless (-e $filename) {
print "Can't find file $filename";
exit ;
}
my $fullfilename = "$filename.txt";
## start work
#1.1 Normal Excel97
open E2T, ">", $fullfilename or die $!;
my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename);
my($iR, $iC, $oWkS, $oWkC);
foreach my $oWkS (@{$oBook->{Worksheet}}) {
print "--------- SHEET:", $oWkS->{Name}, "\n";
print E2T $oWkS->{Name}, "|";
next unless defined $oWkS->{MinRow} and defined $oWkS->{MaxRow};
for my $iR ($oWkS->{MinRow} .. $oWkS->{MaxRow}) {
for my $iC ($oWkS->{MinCol} .. $oWkS->{MaxCol}) {
$oWkC = $oWkS->{Cells}[$iR][$iC];
next if ! defined $oWkC;
print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC);
print E2T $oWkC->Value, "|";
}
}
print E2T "\n";
}
close E2T;
}
sub xSV_to_XML {
my $IN = "master.xls.txt";
my $OUT = "master.xml";
open (OUT, ">$OUT");
open(IN) or die ("Cannot open file ($IN)");
print OUT <
TOP
my $id = 0;
my @columns =
("category","code","title","summary","prereq","group","subgroup","sequence","rolemandatory","rolerecommended","roleoptional","url","modality","length");
foreach my $row (){
$row =~ s#\s+$##;
$row =~ s#&+#&#g;
$row =~ s#(?<=\|)\|#0|#g;
my
($category,$code,$title,$summary,$prereq,$group,$subgroup,$sequence,$rolemandatory,$rolerecommended,$roleoptional,$url,$modality,$length)
= split ('\|', $row);
#will ignore lank
next if $group eq "";
foreach $_ (@columns){
xmlrow($_);
}
print OUT "\n";
$id++;
}
print OUT "\n";
}
sub xmlrow{
my $colname = shift(@_);
my $ostar="${$colname}\n";
}
init();