テキストデータをExcelファイルに出力2

戻る

# write2excel.pl # 07.09.07 use strict; use Win32::OLE qw(in with); use Win32::OLE::Variant; use Encode; # set perl's OLE module to return Unicode # if you don't do this perl will convert to the current locale Win32::OLE->Option(CP => Win32::OLE::CP_UTF8, Warn=>3); my ($ex,$sheet,$book,%t,$n,@fld); # Read data (unicode) open(IN,"C:\\temp\\map.txt") or die "Can't open the file map.txt.\n"; while(<IN>){ last if (/^END/); if ( /^ITEM/ ) { @fld = split; push(@{ $t{NO} },$fld[1]); push(@{ $t{Ns} },decode("utf8",$fld[2])); push(@{ $t{Cs} },decode("utf8",$fld[3])); } } close(IN); # Get the object $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; $ex->{DisplayAlerts} = 'False'; # open the sample file map.xls $book = $ex->Workbooks->Open("C:\\TEMP\\map.xls"); # write to a particular cell $sheet = $book->Worksheets('Sheet1'); # EXCELファイルにデータを書き込む for $n ( 0 .. $#{ $t{NO} } ) { $t{no} = 4 + $n; $t{NO1} = $t{NO}[$n]; $sheet->Cells($t{no},'A')->{'Value'} = $t{NO1}; $t{XY} = $t{XY_NAME}[$n]; $t{N1} = $t{Ns}[$n]; # $sheet->Cells($t{no},'B')->{'Value'} = encode("shiftjis",$t{N1});# 日本語(shiftjis)OK $sheet->Cells($t{no},'B')->{'Value'} = Variant(VT_BSTR, $t{N1}); # 日本語(Unicode)OK $t{C1} = $t{Cs}[$n]; $sheet->Cells($t{no},'C')->{'Value'} = Variant(VT_BSTR, $t{C1}); # 中国語(Unicode)OK print "$n==>",encode("utf8",$t{C1}),"\n"; } # Save the excel file $book->SaveAs("C:\\TEMP\\map1.xls"); undef $book; undef $ex; __END__

戻る