将 html 表导出到可下载的 Excel 并出现 PHP 问题
所以,我在将表格导出到 Excel 时遇到一些问题。
它已生成但仅出现第一组标头。
我有一个像这样的表格:
<table>
<thead>
<tr>
<th colspan="5">Incoming</th>
</tr>
<tr>
<th>From</th>
<th>To</th>
<th>Date</th>
<th>Duration</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">942367233</td>
<td style="text-align: center;">-</td>
<td style="text-align: center;">15-06-2011 08:24</td>
<td style="text-align: center;">00:00</td>
<td style="text-align: center;">Abandoned</td>
</tr>
<tr>
<td style="text-align: center;">935761500</td>
<td style="text-align: center;">1956</td>
<td style="text-align: center;">15-06-2011 09:20</td>
<td style="text-align: center;">00:00</td>
<td style="text-align: center;">Answered</td>
</tr>
<tr>
<td style="text-align: center;">942367233</td>
<td style="text-align: center;">1957</td>
<td style="text-align: center;">15-06-2011 09:21</td>
<td style="text-align: center;">02:16</td>
<td style="text-align: center;">Answered</td>
</tr>
</tbody>
</table>
表格上方有一个表格
<form action="toexcel.php" method="post" target="_blank"
onsubmit=\'$("#datatodisplay").val($("#data").html())\'>
<input type="image" src="/images/icons/Floppy-48x48.png" width="12" height="12">
<input type="hidden" id="datatodisplay" name="datatodisplay" />
</form>
然后,在 PHP 中它应该生成 excel:
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: filename=ficheroExcel.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $_POST['datatodisplay'];
但只显示第一组标题。我尝试删除标题,但它也不起作用。
怎么了?
So, I'm having some issues exporting tables to excel.
It is generated but just appear the first set of headers.
I have a table like this:
<table>
<thead>
<tr>
<th colspan="5">Incoming</th>
</tr>
<tr>
<th>From</th>
<th>To</th>
<th>Date</th>
<th>Duration</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">942367233</td>
<td style="text-align: center;">-</td>
<td style="text-align: center;">15-06-2011 08:24</td>
<td style="text-align: center;">00:00</td>
<td style="text-align: center;">Abandoned</td>
</tr>
<tr>
<td style="text-align: center;">935761500</td>
<td style="text-align: center;">1956</td>
<td style="text-align: center;">15-06-2011 09:20</td>
<td style="text-align: center;">00:00</td>
<td style="text-align: center;">Answered</td>
</tr>
<tr>
<td style="text-align: center;">942367233</td>
<td style="text-align: center;">1957</td>
<td style="text-align: center;">15-06-2011 09:21</td>
<td style="text-align: center;">02:16</td>
<td style="text-align: center;">Answered</td>
</tr>
</tbody>
</table>
And a form right over the table
<form action="toexcel.php" method="post" target="_blank"
onsubmit=\'$("#datatodisplay").val($("#data").html())\'>
<input type="image" src="/images/icons/Floppy-48x48.png" width="12" height="12">
<input type="hidden" id="datatodisplay" name="datatodisplay" />
</form>
And then, at the PHP which it's supposed to generate the excel:
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: filename=ficheroExcel.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $_POST['datatodisplay'];
But only display the first set of headers. I've tried to remove the headers and it doesn't work neither.
What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 HTML 是错误的。 HTML DTD 规定一张表只能包含一个 thead 和一个 tfoot,但可以包含多个 tbody 标签:
这是您的主要问题; HTML 无效。
除此之外:
Your HTML is wrong. The HTML DTD states that a table may contain only ONE thead and one tfoot, but multiple tbody tags:
This is your main problem; the HTML is invalid.
Apart from that:
尽管聊天,我还是能够解决这个问题。似乎转义字符在 JavaScript 中没有转义,所以我必须添加以下内容来解决问题:
Despite the chat, I were able to solve this. It seems that escape characters weren't escaped in javascript so I've to add the following, solving the issue: