'generate an Excel file using PHP

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

edit 1

<?php
$ch="";
if(isset($_POST['historique']))
{
    if ((!empty($_POST['Date_de_debut']))&& (!empty($_POST['Date_de_fin']))&&(!empty($_POST['Heure_de_debut']))&&(!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))";
        ?>
<table id="tab" border="1">
                <tr>
              <th><font color="red">Date</font></th>
                <th><font color="red">Agent</font></th>
            <th><font color="red">numéro</font></th>    
                </tr>
<?php

$search = " // my query
where operationDate between" .$ch;
$stmt = mssql_query($search);
                             while ($data = mssql_fetch_assoc($stmt))
                             {
                                     ?>
     <tr>
 <td><?php echo utf8_encode ($data['operationDate']);?></td>
 <td><?php echo utf8_encode ($data['fullName']);?></td>
 <td><?php echo utf8_encode ($data['number']);?></td>
                                     </tr>
<?php
     } }

     ?>
        </table>
<?php
}
$output ='';
if(isset($_POST['excel']))
{
    if ((!empty($_POST['Date_de_debut']))&& (!empty($_POST['Date_de_fin']))&&(!empty($_POST['Heure_de_debut']))&&(!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)>0)
  {
    $output.='<table border=1>
            <tr>
                  <th>Date</th>
                  <th>Depanneur</th>
                  <th>numéro</th>
              </tr>
              ';
              while ($row=mssql_fetch_array($res))
              {
                $output .='
                <tr>
                    <td>'.$row["operationDate"].'</td>
                    <td>'.$row["fullName"].'</td>
                    <td>'.$row["number"].'</td>
                </tr>';
              }
                            $output .='</table>';
              header("Content-Type: application/xls;charset=UTF-8");
              header("Content-Disposition: attachement; filename=file.xls");

              echo $output;
                            //mssql_close($conn);
}}}
?>


Solution 1:[1]

You'll have to select the data manually then insert them into the excel sheet, there's php library called PHPEXCEL you can use it.

See this http://www.c-sharpcorner.com/article/export-to-excel-in-php-with-my-sql/

Solution 2:[2]

There are some nice packages out there to generate excel files such as Box Spout and PHP Spreadsheet.

The documentation of both packages is very clear any you will be generating excel files within minutes of browsing through documentation.

Solution 3:[3]

Yes,

It is possible. You can follow this

1) Connect to database:

2) Define a filename of excel

//define separator (defines columns in excel & tabs in word) 
$sep = "\t"; //tabbed character 
$fp = fopen('database.xls', "w"); 
$schema_insert = ""; 
$schema_insert_rows = ""; 
//start of printing column names as names of MySQL fields

Sources - http://www.anillabs.com/2010/03/how-to-create-excel-file-with-mysql-data-using-php-code/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Momen Shaker
Solution 2 Gazmend Sahiti
Solution 3 Ranjit