title: generate an Excel file using PHP
body: <p>I'm new with PHP, I'm developping a WEB application that display a table with informations from an SQL server DATA BASE, the problem that I want to add a button to generate an EXCEL file that contains the table displayed? Is that possible?? 
  @Ranjit this is the PHP code that displays the table and generate the excel file</p>
  
  <p>edit 1</p>
  
  <pre><code>&lt;?php
  $ch="";
  if(isset($_POST['historique']))
  {
      if ((!empty($_POST['Date_de_debut']))&amp;&amp; (!empty($_POST['Date_de_fin']))&amp;&amp;(!empty($_POST['Heure_de_debut']))&amp;&amp;(!empty($_POST['Heure_de_fin'])))
      {
                  $ch= "(CONVERT(Datetime, '".$_POST['Date_de_debut']." ".$_POST['Heure_de_debut'].".000',120)) and (CONVERT(Datetime, '".$_POST['Date_de_fin']." ".$_POST['Heure_de_fin'].".000',120))";
          ?&gt;
  &lt;table id="tab" border="1"&gt;
                  &lt;tr&gt;
                &lt;th&gt;&lt;font color="red"&gt;Date&lt;/font&gt;&lt;/th&gt;
                  &lt;th&gt;&lt;font color="red"&gt;Agent&lt;/font&gt;&lt;/th&gt;
              &lt;th&gt;&lt;font color="red"&gt;numéro&lt;/font&gt;&lt;/th&gt;    
                  &lt;/tr&gt;
  &lt;?php
  
  $search = " // my query
  where operationDate between" .$ch;
  $stmt = mssql_query($search);
                               while ($data = mssql_fetch_assoc($stmt))
                               {
                                       ?&gt;
       &lt;tr&gt;
   &lt;td&gt;&lt;?php echo utf8_encode ($data['operationDate']);?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo utf8_encode ($data['fullName']);?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo utf8_encode ($data['number']);?&gt;&lt;/td&gt;
                                       &lt;/tr&gt;
  &lt;?php
       } }
  
       ?&gt;
          &lt;/table&gt;
  &lt;?php
  }
  $output ='';
  if(isset($_POST['excel']))
  {
      if ((!empty($_POST['Date_de_debut']))&amp;&amp; (!empty($_POST['Date_de_fin']))&amp;&amp;(!empty($_POST['Heure_de_debut']))&amp;&amp;(!empty($_POST['Heure_de_fin'])))
      {
      $rq = "// my query
                                   where operationDate between" ."(CONVERT(Datetime, '".$_POST['Date_de_debut']." ".$_POST['Heure_de_debut'].".000',120)) and (CONVERT(Datetime, '".$_POST['Date_de_fin']." ".$_POST['Heure_de_fin'].".000',120))";
      $res = mssql_query($rq);
    if(mssql_num_rows($res)&gt;0)
    {
      $output.='&lt;table border=1&gt;
              &lt;tr&gt;
                    &lt;th&gt;Date&lt;/th&gt;
                    &lt;th&gt;Depanneur&lt;/th&gt;
                    &lt;th&gt;numéro&lt;/th&gt;
                &lt;/tr&gt;
                ';
                while ($row=mssql_fetch_array($res))
                {
                  $output .='
                  &lt;tr&gt;
                      &lt;td&gt;'.$row["operationDate"].'&lt;/td&gt;
                      &lt;td&gt;'.$row["fullName"].'&lt;/td&gt;
                      &lt;td&gt;'.$row["number"].'&lt;/td&gt;
                  &lt;/tr&gt;';
                }
                              $output .='&lt;/table&gt;';
                header("Content-Type: application/xls;charset=UTF-8");
                header("Content-Disposition: attachement; filename=file.xls");
  
                echo $output;
                              //mssql_close($conn);
  }}}
  ?&gt;
  </code></pre>