I use the library PHPExcel for generating ".xlsx" files. The file is generated, but when I try to open it I get an error: Excel found some unreadable content. Do you want to fix the content?
When I click on "yes" it opens my file with the content I want.
How can I avoid this error? I've already deleted all the spaces after the <?php ?>
tags, I've already checked that I don't have HTML before the file is generated, but I still don't know where my error is.
Here is my code:
<?php
include ('/lib/PHPExcel/PHPExcel.php');
include ('/lib/PHPExcel/PHPExcel/IOFactory.php');
// I use session for now when the excel is generate
$_SESSION['downloadstatus'] = array(
"status" => "pending"
);
// Creation of the Excel File
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator("Temporaris")
->setLastModifiedBy("Temporaris")
->setTitle("Template Relevé des heures intérimaires")
->setSubject("Template excel")
->setDescription("Template excel permettant la création d'un ou plusieurs relevés d'heures")
->setKeywords("Template excel");
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
// Add the content of Excel File
$indiceColumn = "A";
$indiceLine = 1;
$defaultColumns = array("Matricule", "Nom_Prenom", "Siret_etablissement", "Regate_etablissement", "Centre_analytique", "Date");
foreach ($defaultColumns as $columnName) {
$sheet->SetCellValue($indiceColumn . $indiceLine, $columnName);
$indiceColumn++;
}
$listColumnName = EDIXIS_db_query("select csv_nom_col FROM temporaris_eu_config_ent_csv WHERE id_config = " . $id_sel . " ORDER BY id");
while ($columnName = EDIXIS_db_fetch_object($listColumnName)) {
$sheet->SetCellValue($indiceColumn . $indiceLine, $columnName->csv_nom_col);
$indiceColumn++;
}
// Send the Excel File to the user
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="template.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
$_SESSION['downloadstatus'] = array(
"status" => "finished",
"message" => "Done"
);
?>
----- Edit -----
I try to check the difference between a repair file and the error file so i open them with notpad++.
Their is some caracters who are differents between the error file and the repair file :
- some • who became «
- some " who became « too
- This line
‚›.›”Ò© +@SdÕ Eë ݲäÄfÃH*²?ÙÑý®³+ @ î幜ᨽ]YC^!&íÝŒVSN 8é•v‹}œßOn(IY8%Œw0£kHô¶{×ÊÐHá{ôbÖ‚\jd˜ÑeΡa,É%X‘¦èp(>ûhEÆe\° ä‹X «9ÿÄ,d¡DlNÂHwH%ÈÐGS J20
ÁåĪiÅŽÞѦ77åÄiu^xÓºîUÒã0ÓáºXñüûõðíg)u¢ÝØ* ´k•ld‘}ìæƒ"êÔ²“ÏcHù›ý¬A}YŸ9/ÕvWÜ– Šà¡šm {åéúîëüžv5¯>N8>æüsS󦮯xÝp>žàr¤Ú]Òÿa÷”Ò¬³R•Á,ò¯ìuCËúˆ/íò&j+4.Js¶;ƽè’Q‡Œsy$ÀJ‚!883ØéLŒ 2nÄè"ê}ïˆïI0}ÒÈO$–À„Ê6¯Dœ‚Ç Ôÿþ2ÿR¬{i¼ªX>ªtá;ÕÊoÚÂG¼/v¹:ûeº¿PK ”J¤Fs‘{Y³ ¦ xl/theme/theme1.xmlíYOoÛ6¿ïSº·²lÉu‚:EìØëÖ¦
who became this line
Ë7\ ÖIDiV´W¬„ +¸5ölkÖ;›ö‘ûLܺwH‘"çÇ3ž´·kgÙ3
2ÁÏy=8¯‚6~9ç‹»ÉÎR–^K<Ìù¿í^µ*6* |óÄäS£âœ¯rŽIÀÉ4%‡'ñ1 “™–¸Qª'¹1«ª÷ÂA–Zf)Fà$‰|Ôꈌ=ÚÐJ€>'QOkqòf@—^ÜP”3§3yáEëA<º×ÉÃ0L‡›b¥ó×â×ý×¥Ô‰ñc«ð®ÕªQ2ìàb@‰&µâìóØB+S¾§f?П6ÎkµÝ·#€ft¨fWÂAùyóùËâŽw³ª~7©èy»¨>6³º©goªYSUã . 'ªÛ'ýö@)È&[(UYÊbßéÊž·LÓ°¬ GzŸ·hœ4´(ÍÙí÷’K¡‰™æòD€µËh>hf¨Ó™YÉnåèbúuïYèY´}2ÄOK"e—W"ÎÁcPêÿ•ÿ )Öƒ4^Õl†€:]ùεò[¶H÷%®W¿L÷PK «J¤Fs‘{Y³ ¦ xl/theme/theme1.xmlíYOoÛ6¿ïSº·²lÉu‚:EìØëÖ¦
So i think it's an encode problem. But i still don't know how can i resolve it.