Hello All,
I have the following code to export a MySQL database into Excel format and it's working great. I'm curious if it's possible to add an image(s) to the Excel document as well. For example, could I include a file like "graph.jpg" somehow in the code so it appears in the spreadsheet when it's exported? Thanks.
<?php
$DB_Server = "localhost";
$DB_Username = "";
$DB_Password = "";
$DB_DBName = "mydb";
$DB_TBLName = "mytable";
$sql = "Select * from $DB_TBLName";
$Use_Title = 1;
$now_date = date('m-d-Y H:i');
$title = "data as of $now_date";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
$result = @mysql_query($sql,$Connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
if (isset($w) && ($w==1))
{
$file_type = "msword";
$file_ending = "doc";
}else {
$file_type = "vnd.ms-excel";
$file_ending = "xls";
}
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=data.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
if (isset($w) && ($w==1))
{
if ($Use_Title == 1)
{
echo("$title\n\n");
}
$sep = "\n";
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
$field_name = mysql_field_name($result,$j);
$schema_insert .= "$field_name:\t";
if(!isset($row[$j])) {
$schema_insert .= "NULL".$sep;
}
elseif ($row[$j] != "") {
$schema_insert .= "$row[$j]".$sep;
}
else {
$schema_insert .= "".$sep;
}
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n----------------------------------------------------\n";
}
}else{
if ($Use_Title == 1)
{
echo("$title\n");
}
$sep = "\t";
for ($i = 0; $i < mysql_num_fields($result); $i++)
{
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
}
?>