Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

How to export html table to excel or pdf in php

In my php page i have a table and if the user requires he has to export that table to excel sheet..

The code for displaying the table is:

$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>";

how do i export this table to excel sheet. And what should b the code in excel.php. Please help me.. thank you in advance..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Either you can use CSV functions or PHPExcel

or you can try like below

<?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;
?>

The header for .xlsx files is Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...