Excelデータの作成
Win32::OLEでExcelを操作することができます。
Excelがインストールされている必要があります。
# Excelを起動してデータを入力する
use strict;
use Win32::OLE;
my $excel;
eval {
Win32::OLE->GetActiveObject('Excel.Application');
};
if ($@) {
die "Excelが入っていません。 $@";
}
unless (defined $excel) {
$excel = Win32::OLE->new('Excel.Application', '')
or die "Excelが起動できません。";
}
my $book = $excel->Workbooks->Add; # ワークシートの作成
my $sheet = $book->Worksheets(1);
# データの作成
$sheet->Cells(1, 1)->{Value} = "数字";
foreach my $i (2 .. 11) {
$sheet->Cells($i, 1)->{Value} = $i - 1;
}
$excel->{Visible} = 1; # Excelを表示する
# Excelデータファイルを作成する
use strict;
use Win32::OLE;
my $excel;
eval {
Win32::OLE->GetActiveObject('Excel.Application');
};
if ($@) {
die "Excelが入っていません。 $@";
}
unless (defined $excel) {
$excel = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
or die "Excelが起動できません。";
}
my $book = $excel->Workbooks->Add; # ワークシートの作成
my $sheet = $book->Worksheets(1);
$sheet->Cells(1, 1)->{Value} = "数字";
foreach my $i (2 .. 11) {
$sheet->Cells($i, 1)->{Value} = $i - 1;
}
my $file = 'C:\test.xls'; # 保存ファイル名
unlink $file;
$book->SaveAs($file); # ファイルを保存する
$book->Close();
$excel->Quit();
関連項目
・Excelでグラフの作成・Excelパスワード設定
