Php 生成Excel文件时出错
我使用库PHPExcel生成“.xlsx”文件。该文件已生成,但当我尝试打开它时,出现错误:Php 生成Excel文件时出错,php,phpexcel,Php,Phpexcel,我使用库PHPExcel生成“.xlsx”文件。该文件已生成,但当我尝试打开它时,出现错误:Excel发现一些不可读的内容。是否要修复内容?当我单击“是”时,它会打开包含所需内容的文件 如何避免这个错误?我已经删除了标记之后的所有空格,在生成文件之前,我已经检查了我没有HTML,但是我仍然不知道我的错误在哪里 这是我的密码: <?php include ('/lib/PHPExcel/PHPExcel.php'); include ('/lib/PHPExcel/PHPExcel/IOFa
Excel发现一些不可读的内容。是否要修复内容?
当我单击“是”时,它会打开包含所需内容的文件
如何避免这个错误?我已经删除了
标记之后的所有空格,在生成文件之前,我已经检查了我没有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++打开它们
它们是错误文件和修复文件之间的一些不同字符:
- 有些人成了«
- 有些人也变成了«
- 这条线
“e-W”cPè4èl†:]HèH÷Wèl÷PKèf{Y³_xls/主题/theme1.xmlíYOoè6èS:lè236; e>
因此,我认为这是一个编码问题。但我仍然不知道如何解决它。问题是最后的$\u会话代码
$_SESSION['downloadstatus'] = array(
"status" => "finished",
"message" => "Done"
);
由于标题已经被输出,我几乎可以肯定您会在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”替换每个“e”,现在就可以了。在这种情况下的标准注释……在文本编辑器中打开文件,查找前导/尾随空格、BOM表标题、代码回显的任何明文消息php://output 一旦你发现了其中的任何一个,修复它们,肮脏的解决方法就是使用ob_end_clean()在saveAs测试之前,请尝试保存到Web服务器上的文件,而不是php://output,并查看Excel是否可以读取该文件当我尝试将其保存为服务器上的文件时,我仍然存在问题,并且当我使用notpad++打开该文件时,除了[Content\u Types]之外,我找不到任何可读的文件.xml但如果我的内存很好,这是正常的如果Excel抱怨无法读取的内容,那么通常会有一些东西从某个地方注入到文件中……但是如果在将文件保存到服务器时遇到这个问题,那么可能在PHPExcel中发现了一个bug,尽管使用如此简单的脚本,很难对其进行imag可能导致损坏的ine…从csv_nom_col表中选择了多少项?我尝试了您的解决方案,但它不起作用,我检查了我的php日志文件,启动脚本时没有显示任何内容。