生成 Excel 文件时出错

Error while generating an Excel file

我使用 PHPExcel 库生成“.xlsx”文件。文件已生成,但当我尝试打开它时出现错误:Excel found some unreadable content. Do you want to fix the content? 当我单击 "yes" 时,它会打开包含我想要的内容的文件。

如何避免这个错误?我已经把<?php ?>标签后面的空格都删掉了,在生成文件之前我已经检查过我没有HTML,但是我还是不知道我的错误在哪里.

这是我的代码:

<?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"
);
?>

-----编辑-----

我试着检查修复文件和错误文件之间的区别,所以我用 notpad++ 打开它们。

他们是一些错误文件和修复文件之间不同的字符:

‚›.›”Ò© +@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ìØëÖ¦

谁成了这条线

Ë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ìØëÖ¦

所以我认为这是一个编码问题。但是我还是不知道怎么解决。

问题出在最后的 $_SESSION 代码上。

$_SESSION['downloadstatus'] = array(
    "status"  => "finished",
    "message" => "Done"
);

由于 headers 已经输出,我几乎可以肯定您会在 excel 文档的末尾附加一个 php 警告。进行以下更改,它应该会起作用。

$_SESSION['downloadstatus'] = array(
    "status"  => "finished",
    "message" => "Done"
);

// 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');
exit;

我发现这个问题太愚蠢了。只是不允许在 $objPHPExcel->getProperties() 中放置一些特殊字符,所以我将每个 'é' 替换为 'e' 现在它可以工作了。