title: PHPExcel file cannot open file because the file format or file extension is not valid
body: <p>I'm stuck with this problem, it's not displaying the actual excel file. Please check my code below:</p>
  
  <pre><code>/** 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-&gt;getProperties()-&gt;setTitle("Payroll");
  
  if(!$result){
  die("Error");
  }
  $col = 0; 
  $row = 2; 
  while($mrow = mysql_fetch_assoc($result)) { 
  $col = 0; 
  foreach($mrow as $key=&gt;$value) { 
      $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $value); 
      $col++; 
  } 
  $row++;  
  } 
  
   // Set active sheet index to the first sheet, so Excel opens this as the first sheet
   $objPHPExcel-&gt;setActiveSheetIndex(0)
          -&gt;setCellValue('A1', 'Scholar Id')
          -&gt;setCellValue('B1', 'Lastname')
          -&gt;setCellValue('C1', 'Middlename')
          -&gt;setCellValue('D1', 'Firstname')
          -&gt;setCellValue('E1', 'Barangay')
          -&gt;setCellValue('F1', 'Level')
          -&gt;setCellValue('G1', 'Allowance')
          -&gt;setCellValue('H1', 'Has claimed?');
          $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A1:H1')-&gt;getFont()-&gt;setBold(true);
          $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('A')-&gt;setWidth(12);
          $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('B')-&gt;setWidth(18);
          $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('C')-&gt;setWidth(18);
          $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('D')-&gt;setWidth(18);
          $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('E')-&gt;setWidth(18);
          $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('F')-&gt;setWidth(12);
  $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('G')-&gt;setWidth(12);
  $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('H')-&gt;setWidth(14);
  $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A1:H1')-&gt;getAlignment()- &gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  $objPHPExcel-&gt;getActiveSheet()-&gt;setShowGridlines(true);
  
  $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A1:H1')-&gt;applyFromArray(
  array(
      'fill' =&gt; array(
          'type' =&gt; PHPExcel_Style_Fill::FILL_SOLID,
          'color' =&gt; array('rgb' =&gt; '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-&gt;save('php://output');
  </code></pre>