previousSpreadsheet_Excel_Writer (Previous) (Next) Tutoriel sur le formattagenext

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

Introduction

Introduction – Comment générer des fichiers Excel

Qu'est-ce Spreadsheet_Excel_Writer?

Spreadsheet_Excel_Writer est un utilitaire pour créer des fichiers Excel sans avoir besoin des composants COM. Les fichiers générés par l'actuel version de Spreadsheet_Excel_Writer correspond au format Excel 5 (BIFF5), toutes les fonctionnalités jusqu'à cette version doivent donc être disponibles.

Utilisation

L'utilisation la plus commune de Spreadsheet_Excel_Writer est de mettre beaucoup d'informations dans un formulaire de feuille de calcul, qui sera facilement manipulable avec un programme de manipulation de feuilles de calcul comme Excel (ou OpenOffice).

Voici un exemple concret :

Utilisation typique

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

// Création d'un manuel de travail
$workbook = new Spreadsheet_Excel_Writer();

// Envoi des en-têtes HTTP
$workbook->send('test.xls');

// Création d'une feuille de travail
$worksheet =& $workbook->addWorksheet('My first worksheet');

// Les données actuelles
$worksheet->write(00'Nom');
$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);

// Envoi du fichier
$workbook->close();
?>

La première chose à noter est que nous créons un manuel de travail avant d'ajouter une feuille de travail. Toutes les feuilles de travail sont contenues dans un manuel de travail, et un manuel de travail contient plusieurs feuilles de travail.

Une autre chose importante, que vous devez avoir en tête lors de la programmation avec Spreadsheet_Excel_Writer, est l'utilisation du signe & (ET commercial) lors de la création de la feuille de travail. L'utilisation du ET commercial signifie que nous référençons un objet WorkSheet au lieu de le copier. Si vous ne savez pas ce que cela signifie, ne vous en faite pas, tout ce dont vous avez besoin de vous souvenir est de toujours utiliser ce "&" lors de l'appel à addWorksheet() pour créer une feuille de travail ou addFormat() pour créer un format.

Sauvegarde dans un fichier

Vous avez du remarquer également la ligne suivante :


// Envoi des en-têtes HTTP
$workbook->send('test.xls');
Ce qui signifie que nous envoyons notre feuille de calcul au navigateur. Et si nous voulons uniquement sauvegarder cette feuille de calcul sur notre machine ? Vous n'avez qu'à ommettre cette ligne et passer un chemin de fichier valide au constructeur de WorkBook.

Par exemple, si vous voulez sauvegarder la feuille de calcul créée dans notre premier exemple dans un fichier nommé 'test.xls', nous devons le faire comme cela :

Sauvegarder dans un fichier

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

// Nous donnons un chemin à notre fichier ici
$workbook = new Spreadsheet_Excel_Writer('test.xls');

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

$worksheet->write(00'Nom');
$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);

// Nous devons toujours explicitement fermer le manuel de travail
$workbook->close();
?>

Plus d'exemples

Si vous voulez avoir plus d'exemples concernant le formattage (police de caractères, couleur des cellules, alignement du texte, etc...) avec Spreadsheet_Excel_Writer, vous pouvez consulter le tutoriel sur le formattage ici.

previousSpreadsheet_Excel_Writer (Previous) (Next) Tutoriel sur le formattagenext

Download Documentation Last updated: Sun, 18 Oct 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