javascript - PHP 不生成 Excel 文件

标签 javascript php html mysql

我正在使用以下脚本生成我的 php 的 Excel 文件。它给了我以下错误。这可能是什么原因 -

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\geochronology\library\admin\generate-excel.php:45) in C:\xampp\htdocs\geochronology\library\admin\generate-excel.php on line 56

<?php
// Database Connection file
include('includes/config.php');
?>
<table border="1">
<thead>
<tr>
<th>Sr.</th>
<th>User Name</th>
<th>Affiliation</th>
<th>Sample Type</th>
<th>Sample ID</th>
<th>Date</th>
</tr>
</thead>
<?php
// File name
$filename="Sample-summary";
// Fetching data from data base
        $search=$_POST['search2'];
        $option=$_POST['option2'];
        $period=$_POST['period2'];
        $datefrom=$_POST['datefrom2'];
        $dateto=$_POST['dateto2'];
        $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));


            if($period ==null){
            $sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date 
            FROM tblfacility 
            JOIN tbluser on tbluser.id=tblfacility.user 
            where ".$search." ='".$option."' ";}
            else{
            $sql="SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where ".$search." ='".$option."' AND time between '".$datefrom."' and '".$dateto."' ";
            }
            $query = $dbh->prepare($sql);
            $query->execute();
            $results=$query->fetchAll(PDO::FETCH_OBJ);
            //echo "<prep>";
            //echo "this is the final";
            print_r($sql);
            $cnt=1;         
            foreach($results as $result){
?>
            <tr>
                <td><?php echo $cnt;?></td>
                <td><?php echo $row['user'];?></td>
                <td><?php echo $row['affiliation'];?></td>
                <td><?php echo $row['type'];?></td>
                <td><?php echo $row['sampleid'];?></td>
                <td><?php echo $row['time'];?></td>
            </tr>
<?php
$cnt++;
// Genrating Execel  filess
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename."-Report.xls");
header("Pragma: no-cache");
header("Expires: 0");
            }
            ?>
</table>

最佳答案

要直接回答您的问题,标题必须位于文件的第一位。将所有 header 设置移至第一个,然后再进行其他所有设置(可能 config.php 除外,具体取决于您在该包含中执行的操作)

确切的错误是由第 5 行中的输出引起的。只要您输出任何内容,就会(自动)发送 header 。一旦发送输出,再发送更多 header 就为时已晚。

另请注意您收到的其他评论,您的代码很容易受到攻击,并且有更好的方法可以做到这一点。

关于javascript - PHP 不生成 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58968319/

相关文章:

javascript - 如何使用 html 和 javascript 创建音频播放器

javascript - 在 JavaScript 中将数组保存到 cookies

PHP/MySQL 在某些查询中没有返回结果

php - Sublime 3 编辑器语法突出显示 PHP + HTML 不起作用

javascript - jquery ajax jsonp : Invalid left-hand side assignment

JavaScript循环选择选项

php - 连接表上的 ORDER BY

php - 根据数据库中的数据动态更改 CSS 背景图像 (WordPress)

html - 两个相同高度的元素+绝对定位的 child

javascript - 上下文未定义 JavaScript