PHPExcelでExcelファイルをパースする
普段PHP書いてるわけじゃないので、あまり適切でない記述があればご指摘いただけるとありがたいです。
PEARではなくローカルでコピーしてAP資材の中に含めたので以下のような感じにしました。rhacoを使っていてトップ階層から参照している形です。
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__).'/library/phpexcel/'); include 'PHPExcel.php'; include 'PHPExcel/IOFactory.php';
アップロードした中身を読み込んでチェックするなどの処理をベタに書いてみたのが以下のようなサンプル。
$excelFilename = $_FILES['excelfile']['tmp_name']; $reader = PHPExcel_IOFactory::createReader('Excel5'); $file_not_found = false; try { $xl = $reader->load($excelFilename); } catch (Exception $e) { $file_not_found = true; } if ( ! $file_not_found ) { /** * search target sheet */ for( $i=0; $i<$xl->getSheetCount(); $i++ ) { $xl->setActiveSheetIndex($i); $sheet = $xl->getActiveSheet(); if ( strcmp($sheet->getCell('A1')->getValue(), "何かしらの判定条件") ) break; $sheet = null; } /** * read target sheet */ if ( $sheet != null ) { // 普通の文字列の値の場合は単にgetValue()するだけ $some_str = $sheet->getCell('B1')->getValue(); // 日付の取得 // セルの書式指定を文字列にしてれば普通にgetValueでいいけど // そうとは限らない場合、PHPオブジェクトに変換してから日付処理する $some_date = strftime("%Y/%m/%d", PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell('C1')->getValue())); } else { $flow->setVariable(CommonConstant::ERROR_MESSAGE, 'Excelファイルに処理対象のシートが見つかりませんでした。'); } } else { $flow->setVariable(CommonConstant::ERROR_MESSAGE, 'Excelファイルを指定して下さい。'); }