You are on page 1of 4

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.

01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Detalle del concepto</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="keywords" content="programacin, webmaster, tutorial">
<meta name="description" content="El objetivo de este sitio es ser un repositori
o de conceptos sobre php html xhtml css mysql javascript">
<link rel="StyleSheet" href="css/estilos.css" type="text/css">
<style type="text/css">
#contenido {
padding:10px;
background-color:#f0f0f0;
}
legend { font-family: sans-serif; padding:10px; }
fieldset {padding:10px;};
.titulo { font-family: arial; font-size:1.8em;}
.titulo2 { font-family: arial; font-size:1.4em; }
.comentarios { font-family: monospace; font-size:1.05em }
</style>
<script language="JavaScript">
function abrirVentana()
{
alto=(screen.availHeight/2)+100;
ancho=screen.availWidth-10;
window.open('', 'ventanaForm', 'top=0,left=0,width='+ancho+',height='+alto+',r
esizable=yes,scrollbars=yes,menubar=yes,status=yes,toolbar=yes,location=yes');
}
</script>
</head>
<body>
<div id="contenedor">
<div id="cabecera">
<h1><a href="index.php" class="enlaceindex">Cdigo fuente Ya.</a></h1>
</div>
<div id="subcabecera">
<p>Puede ejecutar la o las p&aacute;ginas para probar el concepto inmediatam
ente.</p>
<p>&nbsp;</p>
</div>
<div id="contenido">
<span class="titulo"><strong>Concepto:</strong>Exportar una tabla de mysql a Exc
el</span><br><br><span class="titulo2"><strong>El concepto trata sobre:</strong>
php,mysql</span><br><span class="titulo2"><strong>Autor:</strong></span><br><for
m method="post" action="http://www.problemasya.com.ar/ejecuta1.php" target="vent
anaForm" onsubmit="abrirVentana()"> <input type="submit" value="Ejecutar el p
rograma." class="botones">
<div>
<h2><a href="index.php?inicio=20">Retornar</a></h2>
</div>
<fieldset><legend><strong>Nombre del archivo:</strong>pagina1.php<br></legend><i
nput type="hidden" name="nomarchivo0" value="pagina1.php"><textarea name="archiv
o0" rows="10" cols="110">&lt;?php
$conexion=mysql_connect(&quot;localhost&quot;,&quot;pepe&quot;,&quot;123456&quot
;) or
die(&quot;Problemas en la conexion&quot;);

mysql_select_db(&quot;codigofuenteya&quot;,$conexion) or
die(&quot;Problemas en la selecci&oacute;n de la base de datos&quot;);
$registros=mysql_query(&quot;drop table if exists usuarios&quot;,$conexion) or
die(&quot;Problemas en el select:&quot;.mysql_error());
$registros=mysql_query(&quot;create table usuarios (
nombre varchar(30),
clave varchar(30)
)&quot;,$conexion) or
die(&quot;Problemas en el select:&quot;.mysql_error());
mysql_query(&quot;insert into usuarios(nombre,clave) values ('juan','123')&quot;
,$conexion) or
die(mysql_error());
mysql_query(&quot;insert into usuarios(nombre,clave) values ('ana','abc')&quot;,
$conexion) or
die(mysql_error());
mysql_query(&quot;insert into usuarios(nombre,clave) values ('carlos','xyz')&quo
t;,$conexion) or
die(mysql_error());
$datos='';
$cabecera='';
$registros=mysql_query(&quot;select nombre,clave from usuarios&quot;,$conexion)
or
die(mysql_error());
$cant=mysql_num_fields($registros) or
die(mysql_error());
for($f=0;$f&lt;$cant;$f++)
{
$cabecera.=mysql_field_name($registros,$f).&quot;\t&quot;;
}
while($fila = mysql_fetch_row($registros))
{
$linea = '';
foreach($fila as $valor)
{
if ((!isset($valor)) || ($valor == &quot;&quot;))
{
$valor = &quot;\t&quot;;
}
else
{
$valor = str_replace('&quot;', '&quot;&quot;', $valor);
$valor = '&quot;' . $valor . '&quot;' . &quot;\t&quot;;
}
$valor = stripslashes($valor);
$linea .= $valor;
}
$datos .= trim($linea).&quot;\n&quot;;
}
$datos = str_replace(&quot;\r&quot;,&quot;&quot;,$datos);
if ($datos == &quot;&quot;)
{
$datos = &quot;\n (0) registros encontrados!\n&quot;;
}

$nomarchivo='prueba1.xls';
header(&quot;Content-type: application/x-msdownload&quot;);
header(&quot;Content-Disposition: attachment; filename=$nomarchivo&quot;);
header(&quot;Pragma: no-cache&quot;);
header(&quot;Expires: 0&quot;);
print &quot;$cabecera\n$datos&quot;;
?&gt;
</textarea><br><strong>Comentarios</strong><div class="comentarios"><pre style="
">Creamos una tabla e insertamos como ejemplo varios registros:
$conexion=mysql_connect(&quot;localhost&quot;,&quot;pepe&quot;,&quot;123456&quot
;) or
die(&quot;Problemas en la conexion&quot;);
mysql_select_db(&quot;codigofuenteya&quot;,$conexion) or
die(&quot;Problemas en la selecci&oacute;n de la base de datos&quot;);
$registros=mysql_query(&quot;drop table if exists usuarios&quot;,$conexion) or
die(&quot;Problemas en el select:&quot;.mysql_error());
$registros=mysql_query(&quot;create table usuarios (
nombre varchar(30),
clave varchar(30)
)&quot;,$conexion) or
die(&quot;Problemas en el select:&quot;.mysql_error());
mysql_query(&quot;insert into usuarios(nombre,clave) values ('juan','123')&quot;
,$conexion) or
die(mysql_error());
mysql_query(&quot;insert into usuarios(nombre,clave) values ('ana','abc')&quot;,
$conexion) or
die(mysql_error());
mysql_query(&quot;insert into usuarios(nombre,clave) values ('carlos','xyz')&quo
t;,$conexion) or
die(mysql_error());
Recuperamos todos los registros y mediante un for recuperamos los nombres de los
campos:
for($f=0;$f&lt;$cant;$f++)
{
$cabecera.=mysql_field_name($registros,$f).&quot;\t&quot;;
}
Luego mediante un while y un for interno recuperamos todos los datos de cada fil
a de la tabla
y generamos un string con la informaci&oacute;n correspondiente:
while($fila = mysql_fetch_row($registros))
{
$linea = '';
foreach($fila as $valor)
{
if ((!isset($valor)) || ($valor == &quot;&quot;))
{
$valor = &quot;\t&quot;;
}
else
{
$valor = str_replace('&quot;', '&quot;&quot;', $valor);
$valor = '&quot;' . $valor . '&quot;' . &quot;\t&quot;;

}
$valor = stripslashes($valor);
$linea .= $valor;
}
$datos .= trim($linea).&quot;\n&quot;;
}
Generamos una cabecera que indica que la informaci&oacute;n a suministrar debe s
er procesada por
una aplicaci&oacute;n:
header(&quot;Content-type: application/x-msdownload&quot;);
</pre></div><script type="text/javascript"><!-google_ad_client = "pub-4669394804436935";
/* GRIS300x250, creado 16/03/08 */
google_ad_slot = "0299140816";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</fieldset> <input type="submit" value="Ejecutar el programa.
" class="botones">
<div>
<h2><a href="index.php?inicio=20">Retornar</a></h2>
</div>
<br>
<table width="650px">
<tr>
<td align="left">
</td>
</tr>
</table>
<br>
</div>
<div id="pie" style="text-align:center">
Cdigo Fuente Ya - 2015
</div>
</div>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-628756-16";
urchinTracker();
</script>
</body>
</html>

You might also like