PHP:导出HTML表格到Excel包括整个表单?
我有一个PHP文件显示一个HTML表单,并提交,从MySQL数据创build一个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>
显示,提交button和所有。
谢谢。
在标题调用之前,不应该将输出发送到浏览器。
在提交过程下定义你的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..>
所有写入输出缓冲区的数据都将被包含在内。 标题调用最好在任何输出之前完成。 但是如果你的文件是如何组织的,你可以这样做:
<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 } ?>
如果你在performance层(在你的HTML文件中有条件的ifs)做你的逻辑,可能足以包装你的表单代码
if(!isset($_POST['submit'])){
你最好的办法是将导出操作redirect到其他脚本:)