Numan Karaaslan
Numan Karaaslan

Reputation: 1645

PHPExcel output file is empty

I downloaded PHPExcel library but could not use it. I am using one of the examples in the Library but it downloads an empty excel file. Here is my code.

require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("document");
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Hello')
        ->setCellValue('B1', 'world!');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->setActiveSheetIndex(0);
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment;filename="deneme.xls"');

Am i forgetting something here? It is simple but downloaded excel file is empty. Does anyone have an idea?

EDIT: Solved: I have to add these lines:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

Upvotes: 0

Views: 4397

Answers (2)

Frank Linno WELL
Frank Linno WELL

Reputation: 27

Add this:

header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
die();

Upvotes: 3

Mark Baker
Mark Baker

Reputation: 212412

You're forgetting to instantiate a writer and then save

Upvotes: 1

Related Questions