# ************************************************* # Start Excel and create new workbook with 9 sheets use Win32::OLE qw(in valof with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::NLS qw(:DEFAULT :LANG :SUBLANG); my $lgid = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT); $Win32::OLE::LCID = MAKELCID($lgid); $Win32::OLE::Warn = 3; my $Excel = Win32::OLE->new('Excel.Application', 'Quit'); $Excel->{SheetsInNewWorkbook} = 9; my $Book = $Excel->Workbooks->Add; my $Sheet = $Book->Worksheets(1); my $sheet_count = 0; # ************************************************* # Read each 'selected' file and load them into an array my @rows; my @fields; foreach $file (@csvFiles) { open (csvFILE, $file) || die "Cannot open $csvFile $!\n"; $csvFile = $file; # Skip the first record (too long) of each file, we'll deal with it later my $inBuf = ; $sheet_count++; while ($inBuf = ) { chomp($inBuf); if (scalar @fields > $line_limit) {last;} # Runaway safety net $inBuf =~ s/^\"//; # Take out any LEADING or $inBuf =~ s/\"$//; # TRAILING double quotes # OK, Process this record @fields = split(/\,/,$inBuf); push @rows, [@fields]; } # End of while inBuf, or we exceeded Safety Net close csvFILE; # **************************************** # Build the Spreadsheet from the CSV Array # print "\nLoading Sheet \($sheet_count\) of $xlsFile from $csvFile \n"; $Sheet = $Book->Worksheets($sheet_count); $Sheet->{Name} = substr($cvsFile,0,6); #This fails # Insert column titles - hmmm wonder why THESE work? my $Range = $Sheet->Range("A1:J1"); $Range->{Value} = [qw(Dept ItemCnt GrSales VoidCnt VoidAmt RefundCnt RefundAmt CouponCnt CouponAmt CustCnt)]; $Range->Font->{Bold} = 1; # This works too # Add csv data to spreadsheet print "\n\nAdding data from $csvFile to Sheet $xlsFile\n"; $Range = $Sheet->Range(sprintf "A2:J%d", 2+$#rows); $Range->{Value} = \@rows; # This fails similar to Name } # End of Foreach csvFile # ********************************************* # Save workbook to file $xlsFile unlink $xlsFile if -f $xlsFile; #This works fine $Book->SaveAs("$directory\\$xlsFile"); $Book->Close;