PHPExcel file cannot open file because file format or file extension is invalid

I am stuck with this problem, it is not displaying the actual Excel file. Please check my code below:

/** Error reporting */ error_reporting(E_ALL); /** PHPExcel */ require_once 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; // Create new PHPExcel object #echo date('H:i:s') . " Create new PHPExcel object\n"; $objPHPExcel = new PHPExcel(); $excel = new PHPExcel(); $objPHPExcel->getProperties()->setTitle("Payroll"); if(!$result){ die("Error"); } $col = 0; $row = 2; while($mrow = mysql_fetch_assoc($result)) { $col = 0; foreach($mrow as $key=>$value) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value); $col++; } $row++; } // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Scholar Id') ->setCellValue('B1', 'Lastname') ->setCellValue('C1', 'Middlename') ->setCellValue('D1', 'Firstname') ->setCellValue('E1', 'Barangay') ->setCellValue('F1', 'Level') ->setCellValue('G1', 'Allowance') ->setCellValue('H1', 'Has claimed?'); $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(18); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12); $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(14); $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getAlignment()- >setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->setShowGridlines(true); $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->applyFromArray( array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'FFFF00') ) ) ); // Save Excel 2007 file echo date('H:i:s') . " Write to Excel2007 format\n"; #$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="payroll.xlsx"'); header('Cache-Control: max-age=0'); $writer->save('php://output'); 
+9
source share
7 answers

Now i'm working! Thanks to this phpexcel for download

I changed the code to this:

 // Save Excel 2007 file #echo date('H:i:s') . " Write to Excel2007 format\n"; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); ob_end_clean(); // We'll be outputting an excel file header('Content-type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename="payroll.xlsx"'); $objWriter->save('php://output'); 

I think this line:

 ob_end_clean(); 

solved my problem.

+42
source

I donโ€™t know if I can help, I had the same problem and I solved with

 ob_end_clean(); 

I will put it right after

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

So do not change the header and save it as xslx anyway, the problem is in the buffer!

+5
source

Please make sure that all files (for example, are included) are in UTF-8 encoding without specification .
You can identify it differently, for example. see link.

Only if you need UTF-8 with a specification , use ob_end_clean(); before sending data to the browser as indicated in other answers here.

+2
source

The most likely culprit, if it is to cut and paste your script, is

 echo date('H:i:s') . " Write to Excel2007 format\n"; 

If you send to the browser for download, there should be no other way out (echo signals, print instructions falling out of and from PHP) than the output generated in php: // the output of PHPExcel itself

+1
source

I have the same problem, the problem is simple. Just enter the code below:

 ob_end_clean(); 

after:

 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
0
source

I think the solution to this problem is the same as here: Google Chrome errors when exporting XLS file using PHP

just add a space between attachments; and file name , thus:

 header("Content-Disposition: attachment; filename=\"Past_Due_Report.xls\""); 

as I see in your own answer, what you did, and probably this is what solved your problem.

0
source

I had the same problem, but the call to ob_end_clean() did not work. My CMS (drupal 7) messed up my headers, so I got a new line (hex 0A ) at the beginning of the contents, even if I called ob_end_clean() before the file output.

Using the code below, I finally got rid of the new line:

  for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); } ob_implicit_flush(1); ob_clean(); 
0
source

All Articles