Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel File format or extension is not valid

I'm using phpexcel for export my query in excel file; however after I created file(which is xslx format), I can not open my file in excel. It gives "the file format or extension is not valid. Verify that the file has not been corrupted and that the file extension matches the file format of the file" error. When I open the file in texteditor(mine is npp) I see my php file's css codes and some part of my html codes. My code is like that;

if( ! empty($_POST['export'])){
  $query = "SELECT * FROM asd ORDER BY asdf LIMIT 10";
  $headings = array('Timestamp', 'h1','h2');
      require 'Classes/PHPExcel.php';

  $objPHPExcel = new PHPExcel();
  $objPHPExcel->getActiveSheet()->setTitle('List of Users');

  $rowNumber = 1;
  $col = 'A';
  foreach($headings as $heading) {
    $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
    $col++;
  }

  $rowNumber = 2;
  while ($row = mysql_fetch_row($result)) {
    $col = 'A';
    foreach($row as $cell) {
        $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 
        $col++;
    }
    $rowNumber++;
  }

$objPHPExcel->getActiveSheet()->freezePane('A2');

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

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xlsx"');
header("Content-Transfer-Encoding: binary ");
//ob_end_clean();
//header('Cache-Control: max-age=0');

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

I'm stucked please help. Thank you.

like image 358
Orcun Avatar asked Jun 07 '13 12:06

Orcun


People also ask

How do I fix file format or file extension is not valid?

You can restore the Excel file format, or the file extension is not valid from previous versions. Select the damaged file. Right-click it and click "Properties" > "Previous Version". A list of previous versions will appear; you need to select the option you are interested in and click "Restore" to recover.

Why my XLSX file is not opening?

But Excel since 2007 saves files in the XLSX format. A mismatch between file extension and Excel version may cause the error “Excel cannot open the file because the file format or file extension is not valid”. Therefore, all you need is to modify the file format and it will become available.


1 Answers

As described in the manual.... if anything else is being output to the browser, this will corrupt the output file.

Open the file in a text editor, and look for leading or trailing whitespace characters (spaces, tabs, newlines) or a BOM marker at the beginning of the output, or for any obvious PHP plaintext error messages in the content. These are the most obvious causes of this problem. Once you've identified the spurious characters, check through your script to see where that output is being generated, and remove it.

In your case, that means don't output your css and html.

EDIT

xlsx is the extension for an OfficeOpenXML Excel2007 file, not for a BIFF 8 xls file.... be consistent in your headers (mime type and file extension) and Writer

Either:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xls"');

or

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="userList.xlsx"');
like image 93
Mark Baker Avatar answered Sep 22 '22 02:09

Mark Baker