Spreadsheet::WriteExcel examples
戻る
#!C:/perl/bin/perl
# excel_start2.pl
# 07.11.28
use strict;
use HTML::Template;
my (%t,@loop,$n);
# ループデータ生成
@loop = ();
for $n ( 1 .. 30 ) {
$t{name} = 'NO' . $n;
$t{value} = $n + 100;
my %row = (
name => $t{name},
value => $t{value}
);
# put this row into the loop by reference
push(@loop, \%row);
}
# HTMLファイル出力
$t{template} = HTML::Template->new(filename => 'excel_start2.htm');
$t{template}->param(LOOP => \@loop);
print $t{template}->output;
1;
-----------------------------------------------------------------------
excel_start2
excel_start2
http://localhost/index.html
-----------------------------------------------------------------------
#!C:/perl/bin/perl
# excel_write2.pl
# 07.11.28
use strict;
use CGI qw/:standard/;
use HTML::Template;
use Spreadsheet::WriteExcel;
use utf8;
my (%t,$n,$e_ref);
# CGIのパラメータ転送
$t{q} = new CGI;
for $n ( 1 .. 20 ) {
$t{name} = 'NO' . $n;
push(@{ $$e_ref{names} },$t{name});
$$e_ref{list}{$t{name}} = $t{q}->param($t{name});
}
# Excel ファイルに出力
($e_ref) = write_excel($e_ref);
# HTMLファイル出力
$t{template} = HTML::Template->new(filename => 'excel_write2.htm');
$t{template}->param(NO1 => $$e_ref{list}{NO1});
print $t{template}->output;
sub write_excel {
my($e_ref) = @_;
my(%t,$n);
# Create a new Excel workbook
$t{workbook} = Spreadsheet::WriteExcel->new("C:\\TEMP\\perl.xls");
# Add a worksheet
$t{worksheet} = $t{workbook}->add_worksheet();
# Add and define a format
$t{format} = $t{workbook}->add_format();
$t{format}->set_bold();
$t{format}->set_color('red');
$t{format}->set_align('center');
# Write a formatted and unformatted string, row and column notation.
$t{col} = $t{row} = 3;
$t{worksheet}->write($t{row}, $t{col}, "Hi Excel!", $t{format});
$t{worksheet}->write(1, $t{col}, "Hi Excel!");
# Write a number and a formula using A1 notation
$t{worksheet}->write('A3', 1.2345);
$t{worksheet}->write('A4', '=SIN(PI()/4)');
$t{worksheet}->write('A5', 'こんにちわ!');
$t{worksheet}->write('A6', '你好!中国!');
for $n ( 0 .. $#{ $$e_ref{names} } ) {
$t{name1} = $$e_ref{names}[$n];
$t{value1} = $$e_ref{list}{$t{name1}};
$t{NO} = $n + 10;
$t{x1} = 'B' . $t{NO};
$t{x2} = 'C' . $t{NO};
$t{worksheet}->write($t{x1}, $t{name1});
$t{worksheet}->write($t{x2}, $t{value1});
}
$t{workbook}->close();
return($e_ref);
}
1;
__END__
-----------------------------------------------------------------------
excel_write2
excel_write2
http://localhost/index.html
選択結果は次のとおりである。
NO1==>
戻る
戻る