【问题标题】:PHP: Exporting HTML table to Excel includes entire form?PHP:将 HTML 表格导出到 Excel 包括整个表格?
【发布时间】:2011-05-25 18:06:21
【问题描述】:

我有一个显示 html 表单的 php 文件,在提交时,从 mysql 数据创建一个 html 表,并将其导出到 excel。

我的问题是整个html表单和表格数据一起输出到excel文件中。

我的代码是:

<html form..>

<?php
if(isset($_POST['submit'])){
//get sql data and create html table in variable (ie $data="<table>..")

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
echo "$data";}
?>

我只想在我的 .xls 中显示“$data”,而是显示整个 &lt;form&gt;,提交按钮等等。

谢谢。

【问题讨论】:

  • 谢谢@shakti-singh @marcin-cylke @inquam 很好的答案和快速的回​​复!我的脚本现在运行良好!

标签: php html excel


【解决方案1】:

在标头调用之前不应向浏览器发送任何输出。

在提交过程下方定义您的 html 表单。

<?php
if(isset($_POST['submit'])){
//get sql data and create html table in variable (ie $data="<table>..")

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
echo "$data";
}
?>

<html form..>

【讨论】:

    【解决方案2】:

    所有写入输出缓冲区的数据都将被包括在内。 标头调用最好在任何输出之前完成。但就文件的组织方式而言,您可以这样做:

    <html form..>
    
        <?php if(isset($_POST['submit'])){ 
         //get sql data and create html table in variable (ie $data="<table>..")
         // Clean the output buffer so it won't mess up your excel export 
         ob_clean();
    
        header("Content-type: application/vnd.ms-excel"); 
        header("Content-Disposition: attachment; filename=$file"); 
        echo $data; // no need for quotes here since you only echo your variable
        }
        ?>
    

    【讨论】:

      【解决方案3】:

      如果您在表示层中执行逻辑(html 文件中的条件 ifs),那么将表单代码包装到其中就足够了

      if(!isset($_POST['submit'])){
      

      最好的方法是将导出操作重定向到其他脚本:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-21
        • 2023-03-03
        • 1970-01-01
        • 2014-04-08
        • 1970-01-01
        • 2014-08-29
        • 2016-05-15
        • 2018-06-24
        相关资源
        最近更新 更多