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; ----------------------------------------------------------------------- <html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head><title>excel_start2</title> <head> <body bgcolor="#fcf5ca"> <h2>excel_start2</h2> <a href="http://localhost/index.html">http://localhost/index.html</a> <hr> <form method="POST" action="excel_write2.pl"> <center> <table> <tr bgcolor="white"><th>name</th><th>value</th></tr> <TMPL_LOOP NAME="LOOP"> <tr bgcolor="white"><td><TMPL_VAR NAME="name"></td><td><TMPL_VAR NAME="value"></td></tr> <input type="hidden" name="<TMPL_VAR NAME="name">" value="<TMPL_VAR NAME="value">"> </TMPL_LOOP> </table> <input type="submit" value="選択"> </form> </center> <hr> </body> </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__ ----------------------------------------------------------------------- <html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head><title>excel_write2</title> <head> <body bgcolor="#fcf5ca"> <h2>excel_write2</h2> <a href="http://localhost/index.html">http://localhost/index.html</a> <hr> 選択結果は次のとおりである。<br> NO1==><TMPL_VAR NAME="NO1"><br> <hr> <a href="http://localhost/scripts/excel_start2.pl">戻る</a> </body> </html>
戻る