我在Laravel 4中练习phpExcel.以下代码基于this phpExcel exercise.它在Laravel外部运行良好.由于我仅修改了一些必要的代码以与安装路径匹配,因此我相信如果使用phpExcel,许多Laravel用户将面临同样的问题.我相信某些专家确实已经解决了这一问题,并希望他们能为我提供帮助.
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
ob_clean();
flush();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('d:/l4/test/01simple.xlsx');
ob_end();
exit;
但是,将代码放入View时,出现以下错误消息:
Excel cannot open the file 01simple.xlsx because the file format or file extension is not valid .
我在Google上搜索了很多,并尝试了几种选择,但到目前为止还没有运气.我看到过类似的问题,有人通过在标头下添加ob_clean()和flush()来解决此问题,但这在Laravel中不起作用.
我有MS excel 2010,可以手动打开下载的文件.我正在使用WAMP(php 5.4),并已在Firefox和IE上进行了测试.
这是View中的完整代码:
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Taipei');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
$pRoot=dirname(dirname(dirname(__FILE__))).'/vendor';
require_once $pRoot.'/phpoffice/phpexcel/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'TW');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel2007)
//$file = "myfile.xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
ob_clean();
flush();
//readfile($file);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('d:/l4/test/01simple.xlsx');
ob_end();
exit;
解决方法:
首先:
"phpexcel/phpexcel": "dev-master"
将上述行放在ur composer.json的Laravel的require选项中.然后做作曲家更新,之后,你不必做require_once工作,Laravel为你做了.
第二:您是否要求使用Ajax生成此excel动作?如果你愿意,我建议你用通常的方式.因为Ajax请求方法会影响数据格式.
第三:
请勿使用此方法:$objWriter = PHPExcel_IOFactory :: createWriter($objPHPExcel,’Excel2007′);要创建’Excel2007’编写器对象,您只能使用此方法来创建’Excel2005’编写器对象.使用以下方法:
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setOffice2003Compatibility(true);
$objWriter->save($path);
祝你好运!