PerlでExcelのファイルを作成する

http://www.drk7.jp/MT/archives/000565.html

を参考にして、Encodeモジュールに書き直した。

from_toを何度も呼び出しているところがきれいではないが、まぁ一応できているので載せておくことにする。

#!/usr/bin/perl -w
# excelsample.pl
# Last Modified:Sun, 07 Dec 2003 23:28:47 +0900
# ExcelPerlで扱うためのサンプル
#
#

use strict;
use Encode;
use Spreadsheet::WriteExcel;

my $encoding = 'shiftjis';      # このファイルの文字コード
# eucならば 'euc-jp' とする。

# Create a new workbook
my $workbook  = Spreadsheet::WriteExcel->new("excelsample.xls");
my $uni_font  = $workbook->add_format(font => 'Arial Unicode MS');

foreach my $sheet (1..10) {
  my $sheet_name = "Test $sheet";
  Encode::from_to($sheet_name, "$encoding", 'utf16');
  my $worksheet = $workbook->add_worksheet("$sheet_name",1);

  foreach my $num (1..10) {
    my $string = "$sheet の $num番目";
    Encode::from_to($string, "$encoding", "utf16");
    $worksheet->write_unicode($num-1, $num-1, "$string");
  }
}

# excelsample.pl ends here.

Spreadsheet::WriteExcelというモジュールを使うとできる。できたExcelファイルを開こうとしたら、このコンピュータにはExcelがインストールされていなかった。しょうがないので、Excelビューアをインストールしてみた。


これもメモ。窓の杜へのリンク。

http://www.forest.impress.co.jp/article/2004/12/17/msviewer2003.html


Excelがインストールされていなくても、Excelのファイルが作成できるってのは、なんか不思議な感じがする。