PHPExcel generated file via $objWriter->save(‘php://output’)- does it require permissions

codeigniterPHPphpexcel

I have a code in php CodeIgniter that extracts data from database and generates an xls file via PHPExcel.

The problem is that whenever I upload the code into another server, it generates an .xls file with 0kb, and the error while opening is:
"Excel cannot open the file "____" because the file format or file extension not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file".

The incoming data is from same table and all the libraries used are also same.

My questions:
1. Are there any r/w permission applied by the server which makes the phpexcel file 0kb? I am using CodeIgniter.
2. Is there any way to see what is written in the PHPExcel object?
3. Are there any things I am missing out?

My code that generates xls file is:

// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
    $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
    $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

// Set page orientation and size
    $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
    $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);

    $this->load->library('PHPExcel/PHPExcel_IOFactory');

    $file_name = $uri_year . "-" . $uri_month . "_staff_report.xlsx";
// Redirect output to a client web browser (Excel2007)
//ob_end_clean();
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename=' . $file_name);
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//  ob_end_clean();

    $objWriter->save('php://output');

Thank you in advance.

My output as displayed in the test server is:

enter image description here

Solution:
It turns out that the php version of the server1 is 5.2.9
The php version of server2 is 5.1.6

Error generated in server2:
Fatal error: Class 'ZipArchive' not found in /var/www/html/APP_NAME/application/libraries/PHPExcel/Writer/Excel2007.php on line 225

The ZipArchive module requires php version >5.2

After the update, it works.

Thank you guys for your suggestions.

Best Answer

It outputs 0kb or "Excel cannot open the file "_ because the execution of the script encountered unusual error. Enable your error reporting, put this at the top of your script:

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

Yes, you may need to check the proper r/w permission and ownership of PHPExcel.php but the output file does not need anymore.

I have tested your code and it works fine..

<?php

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

/** Include PHPExcel */
require_once('PHPExcel.php');
$objPHPExcel = new PHPExcel();

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

// Set page orientation and size
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

// $this->load->library('PHPExcel/PHPExcel_IOFactory');

$uri_year = "2015";
$uri_month = "now";
$file_name = $uri_year . "-" . $uri_month . "_staff_report.xlsx";

$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', "This is a test");

// Redirect output to a client web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $file_name);
header('Cache-Control: max-age=0');

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

?>

You may need to check if you have the right libraries for Excel2007, i have changed it to Excel5, but you can use Excel2007 and it works fine for me.

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
Related Topic