LoginSignup
7
8

More than 5 years have passed since last update.

PHPからExcelを動かす

Posted at

PHPからOLEを操る仕組みが存在します。
当然ですがWindowsサーバ、かつ該当のアプリがインストールされていることが前提です。

何故かPHP5.4.5以降デフォルト無効になったので、php.iniでphp_com_dotnet.dllを有効にしておきます。

<?php
    $excel = New COM('excel.application');
    $excel->Visible = 1;
    $excel->WorkBooks->Add();

    $worksheet = $excel->Worksheets('Sheet2');
    $worksheet->Activate();
    $worksheet->Cells(1,1)->Value='hoge';
    $worksheet->Cells(1,2)->Value=10;
    $worksheet->Cells(2,2)->Value=20;

    $range = $worksheet->Range('B1:B2');
    $sum = $excel->WorksheetFunction->Sum($range);

    $excel->Quit();

    $word = New COM('word.application');
    $word->Visible = 1;
    $word->Documents->Add();
    $word->Selection->TypeText("fuga");
    $word->Quit();

ExcelとWordのオブジェクトを取得できます。
あとはそれに対して適当に色々できる、らしいのですが、そもそもOLEの仕組みがよくわからない。
COMの中身はPHPのオブジェクトではないので、リフレクション使っても何も出てきません。
具体的にどのようなメソッドがあったりするのかがわからない。
PHPからOLE使ってみたという記事もほとんど見当たらない。

RubyにWin32OLEというモジュールがあり、これが比較的記事が多いので、使い方はそれを参考にするのがいい感じっぽいです。

7
8
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
7
8