duanchao5258 2015-04-30 10:41
浏览 49
已采纳

生成Excel文件时出错

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Ù32ÁÏ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.

  • 写回答

2条回答 默认 最新

  • dongzheng8463 2015-05-04 07:49
    关注

    I find the problem it was so stupid. It's just that it's not allow to put some specials characters in $objPHPExcel->getProperties() so i replace every 'é' by 'e' and now it's work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误