title: PHPExcel save a CSV is adding the webpage HTML as well
body: <p>I have a webpage that has a radio group as the options for the file format you wish to save. Options are:</p>
  
  <ul>
  <li>.xls</li>
  <li>.xlsx</li>
  <li>.csv</li>
  </ul>
  
  <p>All work but the .csv as it also adds the page HTML to the bottom of the file.</p>
  
  <p>Here is what I'm trying (code snippets to show functionality):</p>
  
  <pre><code>// Creating the format
  $data = $this-&gt;getQueryResults();
  
  $objPHPExcel = new PHPExcel();
  $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle("Report"); 
  
  $objPHPExcel-&gt;getProperties()-&gt;setCreator("me");     
  $objPHPExcel-&gt;getProperties()-&gt;setLastModifiedBy("me");                 
  $objPHPExcel-&gt;getProperties()-&gt;setSubject("Report Stuff");     
  $objPHPExcel-&gt;getProperties()-&gt;setDescription("Report Stuff");
  
  // Next I iterate through the data array
  $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue($col.$rowNumber,$cell);
  $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension($col)-&gt;setAutoSize(true);
  
  // check the radio option selected for the file format
  if($this-&gt;radioXLS-&gt;Checked) {
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="'.$excel_name.'.xls"');
      header('Cache-Control: max-age=0');
  }
  
  if($this-&gt;radioXLSX-&gt;Checked) {
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  
      header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
      header('Content-Disposition: attachment;filename="'.$excel_name.'.xlsx"');
      header('Cache-Control: max-age=0');
  }
  
  if($this-&gt;radioCSV-&gt;Checked) {
      ob_end_clean(); // add/removing this does nothing
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
      $objWriter-&gt;setDelimiter(',');
      $objWriter-&gt;setEnclosure('');
      $objWriter-&gt;setLineEnding("\r\n");
      $objWriter-&gt;setSheetIndex(0);
      ob_end_clean(); // add/removing this does nothing
  
      header('Content-Type: text/csv');
      header('Content-Disposition: attachment;filename="'.$excel_name.'.csv"');
      header('Cache-Control: max-age=0');
  }
  
  $objWriter-&gt;save('php://output');
  </code></pre>
  
  <p>Any thoughts as to why it appends the page HTML to the .csv file?</p>
  
  <p>On a side note this is a Prado project if that matters</p>
  
  <p>UPDATE:</p>
  
  <p>A little more...</p>
  
  <p>I have a webpage that generate a report in a tablature format (Think table/grid). On the same page I have the option to save the date in the tablature format to an Excel .xls (somehow .xlsx is not working now, ugh...). The user has the option to save the file in .xls .xlsx .csv, when clicked the file downloads from that page.</p>
  
  <p>Would this cause the webpage already rendered to be added to the output via: <code>php://output</code>?</p>
  
  <p>UPDATE - Solution:</p>
  
  <p>Yep after looking at the excel files it's also adding the webpage HTML. I have also looked at the output buffer PHP functions but still nothing is working</p>
  
  <pre><code>    while(ob_get_level() &gt; 0) {
          ob_end_clean();
      }
  
      if($this-&gt;radioCSV-&gt;Checked) {                        
          header('Content-Type: text/csv');
          header('Content-Disposition: attachment;filename="'.$excel_name.'.csv"');
          header('Cache-Control: max-age=0');
  
          $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
          $objWriter-&gt;setDelimiter(',');
          $objWriter-&gt;setEnclosure('');
          $objWriter-&gt;setLineEnding("\r\n");
          $objWriter-&gt;setSheetIndex(0);
      } elseif($this-&gt;radioXLSX-&gt;Checked) {
          header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
          header("Cache-Control: no-store, no-cache, must-revalidate");
          header("Cache-Control: post-check=0, pre-check=0", false);
          header("Pragma: no-cache");
          header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
          header('Content-Disposition: attachment;filename="'.$excel_name.'.xlsx"');
          header('Cache-Control: max-age=0');
  
          $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
      } else {
          header('Content-Type: application/vnd.ms-excel');
          header('Content-Disposition: attachment;filename="'.$excel_name.'.xls"');
          header('Cache-Control: max-age=0');
  
          $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
      } 
  
      $objWriter-&gt;save('php://output');
      exit();
  </code></pre>