使用PHP生成Excel文件

时间:2017-12-06 09:08:38

标签: php excel web-applications

我是PHP的新手,我正在开发一个WEB应用程序,它显示一个包含来自SQL服务器DATA BASE的信息的表,我想添加一个按钮来生成包含显示的表的EXCEL文件的问题?那可能吗?? @Ranjit这是显示表并生成excel文件的PHP代码

编辑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);
}}}
?>

2 个答案:

答案 0 :(得分:0)

你必须手动选择数据,然后将它们插入Excel工作表,你可以使用名为PHPEXCEL的php库。

看到这个 http://www.c-sharpcorner.com/article/export-to-excel-in-php-with-my-sql/

答案 1 :(得分:-1)

是,

有可能。你可以按照这个

1)连接数据库:

2)定义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

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