gpt4 book ai didi

php - 如何在 php 中将 html 表导出到 excel 或 pdf

转载 作者:IT王子 更新时间:2023-10-29 00:07:41 24 4
gpt4 key购买 nike

在我的 php 页面中,我有一个表格,如果用户需要,他必须将该表格导出到 excel 工作表..

显示表格的代码是:

$sql=mysql_query("SELECT * FROM attendance WHERE (year = '" . 
mysql_real_escape_string($_SESSION['year']) . "') and ( branch= '" .
mysql_real_escape_string(($_SESSION['branch'])). "') and ( sem= '" .
mysql_real_escape_string(($_SESSION['sem'])). "') and (sec= '" .
mysql_real_escape_string(($_SESSION['sec'])). "')"); print "<body
background='bg.jpg'>";
Print "<br><br><BR><center><table border
cellpadding=3><tr><th>idno</th><th>name</th><th>subject</th><th>Held
Classes</th><th>Attended Classes</th></tr>";
while($data=mysql_fetch_array(
$sql )) {

echo "<tr><td>".$data['idno']." </td><td>".$data['name'] . "
<td>".$data['subject']." </td><td>".$data['heldcls'] . "
<td>".$data['attendcls']." </td>"; }
Print "</table><br><br><form action = excel.php method = POST><input type =
'submit' name = 'submit' Value = 'Export to excel'></form></center>";

如何将此表导出到 Excel 工作表。 excel.php 中的代码应该是什么。请帮助我..提前谢谢你..

最佳答案

您可以使用 CSV functionsPHPExcel

或者你可以像下面这样尝试

<?php
$file="demo.xls";
$test="<table ><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
echo $test;
?>

.xlsx 文件的 header 是 Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

关于php - 如何在 php 中将 html 表导出到 excel 或 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12541000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com