PHPExcel - 我的 excel 文件中没有内容
PHPExcel - No content in my excel file
我尝试在 Excel 文件中导出时间表列表,但是当我打开 Excel 文件时出现任何内容。 Excel 显示此消息:
"Désolé... Nous avons trouvé un problème dans le contenu de "test_export.xlsx", mais nous pouvons essayer de recupérer le maximum de contenu. Si la source de ce classeur est fiable, cliquez sur Oui."
如果我点击是,我的电子表格是空的。
这是我的 TimesheetController 中的代码:
public function exportAction()
{
$request = $this->getRequest();
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$timesheetsArray = array();
$timesheetsObject = array();
if($request->isPost())
{
$data = $request->getPost();
foreach($data['rows'] as $id)
{
$timesheetsObject[] = $em->getRepository($this->repositoryName)->find($id);
}
foreach($timesheetsObject as $object){
$object->setIdFiche((string)$object->getIdFiche());
$object->setEvaUser((string)$object->getEvaUser());
$object->setId((string)$object->getId());
$object->setProject((string)$object->getProject());
$object->setUser((string)$object->getUser());
$object->setEstimatedTime((string)$object->getEstimatedTime());
$object->setTimeSpend((string)$object->getTimeSpend());
$object->setStartDate($object->getStartDate()->format('Y-m-d H:i:s'));
$object->setEndDate($object->getEndDate()->format('Y-m-d H:i:s'));
$timesheetsArray[] = $object->getArrayCopy();
}
}
$export = new Export\ExportExcel();
$export->export($timesheetsArray);
return new ViewModel(array(
'export' => $export
));
}
这是我的导出Excel class :
class ExportExcel {
public function __construct(){
}
function export($timesheets)
{
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
$workbook = new \PHPExcel();
$workbook->createSheet();
$workbook->getActiveSheet()->fromArray($timesheets, NULL, 'A2');
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test_export.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
$objWriter->save('php://output');
}
}
我使用 PHP、ZendFramework 和 DoctrineORM。
你有解决办法吗?
谢谢!
对不起,如果我的英语不好。
发现错误:实际上,该函数有两次调用,在这两次调用之间,isPost() 被重置为 'false',因此第二次调用时条件不成立。这就是 Excel 数组最后为空的原因。
所以我找到了第二个电话的位置。但是,如果我删除它,我的 Excel 文件将不会保存,因为我的“导出”按钮不会转到导出视图。
你有解决办法吗?
我尝试在 Excel 文件中导出时间表列表,但是当我打开 Excel 文件时出现任何内容。 Excel 显示此消息:
"Désolé... Nous avons trouvé un problème dans le contenu de "test_export.xlsx", mais nous pouvons essayer de recupérer le maximum de contenu. Si la source de ce classeur est fiable, cliquez sur Oui."
如果我点击是,我的电子表格是空的。
这是我的 TimesheetController 中的代码:
public function exportAction()
{
$request = $this->getRequest();
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$timesheetsArray = array();
$timesheetsObject = array();
if($request->isPost())
{
$data = $request->getPost();
foreach($data['rows'] as $id)
{
$timesheetsObject[] = $em->getRepository($this->repositoryName)->find($id);
}
foreach($timesheetsObject as $object){
$object->setIdFiche((string)$object->getIdFiche());
$object->setEvaUser((string)$object->getEvaUser());
$object->setId((string)$object->getId());
$object->setProject((string)$object->getProject());
$object->setUser((string)$object->getUser());
$object->setEstimatedTime((string)$object->getEstimatedTime());
$object->setTimeSpend((string)$object->getTimeSpend());
$object->setStartDate($object->getStartDate()->format('Y-m-d H:i:s'));
$object->setEndDate($object->getEndDate()->format('Y-m-d H:i:s'));
$timesheetsArray[] = $object->getArrayCopy();
}
}
$export = new Export\ExportExcel();
$export->export($timesheetsArray);
return new ViewModel(array(
'export' => $export
));
}
这是我的导出Excel class :
class ExportExcel {
public function __construct(){
}
function export($timesheets)
{
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
$workbook = new \PHPExcel();
$workbook->createSheet();
$workbook->getActiveSheet()->fromArray($timesheets, NULL, 'A2');
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test_export.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
$objWriter->save('php://output');
}
}
我使用 PHP、ZendFramework 和 DoctrineORM。
你有解决办法吗?
谢谢!
对不起,如果我的英语不好。
发现错误:实际上,该函数有两次调用,在这两次调用之间,isPost() 被重置为 'false',因此第二次调用时条件不成立。这就是 Excel 数组最后为空的原因。
所以我找到了第二个电话的位置。但是,如果我删除它,我的 Excel 文件将不会保存,因为我的“导出”按钮不会转到导出视图。
你有解决办法吗?