2011-05-25 73 views
1

我有一个PHP文件,它显示了一个html表单,并在提交时,从mysql数据创建一个html表,并将其导出到excel。PHP:将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";} 
?> 

我想只有“$ DATA”在我的.xls显示,而是整个<form>正在显示,提交按钮和所有。

谢谢。

+0

谢谢@沙克提 - 辛格@马尔钦 - cylke @inquam伟大的答案,并快速作出回应!我的脚本现在很漂亮! – Gianna 2011-05-25 10:36:28

回答

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..> 
0

写入到输出缓冲区的所有数据都将包含在内。 最好在任何输出之前完成标题调用。但在如何您的文件组织的情况下,你可以这样做:

<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 
    } 
    ?> 
0

如果(在HTML文件中有条件IFS)做你的逻辑在表示层比也许就足够了包装你的表单代码为

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

祢最好的办法是导出操作重定向到其他一些脚本:)