previousSpreadsheet_Excel_Writer (Previous) (Next) 書式設定のチュートリアルnext

View this page in Last updated: Sun, 21 Jun 2009
English | Brazilian Portuguese | Chinese | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Turkish

導入

導入 – Excel ファイルを生成する方法

Spreadsheet_Excel_Writer って何?

Spreadsheet_Excel_Writer は、 COM コンポーネントを必要とせずに Excel ファイルを作成するためのツールです。 Spreadsheet_Excel_Writer の現行版によって生成されたファイルは、 Excel5(BIFF5) フォーマットに相当します。 したがって、そのバージョンのエクセルの持つ機能はすべて使用可能です (ただし、それ以降のバージョンの新機能は利用できません)。

使用法

Spreadsheet_Excel_Writer の一般的な使用法は、 膨大な (あるいはそれほどでもない) 量の情報をスプレッドシート形式に まとめ、そこらじゅうにあふれている Excel (あるいは OpenOffice) のような表計算プログラムで処理しやすくすることです。

それでは、実際どのように使用するのかを見てみましょう。

典型的な使用法

<?php
require_once 'Spreadsheet/Excel/Writer.php';

// ワークブックを作成します
$workbook = new Spreadsheet_Excel_Writer();

// HTTP ヘッダを送信します
$workbook->send('test.xls');

// ワークシートを作成します
$worksheet =& $workbook->addWorksheet('My first worksheet');

// データを書き込みます
$worksheet->write(00'Name');
$worksheet->write(01'Age');
$worksheet->write(10'John Smith');
$worksheet->write(1130);
$worksheet->write(20'Johann Schmidt');
$worksheet->write(2131);
$worksheet->write(30'Juan Herrera');
$worksheet->write(3132);

// ファイルを送信します
$workbook->close();
?>

ワークシートの前に、まずワークブックを作成したということに注意しましょう。 ワークシートはすべてワークブック内に含まれています。 また、ひとつのワークブックの中には複数のワークシートが含まれているかもしれません。

Spreadsheet_Excel_Writer を使用するプログラムを 書く際に注意すべき点がもうひとつあります。それは、ワークシートを 作成する際にアンパサンド符号 (&) を使用するということです。 アンパサンドは、ワークシートオブジェクトをコピーするのではなく その参照を使用するということを意味します。もし何のことだか さっぱりわからないとしても心配することはありません。 ワークシートを作成するために addWorksheet() をコールしたり 書式設定を作成するために addFormat() をコールしたりする際には必ずアンパサンドを使用する、とだけ 覚えておきましょう。

ファイルへの保存

さらに次の行が気になる人もいるでしょう。


// HTTP ヘッダを送信します
$workbook->send('test.xls');
これは、ブラウザにスプレッドシートを送信しているということを意味します。 単にスプレッドシートをマシン上に保存したいだけの場合には どうすればいいのでしょうか。そのためには、単にごの行を省略し、 その代わりに有効なファイルパスをワークブックのコンストラクタに 与えればよいのです。

たとえば、さきほどの例で作成したスプレッドシートを 'test.xls' という名前で保存したければ、以下のように記述します。

ファイルへの保存

<?php
require_once 'Spreadsheet/Excel/Writer.php';

// ファイルへのパスをここで指定します
$workbook = new Spreadsheet_Excel_Writer('test.xls');

$worksheet =& $workbook->addWorksheet('My first worksheet');

$worksheet->write(00'Name');
$worksheet->write(01'Age');
$worksheet->write(10'John Smith');
$worksheet->write(1130);
$worksheet->write(20'Johann Schmidt');
$worksheet->write(2131);
$worksheet->write(30'Juan Herrera');
$worksheet->write(3132);

// この場合でも、ワークブックを明示的に閉じる必要があります
$workbook->close();
?>

その他の教本

Spreadsheet_Excel_Writer の書式設定 (フォント・セルの色・文字の配置など) に関して学習したければ、 ここでフォーマット教本をチェックすることができます。

previousSpreadsheet_Excel_Writer (Previous) (Next) 書式設定のチュートリアルnext

Download Documentation Last updated: Sun, 21 Jun 2009
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: info@joomla-r-us.com
Thanks for improving the original spreadsheet-excel-writer!

I have integrated the PEAR spreadsheet-excel-writer into a Joomla component. The component allows you to define Worksheets which are populated with data from the database, by specifying your own custom SQL queries. One or more Worksheets can then be assembled into a Workbook - all in a nice user friendly UI within the Joomla framework.

If you have a Joomla website, or can set one up, please check out:
http://joomla-r-us.com

Note by: holubecml@gmail.com
Is it possible to let the script generate an xls file from html form and send it automatically as an attachement in an email?
Thans a lot,
Miro, Slovakia
Note by: fatrandy13@yahoo.com (Mike M.)
A simple chart/graph function would be great... You specify the rows and columns for the chart and chart type (bar, line, pie, etc...)


// make it a worksheet of its own
$chart_worksheet =& $workbook->addWorksheet($my_chart);

// add it to an existing worksheet...
$worksheet->addGraph(int $startCol, int $endCol, int $startRow, int $endRow, mixed $graphType);
Note by: chris@pineconehill.com
As of PHP5, all objects are passed as reference, so there's no need to use '=&'.
Note by: alangrus
CSVtoXLS.php Program Notes

I am often asked to provide output from the database in a spreadsheet XLS file. I built a shell around spreadsheet-excel-writer to use as a production conversion tool, to accomplish high format XLS file creation from an ASCII CSV file. As a PHP program, it should work on any O/S that you can install PHP 4.3 or better onto.

CSVtoXLS.php is a PHP utility to convert CSV data into business class XLS spreadsheet files, tested as compatible with OpenOffice 2.0 Calc and Excel 2000. It is provides for a high level of spreadsheet formatting and also offers formulas. CSVtoXLS.php takes advantage of those features and provides meaningful formatting and a simple Column SUM automatically generated at the bottom of any Decimal Column.

See Narrative and Download at http://performantsystems.com/CSVtoXLS.html
- Alan Gruskoff