1

I am experiencing an error on my code "File Format or File Extension not valid". I am using the PHP Excel class

This is my code:

<?php
session_start();
ini_set('max_execution_time', 1200); //20 mins
ob_start();

/** Error reporting */
error_reporting(E_ALL);

 /** Include path **/
ini_set('include_path', ini_get('include_path').';../classes/');

/** PHPExcel */
include '../classes/PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include '../classes/PHPExcel/Writer/Excel2007.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();


require_once '../classes/PHPExcel.php';


ini_set('memory_limit', '-1');
    //Untested... pulled from the manual as the way to write with PHPExcel
    //Save Excel 2007 file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();
    //We'll be outputting an excel file
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    //It will be called file.xls
    header("Content-Disposition: attachment;filename=\"Past_Due_Report.xls\"");
    header("Cache-Control: max-age=0");
    $objWriter->save('php://output');
    exit();
?>

Any help will be greatly appreciated Thanks

Bongsky
  • 493
  • 3
  • 12
  • 23
  • instead of opening the file save it and look at it in a text editor –  Jun 23 '17 at 04:05
  • I think when we are using PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007') i.e. "Excel2007" then the file extension will be .xlsx... so you have tho change the "Past_Due_Report.xls" to "Past_Due_Report.xlsx" – Ashu Jun 23 '17 at 04:20
  • @pAsh i already changed to .xlsx, same error – Bongsky Jun 23 '17 at 04:22
  • remove '\' before & after the -> Past_Due_Report.xlsx – Ashu Jun 23 '17 at 04:27
  • @pAsh already removed '\' still getting the same error – Bongsky Jun 23 '17 at 05:17

2 Answers2

1

i think the solution of this problem is the same as here: Google Chrome errors while exporting XLS file using PHP

just add a space between attachement; and filename, that way :

header("Content-Disposition: attachment; filename=\"Past_Due_Report.xls\"");
cetipabo
  • 453
  • 5
  • 12
-1

Below code works from my side.. In your code you create object 1st "PHPExcel" then include the file.. i do some needful changes. check link : https://github.com/PHPOffice/PHPExcel

session_start();
ini_set('max_execution_time', 1200); //20 mins
ob_start();
/** Error reporting */
error_reporting(E_ALL);
 /** Include path **/
ini_set('include_path', ini_get('include_path').';../classes/');

/** PHPExcel */
/*include '../classes/PHPExcel.php';*/

/** PHPExcel_Writer_Excel2007 */
/*include '../classes/PHPExcel/Writer/Excel2007.php';*/

require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
ini_set('memory_limit', '-1');
    //Untested... pulled from the manual as the way to write with PHPExcel
    //Save Excel 2007 file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();
    //We'll be outputting an excel file
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    //It will be called file.xls
    header("Content-Disposition: attachment;filename=\"Past_Due_Report.xls\"");
    header("Cache-Control: max-age=0");
    $objWriter->save('php://output');
    exit();
Ashu
  • 1,320
  • 2
  • 10
  • 24