Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP生成Excel文件_Php_Excel_Web Applications - Fatal编程技术网

使用PHP生成Excel文件

使用PHP生成Excel文件,php,excel,web-applications,Php,Excel,Web Applications,我是PHP新手,我正在开发一个WEB应用程序,它显示一个包含SQL server数据库信息的表格,问题是我想添加一个按钮来生成一个包含所显示表格的EXCEL文件?这可能吗?? @Ranjit这是显示表格并生成excel文件的PHP代码 编辑1 <?php $ch=""; if(isset($_POST['historique'])) { if ((!empty($_POST['Date_de_debut']))&& (!empty($_POST['Date_de_f

我是PHP新手,我正在开发一个WEB应用程序,它显示一个包含SQL server数据库信息的表格,问题是我想添加一个按钮来生成一个包含所显示表格的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);
}}}
?>

您必须手动选择数据,然后将其插入excel工作表,您可以使用名为PHPEXCEL的php库

看到这个了吗 是的

这是可能的。你可以照这个做

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

来源-

我已经连接到数据库,因为当我单击“显示”按钮时,信息显示在屏幕上,我需要重新连接以导出excel文件上的信息吗??需要我再次执行查询并再次获取信息以将其存储在excel文件中吗??或者我必须在第一个查询中直接传递它们?PS:我使用的是MICROSOFT SQL Server我确实生成了一个excel文件,但问题是该文件不仅生成了表格,还生成了HTML页面,甚至按钮和编辑文本。太长了,我如何将其写入注释?能否从文件中删除HTML。因为,该文件中不需要Html。