Downloading MySQL to Excel Class Question

Topics: User Forum
Jun 17, 2007 at 1:46 PM
Hi guys... Well I am very much a noob so am hoping someone can help me out... I initially was using some very simple php code to generate my Excel file's from my database...


$query = "SELECT * FROM `table`";
$result = mysql_query($query) or die('Error, query failed');

$tsv = array();
$html = array();
while($row = mysqlfetcharray($result, MYSQL_NUM))
$tsv[] = implode("\t", $row);
$html[] = "<tr><td>" .implode("</td><td>", $row) . "</td></tr>";

$tsv = implode("\r\n", $tsv);
$html = "<table>" . implode("\r\n", $html) . "</table>";

$fileName = 'mysql-to-excel.xls';
header("Content-type: application/");
header("Content-Disposition: attachment; filename=$fileName");

echo $tsv;
//echo $html;


It donwloads the info I need to but is very messy and doesn't have the table column names, people have pointed me in this direction, I have had a look at the source code. The things that can be done look great! But as I'm new to php was hoping someone could tell me which class or file I should use? I don't want to do anything overlly complicated just download the table, and have the column names as headings... I guess if I could pick exactly which columns where downloaded from the table that would be great too, but anything to get me started!

Thanks all in advance I hope I've posted in the correct area!
Jun 18, 2007 at 9:29 AM
As for the specific column names, change $query = "SELECT * FROM `table`"; to $query = "SELECT columnX, columnY, ..., columnZ FROM `table`";

As for PHPExcel, I'd suggest you take a look at the examples in the Tests directory of the download to get you started.