Aquie mi codigo:
Código PHP:
<?
//EXPORTAR EMPRESAS
$consultaempresa = "SELECT id_empresa, razonsocial, ruc, direccion, distrito, telefono, fax, email, website, giro, imagen FROM empresa";
$exportarExcelempresa = mysql_query($consultaempresa, $dbh) or die (mysql_error());
if (mysql_num_rows($exportarExcelempresa))
{
$shtml="<table width='900' border='1' cellpadding='0' cellspacing='0' bordercolor='#B9B9B9'>";
$shtml=$shtml."<tr>";
$shtml=$shtml."<td colspan='11'><strong>REPORTE DE EMPRESAS POR ORDEN ALFABETICO </strong></td>";
$shtml=$shtml."</tr>";
$shtml=$shtml."<tr>";
$shtml=$shtml."<td colspan='11'></td>";
$shtml=$shtml."</tr>";
$shtml=$shtml."<tr>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Id</strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Razon Social </strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Ruc </strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Direccion</strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Distrito</strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Telefono</strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Fax</strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Email</strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Web Site</strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Giro</strong></td>";
$shtml=$shtml."<td bgcolor='#EEEEEE'><strong>Imagen</strong></td>";
$shtml=$shtml."</tr>";
while($row_empresa = mysql_fetch_array($exportarExcelempresa))
{
$shtml=$shtml."<tr>";
$shtml=$shtml."<td>".$row_empresa['id_empresa']."</td>";
$shtml=$shtml."<td>".$row_empresa['razonsocial']."</td>";
$shtml=$shtml."<td>".$row_empresa['ruc']."</td>";
$shtml=$shtml."<td>".$row_empresa['direccion']."</td>";
$shtml=$shtml."<td>".$row_empresa['distrito']."</td>";
$shtml=$shtml."<td>".$row_empresa['telefono']."</td>";
$shtml=$shtml."<td>".$row_empresa['fax']."</td>";
$shtml=$shtml."<td>".$row_empresa['email']."</td>";
$shtml=$shtml."<td>".$row_empresa['website']."</td>";
$shtml=$shtml."<td>".$row_empresa['giro']."</td>";
$shtml=$shtml."<td>".$row_empresa['imagen']."</td>";
$shtml=$shtml."</tr>";
}
}
$shtml.="</table>";
$scarpeta="excel/";
$enlaceempresa=$scarpeta."relacionempresas.xls"; //ruta del archivo a generar
$fp=fopen($enlaceempresa,"w");
fwrite($fp,$shtml);
fclose($fp);
?>