LoginSignup
77
74

More than 5 years have passed since last update.

PHPでExcelファイルを読み込んで配列にするまでのサムシング

Last updated at Posted at 2013-11-21

ターゲットのxlsxさん

スクリーンショット 2013-11-22 6.00.17.png

必要なもの

・ ターゲットのxlsx
・ PHPExcelライブラリ ( http://phpexcel.codeplex.com/releases/view/107442

readPHPExcel

// ファイル名の指定
$readFile = "example.xlsx";

// 連想配列でデータ受け取り
$data = readXlsx($readFile);

// 出力確認
print '<pre>';
var_dump($data);
print '</pre>';

// ファイル名渡したら配列返すラッパー関数
function readXlsx($readFile)
{
    // ライブラリファイルの読み込み (パス指定し直す)
    require_once dirname(__FILE__) . '/PHPExcel/IOFactory.php';

    // ファイルの存在チェック
    if (!file_exists($readFile)) {
        exit($readFile. "が見つかりません。" . EOL);
    }

    // xlsxをPHPExcelに食わせる
    $objPExcel = PHPExcel_IOFactory::load($readFile);

    // 配列形式で返す
    return $objPExcel->getActiveSheet()->toArray(null,true,true,true);
}


 // toArrayの説明
/**
 * Create array from worksheet
 *
 * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist
 * @param boolean $calculateFormulas Should formulas be calculated?
 * @param boolean $formatData  Should formatting be applied to cell values?
 * @param boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero
 *                               True - Return rows and columns indexed by their actual row and column IDs
 * @return array
 */

以下結果

スクリーンショット 2013-11-22 5.58.28.png

感想

日本語対応もしているみたいだし、
クライアントにテンプレートを渡してデータ入れてもらったら、
所定のディレクトリに配置するやり方でシステム更新ができるようになりそう。

ライセンスがLGPLなとこだけ注意が必要かもしれません。

14.01.10 追記

以前の記事をカスタマイズして自分なりに使いやすい形にしてみました。
ExcelをPHPで読み込んで1行目を連想配列のキーにするクラス書いたよ。

77
74
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
77
74