我使用PHPExcel库从PHP生成excel文件,当它自动下载所有数据时,问题是chrome只显示没有数据的列标题,同时firefox正确下载文件。
我的代码是:
<?php
//Code pour les nouvelles demandes d'agréments
//echo $_POST['sql'] . '\n\n\n';
//IMPORTANT : Il ne faut PAS inclure la connexion mais la mettre comme ci-dessous car bug connu avec PHPEXCEL (fichier gÈnÈrÈ via le navigateur inexploitable !!!!)
try
{
$bdd = new PDO('mysql:host=127.0.0.1;dbname=qque', 'qqe', 'prive', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
}
catch (PDOException $e)
{
print "Erreur !: " . $e->getMessage() . "<br/>";
die();
}
if (!isset($_SESSION))
{
session_start();
}
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)->setTitle('Résultat filtre');
$pharmaciens = $bdd->prepare($_POST['sql']);
$pharmaciens->execute();
$res = $pharmaciens->fetchAll(PDO::FETCH_OBJ);
//print_r($res);
$i = 2;
$temp_nombre_jours = 0;
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'NOM');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'PRENOM');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'ADRESSE');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'CODE POSTAL');
$objPHPExcel->getActiveSheet()->SetCellValue('E1', 'LOCALITE');
$objPHPExcel->getActiveSheet()->SetCellValue('F1', 'PAYS');
//$objPHPExcel->getActiveSheet()->SetCellValue('E1', 'ANNEE DEBUT AGREMENT');
$objPHPExcel->getActiveSheet()->SetCellValue('G1', 'TELEPHONE');
$objPHPExcel->getActiveSheet()->SetCellValue('H1', 'MAIL');
$objPHPExcel->getActiveSheet()->SetCellValue('I1', 'DEBUT AGREMENT');
//redimensionnement des colonnes afin d'avoir des colonnes en autosize
for($col = 'A'; $col !== 'L'; $col++)
{
$objPHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
foreach($res as $donnees)
{
$styleArray = array('font' => array('bold' => true));
//je mets en gras le contenu de ces cellules
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('B1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('C1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('D1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('E1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('F1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('G1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('H1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('I1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('J1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('K1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$i, $donnees->nom);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$i, $donnees->prenom);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$i, $donnees->adresse);
//pays
if(strtolower($donnees->pays) == "belgique")
{
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i, $donnees->code_postal_be);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i, $donnees->libelle_be);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i, "BELGIQUE");
}
elseif(strtolower($donnees->pays) == "france")
{
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i, $donnees->code_postal_fr);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i, $donnees->libelle_fr);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i, "FRANCE");
}
else
{
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$i, $donnees->code_postal_lux);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i, $donnees->libelle_lux);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$i, "LUXEMBOURG");
}
//$objPHPExcel->getActiveSheet()->SetCellValue('E'.$i, $donnees->SUBSTR(date_debut, 1, 4));
$objPHPExcel->getActiveSheet()->SetCellValueExplicit('G'.$i, $donnees->telephone, PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$i, $donnees->mail);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$i, $donnees->date_debut);
$i = $i+1;
}//fin du foreach
try
{
$writer = new PHPExcel_Writer_Excel2007($objPHPExcel);
$writer->save(str_replace('.php', '.xlsx', __FILE__));
echo 'Exportation OK, veuillez cliquer <a href="http://stageoff.ulb.ac.be/classes/Excel/Classes/filtre_generation_excel.xlsx"><img src="../../../images/excel.jpg" /></a> pour sauvegarder le document';
}
catch (PDOException $e)
{
print "Erreur !: " . $e->getMessage() . "<br/>";
die();
}
?>