title: PHPExcel Force Download Issue
body: <p>I know this might have been asked in several pieces, but I could not find an exact answer to the issue. I am using PHPExcel to generate an Excel file (obviously), and the code works to generate the file, but not when I include the code for Force Download, it corrupts the file. My latest version of the script looks like this:</p>
  
  <pre><code>function make_xls_spreadsheet(){
  
  /** Error reporting */
  error_reporting(E_ALL);
  
  /* Set the save path */
  define('XLSX_SAVE_PATH', 'tmp/');
  
  /** Include path **/
  set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
  
  /** PHPExcel */
  include 'PHPExcel.php';
  
  /** PHPExcel_Writer_Excel2007 */
  include 'PHPExcel/Writer/Excel2007.php';
  
  
  /* Create a new PHPExcel Object */
  $objPHPExcel = new PHPExcel();
  
  
  /* Add some metadata to the file */
  $objPHPExcel-&gt;getProperties()-&gt;setCreator("Maarten Balliauw");
  $objPHPExcel-&gt;getProperties()-&gt;setLastModifiedBy("Maarten Balliauw");
  $objPHPExcel-&gt;getProperties()-&gt;setTitle("Office 2007 XLSX Test Document");
  $objPHPExcel-&gt;getProperties()-&gt;setSubject("Office 2007 XLSX Test Document");
  $objPHPExcel-&gt;getProperties()-&gt;setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
  
  /* Set active worksheet to first */
  $objPHPExcel-&gt;setActiveSheetIndex(0);
  $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Segments');
  
  /* Add some data to the worksheet */
  $objPHPExcel-&gt;setActiveSheetIndex(0);
  $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue('A1', 'Hello');
  $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue('B2', 'world!');
  $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue('C1', 'Hello');
  $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue('D2', 'world!');
  
  
  /* Write to server */
  $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  
  $filename = "tony1.xlsx";
  
  // Works fine up to here
  
  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition: attachment;filename="'.$filename.'"');
  header('Cache-Control: max-age=0');
  
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  //$objWriter-&gt;save('php://output');
  $objWriter-&gt;save(XLSX_SAVE_PATH . $filename);
  readfile(XLSX_SAVE_PATH . $filename);
  
  
  echo "DONE!";
  
  $objPHPExcel-&gt;disconnectWorksheets();
  unset($objPHPExcel);
  
  
  
  }
  </code></pre>
  
  <p>Remember, when I remove the force code section, the file generates and I can FTP it down fine. However, doing both generating and forcing the file gives me a corrupt file. Normally I can click "Open &amp; Repair" (Office2011 MacOSX) but obviously this is not desirable.</p>
  
  <p>Could someone please help me understand:</p>
  
  <ol>
  <li>Why it is being generated as corrupt? And why it works fine when I don't force download.</li>
  <li>What the proper order for saving/forcing is (using PHP's header() function)</li>
  <li>If there is a better way of doing this.</li>
  </ol>
  
  <p>Much appreciated!!</p>
  
  <p>**** Update **** Here is the code when I click "Fix &amp; Repair":</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"&gt;
      &lt;logFileName&gt;Repair Result to tony1 03178.xml&lt;/logFileName&gt;
      &lt;summary&gt;Errors were detected in file 'Macintosh HD:Users:tony.diloreto:Downloads:tony1.xlsx'&lt;/summary&gt;
      &lt;additionalInfo&gt;&lt;info&gt;Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.&lt;/info&gt;&lt;/additionalInfo&gt;
  &lt;/recoveryLog&gt;
  </code></pre>