creating excel file using php..need help..
helo there..can anyone help me on how to make excel file using php. the data will be retrieve from multiple table in database. i've done excel file where the data from single table and i've made it. but for multiple table, i don't know how to do it..plz help me...:confused:
e.g: excel code for single table in database.
Code:
<?
include "connectdb.php";
$id = $_REQUEST['id'];
mysql_select_db('rocincorporation', $dbProd);
$SQL = "select a.*,b.vchdescription as vchcompanytype,c.vchdescription as vchcompanystatus,
d.vchdescription as status,e.vchdescription as country
from (rocincorporation.rocincorporation a
left join parameters.parameters b on b.vchcode = a.chrcompanytype and b.vchparametertype = 'Company Category Eng'
left join parameters.parameters c on c.vchcode = a.chrcompanystatus and c.vchparametertype = 'Status Of Company'
left join parameters.parameters d on d.vchcode = a.chrstatusofcompany and d.vchparametertype = 'CompanyStatus'
left join parameters.parameters e on e.vchcode = a.vchcompanycountry and e.vchparametertype = 'Country')
where a.companyno='$id'";
//echo $SQL;
$result = mysql_query($SQL, $dbProd)or die(mysql_error());
$count = mysql_num_fields($result);
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}
while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);
# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}
$header=$header."\n";
$dir = "Data/";
$fname='Excel-'.$id.'.xls';
$fp = fopen($dir.$fname,'w');
fwrite ($fp,$header);
fwrite ($fp,$data);
?>