晝過ぎに起きて出社。けふのFULL SWINGの演奏會はち○っ○に一人で聽きに行つて貰ふ事に。今週も酷い週に成り然うだなあ。
PerlでExcelファイルを讀む爲のモジュール。Linux上のPerlで直接Excelファイルを讀む事が出來るのがミソ。Excelファイルを一々CSVに變換し無くても直接讀込めるので便利。此方が作者に據る解説之頁。
一枚のシートに日本語・簡體字・繁體字・ハングルの混じつたファイルを讀む爲にはFmtUnicodeを用ひ無いと駄目の樣子。
セル(Spreadsheet::ParseExcel::Cell)のプロパティで覺えて措くのはValue, Val, Type, Code位か。
セルの中身に據つてエンコーディングが異なるので、注意が必要。
以下のコードで變數$cellはSpreadsheet::ParseExcel::Cellのインスタンス。
use utf8;
use strict;
use Encode qw/decode encode/;
use Encode::JP;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode;
(中略)
# セルのエンコーディングをチェックする。
# 値が定義されていなければ ASCII。
my $val;
if (! defined($cell->{Code})) {
$val = &decode("ascii", $cell->Value);
}
# 値が定義されていれば UCS2 か Shift_JIS
elsif ($cell->{Code} eq 'ucs2') {
$val = &decode("UCS2-BE", $cell->Value);
}
elsif ($cell->{Code} eq '_native_') {
$val = &decode("shiftjis", $cell->Value);
}
# どれにも當て嵌まらなければ不明なエンコーディング。
else {
warn(sprintf("Unknown encoding. (%d, %d)", $row, $col));
}
此れで變數$valにセルの中身が文字化けせずに入る。
*1 "seem to be" と書かれてゐたので「確實では無い」のだと思はれる。
PHPでExcelのファイルを讀む爲のクラス。此方が日本語に據る解説。何故か愛生会へのリンクが有る。
日本語・簡體字・繁體字・ハングルの入雑じつたExcelファイルを讀込ませて表示させてみたら酷く文字化けした。結構な時間調べてみたらreader.phpでiconvを使用してUTF-16LE (Excelの内部エンコード?)を所定のエンコードに變換してゐる箇所が在つた。會社で使用してゐる計算機ではPHPをiconv付きで構築する事が出來無かつたので、其部分をmb_convert_encoding()を使用する樣に手を入れたら良く成つた。
> *** reader.php.org Sat Sep 11 18:31:00 2004
> --- reader.php Mon Sep 27 16:05:21 2004
> ***************
> *** 137,143 ****
>
> function read($sFileName) {
> $errlevel = error_reporting();
> ! error_reporting($errlevel ^ E_NOTICE);
>
> $res = $this->_ole->read($sFileName);
>
> --- 137,144 ----
>
> function read($sFileName) {
> $errlevel = error_reporting();
> ! // error_reporting($errlevel ^ E_NOTICE);
> ! error_reporting($errlevel ^ E_ERROR);
>
> $res = $this->_ole->read($sFileName);
>
> ***************
> *** 767,773 ****
> --- 768,783 ----
>
> function _encodeUTF16($string){
> if ($this->_defaultEncoding){
> + // It is better to use mb_convert_encoding rather than using iconv.
> + // If it is why mb_convert_encoding since it is the extended module of PHP.
> + // In order to use mb_convert_encoding As the option of configure
> + // what is necessary is to attach --enable-mbstring and just to compile.
> + /*
> return (Spreadsheet_Excel_Reader_HAVE_ICONV) ? iconv('UTF-16LE', $this->_defaultEncod\
> ing, $string): $string;
> + */
> + return mb_convert_encoding ( $string,
> + $this->_defaultEncoding,
> + 'UTF-16LE' );
> }else{
> return $string;
> }