I am trying to create a new Excel file by using PHPExcel library with this code :
include ('/lib/PHPExcel/PHPExcel/IOFactory.php');
include ('/lib/PHPExcel/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()
->setCreator("admin")
->setLastModifiedBy("admin")
->setTitle("Test")
->setSubject("template file")
->setDescription("template file")
->setKeywords("Prout");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(dirname(__FILE__).'/file/test.xlsx');
The file is created, but when i try to open it my excel 2010 tells me there is unreadable content inside the file and i can't open it.
I tried a different way on a different forum but i am always arriving to the same result. I do this manipulation by using Ajax with jquery. Can this be the cause of this error?
I find how to resolve my problem ... but only half of it. Because it's work with this code :
$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);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', "12");
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="excel.xls"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
But it's Excel5 not Excel2007 and when i remplace Excel5 by Excel2007 i have the same error message.
Did you try this ?
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);