9 * @package Custom\Export
10 * @author Sébastien Chazallet <sebastien.chazallet@laposte.net>
11 * @copyright 2011 InsPyration
12 * @license GNU GPL http://www.gnu.org/licenses/gpl.html
13 * @link http://zf2.biz
18 namespace Custom\Export;
21 * @see Spreadsheet_Excel_Writer
24 require_once 'Spreadsheet/Excel/Writer.php';
27 * Cette classe permet de renvoyer un fichier Excel.
29 * Elle rajoute des facilités telles que :
30 * > la gestion de la ligne courante
31 * > la gestion de la case courante
32 * > le passage automatisé à la case suivante (celle de droite)
33 * > le passage automatisé à la ligne suivante (celle du dessous)
34 * > la gestion des formats
37 * Elle permet d'aider à générer facilement des exports Excel,
38 * mais ne contient PAS de logique de présentation,
39 * conformément aux principes de séparation du code
41 * @category Components
42 * @package Custom\Export
43 * @author Sébastien Chazallet <sebastien.chazallet@laposte.net>
44 * @copyright 2011 InsPyration
45 * @license GNU GPL http://www.gnu.org/licenses/gpl.html
46 * @link http://zf2.biz
51 abstract class AbstractWorkbook
56 * @var Spreadsheet_Excel_Writer
62 * @var Spreadsheet_Excel_Worksheet
64 protected $current_worksheet;
70 protected $current_line = 0;
75 protected $current_column = 0;
85 * Formats disponibles.
88 protected $formats = array();
92 * Liste des styles utilisés pour les chiffres
93 * constante de type tableau
96 protected $suffixes_entier = array(
102 * Créateur de l'export
104 * @param array $datas données à écrire
108 public function build ($datas)
110 $this->datas = $datas;
111 //Initialisation de la feuille Excel.
112 $this->initWorkbook();
113 // Initialisation de la feuille courante
114 $this->initCurrentWorksheet();
115 // Initialisation des formats
116 $this->initFormats();
118 //On ecrit les données
121 // Finalisation du formatage
122 $this->postFormats();
124 // Fermeture de la feuille Excel
125 $this->closeWorkbook();
130 * Initialisation de l'export Excel.
131 * Mise en place du nom du fichier et de l'UTF-8.
135 protected function initWorkbook()
137 $this->workbook = new \Spreadsheet_Excel_Writer();
138 $this->workbook->setVersion(8);
139 $this->workbook->send($this->nomFichier());
143 * Nom du fichier utilisé pour l'export.
147 abstract protected function nomFichier();
150 * Initialisation d'une feuille qui devient la feuille courante.
154 protected function initCurrentWorksheet()
156 $this->current_worksheet = $this->workbook->addWorksheet();
157 $this->current_worksheet->setInputEncoding('utf-8');
161 * Permet de composer les formats à utiliser.
165 protected function initFormats()
167 $this->initFormatTitreString();
168 $this->initFormatCaseString();
169 $this->initFormatCaseChiffre();
174 * Initialisation d'un format pour un classeur
178 protected function initFormatTitreString()
180 $format = &$this->workbook->addFormat();
181 $format->setSize(10);
183 $format->setAlign('center');
184 $format->setAlign('vcenter');
185 $format->setBottom(1);
188 $format->setRight(1);
189 $format->setTextWrap();
190 $format->setFgColor(30);
191 $this->formats['titre_string'] = $format;
195 * Initialisation d'un format pour un classeur
199 protected function initFormatCaseString()
201 $format = &$this->workbook->addFormat();
202 $format->setSize(10);
203 $format->setAlign('left');
204 $format->setAlign('vcenter');
206 $format->setBottom(1);
207 $format->setRight(1);
209 $this->formats['case_string'] = $format;
213 * Initialisation d'un format pour un classeur
217 protected function initFormatCaseChiffre()
219 $format = &$this->workbook->addFormat();
220 $format->setSize(10);
221 $format->setAlign('center');
222 $format->setAlign('vcenter');
224 $format->setBottom(1);
225 $format->setRight(1);
227 $format->setNumFormat('0.00');
228 $this->formats['case_chiffre'] = $format;
232 * Méthode d'écriture des données dans la feuille courante.
236 abstract protected function writeData();
239 * Mise en forme après écriture.
243 abstract protected function postFormats();
246 * Fermeture propre de l'export Excel.
247 * Clot son écriture et renvoie l'objet au client.
251 protected function closeWorkbook()
253 $this->workbook->close();
260 * Méthode permettant d'écrire quelque chose dans une case.
262 * @param string $line numero de la ligne, commence par 0.
263 * @param string $column numero de la colonne, commence par 0.
264 * @param string $value valeur à inscrire dans la casae
265 * @param string $format format à utiliser pour la case
269 protected function ecrireCase($line, $column, $value, $format)
271 $details = explode('_', $format);
272 if (in_array($details[1], $this->suffixes_entier)) {
278 $this->current_worksheet->writeBlank(
281 $this->formats[$format]
284 if ($int === false) {
285 $this->current_worksheet->writeString(
289 $this->formats[$format]
292 $this->current_worksheet->writeNumber(
296 $this->formats[$format]
303 * Méthode permettant d'écrire quelque chose dans la case courante
305 * @param string $value valeur à mettre dans la case courante
306 * @param string $format format à utiliser pour la case courante
310 protected function ecrireCaseCourante($value, $format)
312 $line = $this->current_line;
313 $column = $this->current_column;
316 $this->current_column,
324 * Méthode permettant de merger avec la case précédente.
326 * @param string $line numero de la ligne, commence par 0.
327 * @param string $column numero de la colonne, commence par 0.
331 protected function mergerAvecCasePrecedente($line=null, $column=null)
333 if ($line === null ) {
334 $line = $this->current_line;
336 if ($column === null) {
337 $column = $this->current_column;
339 $this->current_worksheet->mergeCells(
348 * Passage à la case suivante.
352 protected function next()
354 $this->current_column++;
358 * Passage à la ligne suivante.
362 protected function nextLine()
364 $this->current_column=0;
365 $this->current_line++;