2014年6月6日 星期五

perl csv to excel saveas overwrite

Reference:
http://www.perlmonks.org/bare/?node_id=823261

use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

# global setting
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new
my $Excel_to_write = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
$Excel_to_write->{SheetsInNewWorkBook} = 1;
$Excel_to_write->{Visible} = 1;
$Excel_to_write->{DisplayAlerts} = 0;

my $Book_to_write = $Excel_to_write->Workbooks->Add;
my $Sheet_to_write = $Book_to_write->Worksheets(1);
open (CSV, "C:/tmp/ASSY.CSV");
$col = $row = 1;
while (<CSV>) {
chomp;
my @line = split(",",$_);
for $cell (@line){
$Sheet_to_write->Cells($row,$col)->{'Value'} = $cell;
$col++;
}
$row++; $col = 1;
}
close(CSV);
$Book_to_write->SaveAs( {
Filename => "C:\\tmp\\Assy.xls",
FileFormat => xlWorkbookNormal}
);
$Book_to_write->Close;

沒有留言:

張貼留言