php PHPExcel 文件无法打开文件,因为文件格式或文件扩展名无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14252465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel file cannot open file because the file format or file extension is not valid
提问by
I'm stuck with this problem, it's not displaying the actual excel file. Please check my code below:
我被这个问题困住了,它没有显示实际的 excel 文件。请在下面检查我的代码:
/** Error reporting */
error_reporting(E_ALL);
/** PHPExcel */
require_once 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
// Create new PHPExcel object
#echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();
$excel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Payroll");
if(!$result){
die("Error");
}
$col = 0;
$row = 2;
while($mrow = mysql_fetch_assoc($result)) {
$col = 0;
foreach($mrow as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
}
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Scholar Id')
->setCellValue('B1', 'Lastname')
->setCellValue('C1', 'Middlename')
->setCellValue('D1', 'Firstname')
->setCellValue('E1', 'Barangay')
->setCellValue('F1', 'Level')
->setCellValue('G1', 'Allowance')
->setCellValue('H1', 'Has claimed?');
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(14);
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getAlignment()- >setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->setShowGridlines(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->applyFromArray(
array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'FFFF00')
)
)
);
// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
#$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="payroll.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
回答by
I got it working now! Thanks to this phpexcel to download
我现在开始工作了!感谢这个phpexcel下载
I changed the code to this:
我把代码改成这样:
// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="payroll.xlsx"');
$objWriter->save('php://output');
I think this line:
我认为这一行:
ob_end_clean();
solved my problem.
解决了我的问题。
回答by marco burrometo
I don't know if i can help i had the same problem and i solved with
我不知道我是否可以帮助我遇到了同样的问题并且我解决了
ob_end_clean();
i put it just after
我把它放在后面
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
So don't change the header and save it as xslx anyway, the problem is the buffer!
所以无论如何不要更改标题并将其保存为xslx,问题是缓冲区!
回答by Andron
Please make sure that all files (e.g. that are included) are in UTF-8 without BOMencoding.
You can identify this in different ways, e.g. see this link.
请确保所有文件(例如包含的文件)都是 UTF-8,没有 BOM编码。
您可以通过不同的方式识别这一点,例如查看此链接。
Only if you need UTF-8 with BOM- please use ob_end_clean();before data outputing to browser, as pointed in other answers here.
仅当您需要带有 BOM 的UTF-8时- 请ob_end_clean();在将数据输出到浏览器之前使用,如此处其他答案中所述。
回答by Mark Baker
The most likely culprit if this is a cut-and-paste of your script is the
如果这是脚本的剪切和粘贴,最有可能的罪魁祸首是
echo date('H:i:s') . " Write to Excel2007 format\n";
If you're sending to the browser for download, then there must be no other output (echoes, print statements, dropping in and out of PHP) than the output generated to php://output by PHPExcel itself
如果您发送到浏览器进行下载,那么除了 PHPExcel 本身生成到 php://output 的输出之外,必须没有其他输出(回声、打印语句、放入和取出 PHP)
回答by Ahmad Wijaya
I have the same problem, the problem is simple. Just put code below :
我有同样的问题,问题很简单。只需将代码放在下面:
ob_end_clean();
after :
后 :
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
回答by cetipabo
i think the solution of this problem is the same as here: Google Chrome errors while exporting XLS file using PHP
我认为此问题的解决方案与此处相同: Google Chrome errors while exporting XLS file using PHP
just add a space between attachement;and filename, that way :
只需在附件之间添加一个空格;和文件名,这样:
header("Content-Disposition: attachment; filename=\"Past_Due_Report.xls\"");
as i can see in your own answer that's what you did, and is probably what fixed your problem.
正如我在您自己的回答中看到的那样,这就是您所做的,并且可能解决了您的问题。
回答by ARN
I had the same problem but calling ob_end_clean()didn't work. My CMS (drupal 7) had messed up my headers so I got a new line (hex 0A) at the beginning of the content even if I called ob_end_clean()before the file output.
我遇到了同样的问题,但呼叫ob_end_clean()不起作用。我的 CMS(drupal 7)弄乱了我的标题,所以0A即使我ob_end_clean()在文件输出之前调用,我在内容的开头也有一个新行(十六进制)。
With the code below I finaly got rid of the leading new line:
使用下面的代码,我最终摆脱了领先的新行:
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);
ob_clean();
回答by Python
This answer save my life. Thanks @ARN
这个答案救了我的命。谢谢@ARN
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="export_result.xlsx"');
for ($i = 0; $i < ob_get_level(); $i++) {
ob_end_flush();
}
ob_implicit_flush(1);
ob_clean();
$objWriter->save("php://output");

