title: PHPExcel file could be corrupted or unsafe
body: <p>When I generate an Excel report using PHPExcel I get this error: </p>
  
  <blockquote>
    <p>"The file format and extension of 'test.xls' don't match. The file
    could be corrupted or unsafe. Unless you trust its source, don't open
    it. Do you want to open it anyway?"</p>
  </blockquote>
  
  <p>This is my set up -- PHP 5.4/PHPExcel 1.7.9/Windows 7 </p>
  
  <p>When I click 'OK' to open the unsafe excel file anyway it either is empty or gibberish. </p>
  
  <p><strong>HTML Code:</strong> it allows the the user to select a report from the drop down then they select the button if they either want to preview the report, generate an excel file, or create a PDF file. But in this case I'm working on getting the excel files to generate. </p>
  
  <pre><code>&lt;!DOCTYPE html"&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;meta charset=utf-8" /&gt;
          &lt;title&gt;Generate Reports&lt;/title&gt;
  
        &lt;link rel="stylesheet" type="text/css" href="style.css" media="screen" /&gt;
  
          &lt;!--Display the Criteria screen only if Title Org Codes Report (rpt3) is selected--&gt; 
          &lt;script type="text/javascript"&gt;
              function showForm() {     
                  var selopt = document.getElementById("selReport").value;
  
                  if (selopt === "rpt3") {
                      document.getElementById("criteria").style.display = "block";
                  }
  
                  else {
                      document.getElementById("criteria").style.display = "none";
                  }
  
              }
        &lt;/script&gt;
  
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div id="wrapper"&gt;
  
                  &lt;div id="content"&gt;
  
                      &lt;div id="reports" class="center"&gt;
                          &lt;h3&gt; Generate Reports: &lt;/h3&gt;
  
                          &lt;!--Display the list of reports--&gt;
                          &lt;form id="frm1" name="frm1"  method="post" action="Reports.php"&gt;
                              &lt;!-- Excel, PDF, View buttons --&gt;
                              &lt;input type="submit" value="Preview Report" id="view" name="view"&gt;
                              &lt;input type="submit" value="Export to Excel" id="excel" name="excel"&gt;
                              &lt;input type="submit" value="Publish as PDF" id="pdf" name="pdf"&gt;
                              &lt;br&gt;&lt;br&gt;&lt;br&gt;
  
                              Select a Report:
                              &lt;select id="selReport" name="selReport" onclick="showForm();"&gt;
                                  &lt;option&gt;&lt;/option&gt;
                                  &lt;option value="rpt1"&gt;Units/Ranges Summary&lt;/option&gt;
                                  &lt;option value="rpt2"&gt;Divisions Table&lt;/option&gt;  
                                  &lt;option value="rpt3"&gt;Title Codes&lt;/option&gt;
                              &lt;/select&gt;
  
  
                              &lt;!--Creates the criteria drop down menu--&gt;           
                              &lt;div id="criteria" style="display:none"&gt;
                                  &lt;br&gt;&lt;br&gt;&lt;h3&gt;Selection Criteria for Reports:&lt;/h3&gt;
                                      Title File Status:
                                      &lt;select name="selCriteria" id="selCriteria" onchange="showForm();"&gt;
                                          &lt;option&gt;&lt;/option&gt;
                                          &lt;option value="active"&gt;Active&lt;/option&gt;
                                          &lt;option value="inactive"&gt;Inactive&lt;/option&gt;
                                          &lt;option value="all"&gt;All&lt;/option&gt;
                                      &lt;/select&gt;    
                              &lt;/div&gt;  &lt;!--end criteria --&gt;
  
                          &lt;/form&gt; &lt;!-- end frm1  --&gt;
  
                       &lt;/div&gt; &lt;!-- end #reports --&gt;
  
                  &lt;/div&gt; &lt;!-- end #content --&gt;
  
      &lt;/div&gt; &lt;!-- end #wrapper --&gt;
  &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>
  
  <p>Here is the <strong>PHP file</strong>: where the PHPExcel code is executed. I built if else statements to indicate which report the user selected and run only that report.</p>
  
  <pre><code>  &lt;?php
      //Get the user selection and put into variables
      $varRpt = $_POST['selReport'];
      $varCrit = $_POST['selCriteria'];
  
      // require the PHPExcel classes
      require 'PHPExcel/Classes/PHPExcel.php'; 
  
      // PHPExcel_Writer_Excel2007 
      require 'PHPExcel/Classes/PHPExcel/Writer/Excel2007.php';
  
      //-------------------------------
      // Connect to the MySQL database
      //-------------------------------
      $dbhost = "localhost"; 
      $dbuser = "root"; 
      $dbpass = "*******"; 
      $dbname = "test"; 
  
      mysql_connect($dbhost,$dbuser,$dbpass)or die ("Could not connect to mysql because ".mysql_error()); 
  
      mysql_select_db($dbname)or die("Unable to select database" .mysql_error()); 
  
      //-------------------------------------
      // Figure out which report to generate
      //-------------------------------------
  
          if ($varRpt == "rpt1")
          {
              $query =  "SELECT a.div_id, a.unit_id, b.unit_beg, b.unit_end, a.title_org, c.long_desc, c.short_desc FROM depunits a LEFT OUTER JOIN unitRanges b ON ( a.unit_id )= ( b.unit_id )  LEFT OUTER JOIN orgcodes c ON ( a.title_org )= ( c.l1l5_id ) ORDER BY a.div_id, a.unit_id" ;
  
              //Column headings
              $headings = array('Div_id','Unit Id','Unit Begin','Unit End','Title Org','Long Desc','Short Desc');
  
              // Sheet name
              $title = "Summary Report Units/Ranges";
  
              // Name of the saved excel file
              $filename = "Rpt1_" . date('Ymd') . ".xls";  
          }
          else
              if ($varRpt == "rpt2")
                  {
  
                      $query = "SELECT alldiv_id, div_id, L1l2_id, L2_id, L1l3_id, L2l3_id, Exec_beg, Exec_end, Csa_id, Area_id, Area_Desc, Short_Desc, Long_Desc FROM divisions WHERE avail_ind='Y' AND active_ind='Y' ORDER BY alldiv_id "; 
  
                      // Column Labels
                      $headings = array('All','Div','L1L2','L2','L1L3','L2L3','Exec Begin','Exec End','CSA','Area Id','Area Desc','Short Desc','Long Desc');
  
                      // Report Title 
                      $title = "Divisions Table";
  
                      // name of the saved excel file
                      $filename = "Rpt2_" . date('Ymd') . ".xls"; 
  
                  } // end $varRpt == "rpt2"
  
                  else
                      if ($varRpt == "rpt3")
                      {
                          //Column heading
                          $headings = array('Title Code','Short Title','Long Title','Status');
  
                          // Report title
                          $title = "Title Codes";
  
                          // Name of the saved file
                          $filename = "Rpt3_" . date('Ymd') . ".xls";
  
                          if ($varCrit == "active")
                          {
                              $query = "SELECT L2l5, Stitl, Ltitl, Status FROM Tl2l5 WHERE UPPER(TRIM(status))= 'A' ORDER BY L2l5";
                          }
                          else
                              if ($varCrit == "inactive")
                              {
                                  $query = "SELECT L2l5, Stitl, Ltitl, Status FROM Tl2l5 WHERE UPPER(TRIM(status))= 'I' ORDER BY L2l5";
                              }
                              else
                                  if ($varCrit == "all")
                                  {
                                      $query = "SELECT L2l5, Stitl, Ltitl, Status FROM Tl2l5 ORDER BY L2l5";
                                  } 
                      }
  
  
  
  //-----------------------------------------
  // Insert data into Excel Report template
  //-----------------------------------------
  
      // Create a new PHPExcel object 
      $objPHPExcel = new PHPExcel(); 
  
  
      // Sheet name
      $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('sheet1'); 
  
      // Set the default font type (Arial) and the size (10)
      $objPHPExcel-&gt;getActiveSheet()-&gt;getDefaultStyle()-&gt;getFont()-&gt;setName('Arial')-&gt;setSize(10);
  
      // Set the column headings to row 3 and data to start on row 4
      $rowHeadStart = 3; 
      $rowDataStart = 4;
  
      //Merge Cells for the report titles
      $objPHPExcel-&gt;getActiveSheet()-&gt;mergeCells("A1:T1"); // Report Title
      $objPHPExcel-&gt;getActiveSheet()-&gt;mergeCells("A2:T2"); // Date
  
      //Set Cell Text
      $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue("A1", $title);
      $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A2', date("m/d/Y"));
  
      //Make Report title bold 
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle("A1:T1")-&gt;applyFromArray(array("font" =&gt; array( "bold" =&gt; true)));
      //Make the date italicized
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle("A2:T2")-&gt;applyFromArray(array("font" =&gt; array( "italic" =&gt; true)));         
      //Make Column headers bold
      $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle("A3:T3")-&gt;applyFromArray(array("font" =&gt; array( "bold" =&gt; true)));
  
  
      //---------------------------------------------
      // Loop through to display the column headings
      //---------------------------------------------
      $col = 'A'; 
      foreach($headings as $heading) 
      { 
         $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue($col.$rowHeadStart,$heading); 
         $col++; 
      } // end $headings as $heading
  
      //-------------------------------------------------
      // Loop through the result set to display the data
      //-------------------------------------------------
      while ($row = mysql_fetch_row($result)) 
      { 
         $col = 'A'; 
         foreach($row as $cell) 
         { 
            $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue($col.$rowDataStart,$cell); 
            $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension($col)-&gt;setAutoSize(true);
            $col++; 
  
         } // end $row as $cell
         $rowDataStart++; 
  
  
         //-----------------------
         // Page/Cell Formatting
         //-----------------------
         //Set font size for the main report title
         $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle("A1")-&gt;getFont()-&gt;setSize(16);
  
         $objPHPExcel-&gt;setActiveSheetIndex(0);   
  
         // Left align the entire document
         $objPHPExcel-&gt;getDefaultStyle()-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); 
  
         // Set the page orientation to landscape
         $objPHPExcel-&gt;getActiveSheet()-&gt;getPageSetup()-&gt;setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
  
      } //end $row = mysql_fetch_row($result)
  
  
      // Redirect output to a client’s web browser (Excel2007)
      header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
      header('Content-Disposition: attachment;filename="0teste.xlsx"');
      header('Cache-Control: max-age=0');
  
     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
     $objWriter-&gt;save('php://output');
  
  
      exit(); 
  
   ?&gt;
  </code></pre>
  
  <p>A week ago the reports were able to generate using the code above but recently for some reason it's giving me the error that the file could be corrupted or unsafe. My PHPExcel class files are in my project folder so the paths are able to be referenced. I'm unsure how to fix this error. </p>
  
  <p>If anybody could shine some light on this issue I would really appreciate it. </p>
  
  <p>Thank you.</p>