Hi, How I can Export PHP Result which shown in table to excel using a Export Button on the Same Page.


Recommended Answers

All 7 Replies

This code show the result in a table cells so i need to add a excel button which export the data in excel file for the use. please help and thanks in advance.

$con = mysql_connect("localhost","xxxx","xxxx");
if (!$con)
  die('Could not connect: ' . mysql_error());

$a= $_POST["SiteId"];
mysql_select_db("onm", $con);

$result = mysql_query("SELECT * FROM sitepinfo
WHERE SiteId='$a' ");

while($row = mysql_fetch_array($result))
  echo "<table cellpadding=2 cellspacing=2 width=100%>

  echo "<tr>";
  echo "<th bgcolor=#5D9BCC >SiteID</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['SiteId'] . "</td>";

  echo "</tr>";
  echo "<tr>";
  echo "<th bgcolor=#5D9BCC>Alias</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['Alias'] . "</td>";
    echo "</tr>";
	 echo "<tr>";
  echo "<th bgcolor=#5D9BCC>Address</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['Address'] . "</td>";
    echo "</tr>";
	 echo "<tr>";
  echo "<th bgcolor=#5D9BCC>Region</th>";
  echo "<td bgcolor=#FEE9A9>" . $row['Region'] . "</td>";
    echo "</tr>";
	 echo "<tr>";
	echo "<th bgcolor=#5D9BCC>Agreement</th>";
	 echo "<td bgcolor=#9BCB5D><a href=".$row['pdf'].">".$row['pdf']."</a></td>";

	  echo "</tr>";
	    echo "<tr>";
	echo "<th bgcolor=#5D9BCC>Purchase Order</th>";
	 echo "<td bgcolor=#CB9B5D><a href=".$row['pdf'].">".$row['pdf']."</a></td>";

	  echo "</tr>";
	   echo "<tr>";
	 echo "<td bgcolor=#9B5DCA><a href='update.php?id={$row[SiteId]}'>".stripslashes(Update)."</a>
	  echo "</tr>";
	  echo "<tr>";
	 echo "<td bgcolor=#9B5DCA><a href='delete.php?id={$row[SiteId]}'>".stripslashes(delete)."</a>
	  echo "</tr>";
echo "</table>";


// Connect database. 

// Get data records from table. 
$result=mysql_query("select * from name_list order by id asc");

// Functions for export to excel.
function xlsBOF() { 
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
function xlsEOF() { 
echo pack("ss", 0x0A, 0x00); 
function xlsWriteNumber($Row, $Col, $Value) { 
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); 
echo pack("d", $Value); 
function xlsWriteLabel($Row, $Col, $Value ) { 
$L = strlen($Value); 
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
echo $Value; 
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=orderlist.xls "); 
header("Content-Transfer-Encoding: binary ");


Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, both are start at '0'

xlsWriteLabel(0,0,"List of car company.");

// Make column labels. (at line 3)

$xlsRow = 3;

// Put data records from mysql by while loop.



above code is not working

.xls is proprietary and contains more than just the data, not sure if your functions are accurate, but they appear to match what I have seen before

excel can read/write .csv comma separated values
php can read/write .csv comma separated values
which is great if the data is all you need, not sure if equations transfer

a bunch of tutorials on creating writing .xls with phphttp://www.google.com/search?php .xls tutorial have no idea how myself

(Good Luck)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.