<?php
//------------------------------------------------------------------.
// Variable de session pour informer sur le statut de la génération |
//------------------------------------------------------------------'
@session_start();
$_SESSION['generationStatut'] = "progress";
//----------------------------------------------------------------------------.
// Termine la session courante, après avoir stocké les données, autrement dit |
// on force le stockage de la variable de session pour qu’elle puisse être |
// utilisé dans le script generationStatus.php lancé en AJAX retardé récursif |
//----------------------------------------------------------------------------'
session_write_close();
//------------------------------.
// Création de l'objet PHPExcel |
//------------------------------'
include_once(dirname(__FILE__)."/lib/php/phpExcel/Classes/PHPExcel.php");
$dateGeneration = date('d/m/Y \à H:i:s');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->setTitle('Exemple de generation phpExcel');
// Header document PHPExcel
$activeSheet->setCellValue('A1',utf8_encode("Suivi des ventes et contrôle"));
$activeSheet->setCellValue('B3',"Pays");
$activeSheet->setCellValue('B4',utf8_encode("Contexte"));
$activeSheet->setCellValue('B5',"Dates des ventes");
$activeSheet->setCellValue('B7',utf8_encode("Nombre de vendeur dans le pays"));
$activeSheet->setCellValue('B8',utf8_encode("Nombre d'usine dans le pays"));
$activeSheet->setCellValue('B9',utf8_encode("Nombre de publicité dans le pays"));
$activeSheet->setCellValue('B10',utf8_encode("Taux de vente"));
$activeSheet->setCellValue('B11',utf8_encode("Taux de rentabilité"));
$activeSheet->setCellValue('C3',utf8_encode("Espagne"));
$activeSheet->setCellValue('C4',utf8_encode("Vente d'équipement"));
$activeSheet->setCellValue('C5',utf8_encode("Du 01/01/2010 au 05/02/2010"));
$activeSheet->setCellValue('E7',71);
$activeSheet->setCellValue('E8',37);
$activeSheet->setCellValue('E9',16);
$activeSheet->setCellValue('E10',utf8_encode("52 %"));
$activeSheet->setCellValue('E11',utf8_encode("23 %"));
$activeSheet->setCellValue('B12',utf8_encode("Document généré le ".$dateGeneration));
$activeSheet->mergeCells('A1:G1');
$activeSheet->mergeCells('C3:D3');
$activeSheet->mergeCells('C4:D4');
$activeSheet->mergeCells('C5:D5');
$activeSheet->mergeCells('B7:D7');
$activeSheet->mergeCells('B8:D8');
$activeSheet->mergeCells('B9:D9');
$activeSheet->mergeCells('B10:D10');
$activeSheet->mergeCells('B11:D11');
$activeSheet->mergeCells('B12:D12');
// Mise en page du Header document PHPExcel
$activeSheet->getStyle('A1:G1')->applyFromArray(
array(
'font' => array('bold' => true, 'size'=>16),
'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER),
'borders'=>array('allborders'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN)),
'fill'=>array('type'=>PHPExcel_Style_Fill::FILL_SOLID,'color'=>array('argb'=>'FF99CCFF'))
)
);
$activeSheet->getStyle('B12')->applyFromArray(array('font' => array('italic' => true)));
$activeSheet->getStyle('B2:B11')->applyFromArray(array('font' => array('bold' => true)));
$activeSheet->getStyle('B3:D5')->applyFromArray(
array('borders'=>array('allborders'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN)))
);
$activeSheet->getStyle('B7:E11')->applyFromArray(
array('borders'=>array('allborders'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN)))
);
$activeSheet->getStyle('E7:E11')->applyFromArray(
array('alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER))
);
// Body document PHPExcel
$activeSheet->setCellValueByColumnAndRow(0, 14, "Ident");
$activeSheet->setCellValueByColumnAndRow(1, 14, "Nom");
$activeSheet->setCellValueByColumnAndRow(2, 14, "Secteur");
$activeSheet->setCellValueByColumnAndRow(3, 14, "Nature");
$activeSheet->setCellValueByColumnAndRow(4, 14, "Ref");
$activeSheet->setCellValueByColumnAndRow(5, 14, "Date\n".utf8_encode("Réponse"));
$activeSheet->setCellValueByColumnAndRow(6, 14, "Controle\n".utf8_encode("des produits"));
$activeSheet->getStyle('A14:G14')->applyFromArray(
array(
'font' => array('bold' => true),
'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER),
'borders'=>array('allborders'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN)),
'fill'=>array('type'=>PHPExcel_Style_Fill::FILL_SOLID,'color'=>array('argb'=>'FF99CCFF'))
)
);
$activeSheet->getStyle('F14')->getAlignment()->setWrapText(true);
$activeSheet->getStyle('G14')->getAlignment()->setWrapText(true);
$activeSheet->getStyle('A14:G14')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
$arrayNatureLibelle = array('Agroalimentaire', 'Usine', 'Université');
$arrayNatureCode = array(151,152,153,168,198,286,292,336,541,542,543,556);
$ligne = 15;
for($i=0;$i<71;$i++) {
$rneEcole = "XID".($i+10);
$nom = "VENTE ".$i;
$secteur = (rand(0,1)==0) ? 'GLOBAL' : 'ATOME';
$natureLibelle = $arrayNatureLibelle[array_rand($arrayNatureLibelle,1)];
$natureCode = $arrayNatureCode[array_rand($arrayNatureCode,1)];
$dateReponse = date("d/m/Y",strtotime("2010-01-01 + "
.rand(0,round((strtotime('2010-02-05')-strtotime('2010-01-01'))/(60*60*24)))." days"));
$campagneValiderEcole = (rand(0,1)==0) ? 'OUI' : 'NON';
$activeSheet->setCellValueByColumnAndRow(0, $ligne, utf8_encode($rneEcole));
$activeSheet->setCellValueByColumnAndRow(1, $ligne, utf8_encode($nom));
$activeSheet->setCellValueByColumnAndRow(2, $ligne, utf8_encode($secteur));
$activeSheet->setCellValueByColumnAndRow(3, $ligne, utf8_encode($natureLibelle));
$activeSheet->setCellValueByColumnAndRow(4, $ligne, utf8_encode($natureCode));
$activeSheet->setCellValueByColumnAndRow(5, $ligne, utf8_encode($dateReponse));
$activeSheet->setCellValueByColumnAndRow(6, $ligne, utf8_encode($campagneValiderEcole));
if(strcmp($campagneValiderEcole,'OUI')==0) {
$activeSheet->getStyle('A'.$ligne.':G'.$ligne)->applyFromArray(
array('fill'=>array('type'=>PHPExcel_Style_Fill::FILL_SOLID,'color'=>array('argb'=>'FFCCFFCC')))
);
}
$ligne++;
}
$activeSheet->getStyle('A15:G'.($ligne-1))->applyFromArray(
array(
'borders'=>array(
'left'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN ),
'right'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN ),
'bottom'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN ),
'vertical'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN ),
'horizontal'=>array('style'=>PHPExcel_Style_Border::BORDER_THIN )
)
)
);
$styleColonneCentrer = array('alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER));
$activeSheet->getStyle('C15:C'.($ligne-1))->applyFromArray($styleColonneCentrer);
$activeSheet->getStyle('E15:G'.($ligne-1))->applyFromArray($styleColonneCentrer);
$activeSheet->getColumnDimension('A')->setWidth(12);
$activeSheet->getColumnDimension('B')->setWidth(40);
$activeSheet->getColumnDimension('C')->setWidth(12);
$activeSheet->getColumnDimension('D')->setWidth(40);
$activeSheet->getColumnDimension('E')->setWidth(6);
$activeSheet->getColumnDimension('F')->setWidth(12);
$activeSheet->getColumnDimension('G')->setWidth(22);
// Set Zone d'impression
$activeSheet->getPageSetup()->setPrintArea('A1:G'.($ligne));
$activeSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$activeSheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$activeSheet->getPageMargins()->SetLeft(0.1);
$activeSheet->getPageMargins()->SetRight(0.1);
$activeSheet->getPageMargins()->setTop(0.4);
$activeSheet->getPageMargins()->setBottom(0.6);
$activeSheet->getPageMargins()->setHeader(0.1);
$activeSheet->getPageMargins()->setFooter(0.4);
$activeSheet->getHeaderFooter()->setOddHeader("&C&HAPPLICATION v.0.1");
$activeSheet->getHeaderFooter()->setOddFooter('&L&BGeneration flux de donnees&RPage &P / &N');
$activeSheet->getPageSetup()->setHorizontalCentered(true);
$activeSheet->getPageSetup()->setVerticalCentered(false);
$activeSheet->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(14, 14);
//-----------------------------------------------------.
// Création du writer en fonction du moge passé en GET |
//-----------------------------------------------------'
$date = date("Y_m_d_H.i.s");
$mode = (isset($_GET['mode'])) ? $_GET['mode'] : null;
$nomFichier = "test_".$date;
switch ($mode) {
// Librairie PDF de phpExcel par défaut utilisé (basé sur TCPDF)
case 'PDF':
ini_set('memory_limit', '-1');
$objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
break;
// On utilise plutot MPDF pour regler les problèmes de no-border et amélioré les performances
case 'MPDF':
ini_set('memory_limit', '-1');
$objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
$objWriter->setUseInlineCSS(true);
ob_clean();
$html = $objWriter->generateHTMLHeader();
$html .= $objWriter->generateStyles();
$html .= $objWriter->generateSheetData();
$html .= $objWriter->generateHTMLFooter();
include_once(dirname(__FILE__)."/lib/php/MPDF46/mpdf.php");
$mpdf = new mPDF();
$mpdf->SetHeader(
array (
'L' => array(
'content' => '',
'font-size' => 10,'font-style' => 'B','font-family' => 'FreeSerif','color'=>'#000000'),
'C' => array(
'content' => ' Exemple',
'font-size' => 9,'font-style' => 'B','font-family' => 'FreeSerif','color'=>'#000000'),
'R' => array(
'content' => '',
'font-size' => 10,'font-style' => 'B','font-family' => 'FreeSerif','color'=>'#000000'),
'line' => 1
),
'O'
);
$mpdf->SetFooter(
array (
'L' => array(
'content' =>
'Suivi de validation des directeurs',
'font-size' => 9,'font-style' => '','font-family' => 'FreeSerif','color'=>'#000000'),
'C' => array(
'content' => '',
'font-size' => 9,'font-style' => 'B','font-family' => 'FreeSerif','color'=>'#000000'),
'R' => array(
'content' => 'Page {PAGENO} / {nbpg}',
'font-size' => 9,'font-style' => '','font-family' => 'FreeSerif','color'=>'#000000'),
'line' => 1
),
'O'
);
$mpdf->WriteHTML($html);
break;
// Génération du document en version Excel5
Case 'Excel5':
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
break;
default:
echo "<pre>Le mode de génération choisi <b>(.".$mode.".)</b> n'existe pas.</pre>";
}
//-----------------------------------------------.
// Informe que la génération du fichier est fini |
//-----------------------------------------------'
session_start();
$_SESSION['generationStatut'] = "fini";
//--------------------------------------------------.
// Envoi le fichier au browser selon le mode choisi |
//--------------------------------------------------'
ob_clean();
switch ($mode) {
case 'PDF':
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename=".$nomFichier.".pdf");
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
break;
case 'MPDF':
$mpdf->Output($nomFichier.".pdf",D);
break;
Case 'Excel5':
header('Content-type:application/vnd.ms-excel');
header('Content-Disposition:inline;filename='.$nomFichier.'.xls');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date dans le passé
$objWriter->save('php://output');
break;
}
?>